Skip to content

fix(language-service): wait for tsserver to be ready when requesting auto insert .value #3914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/language-service/lib/plugins/vue-autoinsert-dotvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function create(ts: typeof import('typescript')): ServicePlugin {
return {
name: 'vue-autoinsert-dotvalue',
create(context): ServicePluginInstance {
let currentReq = 0;
return {
async provideAutoInsertionEdit(document, position, lastChange) {

Expand All @@ -19,6 +20,12 @@ export function create(ts: typeof import('typescript')): ServicePlugin {
if (!isCharacterTyping(document, lastChange))
return;

const req = ++currentReq;
// Wait for tsserver to sync
await sleep(250);
if (req !== currentReq)
return;

const enabled = await context.env.getConfiguration?.<boolean>('vue.autoInsert.dotValue') ?? true;
if (!enabled)
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to check this configuration before the newly added code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getConfiguration has a send request cost before Volar has cached the option, so it is best to place it after sleep.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But now the unnecessary sleep will trigger for every document change, even if one is not using this option. Seems more wasteful than getting a configuration once that gets cached later.

Copy link
Member Author

@johnsoncodehk johnsoncodehk Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sleep is just setTimeout. Is it expensive if clearTimeout is not called in time?

Expand Down Expand Up @@ -56,6 +63,10 @@ export function create(ts: typeof import('typescript')): ServicePlugin {
};
}

function sleep(ms: number) {
return new Promise<void>(resolve => setTimeout(resolve, ms));
}

function isTsDocument(document: TextDocument) {
return document.languageId === 'javascript' ||
document.languageId === 'typescript' ||
Expand Down
22 changes: 13 additions & 9 deletions packages/typescript-plugin/lib/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as fs from 'fs';
import * as net from 'net';
import { collectExtractProps } from './requests/collectExtractProps';
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos';
import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition';
import { pipeFile } from './utils';

export interface Request {
Expand All @@ -21,39 +25,39 @@ export function startNamedPipeServer() {
if (started) return;
started = true;
const server = net.createServer(connection => {
connection.on('data', async data => {
connection.on('data', data => {
const request: Request = JSON.parse(data.toString());
if (request.type === 'collectExtractProps') {
const result = (await import('./requests/collectExtractProps.js')).collectExtractProps.apply(null, request.args);
const result = collectExtractProps.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getPropertiesAtLocation') {
const result = (await import('./requests/getPropertiesAtLocation.js')).getPropertiesAtLocation.apply(null, request.args);
const result = getPropertiesAtLocation.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getQuickInfoAtPosition') {
const result = (await import('./requests/getQuickInfoAtPosition.js')).getQuickInfoAtPosition.apply(null, request.args);
const result = getQuickInfoAtPosition.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
// Component Infos
else if (request.type === 'getComponentProps') {
const result = (await import('./requests/componentInfos.js')).getComponentProps.apply(null, request.args);
const result = getComponentProps.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getComponentEvents') {
const result = (await import('./requests/componentInfos.js')).getComponentEvents.apply(null, request.args);
const result = getComponentEvents.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getTemplateContextProps') {
const result = (await import('./requests/componentInfos.js')).getTemplateContextProps.apply(null, request.args);
const result = getTemplateContextProps.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getComponentNames') {
const result = (await import('./requests/componentInfos.js')).getComponentNames.apply(null, request.args);
const result = getComponentNames.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getElementAttrs') {
const result = (await import('./requests/componentInfos.js')).getElementAttrs.apply(null, request.args);
const result = getElementAttrs.apply(null, request.args);
connection.write(JSON.stringify(result ?? null));
}
else {
Expand Down