From 6b172614f958ece47e9d06d5e54e61866fbe5c88 Mon Sep 17 00:00:00 2001 From: Vedansh Shetti Date: Sun, 20 Sep 2026 22:29:47 +0200 Subject: [PATCH] refactor: move getUpdatedSelections function inside matchTag for better encapsulation --- extensions/emmet/src/matchTag.ts | 48 +++++++++++++++++++------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/extensions/emmet/src/matchTag.ts b/extensions/emmet/src/matchTag.ts index 1c928fb73710fd..3bf7200d96e863 100644 --- a/extensions/emmet/src/matchTag.ts +++ b/extensions/emmet/src/matchTag.ts @@ -8,7 +8,34 @@ import { validate, getHtmlFlatNode, offsetRangeToSelection } from './util'; import { getRootNode } from './parseDocument'; import { HtmlNode as HtmlFlatNode } from 'EmmetFlatNode'; -export function matchTag() { +function getUpdatedSelections(document: vscode.TextDocument, rootNode: HtmlFlatNode, position: vscode.Position): vscode.Selection | undefined { + const offset = document.offsetAt(position); + const currentNode = getHtmlFlatNode(document.getText(), rootNode, offset, true); + if (!currentNode) { + return; + } + + // If no opening/closing tag or cursor is between open and close tag, then no-op + if (!currentNode.open + || !currentNode.close + || (offset > currentNode.open.end && offset < currentNode.close.start)) { + return; + } + + // Place cursor inside the close tag if cursor is inside the open tag, else place it inside the open tag + const finalOffset = (offset <= currentNode.open.end) ? currentNode.close.start + 2 : currentNode.start + 1; + return offsetRangeToSelection(document, finalOffset, finalOffset); +} + + +/** + * This function implements the "Match Tag" feature in VSCode's HTML editing. When the user triggers it + * (usually via a keybinding or command), it jumps your cursor from an opening HTML tag to its corresponding closing tag, + * or vice versa. + * In short: It’s the logic behind "jump to matching tag." + * @returns {void} Void - Updates editor selections in place. + */ +export function matchTag(): void { if (!validate(false) || !vscode.window.activeTextEditor) { return; } @@ -32,22 +59,3 @@ export function matchTag() { editor.revealRange(editor.selections[updatedSelections.length - 1]); } } - -function getUpdatedSelections(document: vscode.TextDocument, rootNode: HtmlFlatNode, position: vscode.Position): vscode.Selection | undefined { - const offset = document.offsetAt(position); - const currentNode = getHtmlFlatNode(document.getText(), rootNode, offset, true); - if (!currentNode) { - return; - } - - // If no opening/closing tag or cursor is between open and close tag, then no-op - if (!currentNode.open - || !currentNode.close - || (offset > currentNode.open.end && offset < currentNode.close.start)) { - return; - } - - // Place cursor inside the close tag if cursor is inside the open tag, else place it inside the open tag - const finalOffset = (offset <= currentNode.open.end) ? currentNode.close.start + 2 : currentNode.start + 1; - return offsetRangeToSelection(document, finalOffset, finalOffset); -}