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

Autocomplete in wrong format (extraneous quotes and parentheses added) #142

Closed
alan-w-255 opened this issue Feb 15, 2020 · 9 comments
Closed
Labels
archived bug language server Issue is not originating from this extension, but from the LSP instead

Comments

@alan-w-255
Copy link

The prompt shows like below.

image

But when i strike the Tab key, the autocompletion is wrong.

image
I am using Godot 3.2, Windows 10, vscode 1.42.1

@mickdekkers
Copy link

It looks like the language server is causing this by returning faulty insertText in the response of the completionItem/resolve method:

[
    // client request
	{
		"jsonrpc": "2.0",
		"id": 61,
		"method": "completionItem/resolve",
		"params": {
			"label": "\"ui_down\"",
			"insertTextFormat": 1,
			"kind": 2,
			"data": {
				"position": { "character": 30, "line": 27 },
				"textDocument": {
					"uri": "file:///c%3A/Users/Mick/Documents/Godot/Dodge%20the%20Creeps%20-%20TEST/Player.gd"
				}
			}
		}
	},
    // server response
	{
		"id": 61,
		"jsonrpc": "2.0",
		"result": {
			"command": { "command": "", "title": "" },
			"data": {
				"context": null,
				"position": { "character": 30, "line": 27 },
				"textDocument": {
					"uri": "file:///c%3A/Users/Mick/Documents/Godot/Dodge%20the%20Creeps%20-%20TEST/Player.gd"
				}
			},
			"deprecated": false,
			"detail": "",
			"documentation": { "kind": "markdown", "value": "" },
			"filterText": "",
			"insertText": "\"ui_down\"(", // <--- this has extra double quotes and opening parenthesis
			"kind": 2,
			"label": "\"ui_down\"",
			"preselect": false,
			"sortText": ""
		}
	}
]

mickdekkers added a commit to mickdekkers/godot that referenced this issue Sep 27, 2020
So far this fixes the `(` being inserted in godotengine/godot-vscode-plugin#142
and also the extra quotes, but only if completion is
trigged by the quote key.

It does not yet fix godotengine/godot-vscode-plugin#184
because the third param to `connect()` actually is a method.
We need more context to be able to determine whether or not to add parentheses.
@mickdekkers
Copy link

I spent a few hours last night and this morning trying to fix this and #184. I got it working partially, but only under some circumstances. Unfortunately I am out of my depth, having never used C++ or looked at the Godot source before. I hope somebody more familiar with both finds this info useful.

To summarize, the issue originates in the language server module of Godot, not the vscode plugin, and the opening parenthesis and the extra quotes have two different causes (but potentially the same solution).

The parameter of is_action_pressed is a string, but the completion code determines it to be a function. Because of this, it enters this if branch where the insertText is set to item.label + "(". This explains the opening parenthesis. Using the debugger I traced the place where the kind is determined to this line in /modules/gdscript/gdscript_editor.cpp. Setting that to KIND_PLAIN_TEXT instead of KIND_FUNCTION fixes the parenthesis issue, but I have not tested what other effects this change might have.

The extra quotes are caused by the completion item label already containing quotes regardless of any quotes that might be in the source file around the cursor. (The label is used as insertText if that is not defined.)

The commit mickdekkers/godot@9c91664 is as far as I got with fixing these issues, but for fixing the quotes especially I'm not sure it's the right approach.


Ideally the completion code would look at the characters around the cursor position and use that context to determine whether or not to add parentheses or extra quotes. Unfortunately the source code is not available inside of the handler for completionItem/resolve, so adding this functionality there is not easy. The source code is available in the GDScriptWorkspace::completion function, where it is read from the file (or cache) and passed to GDScriptLanguage::get_singleton()->complete_code. The completion function is called in the handler for textDocument/completion and does most of the code completion heavy lifting.

I think in order to fix these issues "properly", they will need to be fixed in the complete_code function and/or its helper functions. As it stands, that function does receive the source code but only uses it to parse it to get some context. It doesn't look at the position of the cursor or what characters in the source code may be near it.

What complicates things is that the handler for completionItem/resolve changes the insertText, but the LSP spec says this (emphasis mine):

The request can only delay the computation of the detail and documentation properties. Other properties like sortText, filterText, insertText, textEdit and additionalTextEdits must be provided in the textDocument/completion response and must not be changed during resolve.

I don't know enough about the project to judge what moving this code to the textDocument/completion handler might entail (or whether that would be a good idea).

@ViliamVadocz
Copy link

I am being affected by this and would like to fix it. Who should I get in touch with if I need help? (I haven't worked on a language server before)

@luislodosm
Copy link

Same here:

Captura de pantalla 2021-04-12 a las 23 23 08

Godot 3.2.3, macOS 11.2, Visual Studio Code 1.55

Any updates?

@Yakumo-Yukari
Copy link

There is still this bug, so there is still no fix for this problem?

@Calinou Calinou added the language server Issue is not originating from this extension, but from the LSP instead label Apr 25, 2022
@Calinou
Copy link
Member

Calinou commented Apr 25, 2022

There is still this bug, so there is still no fix for this problem?

This is actually a bug in the language server, not in the extension: #142 (comment)

There's no definitive fix for this bug yet though.

@push-on
Copy link

push-on commented Apr 29, 2022

2022-04-29.11-08-46.mp4

same here

@Exerionius
Copy link

@Calinou is it reported in the main Godot repo?

@Calinou
Copy link
Member

Calinou commented Jun 2, 2022

@Calinou is it reported in the main Godot repo?

Yes: godotengine/godot#48436

Therefore, this issue can be closed as it's not specific to this VS Code extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
archived bug language server Issue is not originating from this extension, but from the LSP instead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants