Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only close tags in supported scope #225

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function insertAutoCloseTag(event: vscode.TextDocumentChangeEvent): void {
let enableAutoCloseSelfClosingTag = config.get<boolean>("enableAutoCloseSelfClosingTag", true);
let isFullMode = config.get<boolean>("fullMode");

if ((isSublimeText3Mode || isFullMode) && event.contentChanges[0].text === "/") {
if ((isSublimeText3Mode || isFullMode) && event.contentChanges[0].text === "/" && isPositionInSupportedScopeForLanguage(editor, originalPosition, languageId)) {
let text = editor.document.getText(new vscode.Range(new vscode.Position(0, 0), originalPosition));
let last2chars = "";
if (text.length > 2) {
Expand All @@ -74,8 +74,9 @@ function insertAutoCloseTag(event: vscode.TextDocumentChangeEvent): void {
}
}

if (((!isSublimeText3Mode || isFullMode) && isRightAngleBracket) ||
(enableAutoCloseSelfClosingTag && event.contentChanges[0].text === "/")) {
if ((((!isSublimeText3Mode || isFullMode) && isRightAngleBracket) ||
(enableAutoCloseSelfClosingTag && event.contentChanges[0].text === "/"))
&& isPositionInSupportedScopeForLanguage(editor, originalPosition, languageId)) {
let textLine = editor.document.lineAt(selection.start);
let text = textLine.text.substring(0, selection.start.character + 1);
let result = /<([_a-zA-Z][a-zA-Z0-9:\-_.]*)(?:\s+[^<>]*?[^\s/<>=]+?)*?\s?(\/|>)$/.exec(text);
Expand Down Expand Up @@ -125,7 +126,7 @@ function insertCloseTag(): void {
let config = vscode.workspace.getConfiguration('auto-close-tag', editor.document.uri);
let excludedTags = config.get<string[]>("excludedTags", []);
let text = editor.document.getText(new vscode.Range(new vscode.Position(0, 0), originalPosition));
if (text.length > 2) {
if (text.length > 2 && isPositionInSupportedScopeForLanguage(editor, originalPosition, editor.document.languageId)) {
let closeTag = getCloseTag(text, excludedTags);
if (closeTag) {
editor.edit((editBuilder) => {
Expand Down Expand Up @@ -182,3 +183,11 @@ function moveSelectionRight(selection: vscode.Selection, shift: number): vscode.
function occurrenceCount(source: string, find: string): number {
return source.split(find).length - 1;
}

function isPositionInSupportedScopeForLanguage(editor: vscode.TextEditor, position: vscode.Position, languageId: string): boolean {
if (languageId === 'glimmer-js' || languageId === 'glimmer-ts') {
const text = editor.document.getText(new vscode.Range(new vscode.Position(0, 0), position));
return text.lastIndexOf('<template>') > text.lastIndexOf('</template>');
}
return true;
}
Loading