golang.org/x/tools/gopls@v0.15.3/internal/cache/filterer.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cache 6 7 import ( 8 "path" 9 "path/filepath" 10 "regexp" 11 "strings" 12 ) 13 14 type Filterer struct { 15 // Whether a filter is excluded depends on the operator (first char of the raw filter). 16 // Slices filters and excluded then should have the same length. 17 filters []*regexp.Regexp 18 excluded []bool 19 } 20 21 // NewFilterer computes regular expression form of all raw filters 22 func NewFilterer(rawFilters []string) *Filterer { 23 var f Filterer 24 for _, filter := range rawFilters { 25 filter = path.Clean(filepath.ToSlash(filter)) 26 // TODO(dungtuanle): fix: validate [+-] prefix. 27 op, prefix := filter[0], filter[1:] 28 // convertFilterToRegexp adds "/" at the end of prefix to handle cases where a filter is a prefix of another filter. 29 // For example, it prevents [+foobar, -foo] from excluding "foobar". 30 f.filters = append(f.filters, convertFilterToRegexp(filepath.ToSlash(prefix))) 31 f.excluded = append(f.excluded, op == '-') 32 } 33 34 return &f 35 } 36 37 // Disallow return true if the path is excluded from the filterer's filters. 38 func (f *Filterer) Disallow(path string) bool { 39 // Ensure trailing but not leading slash. 40 path = strings.TrimPrefix(path, "/") 41 if !strings.HasSuffix(path, "/") { 42 path += "/" 43 } 44 45 // TODO(adonovan): opt: iterate in reverse and break at first match. 46 excluded := false 47 for i, filter := range f.filters { 48 if filter.MatchString(path) { 49 excluded = f.excluded[i] // last match wins 50 } 51 } 52 return excluded 53 } 54 55 // convertFilterToRegexp replaces glob-like operator substrings in a string file path to their equivalent regex forms. 56 // Supporting glob-like operators: 57 // - **: match zero or more complete path segments 58 func convertFilterToRegexp(filter string) *regexp.Regexp { 59 if filter == "" { 60 return regexp.MustCompile(".*") 61 } 62 var ret strings.Builder 63 ret.WriteString("^") 64 segs := strings.Split(filter, "/") 65 for _, seg := range segs { 66 // Inv: seg != "" since path is clean. 67 if seg == "**" { 68 ret.WriteString(".*") 69 } else { 70 ret.WriteString(regexp.QuoteMeta(seg)) 71 } 72 ret.WriteString("/") 73 } 74 pattern := ret.String() 75 76 // Remove unnecessary "^.*" prefix, which increased 77 // BenchmarkWorkspaceSymbols time by ~20% (even though 78 // filter CPU time increased by only by ~2.5%) when the 79 // default filter was changed to "**/node_modules". 80 pattern = strings.TrimPrefix(pattern, "^.*") 81 82 return regexp.MustCompile(pattern) 83 }