Repository: elm-tooling/elm-language-server (ts)

Issue:
Completions for default imports are broken
Regression from dd1d6b8e481acd15cf4bcd176d6a5adba736298b.

Pull request:
Fix completions for default imports
Fixes #560.

Patch:
diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts
index 96e1f78..271ba12 100644
--- a/src/compiler/binder.ts
+++ b/src/compiler/binder.ts
@@ -5,6 +5,7 @@ import { NodeType, TreeUtils } from "../util/treeUtils";
 import { Utils } from "../util/utils";
 import { SyntaxNodeMap } from "./utils/syntaxNodeMap";
 import { Diagnostics, error } from "./diagnostics";
+import { Imports } from "./imports";
 
 export type SymbolMap = MultiMap<string, ISymbol>;
 function createSymbolMap(): SymbolMap {
@@ -107,6 +108,10 @@ export function bindTreeContainer(sourceFile: ISourceFile): void {
 
     symbolLinks.set(node, container);
 
+    if (node.type === "file") {
+      bindDefaultImports();
+    }
+
     forEachChild(bind);
 
     container = savedContainer;
@@ -271,6 +276,10 @@ export function bindTreeContainer(sourceFile: ISourceFile): void {
     });
   }
 
+  function bindDefaultImports(): void {
+    Imports.getVirtualImports().forEach(bindImportClause);
+  }
+
   function bindImportClause(node: SyntaxNode): void {
     const asClause = node.childForFieldName("asClause");
 
diff --git a/src/compiler/typeChecker.ts b/src/compiler/typeChecker.ts
index 3e60bbe..9254d7c 100644
--- a/src/compiler/typeChecker.ts
+++ b/src/compiler/typeChecker.ts
@@ -846,6 +846,10 @@ export function createTypeChecker(program: IProgram): TypeChecker {
       return "";
     }
 
+    if (module === "List" && name === "List") {
+      return "";
+    }
+
     const moduleImport = findImportModuleNameNode(module, sourceFile)?.parent;
 
     if (!moduleImport) {


Test patch:
diff --git a/test/codeActionTests/addMissingRecordFieldCodeAction.test.ts b/test/codeActionTests/addMissingRecordFieldCodeAction.test.ts
index 657f641..28e5493 100644
--- a/test/codeActionTests/addMissingRecordFieldCodeAction.test.ts
+++ b/test/codeActionTests/addMissingRecordFieldCodeAction.test.ts
@@ -70,7 +70,12 @@ func model =
 
   it("should work with record update expr", async () => {
     const source = `
-		--@ Test.elm
+--@ String.elm
+module String exposing (String)
+
+type String = String
+
+--@ Test.elm
 module Test exposing (..)
 
 func : { field : Int } -> a
@@ -80,7 +85,7 @@ func model =
 		`;
 
     const expectedSource = `
-		--@ Test.elm
+--@ Test.elm
 module Test exposing (..)
 
 func : { field : Int, newProp : String } -> a
diff --git a/test/completionProvider.test.ts b/test/completionProvider.test.ts
index b942f4f..252bd27 100644
--- a/test/completionProvider.test.ts
+++ b/test/completionProvider.test.ts
@@ -1582,4 +1582,44 @@ func =
       "exactMatch",
     );
   });
+
+  it("Completions from default imports", async () => {
+    const source = `
+--@ List.elm
+module List exposing (..)
+
+singleton : a -> List a
+singleton value =
+  [value]
+
+--@ Test.elm
+module Test exposing (..)
+
+func =
+    List.{-caret-}
+`;
+
+    await testCompletions(source, ["singleton"], "exactMatch");
+  });
+
+  it("Completions from default imports with alias", async () => {
+    const source = `
+--@ Platform/Cmd.elm
+module Platform.Cmd exposing (Cmd, batch)
+
+type Cmd msg = Cmd
+
+batch : List (Cmd msg) -> Cmd msg
+batch =
+  Elm.Kernel.Platform.batch
+
+--@ Test.elm
+module Test exposing (..)
+
+func =
+    Cmd.{-caret-}
+`;
+
+    await testCompletions(source, ["Cmd", "batch"], "exactMatch");
+  });
 });
diff --git a/test/typeInference.test.ts b/test/typeInference.test.ts
index dc38251..9ee374c 100644
--- a/test/typeInference.test.ts
+++ b/test/typeInference.test.ts
@@ -28,6 +28,16 @@ apR x f =
 eq : a -> a -> Bool
 eq =
   Elm.Kernel.Utils.equal
+
+--@ String.elm
+module String exposing (String)
+
+type String = String
+
+--@ Char.elm
+module Char exposing (Char)
+
+type Char = Char
 `;
 describe("test type inference", () => {
   const treeParser = new SourceTreeParser();