Debian Codegraph @

gopls/internal/cache/filterer.go

raw symbols SCIP
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 // PathIncludeFunc creates a function that determines if a given file path
15 // should be included based on a set of inclusion/exclusion rules.
16 //
17 // The `rules` parameter is a slice of strings, where each string represents a
18 // filtering rule. Each rule consists of an operator (`+` for inclusion, `-`
19 // for exclusion) followed by a path pattern. See more detail of rules syntax
20 // at [settings.BuildOptions.DirectoryFilters].
21 //
22 // Rules are evaluated in order, and the last matching rule determines
23 // whether a path is included or excluded.
24 //
25 // Examples:
26 // - []{"-foo"}: Exclude "foo" at the current depth.
27 // - []{"-**foo"}: Exclude "foo" at any depth.
28 // - []{"+bar"}: Include "bar" at the current depth.
29 // - []{"-foo", "+foo/**/bar"}: Exclude all "foo" at current depth except
30 // directory "bar" under "foo" at any depth.
31 func PathIncludeFunc(rules []string) func(string) bool {
32 var matchers []*regexp.Regexp
33 var included []bool
34 for _, filter := range rules {
35 filter = path.Clean(filepath.ToSlash(filter))
36 // TODO(dungtuanle): fix: validate [+-] prefix.
37 op, prefix := filter[0], filter[1:]
38 // convertFilterToRegexp adds "/" at the end of prefix to handle cases
39 // where a filter is a prefix of another filter.
40 // For example, it prevents [+foobar, -foo] from excluding "foobar".
41 matchers = append(matchers, convertFilterToRegexp(filepath.ToSlash(prefix)))
42 included = append(included, op == '+')
43 }
44
45 return func(path string) bool {
46 // Ensure leading and trailing slashes.
47 if !strings.HasPrefix(path, "/") {
48 path = "/" + path
49 }
50 if !strings.HasSuffix(path, "/") {
51 path += "/"
52 }
53
54 // TODO(adonovan): opt: iterate in reverse and break at first match.
55 include := true
56 for i, filter := range matchers {
57 if filter.MatchString(path) {
58 include = included[i] // last match wins
59 }
60 }
61 return include
62 }
63 }
64
65 // convertFilterToRegexp replaces glob-like operator substrings in a string file path to their equivalent regex forms.
66 // Supporting glob-like operators:
67 // - **: match zero or more complete path segments
68 func convertFilterToRegexp(filter string) *regexp.Regexp {
69 if filter == "" {
70 return regexp.MustCompile(".*")
71 }
72 var ret strings.Builder
73 ret.WriteString("^/")
74 segs := strings.SplitSeq(filter, "/")
75 for seg := range segs {
76 // Inv: seg != "" since path is clean.
77 if seg == "**" {
78 ret.WriteString(".*")
79 } else {
80 ret.WriteString(regexp.QuoteMeta(seg))
81 }
82 ret.WriteString("/")
83 }
84 pattern := ret.String()
85
86 // Remove unnecessary "^.*" prefix, which increased
87 // BenchmarkWorkspaceSymbols time by ~20% (even though
88 // filter CPU time increased by only by ~2.5%) when the
89 // default filter was changed to "**/node_modules".
90 pattern = strings.TrimPrefix(pattern, "^/.*")
91
92 return regexp.MustCompile(pattern)
93 }