Skip to content

Commit 09a9ef0

Browse files
committed
feat(selectRange): add select range support
1 parent 66e5ae6 commit 09a9ef0

File tree

6 files changed

+88
-17
lines changed

6 files changed

+88
-17
lines changed

.vim/coc-settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Runtimepath",
44
"Vimruntime",
55
"Workdir",
6+
"endfunction",
67
"fitem",
78
"fitems",
89
"flist",
@@ -12,6 +13,7 @@
1213
"lnum",
1314
"msglog",
1415
"neco",
16+
"rlist",
1517
"runtimepath's",
1618
"runtimepaths",
1719
"shvl",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- go to references
1717
- document highlight
1818
- folding range
19+
- select range
1920
- rename
2021
- snippets
2122
- diagnostic

src/handles/selectionRange.ts

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,72 @@
1-
import {FoldingRange, FoldingRangeParams} from "vscode-languageserver";
1+
import {SelectionRangeParams, SelectionRange, Range, Position} from "vscode-languageserver";
22

33
import {workspace} from "../server/workspaces";
4+
import {documents} from "../server/documents";
45

5-
export const selectionRangeProvider = (params: FoldingRangeParams) => {
6-
const res: FoldingRange[] = [];
7-
const { textDocument } = params;
6+
export const selectionRangeProvider = (params: SelectionRangeParams): SelectionRange[] => {
7+
const selectRanges: SelectionRange[] = [];
8+
const { textDocument, positions } = params;
9+
if (!positions || positions.length === 0) {
10+
return selectRanges
11+
}
812
const buffer = workspace.getBufferByUri(textDocument.uri);
9-
if (!buffer) {
10-
return res;
13+
const document = documents.get(textDocument.uri)
14+
if (!buffer || !document) {
15+
return selectRanges;
1116
}
1217
const globalFunctions = buffer.getGlobalFunctions();
1318
const scriptFunctions = buffer.getScriptFunctions();
14-
return Object.values(globalFunctions).concat(Object.values(scriptFunctions))
19+
20+
const funcs = Object.values(globalFunctions).concat(Object.values(scriptFunctions))
1521
.reduce((pre, cur) => {
1622
return pre.concat(cur);
1723
}, [])
18-
.map<FoldingRange>((func) => {
19-
return {
20-
startLine: func.startLine - 1,
21-
startCharacter: func.startCol - 1,
22-
endLine: func.endLine - 1,
23-
endCharacter: func.endCol - 1,
24-
kind: "region",
25-
};
26-
});
24+
let range = Range.create(positions[0], positions[0])
25+
if (positions.length > 1) {
26+
range = Range.create(positions[0], positions[positions.length - 1])
27+
}
28+
const ranges: Range[] = []
29+
funcs.forEach(item => {
30+
const p = item.range
31+
const line = document.getText(Range.create(
32+
Position.create(p.endLine - 1, 0),
33+
Position.create(p.endLine, 0)
34+
))
35+
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)
38+
)
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)) {
42+
if (ranges.length === 0) {
43+
ranges.push(newRange)
44+
} else {
45+
let i = 0;
46+
for(const len = ranges.length; i < len; i++) {
47+
if (ranges[i].start.line <= newRange.start.line && ranges[i].end.line >= newRange.end.line) {
48+
ranges.splice(i, 0, newRange)
49+
break
50+
}
51+
}
52+
if (i === ranges.length) {
53+
ranges.push(newRange)
54+
}
55+
}
56+
}
57+
})
58+
if (ranges.length) {
59+
selectRanges.push(
60+
ranges.reverse().reduce((pre, cur, idx) => {
61+
if (idx === 0) {
62+
return pre
63+
}
64+
return {
65+
range: cur,
66+
parent: pre
67+
}
68+
}, {range: ranges[0]} as SelectionRange)
69+
)
70+
}
71+
return selectRanges
2772
};

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import config from "./server/config";
1717
import { connection } from "./server/connection";
1818
import { documents } from "./server/documents";
1919
import { next, unsubscribe } from "./server/parser";
20+
import {selectionRangeProvider} from "./handles/selectionRange";
2021

2122
// lsp initialize
2223
connection.onInitialize((param: InitializeParams) => {
@@ -74,6 +75,7 @@ connection.onInitialize((param: InitializeParams) => {
7475
textDocumentSync: TextDocumentSyncKind.Incremental,
7576
documentHighlightProvider: true,
7677
foldingRangeProvider: true,
78+
selectionRangeProvider: true,
7779
hoverProvider: true,
7880
completionProvider: {
7981
triggerCharacters: [".", ":", "#", "[", "&", "$", "<", '"', "'"],
@@ -131,5 +133,8 @@ connection.onDocumentHighlight(documentHighlightProvider);
131133
// folding range
132134
connection.onFoldingRanges(foldingRangeProvider);
133135

136+
// select range
137+
connection.onSelectionRanges(selectionRangeProvider)
138+
134139
// lsp start
135140
connection.listen();

src/lib/vimparser.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export declare interface INode {
3333

3434
export declare class StringReader {
3535
public buf: string[];
36-
public pos: Array<[number, number, number]>;
36+
public pos: [number, number, number][];
3737
constructor(lines: string[])
3838
}
3939

src/server/buffer.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ export interface IFunction {
111111
startCol: number;
112112
endLine: number;
113113
endCol: number;
114+
range: {
115+
startLine: number;
116+
startCol: number;
117+
endLine: number;
118+
endCol: number;
119+
}
114120
}
115121

116122
export interface IFunRef {
@@ -570,6 +576,12 @@ export class Buffer {
570576
startCol: pos.col,
571577
endLine: endfunction!.pos.lnum,
572578
endCol: endfunction!.pos.col,
579+
range: {
580+
startLine: node.pos.lnum,
581+
startCol: node.pos.col,
582+
endLine: endfunction!.pos.lnum,
583+
endCol: endfunction!.pos.col,
584+
}
573585
};
574586
if (globalFuncPattern.test(name)) {
575587
if (!this.globalFunctions[name] || !Array.isArray(this.globalFunctions[name])) {
@@ -618,6 +630,12 @@ export class Buffer {
618630
startCol: pos.col,
619631
endLine: pos.lnum,
620632
endCol: pos.col,
633+
range: {
634+
startLine: pos.lnum,
635+
startCol: pos.col,
636+
endLine: pos.lnum,
637+
endCol: pos.col,
638+
}
621639
};
622640
if (globalFuncPattern.test(name)) {
623641
if (!this.globalFunctions[name] || !Array.isArray(this.globalFunctions[name])) {

0 commit comments

Comments
 (0)