Skip to content

Commit

Permalink
refactor: inject auto insert check
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 8, 2022
1 parent 5709a50 commit ebf999c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
11 changes: 10 additions & 1 deletion extensions/vscode-vue-language-features/src/common.ts
Expand Up @@ -97,7 +97,16 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
doctor.register(context, semanticClient);
componentMeta.register(context, semanticClient);

registerAutoInsertion(context, [syntacticClient, semanticClient]);
const supportedLanguages: Record<string, boolean> = {
vue: true,
markdown: true,
javascript: true,
typescript: true,
javascriptreact: true,
typescriptreact: true,
};

registerAutoInsertion(context, [syntacticClient, semanticClient], document => supportedLanguages[document.languageId]);
registerShowVirtualFiles('volar.action.showVirtualFiles', context, semanticClient);
registerWriteVirtualFiles('volar.action.writeVirtualFiles', context, semanticClient);
registerFileReferences('volar.vue.findAllFileReferences', context, semanticClient);
Expand Down
28 changes: 13 additions & 15 deletions packages/vscode-language-client/src/features/autoInsertion.ts
Expand Up @@ -2,16 +2,11 @@ import * as vscode from 'vscode';
import type { BaseLanguageClient } from 'vscode-languageclient';
import { AutoInsertRequest } from '@volar/language-server';

export async function register(context: vscode.ExtensionContext, clients: BaseLanguageClient[]) {

const supportedLanguages: Record<string, boolean> = {
vue: true,
markdown: true,
javascript: true,
typescript: true,
javascriptreact: true,
typescriptreact: true,
};
export async function register(
context: vscode.ExtensionContext,
clients: BaseLanguageClient[],
active: (document: vscode.TextDocument) => boolean,
) {

let isEnabled = false;
let timeout: NodeJS.Timeout | undefined;
Expand All @@ -28,7 +23,7 @@ export async function register(context: vscode.ExtensionContext, clients: BaseLa
return;
}
let document = editor.document;
if (!supportedLanguages[document.languageId]) {
if (!active(document)) {
return;
}
isEnabled = true;
Expand All @@ -49,7 +44,7 @@ export async function register(context: vscode.ExtensionContext, clients: BaseLa

const lastChange = contentChanges[contentChanges.length - 1];

doAutoInsert(document, lastChange, async (document, position, lastChange) => {
doAutoInsert(document, lastChange, async (document, position, lastChange, isCancel) => {

for (const client of clients) {

Expand All @@ -63,6 +58,8 @@ export async function register(context: vscode.ExtensionContext, clients: BaseLa
},
};

if (isCancel()) return;

const result = await client.sendRequest(AutoInsertRequest.type, params);

if (result !== undefined) {
Expand All @@ -80,13 +77,13 @@ export async function register(context: vscode.ExtensionContext, clients: BaseLa
function doAutoInsert(
document: vscode.TextDocument,
lastChange: vscode.TextDocumentContentChangeEvent,
provider: (document: vscode.TextDocument, position: vscode.Position, lastChange: vscode.TextDocumentContentChangeEvent) => Thenable<string | vscode.TextEdit | null | undefined>,
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 = new vscode.Position(rangeStart.line, rangeStart.character + lastChange.text.length);
provider(document, position, lastChange).then(text => {
provider(document, position, lastChange, () => vscode.window.activeTextEditor?.document.version !== version).then(text => {
if (text && isEnabled) {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
Expand All @@ -96,7 +93,8 @@ export async function register(context: vscode.ExtensionContext, clients: BaseLa
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 {
}
else {
activeEditor.insertSnippet(new vscode.SnippetString(text), position);
}
}
Expand Down

0 comments on commit ebf999c

Please sign in to comment.