| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package server |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"path/filepath" |
| 13 |
"strings" |
| 14 |
"sync" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/cache" |
| 17 |
"golang.org/x/tools/gopls/internal/file" |
| 18 |
"golang.org/x/tools/gopls/internal/golang" |
| 19 |
"golang.org/x/tools/gopls/internal/label" |
| 20 |
"golang.org/x/tools/gopls/internal/protocol" |
| 21 |
"golang.org/x/tools/internal/event" |
| 22 |
"golang.org/x/tools/internal/jsonrpc2" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
type ModificationSource int |
| 27 |
|
| 28 |
const ( |
| 29 |
|
| 30 |
FromDidOpen = ModificationSource(iota) |
| 31 |
|
| 32 |
|
| 33 |
FromDidChange |
| 34 |
|
| 35 |
|
| 36 |
FromDidChangeWatchedFiles |
| 37 |
|
| 38 |
|
| 39 |
FromDidSave |
| 40 |
|
| 41 |
|
| 42 |
FromDidClose |
| 43 |
|
| 44 |
|
| 45 |
FromDidChangeConfiguration |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
FromRegenerateCgo |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
FromInitialWorkspaceLoad |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
FromCheckUpgrades |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
FromResetGoModDiagnostics |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
FromToggleCompilerOptDetails |
| 66 |
) |
| 67 |
|
| 68 |
func (m ModificationSource) String() string { |
| 69 |
switch m { |
| 70 |
case FromDidOpen: |
| 71 |
return "opened files" |
| 72 |
case FromDidChange: |
| 73 |
return "changed files" |
| 74 |
case FromDidChangeWatchedFiles: |
| 75 |
return "files changed on disk" |
| 76 |
case FromDidSave: |
| 77 |
return "saved files" |
| 78 |
case FromDidClose: |
| 79 |
return "close files" |
| 80 |
case FromRegenerateCgo: |
| 81 |
return "regenerate cgo" |
| 82 |
case FromInitialWorkspaceLoad: |
| 83 |
return "initial workspace load" |
| 84 |
case FromCheckUpgrades: |
| 85 |
return "from check upgrades" |
| 86 |
case FromResetGoModDiagnostics: |
| 87 |
return "from resetting go.mod diagnostics" |
| 88 |
default: |
| 89 |
return "unknown file modification" |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error { |
| 94 |
ctx, done := event.Start(ctx, "server.DidOpen", label.URI.Of(params.TextDocument.URI)) |
| 95 |
defer done() |
| 96 |
|
| 97 |
uri := params.TextDocument.URI |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if len(s.session.Views()) == 0 { |
| 107 |
dir := uri.DirPath() |
| 108 |
s.addFolders(ctx, []protocol.WorkspaceFolder{{ |
| 109 |
URI: string(protocol.URIFromPath(dir)), |
| 110 |
Name: filepath.Base(dir), |
| 111 |
}}) |
| 112 |
} |
| 113 |
return s.didModifyFiles(ctx, FromDidOpen, file.Modification{ |
| 114 |
URI: uri, |
| 115 |
Action: file.Open, |
| 116 |
Version: params.TextDocument.Version, |
| 117 |
Text: []byte(params.TextDocument.Text), |
| 118 |
LanguageID: params.TextDocument.LanguageID, |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
func (s *server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error { |
| 123 |
ctx, done := event.Start(ctx, "server.DidChange", label.URI.Of(params.TextDocument.URI)) |
| 124 |
defer done() |
| 125 |
|
| 126 |
uri := params.TextDocument.URI |
| 127 |
text, err := s.changedText(ctx, uri, params.ContentChanges) |
| 128 |
if err != nil { |
| 129 |
return err |
| 130 |
} |
| 131 |
return s.didModifyFiles(ctx, FromDidChange, file.Modification{ |
| 132 |
URI: uri, |
| 133 |
Action: file.Change, |
| 134 |
Version: params.TextDocument.Version, |
| 135 |
Text: text, |
| 136 |
}) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
func (s *server) warnAboutModifyingGeneratedFiles(ctx context.Context, uri protocol.DocumentURI) error { |
| 142 |
s.changedFilesMu.Lock() |
| 143 |
_, ok := s.changedFiles[uri] |
| 144 |
if !ok { |
| 145 |
s.changedFiles[uri] = struct{}{} |
| 146 |
} |
| 147 |
s.changedFilesMu.Unlock() |
| 148 |
|
| 149 |
|
| 150 |
if ok { |
| 151 |
return nil |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
snapshot, release, err := s.session.SnapshotOf(ctx, uri) |
| 160 |
if err != nil { |
| 161 |
return err |
| 162 |
} |
| 163 |
isGenerated := golang.IsGenerated(ctx, snapshot, uri) |
| 164 |
release() |
| 165 |
if isGenerated { |
| 166 |
msg := fmt.Sprintf("Warning: editing %s, a generated file.", uri.Base()) |
| 167 |
showMessage(ctx, s.client, protocol.Warning, msg) |
| 168 |
} |
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
func (s *server) DidChangeWatchedFiles(ctx context.Context, params *protocol.DidChangeWatchedFilesParams) error { |
| 173 |
ctx, done := event.Start(ctx, "server.DidChangeWatchedFiles") |
| 174 |
defer done() |
| 175 |
|
| 176 |
modifications := make([]file.Modification, len(params.Changes)) |
| 177 |
for i, change := range params.Changes { |
| 178 |
action := changeTypeToFileAction(change.Type) |
| 179 |
modifications[i] = file.Modification{ |
| 180 |
URI: change.URI, |
| 181 |
Action: action, |
| 182 |
OnDisk: true, |
| 183 |
} |
| 184 |
} |
| 185 |
return s.didModifyFiles(ctx, FromDidChangeWatchedFiles, modifications...) |
| 186 |
} |
| 187 |
|
| 188 |
func (s *server) DidSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error { |
| 189 |
ctx, done := event.Start(ctx, "server.DidSave", label.URI.Of(params.TextDocument.URI)) |
| 190 |
defer done() |
| 191 |
|
| 192 |
var text []byte |
| 193 |
if params.Text != nil { |
| 194 |
text = []byte(*params.Text) |
| 195 |
} |
| 196 |
return s.didModifyFiles(ctx, FromDidSave, file.Modification{ |
| 197 |
URI: params.TextDocument.URI, |
| 198 |
Action: file.Save, |
| 199 |
Text: text, |
| 200 |
}) |
| 201 |
} |
| 202 |
|
| 203 |
func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error { |
| 204 |
ctx, done := event.Start(ctx, "server.DidClose", label.URI.Of(params.TextDocument.URI)) |
| 205 |
defer done() |
| 206 |
|
| 207 |
return s.didModifyFiles(ctx, FromDidClose, file.Modification{ |
| 208 |
URI: params.TextDocument.URI, |
| 209 |
Action: file.Close, |
| 210 |
Version: -1, |
| 211 |
Text: nil, |
| 212 |
}) |
| 213 |
} |
| 214 |
|
| 215 |
func (s *server) didModifyFiles(ctx context.Context, cause ModificationSource, modifications ...file.Modification) error { |
| 216 |
|
| 217 |
s.fileWatcherMu.Lock() |
| 218 |
if s.fileWatcher != nil { |
| 219 |
s.fileWatcher.Poke() |
| 220 |
} |
| 221 |
s.fileWatcherMu.Unlock() |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
var wg sync.WaitGroup |
| 233 |
wg.Add(1) |
| 234 |
defer wg.Done() |
| 235 |
|
| 236 |
if s.Options().VerboseWorkDoneProgress { |
| 237 |
work := s.progress.Start(ctx, DiagnosticWorkTitle(cause), "Calculating file diagnostics...", nil, nil) |
| 238 |
go func() { |
| 239 |
wg.Wait() |
| 240 |
work.End(ctx, "Done.") |
| 241 |
}() |
| 242 |
} |
| 243 |
|
| 244 |
s.stateMu.Lock() |
| 245 |
if s.state >= serverShutDown { |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
s.stateMu.Unlock() |
| 250 |
return errors.New("server is shut down") |
| 251 |
} |
| 252 |
s.stateMu.Unlock() |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
modifications = s.session.ExpandModificationsToDirectories(ctx, modifications) |
| 257 |
|
| 258 |
s.handleModuleChanges(ctx, modifications, cause) |
| 259 |
|
| 260 |
viewsToDiagnose, err := s.session.DidModifyFiles(ctx, modifications) |
| 261 |
if err != nil { |
| 262 |
return err |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
for _, mod := range modifications { |
| 267 |
s.mustPublishDiagnostics(mod.URI) |
| 268 |
|
| 269 |
if cause == FromDidChange && mod.Action == file.Change { |
| 270 |
if err := s.warnAboutModifyingGeneratedFiles(ctx, mod.URI); err != nil { |
| 271 |
return err |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
modCtx, modID := s.needsDiagnosis(ctx, viewsToDiagnose) |
| 277 |
|
| 278 |
wg.Go(func() { |
| 279 |
s.diagnoseChangedViews(modCtx, modID, viewsToDiagnose, cause) |
| 280 |
}) |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
return s.updateWatchedDirectories(ctx) |
| 286 |
} |
| 287 |
|
| 288 |
func (s *server) handleModuleChanges(ctx context.Context, modifications []file.Modification, cause ModificationSource) { |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
if cause == FromDidChangeWatchedFiles { |
| 298 |
for _, m := range modifications { |
| 299 |
switch m.URI.Base() { |
| 300 |
case "go.mod", "go.sum", "go.work", "go.work.sum": |
| 301 |
|
| 302 |
default: |
| 303 |
|
| 304 |
return |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
uris := make(map[protocol.DocumentURI]struct{}) |
| 310 |
for _, m := range modifications { |
| 311 |
if (m.URI.Base() == "go.mod" || m.URI.Base() == "go.work") && (m.Action == file.Create || m.Action == file.Save) { |
| 312 |
uris[m.URI] = struct{}{} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
for uri := range uris { |
| 317 |
go s.checkDependencyChanges(ctx, uri) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
func (s *server) needsDiagnosis(ctx context.Context, viewsToDiagnose map[*cache.View][]protocol.DocumentURI) (context.Context, uint64) { |
| 326 |
s.modificationMu.Lock() |
| 327 |
defer s.modificationMu.Unlock() |
| 328 |
if s.cancelPrevDiagnostics != nil { |
| 329 |
s.cancelPrevDiagnostics() |
| 330 |
} |
| 331 |
modCtx := context.WithoutCancel(ctx) |
| 332 |
modCtx, s.cancelPrevDiagnostics = context.WithCancel(modCtx) |
| 333 |
s.lastModificationID++ |
| 334 |
modID := s.lastModificationID |
| 335 |
|
| 336 |
for v := range viewsToDiagnose { |
| 337 |
if needs, ok := s.viewsToDiagnose[v]; !ok || needs < modID { |
| 338 |
s.viewsToDiagnose[v] = modID |
| 339 |
} |
| 340 |
} |
| 341 |
return modCtx, modID |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
func DiagnosticWorkTitle(cause ModificationSource) string { |
| 347 |
return fmt.Sprintf("diagnosing %v", cause) |
| 348 |
} |
| 349 |
|
| 350 |
func (s *server) changedText(ctx context.Context, uri protocol.DocumentURI, changes []protocol.TextDocumentContentChangeEvent) ([]byte, error) { |
| 351 |
if len(changes) == 0 { |
| 352 |
return nil, fmt.Errorf("%w: no content changes provided", jsonrpc2.ErrInternal) |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
if len(changes) == 1 && changes[0].Range == nil && changes[0].RangeLength == nil { |
| 358 |
changeFull.Inc() |
| 359 |
return []byte(changes[0].Text), nil |
| 360 |
} |
| 361 |
return s.applyIncrementalChanges(ctx, uri, changes) |
| 362 |
} |
| 363 |
|
| 364 |
func (s *server) applyIncrementalChanges(ctx context.Context, uri protocol.DocumentURI, changes []protocol.TextDocumentContentChangeEvent) ([]byte, error) { |
| 365 |
fh, err := s.session.ReadFile(ctx, uri) |
| 366 |
if err != nil { |
| 367 |
return nil, err |
| 368 |
} |
| 369 |
content, err := fh.Content() |
| 370 |
if err != nil { |
| 371 |
return nil, fmt.Errorf("%w: file not found (%v)", jsonrpc2.ErrInternal, err) |
| 372 |
} |
| 373 |
for i, change := range changes { |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
m := protocol.NewMapper(uri, content) |
| 379 |
if change.Range == nil { |
| 380 |
return nil, fmt.Errorf("%w: unexpected nil range for change", jsonrpc2.ErrInternal) |
| 381 |
} |
| 382 |
start, end, err := m.RangeOffsets(*change.Range) |
| 383 |
if err != nil { |
| 384 |
return nil, err |
| 385 |
} |
| 386 |
if end < start { |
| 387 |
return nil, fmt.Errorf("%w: invalid range for content change", jsonrpc2.ErrInternal) |
| 388 |
} |
| 389 |
var buf bytes.Buffer |
| 390 |
buf.Write(content[:start]) |
| 391 |
buf.WriteString(change.Text) |
| 392 |
buf.Write(content[end:]) |
| 393 |
content = buf.Bytes() |
| 394 |
if i == 0 { |
| 395 |
|
| 396 |
s.checkEfficacy(fh.URI(), fh.Version(), change) |
| 397 |
} |
| 398 |
} |
| 399 |
return content, nil |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
func (s *server) checkEfficacy(uri protocol.DocumentURI, version int32, change protocol.TextDocumentContentChangePartial) { |
| 404 |
s.efficacyMu.Lock() |
| 405 |
defer s.efficacyMu.Unlock() |
| 406 |
if s.efficacyURI != uri { |
| 407 |
return |
| 408 |
} |
| 409 |
|
| 410 |
if version != s.efficacyVersion && version != s.efficacyVersion+1 { |
| 411 |
return |
| 412 |
} |
| 413 |
|
| 414 |
for _, item := range s.efficacyItems { |
| 415 |
if item.TextEdit == nil { |
| 416 |
continue |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
edit, err := protocol.SelectCompletionTextEdit(item, false) |
| 427 |
if err != nil { |
| 428 |
continue |
| 429 |
} |
| 430 |
if edit.Range.Start == change.Range.Start { |
| 431 |
|
| 432 |
if (change.RangeLength == nil || *change.RangeLength == 0) && len(change.Text) == 1 { |
| 433 |
|
| 434 |
continue |
| 435 |
} |
| 436 |
ix := strings.Index(edit.NewText, "$") |
| 437 |
if ix < 0 && strings.HasPrefix(change.Text, edit.NewText) { |
| 438 |
|
| 439 |
complUsed.Inc() |
| 440 |
return |
| 441 |
} |
| 442 |
if ix > 1 && strings.HasPrefix(change.Text, edit.NewText[:ix]) { |
| 443 |
|
| 444 |
complUsed.Inc() |
| 445 |
return |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
complUnused.Inc() |
| 450 |
} |
| 451 |
|
| 452 |
func changeTypeToFileAction(ct protocol.FileChangeType) file.Action { |
| 453 |
switch ct { |
| 454 |
case protocol.Changed: |
| 455 |
return file.Change |
| 456 |
case protocol.Created: |
| 457 |
return file.Create |
| 458 |
case protocol.Deleted: |
| 459 |
return file.Delete |
| 460 |
} |
| 461 |
return file.UnknownAction |
| 462 |
} |