1 // Copyright 2019 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.
14 "golang.org/x/tools/internal/jsonrpc2"
15 "golang.org/x/tools/internal/lsp/protocol"
16 "golang.org/x/tools/internal/lsp/source"
17 "golang.org/x/tools/internal/span"
18 errors "golang.org/x/xerrors"
21 // ModificationSource identifies the originating cause of a file modification.
22 type ModificationSource int
25 // FromDidOpen is a file modification caused by opening a file.
26 FromDidOpen = ModificationSource(iota)
28 // FromDidChange is a file modification caused by changing a file.
31 // FromDidChangeWatchedFiles is a file modification caused by a change to a
33 FromDidChangeWatchedFiles
35 // FromDidSave is a file modification caused by a file save.
38 // FromDidClose is a file modification caused by closing a file.
41 // FromRegenerateCgo refers to file modifications caused by regenerating
42 // the cgo sources for the workspace.
45 // FromInitialWorkspaceLoad refers to the loading of all packages in the
46 // workspace when the view is first created.
47 FromInitialWorkspaceLoad
50 func (m ModificationSource) String() string {
55 return "changed files"
56 case FromDidChangeWatchedFiles:
57 return "files changed on disk"
62 case FromRegenerateCgo:
63 return "regenerate cgo"
64 case FromInitialWorkspaceLoad:
65 return "initial workspace load"
67 return "unknown file modification"
71 func (s *Server) didOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
72 uri := params.TextDocument.URI.SpanURI()
76 // There may not be any matching view in the current session. If that's
77 // the case, try creating a new view based on the opened file path.
79 // TODO(rstambler): This seems like it would continuously add new
80 // views, but it won't because ViewOf only returns an error when there
81 // are no views in the session. I don't know if that logic should go
82 // here, or if we can continue to rely on that implementation detail.
83 if _, err := s.session.ViewOf(uri); err != nil {
84 dir := filepath.Dir(uri.Filename())
85 if err := s.addFolders(ctx, []protocol.WorkspaceFolder{{
86 URI: string(protocol.URIFromPath(dir)),
87 Name: filepath.Base(dir),
92 return s.didModifyFiles(ctx, []source.FileModification{{
95 Version: params.TextDocument.Version,
96 Text: []byte(params.TextDocument.Text),
97 LanguageID: params.TextDocument.LanguageID,
101 func (s *Server) didChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
102 uri := params.TextDocument.URI.SpanURI()
107 text, err := s.changedText(ctx, uri, params.ContentChanges)
111 c := source.FileModification{
113 Action: source.Change,
114 Version: params.TextDocument.Version,
117 if err := s.didModifyFiles(ctx, []source.FileModification{c}, FromDidChange); err != nil {
121 s.changedFilesMu.Lock()
122 defer s.changedFilesMu.Unlock()
124 s.changedFiles[uri] = struct{}{}
128 func (s *Server) didChangeWatchedFiles(ctx context.Context, params *protocol.DidChangeWatchedFilesParams) error {
129 var modifications []source.FileModification
130 for _, change := range params.Changes {
131 uri := change.URI.SpanURI()
135 action := changeTypeToFileAction(change.Type)
136 modifications = append(modifications, source.FileModification{
142 return s.didModifyFiles(ctx, modifications, FromDidChangeWatchedFiles)
145 func (s *Server) didSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error {
146 uri := params.TextDocument.URI.SpanURI()
150 c := source.FileModification{
153 Version: params.TextDocument.Version,
155 if params.Text != nil {
156 c.Text = []byte(*params.Text)
158 return s.didModifyFiles(ctx, []source.FileModification{c}, FromDidSave)
161 func (s *Server) didClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
162 uri := params.TextDocument.URI.SpanURI()
166 return s.didModifyFiles(ctx, []source.FileModification{
169 Action: source.Close,
176 func (s *Server) didModifyFiles(ctx context.Context, modifications []source.FileModification, cause ModificationSource) error {
177 // diagnosticWG tracks outstanding diagnostic work as a result of this file
179 var diagnosticWG sync.WaitGroup
180 if s.session.Options().VerboseWorkDoneProgress {
181 work := s.progress.start(ctx, DiagnosticWorkTitle(cause), "Calculating file diagnostics...", nil, nil)
189 views, snapshots, releases, deletions, err := s.session.DidModifyFiles(ctx, modifications)
194 // Clear out diagnostics for deleted files.
195 for _, uri := range deletions {
196 if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
197 URI: protocol.URIFromSpanURI(uri),
198 Diagnostics: []protocol.Diagnostic{},
205 // Check if the user is trying to modify a generated file.
206 for _, mod := range modifications {
207 if mod.OnDisk || mod.Action != source.Change {
210 snapshot := snapshots[views[mod.URI]]
212 panic("no snapshot assigned for file " + mod.URI)
214 // Ideally, we should be able to specify that a generated file should be opened as read-only.
215 // Tell the user that they should not be editing a generated file.
216 if s.wasFirstChange(mod.URI) && source.IsGenerated(ctx, snapshot, mod.URI) {
217 if err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
218 Message: fmt.Sprintf("Do not edit this file! %s is a generated file.", mod.URI.Filename()),
219 Type: protocol.Warning,
226 // Group files by best view and diagnose them.
227 viewURIs := map[source.View][]span.URI{}
228 for uri, view := range views {
229 viewURIs[view] = append(viewURIs[view], uri)
231 for view, uris := range viewURIs {
233 go func(snapshot source.Snapshot, uris []span.URI) {
234 defer diagnosticWG.Done()
235 s.diagnoseSnapshot(snapshot, uris)
236 }(snapshots[view], uris)
241 for _, release := range releases {
246 // After any file modifications, we need to update our watched files,
247 // in case something changed. Compute the new set of directories to watch,
248 // and if it differs from the current set, send updated registrations.
249 if err := s.updateWatchedDirectories(ctx, snapshots); err != nil {
255 // DiagnosticWorkTitle returns the title of the diagnostic work resulting from a
256 // file change originating from the given cause.
257 func DiagnosticWorkTitle(cause ModificationSource) string {
258 return fmt.Sprintf("diagnosing %v", cause)
261 func (s *Server) wasFirstChange(uri span.URI) bool {
262 s.changedFilesMu.Lock()
263 defer s.changedFilesMu.Unlock()
265 _, ok := s.changedFiles[uri]
269 func (s *Server) changedText(ctx context.Context, uri span.URI, changes []protocol.TextDocumentContentChangeEvent) ([]byte, error) {
270 if len(changes) == 0 {
271 return nil, errors.Errorf("%w: no content changes provided", jsonrpc2.ErrInternal)
274 // Check if the client sent the full content of the file.
275 // We accept a full content change even if the server expected incremental changes.
276 if len(changes) == 1 && changes[0].Range == nil && changes[0].RangeLength == 0 {
277 return []byte(changes[0].Text), nil
279 return s.applyIncrementalChanges(ctx, uri, changes)
282 func (s *Server) applyIncrementalChanges(ctx context.Context, uri span.URI, changes []protocol.TextDocumentContentChangeEvent) ([]byte, error) {
283 fh, err := s.session.GetFile(ctx, uri)
287 content, err := fh.Read()
289 return nil, errors.Errorf("%w: file not found (%v)", jsonrpc2.ErrInternal, err)
291 for _, change := range changes {
292 // Make sure to update column mapper along with the content.
293 converter := span.NewContentConverter(uri.Filename(), content)
294 m := &protocol.ColumnMapper{
296 Converter: converter,
299 if change.Range == nil {
300 return nil, errors.Errorf("%w: unexpected nil range for change", jsonrpc2.ErrInternal)
302 spn, err := m.RangeSpan(*change.Range)
306 if !spn.HasOffset() {
307 return nil, errors.Errorf("%w: invalid range for content change", jsonrpc2.ErrInternal)
309 start, end := spn.Start().Offset(), spn.End().Offset()
311 return nil, errors.Errorf("%w: invalid range for content change", jsonrpc2.ErrInternal)
314 buf.Write(content[:start])
315 buf.WriteString(change.Text)
316 buf.Write(content[end:])
317 content = buf.Bytes()
322 func changeTypeToFileAction(ct protocol.FileChangeType) source.FileAction {
324 case protocol.Changed:
326 case protocol.Created:
328 case protocol.Deleted:
331 return source.UnknownFileAction