all repos

clerk @ 7b8e468208b6ef8bf9db2b5f2f8c1f01b22cb31a

missing tooling for ledger/hledger

clerk/internal/lsp/textdocument_sync.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
loader: evict closed files, 1 month ago
1
package lsp
2
3
import (
4
	"context"
5
	"strconv"
6
7
	"go.lsp.dev/protocol"
8
	"go.lsp.dev/uri"
9
10
	"olexsmir.xyz/clerk/internal/analyzer"
11
	"olexsmir.xyz/clerk/internal/lsp/lsputil"
12
	"olexsmir.xyz/clerk/journal"
13
	"olexsmir.xyz/clerk/journal/ast"
14
	"olexsmir.xyz/clerk/journal/lexer"
15
	"olexsmir.xyz/clerk/journal/parser"
16
)
17
18
func (s *server) DidOpen(ctx context.Context, params *protocol.DidOpenTextDocumentParams) error {
19
	s.openDoc(params.TextDocument.URI, params.TextDocument.Text, params.TextDocument.Version, params.TextDocument.LanguageID)
20
	s.scheduleDiagnostics(ctx)
21
	return nil
22
}
23
24
func (s *server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
25
	s.updateDoc(params.TextDocument.URI, params.TextDocument.Version, params.ContentChanges)
26
	s.scheduleDiagnostics(ctx)
27
	return nil
28
}
29
30
func (s *server) DidClose(ctx context.Context, params *protocol.DidCloseTextDocumentParams) error {
31
	u := params.TextDocument.URI
32
	s.closeDoc(u)
33
	s.loader.Evict(u.Path())
34
35
	// clear closed doc's diagnostics; dependents rebuild from disk since the buffer text is gone.
36
	s.markDependentsDirty(u)
37
	if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
38
		URI:         u,
39
		Diagnostics: []protocol.Diagnostic{},
40
	}); err != nil {
41
		s.log.Warn("clear diagnostics failed", "uri", u, "err", err)
42
	}
43
44
	s.scheduleDiagnostics(ctx)
45
	return nil
46
}
47
48
func (s *server) DidSave(ctx context.Context, params *protocol.DidSaveTextDocumentParams) error {
49
	return nil
50
}
51
52
// Document management
53
54
type docState struct {
55
	text       string
56
	version    int32
57
	languageID protocol.LanguageKind
58
	paths      map[string]bool    // canonical paths of every file in the cached analysis
59
	lineIdx    *lsputil.LineIndex // cached line index for the text
60
61
	analysis *analyzer.Analysis // cached analysis, nil until first build
62
	dirty    bool               // true while the cached analysis may not reflect the current text
63
64
	semTokens   []semanticToken // cached tokens for the current text
65
	semBaseline []uint32        // encoded data of the last response; diff baseline for the next delta request
66
	semGen      uint64          // semantic token generation, increments per response; 0 before the first
67
}
68
69
func (d *docState) resultID() string { return strconv.FormatUint(d.semGen, 10) }
70
func (d *docState) nextResultID() string {
71
	d.semGen++
72
	return d.resultID()
73
}
74
75
func (s *server) openDoc(u uri.URI, text string, version int32, langID protocol.LanguageKind) {
76
	s.mu.Lock()
77
	s.openDocs[u] = docState{
78
		text:       text,
79
		version:    version,
80
		languageID: langID,
81
		lineIdx:    lsputil.NewLineIndex(text),
82
		dirty:      true,
83
	}
84
	s.mu.Unlock()
85
}
86
87
func (s *server) updateDoc(u uri.URI, version int32, changes []protocol.TextDocumentContentChangeEvent) {
88
	s.mu.Lock()
89
	state, ok := s.openDocs[u]
90
	if !ok {
91
		s.mu.Unlock()
92
		return
93
	}
94
	state.version = version
95
	for _, ch := range changes {
96
		switch ev := ch.(type) {
97
		case *protocol.TextDocumentContentChangeWholeDocument:
98
			state.text = ev.Text
99
			state.semTokens = nil
100
			state.lineIdx = lsputil.NewLineIndex(ev.Text)
101
		case *protocol.TextDocumentContentChangePartial:
102
			_ = ev // TODO: incremental edit support
103
		}
104
	}
105
	state.analysis = nil
106
	state.dirty = true
107
	s.openDocs[u] = state
108
	s.mu.Unlock()
109
110
	s.markDependentsDirty(u)
111
}
112
113
// markDependentsDirty dirties every open doc whose cached analysis includes u,
114
// e.g. after u is edited or closed.
115
func (s *server) markDependentsDirty(u uri.URI) {
116
	canon := journal.CanonicalPath(u.Path())
117
	s.mu.Lock()
118
	for du, dstate := range s.openDocs {
119
		if dstate.paths[canon] {
120
			dstate.dirty = true
121
			s.openDocs[du] = dstate
122
		}
123
	}
124
	s.mu.Unlock()
125
}
126
127
func (s *server) getDocState(u uri.URI) (docState, bool) {
128
	s.mu.RLock()
129
	state, ok := s.openDocs[u]
130
	s.mu.RUnlock()
131
	return state, ok
132
}
133
134
func (s *server) closeDoc(u uri.URI) {
135
	s.mu.Lock()
136
	delete(s.openDocs, u)
137
	s.mu.Unlock()
138
}
139
140
func parseJournalStr(content string) *ast.Journal {
141
	l := lexer.New("", []byte(content))
142
	p := parser.New(l)
143
	return p.ParseJournal()
144
}