Skip to content

Commit

Permalink
feat: add "Volar: Reload Project" command
Browse files Browse the repository at this point in the history
close #1605
  • Loading branch information
johnsoncodehk committed Jul 31, 2022
1 parent a819bd2 commit 0f08c0f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
9 changes: 9 additions & 0 deletions extensions/vscode-vue-language-features/package.json
Expand Up @@ -557,6 +557,11 @@
"title": "Restart Vue server",
"category": "Volar"
},
{
"command": "volar.action.reloadProject",
"title": "Reload Project",
"category": "Volar"
},
{
"command": "volar.action.doctor",
"title": "Show doctor panel (WIP)",
Expand Down Expand Up @@ -655,6 +660,10 @@
"command": "volar.action.restartServer",
"when": "volar.activated"
},
{
"command": "volar.action.reloadProject",
"when": "volar.activated"
},
{
"command": "volar.action.doctor",
"when": "volar.activated"
Expand Down
2 changes: 2 additions & 0 deletions extensions/vscode-vue-language-features/src/common.ts
Expand Up @@ -17,6 +17,7 @@ import * as virtualFiles from './features/virtualFiles';
import * as tsconfig from './features/tsconfig';
import * as doctor from './features/doctor';
import * as fileReferences from './features/fileReferences';
import * as reloadProject from './features/reloadProject';

let apiClient: lsp.BaseLanguageClient;
let docClient: lsp.BaseLanguageClient | undefined;
Expand Down Expand Up @@ -137,6 +138,7 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
tsVersion.register('volar.selectTypeScriptVersion', context, [apiClient, docClient].filter(shared.notEmpty));
tsconfig.register('volar.openTsconfig', context, docClient ?? apiClient);
fileReferences.register('volar.vue.findAllFileReferences', apiClient);
reloadProject.register('volar.action.reloadProject', context, [apiClient, docClient].filter(shared.notEmpty));

async function requestReloadVscode() {
const reload = await vscode.window.showInformationMessage(
Expand Down
@@ -0,0 +1,13 @@
import * as vscode from 'vscode';
import * as shared from '@volar/shared';
import type { BaseLanguageClient } from 'vscode-languageclient';

export async function register(cmd: string, context: vscode.ExtensionContext, languageClients: BaseLanguageClient[]) {
context.subscriptions.push(vscode.commands.registerCommand(cmd, () => {
if (vscode.window.activeTextEditor) {
for (const client of languageClients) {
client.sendNotification(shared.ReloadProjectNotification.type, client.code2ProtocolConverter.asTextDocumentIdentifier(vscode.window.activeTextEditor.document));
}
}
}));
}
10 changes: 4 additions & 6 deletions packages/shared/src/requests.ts
Expand Up @@ -52,12 +52,6 @@ export namespace FindFileReferenceRequest {
* Server Requests
*/

export namespace InitDoneRequest {
export type ResponseType = null | undefined;
export type ErrorType = never;
export const type = new vscode.RequestType0<ResponseType, ErrorType>('volar/init');
}

export namespace GetMatchTsConfigRequest {
export type ParamsType = vscode.TextDocumentIdentifier;
export type ResponseType = string | null | undefined;
Expand Down Expand Up @@ -96,6 +90,10 @@ export namespace WriteVirtualFilesNotification {
export const type = new vscode.NotificationType0('volar.action.writeVirtualFiles');
}

export namespace ReloadProjectNotification {
export const type = new vscode.NotificationType<vscode.TextDocumentIdentifier>('volar.action.reloadProject');
}

export namespace DetectDocumentNameCasesRequest {
export type ParamsType = vscode.TextDocumentIdentifier;
export type ResponseType = {
Expand Down
3 changes: 3 additions & 0 deletions packages/vue-language-server/src/features/customFeatures.ts
Expand Up @@ -20,6 +20,9 @@ export function register(
connection.onRequest(shared.GetMatchTsConfigRequest.type, async handler => {
return (await projects.getProject(handler.uri))?.tsconfig;
});
connection.onNotification(shared.ReloadProjectNotification.type, async handler => {
projects.reloadProject(handler.uri);
});
connection.onNotification(shared.WriteVirtualFilesNotification.type, async () => {

const fs = await import('fs');
Expand Down
36 changes: 25 additions & 11 deletions packages/vue-language-server/src/projects.ts
Expand Up @@ -96,7 +96,29 @@ export function createProjects(
documents.onDidClose(change => {
connection.sendDiagnostics({ uri: change.document.uri, diagnostics: [] });
});
connection.onDidChangeWatchedFiles(async handler => {
connection.onDidChangeWatchedFiles(onDidChangeWatchedFiles);

return {
workspaces,
getProject,
reloadProject,
};

async function reloadProject(uri: string) {

const configs: string[] = [];

for (const [_, workspace] of workspaces) {
const config = await workspace.findMatchConfigs(uri);
if (config) {
configs.push(config);
}
}

onDidChangeWatchedFiles({ changes: configs.map(c => ({ uri: c, type: vscode.FileChangeType.Changed })) });
}

async function onDidChangeWatchedFiles(handler: vscode.DidChangeWatchedFilesParams) {

for (const change of handler.changes) {
if (change.type === vscode.FileChangeType.Created) {
Expand Down Expand Up @@ -147,12 +169,7 @@ export function createProjects(

onDriveFileUpdated(undefined);
}
});

return {
workspaces,
getProject,
};
}

async function onDriveFileUpdated(driveFileName: string | undefined) {

Expand Down Expand Up @@ -297,16 +314,13 @@ function createWorkspace(

return {
projects,
getProject,
findMatchConfigs,
getProjectAndTsConfig,
getProjectByCreate,
getInferredProject,
getInferredProjectDontCreate: () => inferredProject,
};

async function getProject(uri: string) {
return (await getProjectAndTsConfig(uri))?.project;
}
async function getProjectAndTsConfig(uri: string) {
const tsconfig = await findMatchConfigs(uri);
if (tsconfig) {
Expand Down

0 comments on commit 0f08c0f

Please sign in to comment.