Skip to content

Commit

Permalink
fix: revert client autoInsertion.ts to fix #2238
Browse files Browse the repository at this point in the history
close #2238
  • Loading branch information
johnsoncodehk committed Dec 25, 2022
1 parent f89202b commit 6310e81
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
45 changes: 19 additions & 26 deletions packages/vscode-language-client/src/features/autoInsertion.ts
Expand Up @@ -30,13 +30,7 @@ export async function register(
}

function onDidChangeTextDocument({ document, contentChanges, reason }: vscode.TextDocumentChangeEvent) {
if (
!isEnabled
|| contentChanges.length !== 1
|| !contentChanges[0].text // delete
|| reason === vscode.TextDocumentChangeReason.Undo
|| reason === vscode.TextDocumentChangeReason.Redo
) {
if (!isEnabled || contentChanges.length === 0 || reason === vscode.TextDocumentChangeReason.Undo || reason === vscode.TextDocumentChangeReason.Redo) {
return;
}
const activeDocument = vscode.window.activeTextEditor?.document;
Expand Down Expand Up @@ -85,33 +79,32 @@ export async function register(
lastChange: vscode.TextDocumentContentChangeEvent,
provider: (document: vscode.TextDocument, position: vscode.Position, lastChange: vscode.TextDocumentContentChangeEvent, isCancel: () => boolean) => Thenable<string | vscode.TextEdit | null | undefined>,
) {
const rangeStart = lastChange.range.start;
const version = document.version;
timeout = setTimeout(() => {
const position = vscode.window.activeTextEditor?.selections.length === 1 && vscode.window.activeTextEditor.selections[0].active;
if (position) {
provider(document, position, lastChange, () => vscode.window.activeTextEditor?.document.version !== version).then(text => {
if (text && isEnabled) {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
const activeDocument = activeEditor.document;
if (document === activeDocument && activeDocument.version === version) {
if (typeof text === 'string') {
const selections = activeEditor.selections;
if (selections.length && selections.some(s => s.active.isEqual(position))) {
activeEditor.insertSnippet(new vscode.SnippetString(text), selections.map(s => s.active));
}
else {
activeEditor.insertSnippet(new vscode.SnippetString(text), position);
}
const position = new vscode.Position(rangeStart.line, rangeStart.character + lastChange.text.length);
provider(document, position, lastChange, () => vscode.window.activeTextEditor?.document.version !== version).then(text => {
if (text && isEnabled) {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
const activeDocument = activeEditor.document;
if (document === activeDocument && activeDocument.version === version) {
if (typeof text === 'string') {
const selections = activeEditor.selections;
if (selections.length && selections.some(s => s.active.isEqual(position))) {
activeEditor.insertSnippet(new vscode.SnippetString(text), selections.map(s => s.active));
}
else {
activeEditor.insertSnippet(new vscode.SnippetString(text.newText), text.range);
activeEditor.insertSnippet(new vscode.SnippetString(text), position);
}
}
else {
activeEditor.insertSnippet(new vscode.SnippetString(text.newText), text.range);
}
}
}
});
}
}
});
timeout = undefined;
}, 100);
}
Expand Down
Expand Up @@ -10,26 +10,28 @@ export default function (): LanguageServicePlugin {
context = _context;
},

async doAutoInsert(document, position) {
async doAutoInsert(document, _, { lastChange }) {

if (document.languageId === 'html' || document.languageId === 'jade') {

const enabled = await context.env.configurationHost?.getConfiguration<boolean>('volar.addSpaceBetweenDoubleCurlyBrackets') ?? true;
if (!enabled)
return;

const prev = document.getText({
start: { line: position.line, character: position.character - 2 },
end: position,
});
if (prev === '{{') {
const next = document.getText({
start: position,
end: { line: position.line, character: position.character + 2 },
});
if (next === '}}') {
return ` $0 `;
}
if (
lastChange.text === '{}'
&& document.getText({
start: { line: lastChange.range.start.line, character: lastChange.range.start.character - 1 },
end: { line: lastChange.range.start.line, character: lastChange.range.start.character + 3 }
}) === '{{}}'
) {
return {
newText: ` $0 `,
range: {
start: { line: lastChange.range.start.line, character: lastChange.range.start.character + 1 },
end: { line: lastChange.range.start.line, character: lastChange.range.start.character + 1 }
},
};
}
}
},
Expand Down

0 comments on commit 6310e81

Please sign in to comment.