|
| 1 | +import * as vscode from "vscode"; |
| 2 | + |
| 3 | +const goToPositionCommandName = "io.orta.typescript-dev.go-to-position"; |
| 4 | +const togglePositionCommandName = "io.orta.typescript-dev.toggle-position"; |
| 5 | + |
| 6 | +export function activatePosition(context: vscode.ExtensionContext) { |
| 7 | + context.subscriptions.push(vscode.commands.registerCommand(goToPositionCommandName, async () => { |
| 8 | + const expr = await vscode.window.showInputBox({ |
| 9 | + prompt: "Zero-based position or comma-separated range. Addition and subtraction expressions are allowed." |
| 10 | + }); |
| 11 | + |
| 12 | + if (vscode.window.activeTextEditor && expr) { |
| 13 | + const range = tryParseRangeExpression(expr); |
| 14 | + if (range !== undefined && vscode.window.activeTextEditor) { |
| 15 | + const start = vscode.window.activeTextEditor.document.positionAt(range[0]); |
| 16 | + const end = range[1] === undefined ? start : vscode.window.activeTextEditor.document.positionAt(range[1]); |
| 17 | + vscode.window.activeTextEditor.selection = new vscode.Selection(start, end); |
| 18 | + } |
| 19 | + |
| 20 | + vscode.window.activeTextEditor.revealRange(vscode.window.activeTextEditor.selection, vscode.TextEditorRevealType.InCenterIfOutsideViewport); |
| 21 | + } |
| 22 | + })); |
| 23 | + |
| 24 | + let positionStatusBarItem: vscode.StatusBarItem; |
| 25 | + let positionStatusBarItemVisible = false; // This seems to be private state of `StatusBarItem`, so I have to track it myself? |
| 26 | + context.subscriptions.push(vscode.commands.registerCommand(togglePositionCommandName, () => { |
| 27 | + if (!positionStatusBarItem) { |
| 28 | + positionStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100.45); |
| 29 | + positionStatusBarItem.command = goToPositionCommandName; |
| 30 | + context.subscriptions.push(positionStatusBarItem); |
| 31 | + context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(() => updatePosition(positionStatusBarItem))); |
| 32 | + context.subscriptions.push(vscode.window.onDidChangeTextEditorSelection(() => updatePosition(positionStatusBarItem))); |
| 33 | + updatePosition(positionStatusBarItem); |
| 34 | + } |
| 35 | + |
| 36 | + if (positionStatusBarItemVisible) { |
| 37 | + positionStatusBarItem.hide(); |
| 38 | + } else { |
| 39 | + positionStatusBarItem.show(); |
| 40 | + } |
| 41 | + positionStatusBarItemVisible = !positionStatusBarItemVisible; |
| 42 | + })); |
| 43 | +} |
| 44 | + |
| 45 | +function updatePosition(statusBarItem: vscode.StatusBarItem) { |
| 46 | + const focus = vscode.window.activeTextEditor?.document.offsetAt(vscode.window.activeTextEditor.selection.active); |
| 47 | + const anchor = vscode.window.activeTextEditor?.document.offsetAt(vscode.window.activeTextEditor.selection.anchor); |
| 48 | + if (focus !== undefined && anchor !== undefined) { |
| 49 | + const lower = Math.min(focus, anchor); |
| 50 | + const upper = Math.max(focus, anchor); |
| 51 | + statusBarItem.text = "Pos " + lower + (upper === lower ? "" : `, ${upper}`); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +export function tryParseRangeExpression(expr: string): readonly [number, number | undefined] | undefined { |
| 56 | + const [startExpr, endExpr] = expr.split(","); |
| 57 | + const start = tryParsePositionExpression(startExpr); |
| 58 | + if (start === undefined) return undefined; |
| 59 | + return [start, endExpr === undefined ? endExpr : tryParsePositionExpression(endExpr)]; |
| 60 | +} |
| 61 | + |
| 62 | +function tryParsePositionExpression(expr: string): number | undefined { |
| 63 | + const enum State { |
| 64 | + ParseOperator, |
| 65 | + ParseOperand, |
| 66 | + } |
| 67 | + |
| 68 | + let state = State.ParseOperand as State; |
| 69 | + let value: number | undefined; |
| 70 | + let operator: "+" | "-" = "+"; |
| 71 | + let pos = 0; |
| 72 | + |
| 73 | + while (pos < expr.length) { |
| 74 | + skipSpace(); |
| 75 | + const current = expr[pos]; |
| 76 | + if (!current) break; |
| 77 | + |
| 78 | + switch (state) { |
| 79 | + case State.ParseOperand: |
| 80 | + if (isNumeric(current)) { |
| 81 | + value = operate(consumeInt()); |
| 82 | + state = State.ParseOperator; |
| 83 | + continue; |
| 84 | + } else if (current === "+") { |
| 85 | + pos++; |
| 86 | + continue; |
| 87 | + } else if (current === "-") { |
| 88 | + flipSign(); |
| 89 | + pos++; |
| 90 | + continue; |
| 91 | + } else { |
| 92 | + return undefined; |
| 93 | + } |
| 94 | + case State.ParseOperator: |
| 95 | + if (current === "+") { |
| 96 | + operator = "+"; |
| 97 | + pos++; |
| 98 | + state = State.ParseOperand; |
| 99 | + } else if (current === "-") { |
| 100 | + operator = "-"; |
| 101 | + pos++; |
| 102 | + state = State.ParseOperand; |
| 103 | + } else { |
| 104 | + return undefined; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return value !== undefined && value >= 0 ? value : undefined; |
| 109 | + |
| 110 | + function consumeInt(): number | undefined { |
| 111 | + let start = pos; |
| 112 | + while (pos < expr.length && isNumeric(expr[pos])) { |
| 113 | + pos++; |
| 114 | + } |
| 115 | + const int = +expr.slice(start, pos); |
| 116 | + if (Number.isSafeInteger(int)) { |
| 117 | + return int; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + function skipSpace() { |
| 122 | + while (pos < expr.length && /\s/.test(expr[pos])) { |
| 123 | + pos++; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + function operate(operand: number | undefined) { |
| 128 | + if (operand === undefined) return value; |
| 129 | + if (value === undefined) value = 0; |
| 130 | + switch (operator) { |
| 131 | + case "+": return value + operand; |
| 132 | + case "-": return value - operand; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + function flipSign() { |
| 137 | + if (operator === "+") { |
| 138 | + operator = "-"; |
| 139 | + } else { |
| 140 | + operator = "+"; |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +function isNumeric(char: string) { |
| 146 | + const code = char.charCodeAt(0); |
| 147 | + return 48 <= code && code <= 57; |
| 148 | +} |
0 commit comments