Skip to content

Commit 372cada

Browse files
authored
pref: reduce the number of LSP RPC calls (#180)
* fix * fix: fix CountFiles * perf: lsp getSemanticTokensRange
1 parent 27d2b06 commit 372cada

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

lang/lsp/client.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ type LSPClient struct {
3636
*lspHandler
3737
tokenTypes []string
3838
tokenModifiers []string
39-
hasSemanticTokensRange bool
4039
files map[DocumentURI]*TextDocumentItem
4140
provider LanguageServiceProvider
4241
ClientOptions
@@ -193,8 +192,6 @@ func initLSPClient(ctx context.Context, svr io.ReadWriteCloser, dir DocumentURI,
193192
if !ok || semanticTokensProvider == nil {
194193
return nil, fmt.Errorf("server did not provide SemanticTokensProvider")
195194
}
196-
semanticTokensRange, ok := semanticTokensProvider["range"].(bool)
197-
cli.hasSemanticTokensRange = ok && semanticTokensRange
198195
legend, ok := semanticTokensProvider["legend"].(map[string]interface{})
199196
if !ok || legend == nil {
200197
return nil, fmt.Errorf("server did not provide SemanticTokensProvider.legend")

lang/lsp/lsp.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,14 @@ func NewURI(file string) DocumentURI {
171171
}
172172

173173
type TextDocumentItem struct {
174-
URI DocumentURI `json:"uri"`
175-
LanguageID string `json:"languageId"`
176-
Version int `json:"version"`
177-
Text string `json:"text"`
178-
LineCounts []int `json:"-"`
179-
Symbols map[Range]*DocumentSymbol `json:"-"`
180-
Definitions map[Position][]Location `json:"-"`
174+
URI DocumentURI `json:"uri"`
175+
LanguageID string `json:"languageId"`
176+
Version int `json:"version"`
177+
Text string `json:"text"`
178+
LineCounts []int `json:"-"`
179+
Symbols map[Range]*DocumentSymbol `json:"-"`
180+
Definitions map[Position][]Location `json:"-"`
181+
SemanticTokens *SemanticTokens `json:"-"`
181182
}
182183

183184
type DocumentSymbol struct {

lang/lsp/lsp_methods.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,27 @@ func (cli *LSPClient) References(ctx context.Context, id Location) ([]Location,
156156
return resp, nil
157157
}
158158

159-
// Some language servers do not provide semanticTokens/range.
160-
// In that case, we fall back to semanticTokens/full and then filter the tokens manually.
161159
func (cli *LSPClient) getSemanticTokensRange(ctx context.Context, req DocumentRange, resp *SemanticTokens) error {
162-
if cli.hasSemanticTokensRange {
163-
if err := cli.Call(ctx, "textDocument/semanticTokens/range", req, resp); err != nil {
160+
f, err := cli.DidOpen(ctx, DocumentURI(req.TextDocument.URI))
161+
if err != nil {
162+
return err
163+
}
164+
165+
if f.SemanticTokens == nil {
166+
req1 := SemanticTokensFullParams{
167+
TextDocument: req.TextDocument,
168+
}
169+
var fullResp SemanticTokens
170+
if err := cli.Call(ctx, "textDocument/semanticTokens/full", req1, &fullResp); err != nil {
164171
return err
165172
}
166-
return nil
167-
}
168-
// fall back to semanticTokens/full
169-
req1 := SemanticTokensFullParams{
170-
TextDocument: req.TextDocument,
171-
}
172-
if err := cli.Call(ctx, "textDocument/semanticTokens/full", req1, resp); err != nil {
173-
return err
173+
f.SemanticTokens = &fullResp
174174
}
175+
176+
resp.ResultID = f.SemanticTokens.ResultID
177+
resp.Data = make([]uint32, len(f.SemanticTokens.Data))
178+
copy(resp.Data, f.SemanticTokens.Data)
179+
175180
filterSemanticTokensInRange(resp, req.Range)
176181
return nil
177182
}

lang/utils/files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func CountFiles(dir string, subfix string, skipdir string) (int, int) {
2828
skipdir, _ = filepath.Abs(filepath.Join(dir, skipdir))
2929
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
3030
if strings.HasPrefix(path, skipdir) {
31-
return nil
31+
return filepath.SkipDir
3232
}
3333
if !info.IsDir() && filepath.Ext(path) == subfix {
3434
count++

testdata/asts/localsession_g.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)