Skip to content

Commit a2919f1

Browse files
committed
feat(selectionRange): add if for while try range support
1 parent f4c62ae commit a2919f1

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed

.vim/coc-settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
"Runtimepath",
44
"Vimruntime",
55
"Workdir",
6+
"argpos",
7+
"cmdpos",
8+
"cond",
9+
"endfor",
610
"endfunction",
11+
"endwhile",
712
"fitem",
813
"fitems",
914
"flist",
1015
"fname",
1116
"fpath",
1217
"iskeyword",
18+
"linepos",
1319
"lnum",
1420
"msglog",
1521
"neco",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vim-language-server",
3-
"version": "1.7.0",
3+
"version": "1.8.0",
44
"description": "vim language server",
55
"keywords": [
66
"viml",

src/handles/selectionRange.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,26 @@ export const selectionRangeProvider = (params: SelectionRangeParams): SelectionR
1414
if (!buffer || !document) {
1515
return selectRanges;
1616
}
17-
const globalFunctions = buffer.getGlobalFunctions();
18-
const scriptFunctions = buffer.getScriptFunctions();
17+
const vimRanges = buffer.getRanges()
18+
if (vimRanges.length === 0) {
19+
return selectRanges
20+
}
1921

20-
const funcs = Object.values(globalFunctions).concat(Object.values(scriptFunctions))
21-
.reduce((pre, cur) => {
22-
return pre.concat(cur);
23-
}, [])
2422
let range = Range.create(positions[0], positions[0])
2523
if (positions.length > 1) {
2624
range = Range.create(positions[0], positions[positions.length - 1])
2725
}
28-
const ranges: Range[] = []
29-
funcs.forEach(item => {
30-
const p = item.range
26+
let ranges: Range[] = []
27+
vimRanges.forEach(vimRange => {
3128
const line = document.getText(Range.create(
32-
Position.create(p.endLine - 1, 0),
33-
Position.create(p.endLine, 0)
29+
Position.create(vimRange.endLine - 1, 0),
30+
Position.create(vimRange.endLine, 0)
3431
))
3532
const newRange = Range.create(
36-
Position.create(p.startLine - 1, p.startCol - 1),
37-
Position.create(p.endLine - 1, p.endCol - 1 + line.slice(p.endCol - 1).split(' ')[0].length)
33+
Position.create(vimRange.startLine - 1, vimRange.startCol - 1),
34+
Position.create(vimRange.endLine - 1, vimRange.endCol - 1 + line.slice(vimRange.endCol - 1).split(' ')[0].length)
3835
)
39-
if (range.start.line >= newRange.start.line
40-
&& range.end.line <= newRange.end.line
41-
&& !(range.start.line === newRange.start.line && range.end.line === newRange.end.line)) {
36+
if (range.start.line >= newRange.start.line && range.end.line <= newRange.end.line) {
4237
if (ranges.length === 0) {
4338
ranges.push(newRange)
4439
} else {
@@ -56,6 +51,11 @@ export const selectionRangeProvider = (params: SelectionRangeParams): SelectionR
5651
}
5752
})
5853
if (ranges.length) {
54+
if (ranges.length > 1) {
55+
ranges = ranges.filter(newRange => {
56+
return range.start.line !== newRange.start.line || range.end.line !== newRange.end.line
57+
})
58+
}
5959
selectRanges.push(
6060
ranges.reverse().reduce((pre, cur, idx) => {
6161
if (idx === 0) {

src/lib/vimparser.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export declare interface INode {
2828
str: string;
2929
value?: any;
3030
endfunction?: INode;
31+
endif?: INode;
32+
endfor?: INode;
33+
endwhile?: INode;
3134
list?: INode[];
3235
}
3336

src/server/buffer.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ export interface IIdentifier {
132132
startCol: number;
133133
}
134134

135+
/*
136+
* xxxx
137+
* endxxx
138+
*
139+
*/
140+
export interface IRange {
141+
startLine: number
142+
startCol: number
143+
endLine: number
144+
endCol: number
145+
}
146+
135147
const globalFuncPattern = /^(g:\w+(\.\w+)*|[a-zA-Z_]\w*(\.\w+)*|\w+(#\w+)+)$/;
136148
const scriptFuncPattern = /^(s:\w+(\.\w+)*|<SID>\w+(\.\w+)*)$/i;
137149
const globalVariablePattern = /^(g:\w+(\.\w+)*|b:\w+(\.\w+)*|\w{1,}(\.\w+)*|\w+(#\w+)+)$/;
@@ -153,6 +165,8 @@ export class Buffer {
153165
private envs: Record<string, IIdentifier[]> = {};
154166
private envRefs: Record<string, IIdentifier[]> = {};
155167

168+
private ranges: IRange[] = []
169+
156170
constructor(
157171
private uri: string,
158172
private projectRoot: string,
@@ -193,6 +207,10 @@ export class Buffer {
193207
return this.localVariableRefs;
194208
}
195209

210+
public getRanges() {
211+
return this.ranges
212+
}
213+
196214
public getProjectRoot() {
197215
return this.projectRoot;
198216
}
@@ -402,6 +420,7 @@ export class Buffer {
402420
this.localVariableRefs = {};
403421
this.envs = {};
404422
this.envRefs = {};
423+
this.ranges = []
405424
}
406425

407426
private resolveCompletionItems(nodes: INode | INode[]) {
@@ -446,6 +465,7 @@ export class Buffer {
446465
case NODE_ELSEIF:
447466
case NODE_ELSE:
448467
case NODE_WHILE:
468+
this.takeRange(node, ['endif', 'endwhile'])
449469
nodeList = nodeList.concat(node.body || []);
450470
nodeList = nodeList.concat(node.cond || []);
451471
nodeList = nodeList.concat(node.elseif || []);
@@ -501,10 +521,12 @@ export class Buffer {
501521
nodeList = nodeList.concat(node.body || []);
502522
nodeList = nodeList.concat(node.right || []);
503523
this.takeFor([].concat(node.left || []).concat(node.list || []));
524+
this.takeRange(node, 'endfor')
504525
break;
505526
case NODE_TRY:
506527
case NODE_CATCH:
507528
case NODE_FINALLY:
529+
this.takeRange(node, 'endtry')
508530
nodeList = nodeList.concat(node.body || []);
509531
nodeList = nodeList.concat(node.catch || []);
510532
nodeList = nodeList.concat(node._finally || []);
@@ -515,6 +537,7 @@ export class Buffer {
515537
nodeList = nodeList.concat(node.left.left);
516538
}
517539
this.takeFunction(node);
540+
this.takeRange(node, 'endfunction')
518541
break;
519542
case NODE_LIST:
520543
nodeList = nodeList.concat(node.value || []);
@@ -810,6 +833,19 @@ export class Buffer {
810833
}
811834
}
812835

836+
private takeRange(node: INode, keys: string | string[]) {
837+
[].concat(keys).forEach(key => {
838+
if (node.pos && node[key] && node[key].pos) {
839+
this.ranges.push({
840+
startLine: node.pos.lnum,
841+
startCol: node.pos.col,
842+
endLine: node[key].pos.lnum,
843+
endCol: node[key].pos.col
844+
})
845+
}
846+
})
847+
}
848+
813849
private takeFor(nodes: INode[]) {
814850
nodes.forEach((node) => {
815851
if (node.type !== NODE_IDENTIFIER || !node.pos) {

0 commit comments

Comments
 (0)