LSP-server-example
48 строк · 1.1 Кб
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.
4package protocol
5
6import (
7"path/filepath"
8"strings"
9)
10
11// InDir checks whether path is in the file tree rooted at dir.
12// It checks only the lexical form of the file names.
13// It does not consider symbolic links.
14//
15// Copied from go/src/cmd/go/internal/search/search.go.
16func InDir(dir, path string) bool {
17pv := strings.ToUpper(filepath.VolumeName(path))
18dv := strings.ToUpper(filepath.VolumeName(dir))
19path = path[len(pv):]
20dir = dir[len(dv):]
21switch {
22default:
23return false
24case pv != dv:
25return false
26case len(path) == len(dir):
27if path == dir {
28return true
29}
30return false
31case dir == "":
32return path != ""
33case len(path) > len(dir):
34if dir[len(dir)-1] == filepath.Separator {
35if path[:len(dir)] == dir {
36return path[len(dir):] != ""
37}
38return false
39}
40if path[len(dir)] == filepath.Separator && path[:len(dir)] == dir {
41if len(path) == len(dir)+1 {
42return true
43}
44return path[len(dir)+1:] != ""
45}
46return false
47}
48}
49