Skip to content

Commit

Permalink
gopls/internal/lsp/source: extract InDir to a new pathutil package
Browse files Browse the repository at this point in the history
Change-Id: Ib977563e100a536c7721a1cf5690f2f639e5f741
Reviewed-on: https://go-review.googlesource.com/c/tools/+/543918
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Nov 20, 2023
1 parent e7d61d9 commit ab6af7d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 56 deletions.
3 changes: 2 additions & 1 deletion gopls/internal/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"golang.org/x/tools/gopls/internal/file"
"golang.org/x/tools/gopls/internal/immutable"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/pathutil"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/tag"
"golang.org/x/tools/internal/gocommand"
Expand Down Expand Up @@ -759,7 +760,7 @@ func isTestMain(pkg *packages.Package, gocache string) bool {
if len(pkg.GoFiles) > 1 {
return false
}
if !InDir(gocache, pkg.GoFiles[0]) {
if !pathutil.InDir(gocache, pkg.GoFiles[0]) {
return false
}
return true
Expand Down
1 change: 0 additions & 1 deletion gopls/internal/lsp/cache/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ var (
RemoveIntermediateTestVariants = source.RemoveIntermediateTestVariants
IsCommandLineArguments = source.IsCommandLineArguments
BundleQuickFixes = source.BundleQuickFixes
InDir = source.InDir
SuggestedFixFromCommand = source.SuggestedFixFromCommand
ToProtocolEdits = source.ToProtocolEdits
NewFilterer = source.NewFilterer
Expand Down
15 changes: 8 additions & 7 deletions gopls/internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"golang.org/x/tools/gopls/internal/lsp/source/methodsets"
"golang.org/x/tools/gopls/internal/lsp/source/typerefs"
"golang.org/x/tools/gopls/internal/lsp/source/xrefs"
"golang.org/x/tools/gopls/internal/pathutil"
"golang.org/x/tools/gopls/internal/persistent"
"golang.org/x/tools/gopls/internal/settings"
"golang.org/x/tools/gopls/internal/vulncheck"
Expand Down Expand Up @@ -905,7 +906,7 @@ func (s *Snapshot) fileWatchingGlobPatterns(ctx context.Context) map[string]stru

// If GOWORK is outside the folder, ensure we are watching it.
gowork, _ := s.view.GOWORK()
if gowork != "" && !InDir(s.view.folder.Dir.Path(), gowork.Path()) {
if gowork != "" && !pathutil.InDir(s.view.folder.Dir.Path(), gowork.Path()) {
patterns[gowork.Path()] = struct{}{}
}

Expand All @@ -914,7 +915,7 @@ func (s *Snapshot) fileWatchingGlobPatterns(ctx context.Context) map[string]stru
for _, dir := range dirs {
// If the directory is within the view's folder, we're already watching
// it with the first pattern above.
if InDir(s.view.folder.Dir.Path(), dir) {
if pathutil.InDir(s.view.folder.Dir.Path(), dir) {
continue
}
// TODO(rstambler): If microsoft/vscode#3025 is resolved before
Expand Down Expand Up @@ -955,7 +956,7 @@ func (s *Snapshot) addKnownSubdirs(patterns map[string]unit, wsDirs []string) {

s.files.Dirs().Range(func(dir string) {
for _, wsDir := range wsDirs {
if InDir(wsDir, dir) {
if pathutil.InDir(wsDir, dir) {
patterns[dir] = unit{}
}
}
Expand Down Expand Up @@ -1029,7 +1030,7 @@ func (s *Snapshot) filesInDir(uri protocol.DocumentURI) []protocol.DocumentURI {
}
var files []protocol.DocumentURI
s.files.Range(func(uri protocol.DocumentURI, _ file.Handle) {
if InDir(dir, uri.Path()) {
if pathutil.InDir(dir, uri.Path()) {
files = append(files, uri)
}
})
Expand Down Expand Up @@ -1139,7 +1140,7 @@ func (s *Snapshot) GoModForFile(uri protocol.DocumentURI) protocol.DocumentURI {
func moduleForURI(modFiles map[protocol.DocumentURI]struct{}, uri protocol.DocumentURI) protocol.DocumentURI {
var match protocol.DocumentURI
for modURI := range modFiles {
if !InDir(filepath.Dir(modURI.Path()), uri.Path()) {
if !pathutil.InDir(filepath.Dir(modURI.Path()), uri.Path()) {
continue
}
if len(modURI) > len(match) {
Expand Down Expand Up @@ -1684,7 +1685,7 @@ searchOverlays:

// When the module is underneath the view dir, we offer
// "use all modules" quick-fixes.
inDir := InDir(viewDir, modDir)
inDir := pathutil.InDir(viewDir, modDir)

if rel, err := filepath.Rel(viewDir, modDir); err == nil {
modDir = rel
Expand Down Expand Up @@ -2179,7 +2180,7 @@ func deleteMostRelevantModFile(m *persistent.Map[protocol.DocumentURI, *memoize.

m.Range(func(modURI protocol.DocumentURI, _ *memoize.Promise) {
if len(modURI) > len(mostRelevant) {
if InDir(filepath.Dir(modURI.Path()), changedFile) {
if pathutil.InDir(filepath.Dir(modURI.Path()), changedFile) {
mostRelevant = modURI
}
}
Expand Down
11 changes: 6 additions & 5 deletions gopls/internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"golang.org/x/mod/semver"
"golang.org/x/tools/gopls/internal/file"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/pathutil"
"golang.org/x/tools/gopls/internal/settings"
"golang.org/x/tools/gopls/internal/vulncheck"
"golang.org/x/tools/internal/event"
Expand Down Expand Up @@ -568,10 +569,10 @@ func (v *View) contains(uri protocol.DocumentURI) bool {
// user. It would be better to explicitly consider the set of active modules
// wherever relevant.
inGoDir := false
if InDir(v.goCommandDir.Path(), v.folder.Dir.Path()) {
inGoDir = InDir(v.goCommandDir.Path(), uri.Path())
if pathutil.InDir(v.goCommandDir.Path(), v.folder.Dir.Path()) {
inGoDir = pathutil.InDir(v.goCommandDir.Path(), uri.Path())
}
inFolder := InDir(v.folder.Dir.Path(), uri.Path())
inFolder := pathutil.InDir(v.folder.Dir.Path(), uri.Path())

if !inGoDir && !inFolder {
return false
Expand All @@ -587,7 +588,7 @@ func (v *View) filterFunc() func(protocol.DocumentURI) bool {
filterer := buildFilterer(folderDir, v.gomodcache, v.folder.Options)
return func(uri protocol.DocumentURI) bool {
// Only filter relative to the configured root directory.
if InDir(folderDir, uri.Path()) {
if pathutil.InDir(folderDir, uri.Path()) {
return pathExcludedByFilter(strings.TrimPrefix(uri.Path(), folderDir), filterer)
}
return false
Expand Down Expand Up @@ -978,7 +979,7 @@ func getViewDefinition(ctx context.Context, runner *gocommand.Runner, fs file.So

// Check if the workspace is within any GOPATH directory.
for _, gp := range filepath.SplitList(def.gopath) {
if InDir(filepath.Join(gp, "src"), folder.Dir.Path()) {
if pathutil.InDir(filepath.Join(gp, "src"), folder.Dir.Path()) {
def.inGOPATH = true
break
}
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/lsp/fake/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"golang.org/x/tools/gopls/internal/lsp/command"
"golang.org/x/tools/gopls/internal/lsp/glob"
"golang.org/x/tools/gopls/internal/lsp/protocol"
"golang.org/x/tools/gopls/internal/lsp/source"
"golang.org/x/tools/gopls/internal/pathutil"
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/jsonrpc2/servertest"
"golang.org/x/tools/internal/xcontext"
Expand Down Expand Up @@ -1302,7 +1302,7 @@ func (e *Editor) renameBuffers(ctx context.Context, oldPath, newPath string) (cl

for path := range e.buffers {
abs := e.sandbox.Workdir.AbsPath(path)
if oldAbs == abs || source.InDir(oldAbs, abs) {
if oldAbs == abs || pathutil.InDir(oldAbs, abs) {
rel, err := filepath.Rel(oldAbs, abs)
if err != nil {
return nil, nil, fmt.Errorf("filepath.Rel(%q, %q): %v", oldAbs, abs, err)
Expand Down
40 changes: 0 additions & 40 deletions gopls/internal/lsp/source/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"go/printer"
"go/token"
"go/types"
"path/filepath"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -424,45 +423,6 @@ func isDirective(c string) bool {
return true
}

// InDir checks whether path is in the file tree rooted at dir.
// It checks only the lexical form of the file names.
// It does not consider symbolic links.
//
// Copied from go/src/cmd/go/internal/search/search.go.
func InDir(dir, path string) bool {
pv := strings.ToUpper(filepath.VolumeName(path))
dv := strings.ToUpper(filepath.VolumeName(dir))
path = path[len(pv):]
dir = dir[len(dv):]
switch {
default:
return false
case pv != dv:
return false
case len(path) == len(dir):
if path == dir {
return true
}
return false
case dir == "":
return path != ""
case len(path) > len(dir):
if dir[len(dir)-1] == filepath.Separator {
if path[:len(dir)] == dir {
return path[len(dir):] != ""
}
return false
}
if path[len(dir)] == filepath.Separator && path[:len(dir)] == dir {
if len(path) == len(dir)+1 {
return true
}
return path[len(dir)+1:] != ""
}
return false
}
}

// IsValidImport returns whether importPkgPath is importable
// by pkgPath
func IsValidImport(pkgPath, importPkgPath PackagePath) bool {
Expand Down
49 changes: 49 additions & 0 deletions gopls/internal/pathutil/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pathutil

import (
"path/filepath"
"strings"
)

// InDir checks whether path is in the file tree rooted at dir.
// It checks only the lexical form of the file names.
// It does not consider symbolic links.
//
// Copied from go/src/cmd/go/internal/search/search.go.
func InDir(dir, path string) bool {
pv := strings.ToUpper(filepath.VolumeName(path))
dv := strings.ToUpper(filepath.VolumeName(dir))
path = path[len(pv):]
dir = dir[len(dv):]
switch {
default:
return false
case pv != dv:
return false
case len(path) == len(dir):
if path == dir {
return true
}
return false
case dir == "":
return path != ""
case len(path) > len(dir):
if dir[len(dir)-1] == filepath.Separator {
if path[:len(dir)] == dir {
return path[len(dir):] != ""
}
return false
}
if path[len(dir)] == filepath.Separator && path[:len(dir)] == dir {
if len(path) == len(dir)+1 {
return true
}
return path[len(dir)+1:] != ""
}
return false
}
}

0 comments on commit ab6af7d

Please sign in to comment.