Skip to content

Commit ff46c7e

Browse files
author
Simon Holthausen
committed
(fix) don't trigger auto close for arrow functions
Fixes #89
1 parent 89ebffc commit ff46c7e

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

packages/svelte-vscode/src/html/autoClose.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,9 @@
66
*--------------------------------------------------------------------------------------------*/
77
'use strict';
88

9-
import {
10-
window,
11-
workspace,
12-
Disposable,
13-
TextDocument,
14-
Position,
15-
SnippetString,
16-
} from 'vscode';
9+
import { window, workspace, Disposable, TextDocument, Position, SnippetString } from 'vscode';
1710

18-
import { TextDocumentContentChangeEvent } from "vscode-languageserver-protocol";
11+
import { TextDocumentContentChangeEvent } from 'vscode-languageserver-protocol';
1912

2013
export function activateTagClosing(
2114
tagProvider: (document: TextDocument, position: Position) => Thenable<string>,
@@ -24,7 +17,7 @@ export function activateTagClosing(
2417
): Disposable {
2518
const disposables: Disposable[] = [];
2619
workspace.onDidChangeTextDocument(
27-
event => onDidChangeTextDocument(event.document, event.contentChanges),
20+
(event) => onDidChangeTextDocument(event.document, event.contentChanges),
2821
null,
2922
disposables,
3023
);
@@ -67,17 +60,35 @@ export function activateTagClosing(
6760
}
6861
const lastChange = changes[changes.length - 1];
6962
const lastCharacter = lastChange.text[lastChange.text.length - 1];
70-
if ("range" in lastChange && (lastChange.rangeLength ?? 0) > 0 || (lastCharacter !== '>' && lastCharacter !== '/')) {
63+
if (
64+
('range' in lastChange && (lastChange.rangeLength ?? 0) > 0) ||
65+
(lastCharacter !== '>' && lastCharacter !== '/')
66+
) {
7167
return;
7268
}
73-
const rangeStart = "range" in lastChange ? lastChange.range.start : new Position(0, document.getText().length);
69+
70+
const docContent = document.getText();
71+
72+
// VSCode has this property, but it's not in the standard typings
73+
const offsetOfPrevCharacter = (<any>lastChange).rangeOffset - 1;
74+
const previousCharacter =
75+
lastChange.text[lastChange.text.length - 2] || docContent[offsetOfPrevCharacter];
76+
// handle false positive of arrow function inside moustache tag so that this
77+
// <div {() =>
78+
// does not trigger auto close
79+
if (previousCharacter === '=') {
80+
return;
81+
}
82+
83+
const rangeStart =
84+
'range' in lastChange ? lastChange.range.start : new Position(0, docContent.length);
7485
const version = document.version;
7586
timeout = setTimeout(() => {
7687
const position = new Position(
7788
rangeStart.line,
7889
rangeStart.character + lastChange.text.length,
7990
);
80-
tagProvider(document, position).then(text => {
91+
tagProvider(document, position).then((text) => {
8192
if (text && isEnabled) {
8293
const activeEditor = window.activeTextEditor;
8394
if (activeEditor) {
@@ -86,11 +97,11 @@ export function activateTagClosing(
8697
const selections = activeEditor.selections;
8798
if (
8899
selections.length &&
89-
selections.some(s => s.active.isEqual(position))
100+
selections.some((s) => s.active.isEqual(position))
90101
) {
91102
activeEditor.insertSnippet(
92103
new SnippetString(text),
93-
selections.map(s => s.active),
104+
selections.map((s) => s.active),
94105
);
95106
} else {
96107
activeEditor.insertSnippet(new SnippetString(text), position);

0 commit comments

Comments
 (0)