Skip to content

Commit

Permalink
(fix) don't trigger auto close for arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Holthausen committed May 16, 2020
1 parent 89ebffc commit ff46c7e
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions packages/svelte-vscode/src/html/autoClose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
Expand All @@ -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,
);
Expand Down Expand Up @@ -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 = (<any>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
// <div {() =>
// 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) {
Expand All @@ -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);
Expand Down

0 comments on commit ff46c7e

Please sign in to comment.