-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
selectionRangeProvider.ts
90 lines (80 loc) · 2.53 KB
/
selectionRangeProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { container } from "tsyringe";
import {
Connection,
Position,
Range,
SelectionRange,
SelectionRangeParams,
} from "vscode-languageserver";
import { URI } from "vscode-uri";
import { SyntaxNode, Tree } from "web-tree-sitter";
import { PositionUtil } from "../positionUtil";
import { ElmWorkspaceMatcher } from "../util/elmWorkspaceMatcher";
import { TreeUtils } from "../util/treeUtils";
import { ISelectionRangeParams } from "./paramsExtensions";
export class SelectionRangeProvider {
private connection: Connection;
constructor() {
this.connection = container.resolve<Connection>("Connection");
this.connection.onSelectionRanges(
new ElmWorkspaceMatcher((param: SelectionRangeParams) =>
URI.parse(param.textDocument.uri),
).handle(this.handleSelectionRangeRequest.bind(this)),
);
}
private handleSelectionRangeRequest = (
params: ISelectionRangeParams,
): SelectionRange[] | null => {
this.connection.console.info(`Selection Ranges were requested`);
const ret: SelectionRange[] = [];
const tree: Tree = params.sourceFile.tree;
params.positions.forEach((position: Position) => {
const nodeAtPosition = TreeUtils.getNamedDescendantForPosition(
tree.rootNode,
position,
);
const newRange = {
start: PositionUtil.FROM_TS_POSITION(
nodeAtPosition.startPosition,
).toVSPosition(),
end: PositionUtil.FROM_TS_POSITION(
nodeAtPosition.endPosition,
).toVSPosition(),
};
ret.push({
range: newRange,
parent: this.getParentNode(nodeAtPosition, newRange),
});
});
return ret ? ret : null;
};
private getParentNode(
node: SyntaxNode,
previousRange: Range,
): SelectionRange | undefined {
if (node.parent) {
const newRange = {
start: PositionUtil.FROM_TS_POSITION(
node.parent.startPosition,
).toVSPosition(),
end: PositionUtil.FROM_TS_POSITION(
node.parent.endPosition,
).toVSPosition(),
};
if (
previousRange.start.line === newRange.start.line &&
previousRange.start.character === newRange.start.character &&
previousRange.end.line === newRange.end.line &&
previousRange.end.character === newRange.end.character
) {
// Skip ranges that match
return this.getParentNode(node.parent, previousRange);
} else {
return {
range: newRange,
parent: this.getParentNode(node.parent, newRange),
};
}
}
}
}