-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/lsp/source: extract InDir to a new pathutil package
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
Showing
7 changed files
with
67 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |