Skip to content

Commit

Permalink
show progress in client
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Nov 3, 2023
1 parent 7e4b1cc commit 7a3b5f4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
45 changes: 2 additions & 43 deletions packages/language-server/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export function createConnection() {

export function startLanguageServer(connection: vscode.Connection, ...plugins: LanguageServerPlugin[]) {

const jobs = new Map<Promise<any>, string>();

let fsProgress: Promise<vscode.WorkDoneProgressServerReporter> | undefined;
let totalJobs = 0;

startCommonLanguageServer(connection, plugins, () => ({
uriToFileName,
fileNameToUri,
Expand Down Expand Up @@ -89,9 +84,7 @@ export function startLanguageServer(connection: vscode.Connection, ...plugins: L
if (uri.startsWith('http://') || uri.startsWith('https://')) { // perf
return await httpSchemaRequestHandler(uri);
}
return withProgress(async () => {
return await connection.sendRequest(FsReadFileRequest.type, uri) ?? undefined;
}, uri);
return await connection.sendRequest(FsReadFileRequest.type, uri) ?? undefined;
},
async readDirectory(uri) {
if (uri.startsWith('__invalid__:')) {
Expand All @@ -100,47 +93,13 @@ export function startLanguageServer(connection: vscode.Connection, ...plugins: L
if (uri.startsWith('http://') || uri.startsWith('https://')) { // perf
return [];
}
return withProgress(async () => {
return await connection.sendRequest(FsReadDirectoryRequest.type, uri);
}, uri);
return await connection.sendRequest(FsReadDirectoryRequest.type, uri);
},
},
getCancellationToken(original) {
return original ?? vscode.CancellationToken.None;
},
}));

async function withProgress<T>(fn: () => Promise<T>, asset: string): Promise<T> {
const path = URI.parse(asset).path;
if (!fsProgress) {
fsProgress = connection.window.createWorkDoneProgress();
fsProgress.then(progress => progress.begin('', 0));
}
const _fsProgress = await fsProgress;
totalJobs++;
let job!: Promise<T>;
try {
job = fn();
jobs.set(job, path);
for (const [_, path] of jobs) {
_fsProgress.report(0, `Loading ${totalJobs - jobs.size} of ${totalJobs} files: ${path}`);
break;
}
return await job;
} finally {
jobs.delete(job);
if (jobs.size === 0) {
_fsProgress.done();
fsProgress = undefined;
}
else {
for (const [_, path] of jobs) {
_fsProgress.report((totalJobs - jobs.size) / totalJobs * 100, `Loading ${totalJobs - jobs.size} of ${totalJobs} files: ${path}`);
break;
}
}
}
}
}

function uriToFileName(uri: string) {
Expand Down
47 changes: 45 additions & 2 deletions packages/vscode/src/features/serverSys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export async function activate(context: vscode.ExtensionContext, client: BaseLan

const subscriptions: vscode.Disposable[] = [];
const textDecoder = new TextDecoder();
const jobs = new Map<Promise<any>, string>();

let startProgress = false;
let totalJobs = 0;

addRequestHandlers();

Expand All @@ -21,8 +25,43 @@ export async function activate(context: vscode.ExtensionContext, client: BaseLan
function addRequestHandlers() {

subscriptions.push(client.onRequest(FsStatRequest.type, stat));
subscriptions.push(client.onRequest(FsReadFileRequest.type, readFile));
subscriptions.push(client.onRequest(FsReadDirectoryRequest.type, readDirectory));
subscriptions.push(client.onRequest(FsReadFileRequest.type, uri => {
return withProgress(() => readFile(uri), uri);
}));
subscriptions.push(client.onRequest(FsReadDirectoryRequest.type, uri => {
return withProgress(() => readDirectory(uri), uri);
}));

async function withProgress<T>(fn: () => Promise<T>, asset: string): Promise<T> {
asset = vscode.Uri.parse(asset).path;
totalJobs++;
let job!: Promise<T>;
try {
job = fn();
jobs.set(job, asset);
if (!startProgress && jobs.size >= 10) {
startProgress = true;
vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async progress => {
progress.report({
message: `Loading ${totalJobs} resources: ${asset}`
});
while (jobs.size) {
for (const [_, asset] of jobs) {
progress.report({
message: `Loading ${totalJobs} resources: ${asset}`,
});
await sleep(100);
break;
}
}
startProgress = false;
});
}
return await job;
} finally {
jobs.delete(job);
}
}

async function stat(uri: string) {

Expand Down Expand Up @@ -114,3 +153,7 @@ export async function activate(context: vscode.ExtensionContext, client: BaseLan
}
}
}

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

0 comments on commit 7a3b5f4

Please sign in to comment.