Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Rudimentary code completion support #27

Merged
merged 10 commits into from
Apr 21, 2023

Conversation

nolanderc
Copy link
Collaborator

@nolanderc nolanderc commented Apr 20, 2023

Uses glslang to get a string containing the GLSL definitions of all builtin functions and variables for the given shader stage. This is then feed through a very naive "parser" to extract the types and names of all global symbols.

I did not feel like writing a parser for GLSL, and the one embedded in glslang is buried deep within implementation details. Instead we use some simple heuristics to find all declarations in a file. For example, input variables and constants follow the same basic pattern:

[[<layout>] <qualifier>] <type> <name> [= <value>];

Thus, if we see either ; or =, whichever comes first, the previous two "words" must have been the name of the symbol and its type.

Using a similar approach we can identify functions: if we ever see a pair of parentheses outside a function body, we assume it is either a layout qualifier or parameter list.

Granted, this solution is not that fault-tolerant, but it works reasonably well. In the future we might want to use an actual parser, but that is more work than I wish to spend on this at the moment.

It should be noted that the current approach does not handle local variables or anything defined within a function body, which would depend on the editing context. Theoretically, this could be implemented by truncating the file to the position of the cursor, and only considering symbols up to that point, but then you have to deal with all the various syntax forms allowed within statements and expressions, which I did not want to deal with (again, a proper parser would solve this).

Also, most editors have some way of automatically completing recently used words in the surrounding context, which already gets us 70% of the way (only missing type hints).

This includes the commit from #26, which I needed for testing, so consider merging that PR first.

The commit 4f84431 additionally fixes #19.

Edit: Here's a screenshot with code completion working:
image
To get this result, I typed mul and then selected the first option. Notice that the type signature of the function is shown. The same is true of all builtin functions and constants.

The LSP spec allows implementations to ignore notifications if they
want. This way glslls doesn't start emitting errors when new
notifications are added in the future.
This fixes svenstaro#19.

As an added benifit, neovim sends a "shutdown" message when closing,
waiting about 1 second until the language server responds before
terminating to give the server a chance to respond. Previously, this
caused a slight hiccup whenever neovim was closed.

By clearly stating that the "shutdown" message is not implemented by
using the "id" field, neovim now closes immediately.
This was referenced Apr 20, 2023
@svenstaro
Copy link
Owner

Probably should update the README accordingly.

@nolanderc nolanderc merged commit 61dd198 into svenstaro:master Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error replies dont include the method identifier
2 participants