|
| 1 | +import { Position, TextEditor, Selection } from "vscode"; |
| 2 | +import { Point, SyntaxNode } from "web-tree-sitter"; |
| 3 | +import { |
| 4 | + Delimiter, |
| 5 | + NodeMatcher, |
| 6 | + NodeMatcherValue, |
| 7 | + SelectionWithContext, |
| 8 | + SelectionWithEditor, |
| 9 | + SurroundingPairModifierSubtype, |
| 10 | +} from "../Types"; |
| 11 | + |
| 12 | +function positionFromPoint(point: Point): Position { |
| 13 | + return new Position(point.row, point.column); |
| 14 | +} |
| 15 | + |
| 16 | +const delimiterToText: Record<Delimiter, String[]> = { |
| 17 | + squareBrackets: ["[", "]"], |
| 18 | + curlyBrackets: ["{", "}"], |
| 19 | + angleBrackets: ["<", ">"], |
| 20 | + parentheses: ["(", ")"], |
| 21 | + singleQuotes: ["'", "'"], |
| 22 | + doubleQuotes: ['"', '"'], |
| 23 | +}; |
| 24 | +function isSyntaxNodeLeftPartOfMatching( |
| 25 | + node: SyntaxNode, |
| 26 | + delimiter: Delimiter |
| 27 | +): boolean { |
| 28 | + return node.type === delimiterToText[delimiter][0]; |
| 29 | +} |
| 30 | +function isSyntaxNodeRightPartOfMatching( |
| 31 | + node: SyntaxNode, |
| 32 | + delimiter: Delimiter |
| 33 | +): boolean { |
| 34 | + return node.type === delimiterToText[delimiter][1]; |
| 35 | +} |
| 36 | + |
| 37 | +export function createSurroundingPairMatcher( |
| 38 | + delimiter: Delimiter | null, |
| 39 | + matchingSubtype: SurroundingPairModifierSubtype |
| 40 | +): NodeMatcher { |
| 41 | + return function nodeMatcher( |
| 42 | + selection: SelectionWithEditor, |
| 43 | + node: SyntaxNode |
| 44 | + ) { |
| 45 | + let delimetersToCheck: Delimiter[]; |
| 46 | + if (delimiter != null) { |
| 47 | + delimetersToCheck = [delimiter]; |
| 48 | + } else { |
| 49 | + delimetersToCheck = [ |
| 50 | + "squareBrackets", |
| 51 | + "curlyBrackets", |
| 52 | + "angleBrackets", |
| 53 | + "parentheses", |
| 54 | + "singleQuotes", |
| 55 | + "doubleQuotes", |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + // This is a special case. |
| 60 | + let nodeLeftOfSelection: SyntaxNode | null = null; |
| 61 | + let nodeRightOfSelection: SyntaxNode | null = null; |
| 62 | + for (const child of node.children) { |
| 63 | + // We iterate from the so we take the **last** node that is good |
| 64 | + if ( |
| 65 | + positionFromPoint(child.endPosition).isBeforeOrEqual( |
| 66 | + selection.selection.start |
| 67 | + ) |
| 68 | + ) { |
| 69 | + nodeLeftOfSelection = child; |
| 70 | + } |
| 71 | + // We iterate from the so we take the **first** node that is good |
| 72 | + if ( |
| 73 | + nodeRightOfSelection == null && |
| 74 | + selection.selection.start.isBeforeOrEqual( |
| 75 | + positionFromPoint(child.startPosition) |
| 76 | + ) |
| 77 | + ) { |
| 78 | + nodeRightOfSelection = child; |
| 79 | + } |
| 80 | + } |
| 81 | + if (nodeLeftOfSelection != null && nodeRightOfSelection != null) { |
| 82 | + let result = doOutwardScan( |
| 83 | + nodeLeftOfSelection, |
| 84 | + nodeRightOfSelection, |
| 85 | + delimetersToCheck, |
| 86 | + matchingSubtype |
| 87 | + ); |
| 88 | + if (result != null) { |
| 89 | + return result; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (node.parent == null) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + // We don't take the next sibling here, because if current node is a |
| 97 | + // closing element of the pair we want to take it. |
| 98 | + return doOutwardScan( |
| 99 | + node.previousSibling, |
| 100 | + node, |
| 101 | + delimetersToCheck, |
| 102 | + matchingSubtype |
| 103 | + ); |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +function doOutwardScan( |
| 108 | + scanLeftStartNode: SyntaxNode | null, |
| 109 | + scanRightStartNode: SyntaxNode | null, |
| 110 | + delimetersToCheck: Delimiter[], |
| 111 | + matchingSubtype: SurroundingPairModifierSubtype |
| 112 | +): NodeMatcherValue[] | null { |
| 113 | + for (const delimiter of delimetersToCheck) { |
| 114 | + let left = scanLeftStartNode; |
| 115 | + while (left != null) { |
| 116 | + if (isSyntaxNodeLeftPartOfMatching(left, delimiter)) { |
| 117 | + break; |
| 118 | + } |
| 119 | + left = left.previousSibling; |
| 120 | + } |
| 121 | + let right = scanRightStartNode; |
| 122 | + while (right != null) { |
| 123 | + if (isSyntaxNodeRightPartOfMatching(right, delimiter)) { |
| 124 | + break; |
| 125 | + } |
| 126 | + right = right.nextSibling; |
| 127 | + } |
| 128 | + if (left != null && right != null) { |
| 129 | + // We have found the matching pair |
| 130 | + if (matchingSubtype === "matchingSubtype") { |
| 131 | + return [ |
| 132 | + { |
| 133 | + node: left, |
| 134 | + selection: { |
| 135 | + selection: new Selection( |
| 136 | + positionFromPoint(left.endPosition), |
| 137 | + positionFromPoint(right.startPosition) |
| 138 | + ), |
| 139 | + context: { |
| 140 | + outerSelection: new Selection( |
| 141 | + positionFromPoint(left.startPosition), |
| 142 | + positionFromPoint(right.endPosition) |
| 143 | + ), |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + ]; |
| 148 | + } else { |
| 149 | + throw new Error("Not implemented"); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return null; |
| 154 | +} |
0 commit comments