1- //! Provider-owned native scan settings. These are not build-system exclusions.
1+ //! A server's declared indexing exclusions, written into the workspace
2+ //! configuration it reads. These are not build-system exclusions.
3+ use lithe_plugin_language_servers:: IndexingExclusions ;
24use serde_json:: { json, Value } ;
35
46/// Overlay the current policy without mutating the user's stored configuration.
5- /// Removing exclusions therefore restores the original settings exactly.
6- pub ( crate ) fn configuration ( provider : & str , root_uri : & str , base : Option < & Value > ) -> Option < Value > {
7- if !matches ! ( provider, "rust" | "go" ) {
7+ /// Removing exclusions therefore restores the original settings exactly. A
8+ /// server that declares no `indexingExclusions` sees its configuration
9+ /// unchanged; Core never guesses the setting from the server or language.
10+ pub ( crate ) fn configuration (
11+ exclusions : Option < & IndexingExclusions > ,
12+ root_uri : & str ,
13+ base : Option < & Value > ,
14+ ) -> Option < Value > {
15+ let Some ( exclusions) = exclusions else {
816 return base. cloned ( ) ;
9- }
17+ } ;
1018 let Some ( root) = url:: Url :: parse ( root_uri)
1119 . ok ( )
1220 . and_then ( |uri| uri. to_file_path ( ) . ok ( ) )
@@ -18,48 +26,25 @@ pub(crate) fn configuration(provider: &str, root_uri: &str, base: Option<&Value>
1826 return base. cloned ( ) ;
1927 }
2028 Some ( with_roots (
21- provider ,
29+ exclusions ,
2230 base,
2331 & lithe_project:: indexing:: excluded_roots ( & root) ,
2432 ) )
2533}
2634
27- fn with_roots ( provider : & str , base : Option < & Value > , roots : & [ String ] ) -> Value {
35+ fn with_roots ( exclusions : & IndexingExclusions , base : Option < & Value > , roots : & [ String ] ) -> Value {
2836 let mut configuration = base. cloned ( ) . unwrap_or_else ( || json ! ( { } ) ) ;
29- match provider {
30- // rust-analyzer.files.exclude takes literal workspace-relative paths, not globs.
31- // https://rust-analyzer.github.io/book/configuration#rust-analyzerfilesexclude
32- "rust" => append_array (
33- & mut configuration,
34- & [ "rust-analyzer" , "files" , "exclude" ] ,
35- roots. to_vec ( ) ,
36- & [ ] ,
37- ) ,
38- // https://go.dev/gopls/settings#directoryFilters: ordered filters, last match wins.
39- // Preserve its documented default when the user did not configure this array.
40- "go" => append_array (
41- & mut configuration,
42- & [ "gopls" , "directoryFilters" ] ,
43- roots
44- . iter ( )
45- . filter ( |path| !path. contains ( [ '*' , '?' , '[' , ']' , '{' , '}' ] ) )
46- . map ( |path| {
47- if path == "." {
48- "-" . into ( )
49- } else {
50- format ! ( "-{path}" )
51- }
52- } )
53- . collect ( ) ,
54- & [ "-**/node_modules" ] ,
55- ) ,
56- _ => { }
57- }
37+ append_array (
38+ & mut configuration,
39+ & exclusions. section_keys ( ) ,
40+ exclusions. entries ( roots) ,
41+ exclusions. default_entries ( ) ,
42+ ) ;
5843 configuration
5944}
6045
6146/// Normalize owned dotted prefixes, so a request for the provider section sees them too.
62- fn append_array ( value : & mut Value , path : & [ & str ] , additions : Vec < String > , defaults : & [ & str ] ) {
47+ fn append_array ( value : & mut Value , path : & [ & str ] , additions : Vec < String > , defaults : & [ String ] ) {
6348 for length in 2 ..=path. len ( ) {
6449 let dotted = path[ ..length] . join ( "." ) ;
6550 let previous = value
@@ -80,8 +65,9 @@ fn append_array(value: &mut Value, path: &[&str], additions: Vec<String>, defaul
8065 if let Some ( array) = slot. as_array_mut ( ) {
8166 for addition in additions {
8267 let value = Value :: String ( addition) ;
83- // Go filters are ordered: an existing exclusion before a later inclusion
84- // must move to the end rather than being treated as a harmless duplicate.
68+ // Filters may be ordered with the last match winning: an existing
69+ // exclusion before a later inclusion must move to the end rather
70+ // than being treated as a harmless duplicate.
8571 array. retain ( |item| item != & value) ;
8672 array. push ( value) ;
8773 }
@@ -124,7 +110,7 @@ mod tests {
124110 "rust-analyzer.files.exclude" : [ "leaf" ]
125111 } ) ;
126112 let original = base. clone ( ) ;
127- let configured = with_roots ( "rust" , Some ( & base) , & [ "vendor" . into ( ) ] ) ;
113+ let configured = with_roots ( & literal ( ) , Some ( & base) , & [ "vendor" . into ( ) ] ) ;
128114 assert_eq ! (
129115 configured[ "rust-analyzer" ] [ "files" ] ,
130116 json!( {
@@ -143,7 +129,11 @@ mod tests {
143129 #[ test]
144130 fn native_exclusions_preserve_settings_and_restore_them_on_undo ( ) {
145131 let base = json ! ( { "rust-analyzer" : { "files" : { "exclude" : [ "existing" ] } , "cargo" : { "allFeatures" : true } } } ) ;
146- let excluded = with_roots ( "rust" , Some ( & base) , & [ "vendor" . into ( ) , "带 空格" . into ( ) ] ) ;
132+ let excluded = with_roots (
133+ & literal ( ) ,
134+ Some ( & base) ,
135+ & [ "vendor" . into ( ) , "带 空格" . into ( ) ] ,
136+ ) ;
147137 assert_eq ! (
148138 excluded[ "rust-analyzer" ] [ "files" ] [ "exclude" ] ,
149139 json!( [ "existing" , "vendor" , "带 空格" ] )
@@ -152,34 +142,56 @@ mod tests {
152142 excluded[ "rust-analyzer" ] [ "cargo" ] ,
153143 base[ "rust-analyzer" ] [ "cargo" ]
154144 ) ;
155- assert_eq ! ( with_roots( "rust" , Some ( & base) , & [ ] ) , base) ;
145+ assert_eq ! ( with_roots( & literal ( ) , Some ( & base) , & [ ] ) , base) ;
156146 let go = json ! ( { "gopls.directoryFilters" : [ "+vendor" , "-cache" ] } ) ;
157147 assert_eq ! (
158- with_roots( "go" , Some ( & go) , & [ "vendor" . into( ) ] ) [ "gopls" ] [ "directoryFilters" ] ,
148+ with_roots( & glob ( ) , Some ( & go) , & [ "vendor" . into( ) ] ) [ "gopls" ] [ "directoryFilters" ] ,
159149 json!( [ "+vendor" , "-cache" , "-vendor" ] )
160150 ) ;
161151 assert_eq ! (
162152 with_roots(
163- "go" ,
153+ & glob ( ) ,
164154 Some ( & json!( { "gopls" : { "directoryFilters" : [ "-vendor" , "+vendor" ] } } ) ) ,
165155 & [ "vendor" . into( ) ]
166156 ) [ "gopls" ] [ "directoryFilters" ] ,
167157 json!( [ "+vendor" , "-vendor" ] )
168158 ) ;
169159 assert_eq ! (
170- with_roots( "go" , None , & [ "vendor" . into( ) ] ) [ "gopls" ] [ "directoryFilters" ] ,
171- json!( [ "-**/node_modules" , "-vendor" ] )
160+ with_roots( & glob ( ) , None , & [ "." . into ( ) , "vendor" . into( ) ] ) [ "gopls" ] [ "directoryFilters" ] ,
161+ json!( [ "-**/node_modules" , "-" , "- vendor"] )
172162 ) ;
173163 }
174164 #[ test]
175165 fn native_patterns_do_not_accidentally_exclude_literal_metacharacter_siblings ( ) {
176166 assert_eq ! (
177- with_roots( "go" , None , & [ "literal*" . into( ) ] ) [ "gopls" ] [ "directoryFilters" ] ,
167+ with_roots( & glob ( ) , None , & [ "literal*" . into( ) ] ) [ "gopls" ] [ "directoryFilters" ] ,
178168 json!( [ "-**/node_modules" ] )
179169 ) ;
180170 assert_eq ! (
181- with_roots( "rust" , None , & [ "literal*" . into( ) ] ) [ "rust-analyzer" ] [ "files" ] [ "exclude" ] ,
171+ with_roots( & literal ( ) , None , & [ "literal*" . into( ) ] ) [ "rust-analyzer" ] [ "files" ] [ "exclude" ] ,
182172 json!( [ "literal*" ] )
183173 ) ;
184174 }
175+
176+ /// A server whose setting takes literal paths, as rust-analyzer's
177+ /// `files.exclude` does.
178+ fn literal ( ) -> IndexingExclusions {
179+ serde_json:: from_value (
180+ json ! ( { "section" : "rust-analyzer.files.exclude" , "entry" : "${path}" } ) ,
181+ )
182+ . unwrap ( )
183+ }
184+
185+ /// A server whose setting takes ordered glob filters with a default, as
186+ /// gopls's `directoryFilters` does.
187+ fn glob ( ) -> IndexingExclusions {
188+ serde_json:: from_value ( json ! ( {
189+ "section" : "gopls.directoryFilters" ,
190+ "entry" : "-${path}" ,
191+ "rootEntry" : "-" ,
192+ "default" : [ "-**/node_modules" ] ,
193+ "pathSyntax" : "glob"
194+ } ) )
195+ . unwrap ( )
196+ }
185197}
0 commit comments