This repository was archived by the owner on Jan 16, 2025. It is now read-only.
Rudimentary code completion support #27
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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:

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.