clerk/internal/lsp/textdocument_sync.go (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com lsp: init server, report diagnostics, 18 days ago
olexsmir@gmail.com lsp: init server, report diagnostics, 18 days ago
| 1 | package lsp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "go.lsp.dev/protocol" |
| 7 | "go.lsp.dev/uri" |
| 8 | ) |
| 9 | |
| 10 | func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error { |
| 11 | s.openDoc(params.TextDocument.URI, params.TextDocument.Text, params.TextDocument.Version, params.TextDocument.LanguageID) |
| 12 | s.scheduleWorkspaceRebuild(ctx) |
| 13 | return nil |
| 14 | } |
| 15 | |
| 16 | func (s *server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error { |
| 17 | s.updateDoc(params.TextDocument.URI, params.TextDocument.Version, params.ContentChanges) |
| 18 | s.scheduleWorkspaceRebuild(ctx) |
| 19 | return nil |
| 20 | } |
| 21 | |
| 22 | func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error { |
| 23 | s.closeDoc(params.TextDocument.URI) |
| 24 | s.scheduleWorkspaceRebuild(ctx) |
| 25 | return nil |
| 26 | } |
| 27 | |
| 28 | func (s *server) DidSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error { |
| 29 | return nil |
| 30 | } |
| 31 | |
| 32 | // Document management |
| 33 | |
| 34 | func (s *server) openDoc(u uri.URI, text string, version int32, langID protocol.LanguageKind) { |
| 35 | s.mu.Lock() |
| 36 | s.openDocs[u] = docState{ |
| 37 | text: text, |
| 38 | version: version, |
| 39 | languageID: langID, |
| 40 | } |
| 41 | s.mu.Unlock() |
| 42 | } |
| 43 | |
| 44 | func (s *server) updateDoc(u uri.URI, version int32, changes []protocol.TextDocumentContentChangeEvent) { |
| 45 | s.mu.Lock() |
| 46 | defer s.mu.Unlock() |
| 47 | state, ok := s.openDocs[u] |
| 48 | if !ok { |
| 49 | return |
| 50 | } |
| 51 | state.version = version |
| 52 | for _, ch := range changes { |
| 53 | switch ev := ch.(type) { |
| 54 | case *protocol.TextDocumentContentChangeWholeDocument: |
| 55 | state.text = ev.Text |
| 56 | case *protocol.TextDocumentContentChangePartial: |
| 57 | _ = ev // TODO: incremental edit support |
| 58 | } |
| 59 | } |
| 60 | s.openDocs[u] = state |
| 61 | } |
| 62 | |
| 63 | func (s *server) closeDoc(u uri.URI) { |
| 64 | s.mu.Lock() |
| 65 | delete(s.openDocs, u) |
| 66 | s.mu.Unlock() |
| 67 | } |
| 68 | |
| 69 | func (s *server) getDocText(u uri.URI) (string, bool) { |
| 70 | s.mu.Lock() |
| 71 | state, ok := s.openDocs[u] |
| 72 | s.mu.Unlock() |
| 73 | return state.text, ok |
| 74 | } |