From ff46c7e946a841504a6be3925ff92a5638b12b3c Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 16 May 2020 10:28:25 +0200 Subject: [PATCH] (fix) don't trigger auto close for arrow functions Fixes #89 --- packages/svelte-vscode/src/html/autoClose.ts | 41 +++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/svelte-vscode/src/html/autoClose.ts b/packages/svelte-vscode/src/html/autoClose.ts index db90e83aa..3490e3aea 100644 --- a/packages/svelte-vscode/src/html/autoClose.ts +++ b/packages/svelte-vscode/src/html/autoClose.ts @@ -6,16 +6,9 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import { - window, - workspace, - Disposable, - TextDocument, - Position, - SnippetString, -} from 'vscode'; +import { window, workspace, Disposable, TextDocument, Position, SnippetString } from 'vscode'; -import { TextDocumentContentChangeEvent } from "vscode-languageserver-protocol"; +import { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol'; export function activateTagClosing( tagProvider: (document: TextDocument, position: Position) => Thenable, @@ -24,7 +17,7 @@ export function activateTagClosing( ): Disposable { const disposables: Disposable[] = []; workspace.onDidChangeTextDocument( - event => onDidChangeTextDocument(event.document, event.contentChanges), + (event) => onDidChangeTextDocument(event.document, event.contentChanges), null, disposables, ); @@ -67,17 +60,35 @@ export function activateTagClosing( } const lastChange = changes[changes.length - 1]; const lastCharacter = lastChange.text[lastChange.text.length - 1]; - if ("range" in lastChange && (lastChange.rangeLength ?? 0) > 0 || (lastCharacter !== '>' && lastCharacter !== '/')) { + if ( + ('range' in lastChange && (lastChange.rangeLength ?? 0) > 0) || + (lastCharacter !== '>' && lastCharacter !== '/') + ) { return; } - const rangeStart = "range" in lastChange ? lastChange.range.start : new Position(0, document.getText().length); + + const docContent = document.getText(); + + // VSCode has this property, but it's not in the standard typings + const offsetOfPrevCharacter = (lastChange).rangeOffset - 1; + const previousCharacter = + lastChange.text[lastChange.text.length - 2] || docContent[offsetOfPrevCharacter]; + // handle false positive of arrow function inside moustache tag so that this + //
+ // does not trigger auto close + if (previousCharacter === '=') { + return; + } + + const rangeStart = + 'range' in lastChange ? lastChange.range.start : new Position(0, docContent.length); const version = document.version; timeout = setTimeout(() => { const position = new Position( rangeStart.line, rangeStart.character + lastChange.text.length, ); - tagProvider(document, position).then(text => { + tagProvider(document, position).then((text) => { if (text && isEnabled) { const activeEditor = window.activeTextEditor; if (activeEditor) { @@ -86,11 +97,11 @@ export function activateTagClosing( const selections = activeEditor.selections; if ( selections.length && - selections.some(s => s.active.isEqual(position)) + selections.some((s) => s.active.isEqual(position)) ) { activeEditor.insertSnippet( new SnippetString(text), - selections.map(s => s.active), + selections.map((s) => s.active), ); } else { activeEditor.insertSnippet(new SnippetString(text), position);