Skip to content

Commit 84488c5

Browse files
authored
feat(complete): improve the speed of autocomplete (#44)
1 parent 0ef7a71 commit 84488c5

File tree

7 files changed

+137
-208
lines changed

7 files changed

+137
-208
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ lsp client config example with coc.nvim
7979
},
8080
"suggest": {
8181
"fromVimruntime": true, // completionItems from vimruntime's vim files
82-
"fromRuntimepath": false // completionItems from runtimepath's vim files, if this is true that fromVimruntime is true
82+
"fromRuntimepath": true // completionItems from runtimepath's vim files, if this is true that fromVimruntime is true
8383
}
8484
},
8585
"filetypes": [ "vim" ],
@@ -109,7 +109,7 @@ lsp client config example with coc.nvim
109109
},
110110
"suggest": {
111111
"fromVimruntime": true, // completionItems from vimruntime's vim files
112-
"fromRuntimepath": false // completionItems from runtimepath's vim files, if this is true that fromVimruntime is true
112+
"fromRuntimepath": true // completionItems from runtimepath's vim files, if this is true that fromVimruntime is true
113113
}
114114
},
115115
"filetypes": [ "vim" ]
@@ -121,7 +121,6 @@ lsp client config example with coc.nvim
121121

122122
- if you want to speed up index, change `gap` to smaller and `count` to greater, this will cause high CPU usage for some time
123123
- if you don't want to index vim's runtimepath files, set `runtimepath` to `false` and you will not get any suggest from those files.
124-
- while `fromRuntimepath` is true, if you have install too many plugins it will slow down the complete
125124

126125
## Usage
127126

@@ -159,6 +158,10 @@ lsp client config example with coc.nvim
159158

160159
![dia](https://user-images.githubusercontent.com/5492542/81494408-503eaf80-92db-11ea-96ac-641d46027623.gif)
161160

161+
## Changelog
162+
163+
- v2.0.0: autocomplete speedup, and `fromRuntimepath` default is change to `true`
164+
162165
## References
163166

164167
- [vim-vimlparser](https://github.com/vim-jp/vim-vimlparser)

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "vim-language-server",
3-
"version": "1.9.0",
3+
"version": "2.0.0",
44
"description": "vim language server",
55
"keywords": [
66
"viml",
77
"vim",
88
"lsp",
99
"language",
10-
"server"
10+
"server",
11+
"autocomplete"
1112
],
1213
"main": "./out/index.js",
1314
"repository": "https://github.com/iamcco/vim-language-server",
@@ -29,20 +30,20 @@
2930
"@types/node": "^11.13.6",
3031
"chai": "^4.2.0",
3132
"chai-as-promised": "^7.1.1",
32-
"fast-glob": "^3.2.2",
33+
"fast-glob": "^3.2.4",
3334
"findup": "^0.1.5",
3435
"mocha": "^7.1.2",
3536
"rxjs": "^6.5.5",
3637
"rxjs-operators": "^1.1.3",
3738
"shvl": "^2.0.0",
38-
"ts-loader": "^7.0.3",
39-
"ts-node": "^8.10.1",
39+
"ts-loader": "^7.0.5",
40+
"ts-node": "^8.10.2",
4041
"tslint": "^6.1.2",
4142
"typescript": "^3.8.3",
4243
"vscode-languageserver": "^6.1.1",
4344
"vscode-languageserver-textdocument": "^1.0.1",
44-
"vscode-uri": "^2.1.1",
45+
"vscode-uri": "^2.1.2",
4546
"webpack": "^4.43.0",
46-
"webpack-cli": "^3.3.11"
47+
"webpack-cli": "^3.3.12"
4748
}
4849
}

src/handles/completion/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CompletionParams,
44
Position,
55
Range,
6+
CompletionList,
67
} from "vscode-languageserver";
78

89
import fuzzy from "../../common/fuzzy";
@@ -25,7 +26,7 @@ import { getProvider } from "./provider";
2526

2627
const provider = getProvider();
2728

28-
export const completionProvider = (params: CompletionParams): CompletionItem[] => {
29+
export const completionProvider = (params: CompletionParams): CompletionList | CompletionItem[] => {
2930

3031
const { textDocument, position } = params;
3132
const textDoc = documents.get(textDocument.uri);
@@ -34,18 +35,24 @@ export const completionProvider = (params: CompletionParams): CompletionItem[] =
3435
Position.create(position.line, 0),
3536
position,
3637
));
37-
const completionItems = provider(line, textDoc.uri, position, []);
38-
if (!config.snippetSupport) {
39-
return removeSnippets(completionItems);
40-
}
4138
const words = getWordFromPosition(textDoc, { line: position.line, character: position.character - 1 });
4239
let word = words && words.word || "";
4340
if (word === "" && words && words.wordRight.trim() === ":") {
4441
word = ":";
4542
}
4643
// options items start with &
4744
const invalidLength = word.replace(/^&/, "").length;
48-
return completionItems.filter((item) => fuzzy(item.label, word) >= invalidLength);
45+
const completionItems = provider(line, textDoc.uri, position, word, invalidLength, []);
46+
if (!config.snippetSupport) {
47+
return {
48+
isIncomplete: true,
49+
items: removeSnippets(completionItems)
50+
}
51+
}
52+
return {
53+
isIncomplete: true,
54+
items: completionItems
55+
}
4956
}
5057
return [];
5158
};

src/handles/completion/provider.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CompletionItem,
33
Position,
44
} from "vscode-languageserver";
5+
import fuzzy from "../../common/fuzzy";
56

67
type Provider = (line: string, uri?: string, position?: Position) => CompletionItem[];
78

@@ -17,19 +18,33 @@ export function getProvider() {
1718
line: string,
1819
uri: string,
1920
position: Position,
21+
word: string,
22+
invalidLength: number,
2023
items: CompletionItem[],
21-
) => pre(
22-
line,
23-
uri,
24-
position,
25-
items.concat(next(line, uri, position)),
26-
);
24+
): CompletionItem[] => {
25+
// 200 items is enough
26+
if (items.length > 200) {
27+
return items.slice(0, 200)
28+
}
29+
const newItems = next(line, uri, position)
30+
.filter((item) => fuzzy(item.label, word) >= invalidLength)
31+
return pre(
32+
line,
33+
uri,
34+
position,
35+
word,
36+
invalidLength,
37+
items.concat(newItems),
38+
)
39+
};
2740

2841
}, (
2942
_line: string,
3043
_uri: string,
3144
_position: Position,
45+
_word: string,
46+
_invalidLength: number,
3247
items: CompletionItem[],
33-
) => items,
48+
): CompletionItem[] => items,
3449
);
3550
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ connection.onInitialize((param: InitializeParams) => {
5252
},
5353
snippetSupport: shvl.get(param, "capabilities.textDocument.completion.completionItem.snippetSupport"),
5454
suggest: {
55-
fromRuntimepath: false,
55+
fromRuntimepath: true,
5656
fromVimruntime: true,
5757
...(suggest || {}),
5858
},

src/server/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default {
3232

3333
get suggest(): ISuggest {
3434
return conf && conf.suggest || {
35-
fromRuntimepath: false,
35+
fromRuntimepath: true,
3636
fromVimruntime: true,
3737
};
3838
},

0 commit comments

Comments
 (0)