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