-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathhtmlMatchingTagPosition.ts
38 lines (31 loc) · 1.34 KB
/
htmlMatchingTagPosition.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument, Position } from '../htmlLanguageTypes';
import { HTMLDocument } from '../parser/htmlParser';
export function findMatchingTagPosition(
document: TextDocument,
position: Position,
htmlDocument: HTMLDocument
): Position | null {
const offset = document.offsetAt(position);
const node = htmlDocument.findNodeAt(offset);
if (!node.tag) {
return null;
}
if (!node.endTagStart) {
return null;
}
// Within open tag, compute close tag
if (node.start + '<'.length <= offset && offset <= node.start + '<'.length + node.tag.length) {
const mirrorOffset = (offset - '<'.length - node.start) + node.endTagStart + '</'.length;
return document.positionAt(mirrorOffset);
}
// Within closing tag, compute open tag
if (node.endTagStart + '</'.length <= offset && offset <= node.endTagStart + '</'.length + node.tag.length) {
const mirrorOffset = (offset - '</'.length - node.endTagStart) + node.start + '<'.length;
return document.positionAt(mirrorOffset);
}
return null;
}