handleFileSystemRequest handles textDocument/did* requests.
(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request)
| 32 | |
| 33 | // handleFileSystemRequest handles textDocument/did* requests. |
| 34 | func (h *Handler) handleFileSystemRequest(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (lsp.DocumentURI, bool, error) { |
| 35 | fs := h.GetFS() |
| 36 | |
| 37 | do := func(uri lsp.DocumentURI, op func() error) (lsp.DocumentURI, bool, error) { |
| 38 | before, beforeErr := h.readFile(ctx, uri) |
| 39 | if beforeErr != nil && !os.IsNotExist(beforeErr) { |
| 40 | // There is no op that could succeed in this case. |
| 41 | // Most commonly occurs when uri refers to a dir, not a file. |
| 42 | return uri, false, beforeErr |
| 43 | } |
| 44 | err := op() |
| 45 | after, afterErr := h.readFile(ctx, uri) |
| 46 | if os.IsNotExist(beforeErr) && os.IsNotExist(afterErr) { |
| 47 | // File did not exist before or after so nothing has changed. |
| 48 | return uri, false, err |
| 49 | } else if afterErr != nil || beforeErr != nil { |
| 50 | // If an error prevented us from reading the file |
| 51 | // before or after then we assume the file changed to be conservative. |
| 52 | return uri, true, err |
| 53 | } |
| 54 | return uri, !bytes.Equal(before, after), err |
| 55 | } |
| 56 | |
| 57 | switch Method(req.Method) { |
| 58 | case LSPMethodTextDocumentDidOpen: |
| 59 | var params lsp.DidOpenTextDocumentParams |
| 60 | if err := json.Unmarshal(*req.Params, ¶ms); err != nil { |
| 61 | return "", false, err |
| 62 | } |
| 63 | return do(params.TextDocument.URI, func() error { |
| 64 | fs.DidOpen(¶ms) |
| 65 | return nil |
| 66 | }) |
| 67 | case LSPMethodTextDocumentDidChange: |
| 68 | var params lsp.DidChangeTextDocumentParams |
| 69 | if err := json.Unmarshal(*req.Params, ¶ms); err != nil { |
| 70 | return "", false, err |
| 71 | } |
| 72 | return do(params.TextDocument.URI, func() error { |
| 73 | if err := fs.DidChange(¶ms); err != nil { |
| 74 | return err |
| 75 | } |
| 76 | uri := params.TextDocument.URI |
| 77 | content, found := fs.get(uri) |
| 78 | if !found { |
| 79 | return &os.PathError{Op: "Open", Path: string(uri), Err: os.ErrNotExist} |
| 80 | } |
| 81 | |
| 82 | // Use debounced diagnostics for better performance |
| 83 | // This prevents diagnostics from running on every keystroke |
| 84 | h.diagnosticsDebouncer.ScheduleDiagnostics(ctx, conn, uri, string(content), h) |
| 85 | |
| 86 | return nil |
| 87 | }) |
| 88 | case LSPMethodTextDocumentDidClose: |
| 89 | var params lsp.DidCloseTextDocumentParams |
| 90 | if err := json.Unmarshal(*req.Params, ¶ms); err != nil { |
| 91 | return "", false, err |
no test coverage detected