Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label completions #3961

Open
GearsDatapacks opened this issue Dec 5, 2024 · 9 comments
Open

Label completions #3961

GearsDatapacks opened this issue Dec 5, 2024 · 9 comments
Labels
help wanted Contributions encouraged priority:medium

Comments

@GearsDatapacks
Copy link
Member

GearsDatapacks commented Dec 5, 2024

It would be nice, when typing function calls or record constructors, to have the LSP autocomplete the names of labels.

In this example, the LSP would suggest the wibble label.

image

@lpil lpil added help wanted Contributions encouraged priority:medium labels Dec 5, 2024
@lpil
Copy link
Member

lpil commented Dec 5, 2024

For sure!!

@vit0rr
Copy link
Contributor

vit0rr commented Dec 6, 2024

For me, it worked well. Am I missing something?
CleanShot 2024-12-06 at 14 27 07@2x

@GearsDatapacks
Copy link
Member Author

Is that a copilot thing perhaps? Afaik this feature is not implemented in the language server

@lpil
Copy link
Member

lpil commented Dec 7, 2024

Yeah that's not coming from the Gleam LS.

@vit0rr
Copy link
Contributor

vit0rr commented Dec 7, 2024

Is that a copilot thing perhaps? Afaik this feature is not implemented in the language server

No, it looks to be related to the vscode. I tried it with a vanilla vscode without copilot and got the same behaviour. But yeah, you're right. It should come from the "lsp-server" - the project's name.

Looking at the code, the fix is probably related to the compiler-core/src/languague-server/completer.rs. Am I right? I'm trying to debug it. Any tips you can give me are welcome.

edit: It looks like your print is using vscode, too. I really do not know how my vscode knows about this type. I'm without copilot, and all that I installed was the Gleam extension

@vit0rr
Copy link
Contributor

vit0rr commented Dec 7, 2024

That's weird.
I've created a test for this:

#[test]
fn constructor_field_completions() {
    let code = r#"
pub type Wibble {
  Wibble(wibble: Int, wobble: Float)
}
pub fn main() {
  let wibble = Wibble()
}
"#;
    assert_completion!(TestProject::for_source(code), Position::new(5, 22));
}
Snapshot 1
---
source: compiler-core/src/language_server/tests/completion.rs
assertion_line: 1870
expression: "\npub type Wibble {\n  Wibble(wibble: Int, wobble: Float)\n}\npub fn main() {\n  let wibble = Wibble()\n}\n"
---
pub type Wibble {
  Wibble(wibble: Int, wobble: Float)
}
pub fn main() {
  let wibble = Wibble(|)
}


----- Completion content -----
Error
  kind:   Constructor
  detail: gleam
  sort:   4_Error
False
  kind:   EnumMember
  detail: gleam
  sort:   4_False
Nil
  kind:   EnumMember
  detail: gleam
  sort:   4_Nil
Ok
  kind:   Constructor
  detail: gleam
  sort:   4_Ok
True
  kind:   EnumMember
  detail: gleam
  sort:   4_True
Wibble
  kind:   Constructor
  detail: fn(Int, Float) -> Wibble
  sort:   2_Wibble
  desc:   app
  edits:
    [5:22-5:22]: "Wibble"
main
  kind:   Function
  detail: fn() -> Wibble
  sort:   2_main
  desc:   app
  edits:
    [5:22-5:22]: "main"
wibble:
  kind:   Field
  detail: Int
  sort:   0_wibble:
wobble:
  kind:   Field
  detail: Float
  sort:   0_wobble:

And note how the wibble and wobble are there. When I compile the code and run it locally, this is the behaviour:

Print 1

CleanShot 2024-12-07 at 16 48 54@2x

Now, just for test, I've changed the priority order:

fn sort_text(kind: CompletionKind, label: &str) -> String {
    let priority: u8 = match kind {
        // CompletionKind::Label => 0,
        // CompletionKind::FieldAccessor => 1,
        // CompletionKind::LocallyDefined => 2,
        // CompletionKind::ImportedModule => 3,
        // CompletionKind::Prelude => 4,
        // CompletionKind::ImportableModule => 5,
        CompletionKind::Label => 5,
        CompletionKind::FieldAccessor => 0,
        CompletionKind::LocallyDefined => 2,
        CompletionKind::ImportedModule => 3,
        CompletionKind::Prelude => 4,
        CompletionKind::ImportableModule => 6,
    };
    format!("{priority}_{label}")
}
Snapshot 2
---
source: compiler-core/src/language_server/tests/completion.rs
assertion_line: 1870
expression: "\npub type Wibble {\n  Wibble(wibble: Int, wobble: Float)\n}\npub fn main() {\n  let wibble = Wibble()\n}\n"
---
pub type Wibble {
  Wibble(wibble: Int, wobble: Float)
}
pub fn main() {
  let wibble = Wibble(|)
}


----- Completion content -----
Error
  kind:   Constructor
  detail: gleam
  sort:   4_Error
False
  kind:   EnumMember
  detail: gleam
  sort:   4_False
Nil
  kind:   EnumMember
  detail: gleam
  sort:   4_Nil
Ok
  kind:   Constructor
  detail: gleam
  sort:   4_Ok
True
  kind:   EnumMember
  detail: gleam
  sort:   4_True
Wibble
  kind:   Constructor
  detail: fn(Int, Float) -> Wibble
  sort:   2_Wibble
  desc:   app
  edits:
    [5:22-5:22]: "Wibble"
main
  kind:   Function
  detail: fn() -> Wibble
  sort:   2_main
  desc:   app
  edits:
    [5:22-5:22]: "main"
wibble:
  kind:   Field
  detail: Int
  sort:   5_wibble:
wobble:
  kind:   Field
  detail: Float
  sort:   5_wobble:

Note how the order now is different. It is also reflected in my vscode:

Print 2

CleanShot 2024-12-07 at 16 52 17@2x

I might be wrong here because I do not have experience with LSP and large compilers. But I didn't understand why the completions options test showed my labels

@lpil
Copy link
Member

lpil commented Dec 7, 2024

VSCode makes suggestions based on strings in the same file. I expect this is what you are seeing here. What happens if you move the definition to a different file?

@Frank-III
Copy link
Contributor

in my test, it works when in different files, I typed Wibble(|) and manually trigger completion by option + esc (in mac) to get:
image

The completer seems to identify it as an expression call and did the completion job, but when I start typing it won't complete anymore since it is recognized as an invalid expression.

@lpil
Copy link
Member

lpil commented Dec 8, 2024

but when I start typing it won't complete anymore since it is recognized as an invalid expression.

Label completion implemented in the Gleam LS needs to work in this case also. Fault tolerance is vital for completions to be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions encouraged priority:medium
Projects
None yet
Development

No branches or pull requests

4 participants