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

Autocompletion is not offered for a parameter with a @class type #1439

Open
carsakiller opened this issue Aug 3, 2022 · 23 comments
Open

Autocompletion is not offered for a parameter with a @class type #1439

carsakiller opened this issue Aug 3, 2022 · 23 comments
Labels
enhancement New feature or request

Comments

@carsakiller
Copy link
Collaborator

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Completion

Expected Behaviour

When using a defined class as the type for a parameter, the fields of that table should be offered as an autocompletion when calling the function.

For an example, using @alias works like so:
image

Actual Behaviour

No autocompletion is automatically offered when using @class as the type for a parameter. If you press CTRL + Space, you can make the completions appear, but they will not appear automatically.

Nothing is offered:
image
Until I press CTRL + Space:
image

Reproduction steps

  1. Use the below code:
    ---@class Options
    ---@field page number
    ---@field active boolean
    
    ---@param opts Options
    local function acceptOptions(opts) end
    
    acceptOptions()
  2. Move your cursor to line 9 (acceptOptions()) and place it between the parentheses
    image
  3. Enter { to start a table literal
  4. No completions are offered
  5. Press CTRL + Space and notice that the first 2 options are actually proper completions from the Options class. These should automatically be offered.

Additional Notes

No response

Log File

No response

@sumneko sumneko added the bug Something isn't working label Aug 5, 2022
@sumneko
Copy link
Collaborator

sumneko commented Aug 12, 2022

1439
I cannot reproduce this.

@sumneko sumneko added the Info Needed More information is required label Aug 12, 2022
@carsakiller
Copy link
Collaborator Author

Interesting. I don't think I have ever had fields for a class completed for me like you show in your GIF.
fieldCompletions

Here are all of the settings I have modified:

{
    "Lua.completion.callSnippet": "Replace",
    "Lua.completion.displayContext": 5,
    "Lua.diagnostics.groupFileStatus": {
        "strict": "Any"
    },
    "Lua.diagnostics.libraryFiles": "Enable",
    "Lua.hint.enable": true,
    "Lua.telemetry.enable": true,
    "Lua.window.statusBar": false,
    "Lua.workspace.userThirdParty": [
        "aPath"
    ]
}

Even using the same simple code as above I am unable to get it working... I have no idea what would cause it, I just assumed it wasn't implemented.

@FlashHit
Copy link

Interesting. I don't think I have ever had fields for a class completed for me like you show in your GIF. fieldCompletions fieldCompletions

Here are all of the settings I have modified:

{
    "Lua.completion.callSnippet": "Replace",
    "Lua.completion.displayContext": 5,
    "Lua.diagnostics.groupFileStatus": {
        "strict": "Any"
    },
    "Lua.diagnostics.libraryFiles": "Enable",
    "Lua.hint.enable": true,
    "Lua.telemetry.enable": true,
    "Lua.window.statusBar": false,
    "Lua.workspace.userThirdParty": [
        "aPath"
    ]
}

Even using the same simple code as above I am unable to get it working... I have no idea what would cause it, I just assumed it wasn't implemented.

What do you mean? Look again you get the 2 fields suggested.

@carsakiller
Copy link
Collaborator Author

What do you mean? Look again you get the 2 fields suggested.

That is only after manually requesting completions by pressing Ctrl + Space, I am not receiving them automatically, as you can see in Sumneko's GIF.

@sumneko
Copy link
Collaborator

sumneko commented Aug 15, 2022

Did you check editor.suggestOnTriggerCharacters ?

@carsakiller
Copy link
Collaborator Author

image
I combed through a bunch of settings that have to do with completion and tried changing them, setting them to their defaults... no luck.

@carsakiller
Copy link
Collaborator Author

I removed all of my settings and disabled all of my extensions in VS Code so that it was running in its default state + this extension, I am still not automatically receiving completions using the sample code... 🤔

@sumneko
Copy link
Collaborator

sumneko commented Aug 17, 2022

You can only debug by yourself, start from script/provider/provider.lua:553

@carsakiller
Copy link
Collaborator Author

carsakiller commented Sep 7, 2022

Sorry for the delay, I have experienced a few issues while debugging.

  1. The ports that are currently in the wiki do not match. The wiki says dbgport=11413 while launch.json says 11428. Which one should the wiki say?

  2. After following the wiki and changing the port to 11428 in the target's settings.json, I am unable to set a breakpoint because "the source file is not loaded"?
    image

@sumneko
Copy link
Collaborator

sumneko commented Sep 7, 2022

  1. As long as the port number is the same.
  2. The breakpoint can only be confirmed after the file is loaded for the first time, which should not affect the breakpoint work.

@serg3295
Copy link
Contributor

serg3295 commented Sep 7, 2022

I'm facing the same problem. No fields are offered when entering {
I checked it both linux and windows.
I have to enter a space to get suggestions for fields.
In version 2.6.3 when ctrl+space was pressed, only the fields specified in the class were offered, however starting from version 2.6.4 , global variables and a lot of other unnecessary things are also offered.

@serg3295
Copy link
Contributor

serg3295 commented Sep 9, 2022

I traced in debugger what happens when calling auto-completion by symbol { in example:

---@class A
---@field p1 integer
---@field p2 integer

---comment
---@param p A
local function f(p) end
f{}

I have set two breakpoints in
provider.lua: 577 local triggerCharacter = params.context and params.context.
and
completion.lua: 2138 local text = state.lua

If { is entered after the f then the breakpoint is triggered in line provider.lua: 577 then there is a return to the function m.doMethod() in proto.lua: 185 await.delay()

If Space is pressed then breakpoints are triggered in provider.lua: 577 and then in completion.lua: 2138

I'm not sure if there should be a transition to completion.lua if { is pressed, but behavior of the program is different when a space is entered.

"Lua.diagnostics.enable" is set to false to reduce the size of the log file.

[10:52:52.654][debug][#0:script/provider/provider.lua:973]: textDocument/semanticTokens/range
[10:52:58.292][debug][#0:script/vm/node.lua:455]: clearNodeCache
[10:52:58.294][info] [#0:script/provider/provider.lua:570]: {
    context = {
        triggerCharacter = "{",
        triggerKind = 2,
    },
    position = {
        character = 2,
        line = 9,
    },
    textDocument = {
        uri = "file:///home/serg/projects/lua-language-server/tempCodeRunnerFile.lua",
    },
}
[10:52:58.294][debug][#0:script/provider/provider.lua:572]: textDocument/completion
[10:52:58.294][debug][#0:script/provider/provider.lua:573]: completion:	2	{
[10:52:58.396][debug][#0:script/provider/provider.lua:973]: textDocument/semanticTokens/range
[10:52:59.521][debug][#0:script/vm/node.lua:455]: clearNodeCache
[10:52:59.527][info] [#0:script/provider/provider.lua:570]: {
    context = {
        triggerCharacter = " ",
        triggerKind = 2,
    },
    position = {
        character = 3,
        line = 9,
    },
    textDocument = {
        uri = "file:///home/serg/projects/lua-language-server/tempCodeRunnerFile.lua",
    },
}
[10:52:59.528][debug][#0:script/provider/provider.lua:572]: textDocument/completion
[10:52:59.528][debug][#0:script/provider/provider.lua:573]: completion:	2	 
[10:52:59.633][debug][#0:script/provider/provider.lua:973]: textDocument/semanticTokens/range

@serg3295
Copy link
Contributor

serg3295 commented Sep 9, 2022

I commented out the line provider.lua:582 return, and completion was offered immediately after pressing {, \n and ,.
However, I think this is a bad decision. There was a discussion on this topic earlier.
If you perform autocompletion by \n then you will not be able to enter a literal table for example in this way:

f{ p1 = 1,
   p2 = 2 }

IMO, the literal table looks much prettier with spaces next to curly braces. 😄

Therefore, I suggest leaving the behavior of the program as it is now.

@serg3295
Copy link
Contributor

serg3295 commented Sep 9, 2022

That's just it would be nice to remove unnecessary suggestions when ctrl+space is pressed
e46
leaving only
e47

@sumneko
Copy link
Collaborator

sumneko commented Sep 15, 2022

Right, it is caused by editor.acceptSuggestionOnEnter .

@carsakiller
Copy link
Collaborator Author

Thank you ❤️

I am able to get completions by pressing space after starting a table literal. Is there a way to get suggestions when not pressing space though? I agree tables look better with a leading space, it is just that I let my code formatter handle that.

Also, apologies I could not debug this - I just kept having problems with getting it to work. I am still unable to set a breakpoint due to "the source file is not loaded"... not sure what it is talking about since I have gotten the debugging to work before.

@sumneko
Copy link
Collaborator

sumneko commented Sep 16, 2022

Is there a way to get suggestions when not pressing space though?

You need to set editor.acceptSuggestionOnEnter to off .

@carsakiller
Copy link
Collaborator Author

Yup, that does the trick. That isn't very intuitive though... I am trying to think of how/where this can be documented...

@carsakiller
Copy link
Collaborator Author

Copying over some of my thoughts from elsewhere:

I don't really like the solution of turning off editor.acceptSuggestionOnEnter. I have gotten used to pressing enter to accept suggestions, and I don't want to disable that feature for all of VS Code just so I can get suggestions when writing a table in Lua.

I would like to see it fixed in a way that doesn't require an entry in the wiki to use. It is also a weird one to document:

So if you want completions in a table, but don't want to enter a space, you'll need to disable a global VS Code setting.

All other completions are intuitive and easy to get and use, why do I have to modify my global settings to receive completions in a table?

@carsakiller carsakiller reopened this Sep 29, 2022
@sumneko
Copy link
Collaborator

sumneko commented Sep 29, 2022

It's hard to describe, you can try to remove script/provider.lua:582 and then typing some codes.

@serg3295
Copy link
Contributor

At moment, you can accept suggestions by pressing enter, but not get suggestion.

@serg3295
Copy link
Contributor

Frankly, neovim offers parameters after pressing enter, but it's not difficult for me to press ctrl+space to get suggestions in VS code.
Peek 2022-09-29 18-54

@sumneko sumneko added enhancement New feature or request and removed bug Something isn't working Info Needed More information is required labels Mar 8, 2023
@sumneko
Copy link
Collaborator

sumneko commented Mar 8, 2023

Add a config Lua.completion.triggerByEnter

  • enable
  • disable
  • auto: enbale when editor.acceptSuggestionOnEnter is off

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants