| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package work |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io/fs" |
| 12 |
"os" |
| 13 |
"path/filepath" |
| 14 |
"sort" |
| 15 |
"strings" |
| 16 |
|
| 17 |
"golang.org/x/tools/gopls/internal/cache" |
| 18 |
"golang.org/x/tools/gopls/internal/file" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol" |
| 20 |
"golang.org/x/tools/internal/event" |
| 21 |
) |
| 22 |
|
| 23 |
func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, position protocol.Position) (*protocol.CompletionList, error) { |
| 24 |
ctx, done := event.Start(ctx, "work.Completion") |
| 25 |
defer done() |
| 26 |
|
| 27 |
|
| 28 |
pw, err := snapshot.ParseWork(ctx, fh) |
| 29 |
if err != nil { |
| 30 |
return nil, fmt.Errorf("getting go.work file handle: %w", err) |
| 31 |
} |
| 32 |
cursor, err := pw.Mapper.PositionOffset(position) |
| 33 |
if err != nil { |
| 34 |
return nil, fmt.Errorf("computing cursor offset: %w", err) |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
use, pathStart, _ := usePath(pw, cursor, cursor) |
| 39 |
if use == nil { |
| 40 |
return &protocol.CompletionList{}, nil |
| 41 |
} |
| 42 |
completingFrom := use.Path[:cursor-pathStart] |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pathPrefixSlash := completingFrom |
| 55 |
pathPrefixAbs := filepath.FromSlash(pathPrefixSlash) |
| 56 |
if !filepath.IsAbs(pathPrefixAbs) { |
| 57 |
pathPrefixAbs = filepath.Join(pw.URI.DirPath(), pathPrefixAbs) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
depthBound := 5 |
| 65 |
pathPrefixDir, pathPrefixBase := pathPrefixAbs, "" |
| 66 |
pathPrefixSlashDir := pathPrefixSlash |
| 67 |
if filepath.Clean(pathPrefixSlash) != "." && !strings.HasSuffix(pathPrefixSlash, "/") { |
| 68 |
depthBound++ |
| 69 |
pathPrefixDir, pathPrefixBase = filepath.Split(pathPrefixAbs) |
| 70 |
pathPrefixSlashDir = dirNonClean(pathPrefixSlash) |
| 71 |
} |
| 72 |
|
| 73 |
var completions []string |
| 74 |
|
| 75 |
const numSeenBound = 10000 |
| 76 |
var numSeen int |
| 77 |
stopWalking := errors.New("hit numSeenBound") |
| 78 |
err = filepath.WalkDir(pathPrefixDir, func(wpath string, entry fs.DirEntry, err error) error { |
| 79 |
if err != nil { |
| 80 |
|
| 81 |
|
| 82 |
return nil |
| 83 |
} |
| 84 |
if numSeen > numSeenBound { |
| 85 |
|
| 86 |
return stopWalking |
| 87 |
} |
| 88 |
numSeen++ |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
rel := strings.TrimPrefix(wpath[len(pathPrefixDir):], string(filepath.Separator)) |
| 95 |
if entry.IsDir() && wpath != pathPrefixDir && !strings.HasPrefix(rel, pathPrefixBase) { |
| 96 |
return filepath.SkipDir |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
if filepath.Base(rel) == "go.mod" { |
| 101 |
relDir := strings.TrimSuffix(dirNonClean(rel), string(os.PathSeparator)) |
| 102 |
completionPath := join(pathPrefixSlashDir, filepath.ToSlash(relDir)) |
| 103 |
|
| 104 |
if !strings.HasPrefix(completionPath, completingFrom) { |
| 105 |
return nil |
| 106 |
} |
| 107 |
if strings.HasSuffix(completionPath, "/") { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
return nil |
| 112 |
} |
| 113 |
completion := completionPath[len(completingFrom):] |
| 114 |
if completingFrom == "" && !strings.HasPrefix(completion, "./") { |
| 115 |
|
| 116 |
completion = join(".", completion) |
| 117 |
} |
| 118 |
|
| 119 |
completions = append(completions, completion) |
| 120 |
} |
| 121 |
|
| 122 |
if depth := strings.Count(rel, string(filepath.Separator)); depth >= depthBound { |
| 123 |
return filepath.SkipDir |
| 124 |
} |
| 125 |
return nil |
| 126 |
}) |
| 127 |
if err != nil && !errors.Is(err, stopWalking) { |
| 128 |
return nil, fmt.Errorf("walking to find completions: %w", err) |
| 129 |
} |
| 130 |
|
| 131 |
sort.Strings(completions) |
| 132 |
|
| 133 |
items := []protocol.CompletionItem{} |
| 134 |
for _, c := range completions { |
| 135 |
items = append(items, protocol.CompletionItem{ |
| 136 |
Label: c, |
| 137 |
InsertText: c, |
| 138 |
}) |
| 139 |
} |
| 140 |
return &protocol.CompletionList{Items: items}, nil |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func dirNonClean(path string) string { |
| 145 |
vol := filepath.VolumeName(path) |
| 146 |
i := len(path) - 1 |
| 147 |
for i >= len(vol) && !os.IsPathSeparator(path[i]) { |
| 148 |
i-- |
| 149 |
} |
| 150 |
return path[len(vol) : i+1] |
| 151 |
} |
| 152 |
|
| 153 |
func join(a, b string) string { |
| 154 |
if a == "" { |
| 155 |
return b |
| 156 |
} |
| 157 |
if b == "" { |
| 158 |
return a |
| 159 |
} |
| 160 |
return strings.TrimSuffix(a, "/") + "/" + b |
| 161 |
} |