Skip to content

Commit 9fdd0e4

Browse files
authored
Merge pull request #214 from sentience/improve-loading-progress
Improve loading progress
2 parents 693886c + 95ff01c commit 9fdd0e4

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/elmWorkspace.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Forest } from "./forest";
1010
import { Imports } from "./imports";
1111
import * as utils from "./util/elmUtils";
1212
import { Settings } from "./util/settings";
13-
import { WorkDoneProgress } from "vscode-languageserver/lib/progress";
1413

1514
const readFile = util.promisify(fs.readFile);
1615
const readdir = util.promisify(fs.readdir);
@@ -43,8 +42,8 @@ export class ElmWorkspace {
4342
this.imports = new Imports(parser);
4443
}
4544

46-
public async init(progress: WorkDoneProgress) {
47-
await this.initWorkspace(progress);
45+
public async init(progressCallback: (percent: number) => void) {
46+
await this.initWorkspace(progressCallback);
4847
}
4948

5049
public hasDocument(uri: URI): boolean {
@@ -69,9 +68,8 @@ export class ElmWorkspace {
6968
return this.rootPath;
7069
}
7170

72-
private async initWorkspace(x: WorkDoneProgress) {
71+
private async initWorkspace(progressCallback: (percent: number) => void) {
7372
let progress = 0;
74-
x.begin("Indexing", progress);
7573
let elmVersion;
7674
try {
7775
elmVersion = await utils.getElmVersion(
@@ -84,9 +82,9 @@ export class ElmWorkspace {
8482
`Could not figure out elm version, this will impact how good the server works. \n ${e.stack}`,
8583
);
8684
}
85+
const pathToElmJson = path.join(this.rootPath.fsPath, "elm.json");
86+
this.connection.console.info(`Reading elm.json from ${pathToElmJson}`);
8787
try {
88-
const pathToElmJson = path.join(this.rootPath.fsPath, "elm.json");
89-
this.connection.console.info(`Reading elm.json from ${pathToElmJson}`);
9088
// Find elm files and feed them to tree sitter
9189
const elmJson = require(pathToElmJson);
9290
const type = elmJson.type;
@@ -186,27 +184,33 @@ export class ElmWorkspace {
186184
}
187185

188186
const promiseList: Promise<void>[] = [];
189-
const progressSteps = (elmFilePaths.length * 2) / 100;
187+
const PARSE_STAGES = 3;
188+
const progressDelta = 100 / (elmFilePaths.length * PARSE_STAGES);
190189
for (const filePath of elmFilePaths) {
191-
progress += progressSteps;
192-
x.report(progressSteps);
193-
promiseList.push(this.readAndAddToForest(filePath));
190+
progressCallback((progress += progressDelta));
191+
promiseList.push(
192+
this.readAndAddToForest(filePath, () => {
193+
progressCallback((progress += progressDelta));
194+
}),
195+
);
194196
}
195197
await Promise.all(promiseList);
196198

197199
this.forest.treeIndex.forEach(item => {
198-
progress += progressSteps;
199-
x.report(progressSteps);
200200
this.connection.console.info(
201201
`Adding imports ${URI.parse(item.uri).fsPath}`,
202202
);
203203
this.imports.updateImports(item.uri, item.tree, this.forest);
204+
progressCallback((progress += progressDelta));
204205
});
205206

206-
x.done();
207-
this.connection.console.info("Done parsing all files.");
207+
this.connection.console.info(
208+
`Done parsing all files for ${pathToElmJson}`,
209+
);
208210
} catch (error) {
209-
this.connection.console.error(error.stack);
211+
this.connection.console.error(
212+
`Error parsing files for ${pathToElmJson}:\n${error.stack}`,
213+
);
210214
}
211215
}
212216

@@ -257,7 +261,10 @@ export class ElmWorkspace {
257261
: `${os.homedir()}/.elm`;
258262
}
259263

260-
private async readAndAddToForest(filePath: IFolder): Promise<void> {
264+
private async readAndAddToForest(
265+
filePath: IFolder,
266+
callback: () => void,
267+
): Promise<void> {
261268
return new Promise(async (resolve, reject) => {
262269
try {
263270
this.connection.console.info(`Adding ${filePath.path.toString()}`);
@@ -273,6 +280,7 @@ export class ElmWorkspace {
273280
tree,
274281
filePath.maintainerAndPackageName,
275282
);
283+
callback();
276284
resolve();
277285
} catch (error) {
278286
this.connection.console.error(error.stack);

src/server.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,24 @@ export class Server implements ILanguageServer {
109109
}
110110

111111
public async init() {
112-
this.elmWorkspaces.forEach(async it => await it.init(this.progress));
112+
this.progress.begin("Indexing Elm", 0);
113+
await Promise.all(
114+
this.elmWorkspaces
115+
.map(ws => ({ ws, indexedPercent: 0 }))
116+
.map((indexingWs, _, all) =>
117+
indexingWs.ws.init((percent: number) => {
118+
// update progress for this workspace
119+
indexingWs.indexedPercent = percent;
120+
121+
// report average progress across all workspaces
122+
const avgIndexed =
123+
all.reduce((sum, { indexedPercent }) => sum + indexedPercent, 0) /
124+
all.length;
125+
this.progress.report(avgIndexed, `${Math.round(avgIndexed)}%`);
126+
}),
127+
),
128+
);
129+
this.progress.done();
113130
}
114131

115132
public async registerInitializedProviders() {

0 commit comments

Comments
 (0)