Repository: elm-tooling/elm-language-server
Pull request #566 (2021-03-26 12:05:56)

Cache possible import cache for code actions
Performance was really bad if you has a file with a lot of missing imports.


Patch:
diff --git a/src/providers/codeAction/importCodeAction.ts b/src/providers/codeAction/importCodeAction.ts
index ea74481..060d34d 100644
--- a/src/providers/codeAction/importCodeAction.ts
+++ b/src/providers/codeAction/importCodeAction.ts
@@ -40,6 +40,8 @@ CodeActionProvider.registerCodeAction({
     });
   },
   getFixAllCodeAction: (params: ICodeActionParams) => {
+    const possibleImportsCache = new Map<string, IPossibleImport[]>();
+
     return CodeActionProvider.getFixAllCodeAction(
       "Add all missing imports",
       params,
@@ -49,6 +51,7 @@ CodeActionProvider.registerCodeAction({
         const firstPossibleImport = getPossibleImports(
           params,
           diagnostic.range,
+          possibleImportsCache,
         )[0];
 
         if (firstPossibleImport) {
@@ -70,16 +73,19 @@ CodeActionProvider.registerCodeAction({
 function getPossibleImports(
   params: ICodeActionParams,
   range: Range,
+  possibleImportsCache?: Map<string, IPossibleImport[]>,
 ): IPossibleImport[] {
   const valueNode = TreeUtils.getNamedDescendantForRange(
     params.sourceFile,
     range,
   );
 
-  const possibleImports = ImportUtils.getPossibleImports(
-    params.program,
-    params.sourceFile,
-  );
+  const cached = possibleImportsCache?.get(params.sourceFile.uri);
+
+  const possibleImports =
+    cached ?? ImportUtils.getPossibleImports(params.program, params.sourceFile);
+
+  possibleImportsCache?.set(params.sourceFile.uri, possibleImports);
 
   // Add import quick fixes
   if (valueNode) {
diff --git a/src/providers/codeActionLs/removeUnusedCodeAction.ts b/src/providers/codeActionLs/removeUnusedCodeAction.ts
index e4f27e6..d8e001d 100644
--- a/src/providers/codeActionLs/removeUnusedCodeAction.ts
+++ b/src/providers/codeActionLs/removeUnusedCodeAction.ts
@@ -39,7 +39,7 @@ CodeActionProvider.registerCodeAction({
   getFixAllCodeAction: (params) => {
     const importsMap = new Map<string, Set<string>>();
     return CodeActionProvider.getFixAllCodeAction(
-      "Remove all reported unused code",
+      "Remove all unused code",
       params,
       errorCodes,
       fixId,


Test patch:
diff --git a/test/codeActionTests/removeUnusedCodeAction.test.ts b/test/codeActionTests/removeUnusedCodeAction.test.ts
index 3b9d64d..cd49cc9 100644
--- a/test/codeActionTests/removeUnusedCodeAction.test.ts
+++ b/test/codeActionTests/removeUnusedCodeAction.test.ts
@@ -104,7 +104,7 @@ func = ""
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );
@@ -132,7 +132,7 @@ func = foo
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );
@@ -160,7 +160,7 @@ func = foo
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );
@@ -188,7 +188,7 @@ func = foo
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );
@@ -216,7 +216,7 @@ func = foo + bar
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );
@@ -244,7 +244,7 @@ func = foo + bar
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );
@@ -272,7 +272,7 @@ func = foo + bar
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );
@@ -300,7 +300,7 @@ func = foo + bar
 
     await testCodeAction(
       source,
-      [{ title: "Remove all reported unused code" }],
+      [{ title: "Remove all unused code" }],
       expectedSource,
       true,
     );