Skip to content

Add simple code snippet support#116998

Open
Goldenlion5648 wants to merge 1 commit intogodotengine:masterfrom
Goldenlion5648:code-snippets-pr
Open

Add simple code snippet support#116998
Goldenlion5648 wants to merge 1 commit intogodotengine:masterfrom
Goldenlion5648:code-snippets-pr

Conversation

@Goldenlion5648
Copy link
Contributor

@Goldenlion5648 Goldenlion5648 commented Mar 3, 2026

Allows users to add code snippets in the editor settings.

Supports 1 spot that the cursor should jump to after insertion, by using "\1" (no quotes) in the user snippet.

Defaults to these snippets:

await get_tree().process_frame
await get_tree().create_timer(\1).timeout

Addresses this proposal (by me) godotengine/godot-proposals#14368

screenrecording-2026-03-02_20-52-10.mp4

The editor settings page (I believe this would be the first setting that uses an array value, unless I missed one)

image

@Goldenlion5648 Goldenlion5648 requested review from a team as code owners March 3, 2026 01:55
@Goldenlion5648 Goldenlion5648 changed the title add simple code snippet support Add simple code snippet support Mar 3, 2026
@Goldenlion5648 Goldenlion5648 requested a review from a team as a code owner March 3, 2026 03:16
@Nintorch Nintorch added this to the 4.x milestone Mar 3, 2026
@AdriaandeJongh
Copy link
Contributor

  • why not use the existing string format indicators such as %s and %d and the like?
  • single line input will not cut it for multiline snippets.
  • i do recall there being other editor styles for arrays where you as and remove fields one by one? a lot of space is wasted with the property name here.

@HolonProduction
Copy link
Member

  • If we do snippets we should use a subset of the LSP snippet syntax IMHO.

  • All editors I know of have named snippets, so that you don't need to type the start of the snippet. Granted that might be hard with the current completion stack though 🤔

  • Implementation wise I'm not happy with this approach. It means, that all options will be treated like snippets. The current GDScript completion options certainly are not programmed with that in mind, so e.g. an animation name might be misinterpreted as snippet if it contains your magic characters.

  • There needs to be an escaping mechanism in place, so that you can configure snippets which produce "\1" in the final code.

@HolonProduction HolonProduction requested a review from a team March 3, 2026 08:38
@Goldenlion5648
Copy link
Contributor Author

@AdriaandeJongh

why not use the existing string format indicators such as %s and %d and the like?

The user is about to type the string, it is not meant to be auto filled with a certain type like string or digit. Also \1 is less commonly used in gdscript, only for regex. (Though I guess I'll add a way to escape)

single line input will not cut it for multiline snippets.

I would have to check if there is a way to add multiline support inside arrays currently.

i do recall there being other editor styles for arrays where you as and remove fields one by one?

Not sure what you're saying here, the trash icon is already there (it was automatically added)

a lot of space is wasted with the property name here.

Is that easily fixable? Isn't this the case for all settings?

@Goldenlion5648
Copy link
Contributor Author

@HolonProduction

If we do snippets we should use a subset of the LSP snippet syntax IMHO.

Is that over complicating for a feature that could be relatively simple?

All editors I know of have named snippets, so that you don't need to type the start of the snippet. Granted that might be hard with the current completion stack though 🤔

I didn't look too closely, but I think there is "display_text" or similar property that allows choosing how the completion should be shown.

Implementation wise I'm not happy with this approach. It means, that all options will be treated like snippets. The current GDScript completion options certainly are not programmed with that in mind, so e.g. an animation name might be misinterpreted as snippet if it contains your magic characters.

what is the preferred way to handle this? Say wrap all snippets entries with $ before and after (or some other magic character) that gets stripped when inserting the snippet, and only if it had the magic character then it should try to move the caret to the \1?

There needs to be an escaping mechanism in place, so that you can configure snippets which produce "\1" in the final code.

I can look into adding this

@HolonProduction
Copy link
Member

If we do snippets we should use a subset of the LSP snippet syntax IMHO.

Is that over complicating for a feature that could be relatively simple?

Emphasis on subset. So in practice this would mean using $0 instead of \1.

All editors I know of have named snippets, so that you don't need to type the start of the snippet. Granted that might be hard with the current completion stack though 🤔

I didn't look too closely, but I think there is "display_text" or similar property that allows choosing how the completion should be shown.

The issue is, that there is no way (that I know of) to have an insertion consistently replace existing text. And if you typed the name you need to replace the name with the actually snippet consistently.
The remark was more meant to clearify that names are not trivial to implement, since I think it's important context when looking at this from a usability point of view.

what is the preferred way to handle this? Say wrap all snippets entries with $ before and after (or some other magic character) that gets stripped when inserting the snippet, and only if it had the magic character then it should try to move the caret to the \1?

This can't be encoded in the string. The insertion text is fully dependent on user input.
You need an additional flag on the options, to indicate the kind of content that is contained in insertText.

single line input will not cut it for multiline snippets.

I would have to check if there is a way to add multiline support inside arrays currently.

You also need to check whether CodeEdit supports multi line inserts. Last time I checked it did not, but maybe that was fixed by now idk.

@Goldenlion5648 Goldenlion5648 requested a review from a team as a code owner March 5, 2026 01:28
@Goldenlion5648
Copy link
Contributor Author

I just noticed some bugs (like when the snippet has $0, it literally inserts that, and multiple \ do not put the cursor at the right spot). Not sure how deep in the "allow escaping" rabbit hole we wanted, but I'll fix it if desired, or simplify the escaping if we don't want to go as deep.

I tried to addressed some feedback:

If we do snippets we should use a subset of the LSP snippet syntax IMHO.

Is that over complicating for a feature that could be relatively simple?

Emphasis on subset. So in practice this would mean using $0 instead of \1.

Now using '$0'

All editors I know of have named snippets, so that you don't need to type the start of the snippet. Granted that might be hard with the current completion stack though 🤔

I didn't look too closely, but I think there is "display_text" or similar property that allows choosing how the completion should be shown.

The issue is, that there is no way (that I know of) to have an insertion consistently replace existing text. And if you typed the name you need to replace the name with the actually snippet consistently. The remark was more meant to clearify that names are not trivial to implement, since I think it's important context when looking at this from a usability point of view.

I opted not to do this. While it would be nice to have, it is a lot more work. I think having snippets at all would be a really nice qol change. Maybe this can be added on in the future?

what is the preferred way to handle this? Say wrap all snippets entries with $ before and after (or some other magic character) that gets stripped when inserting the snippet, and only if it had the magic character then it should try to move the caret to the \1?

This can't be encoded in the string. The insertion text is fully dependent on user input. You need an additional flag on the options, to indicate the kind of content that is contained in insertText.

Done

single line input will not cut it for multiline snippets.

I would have to check if there is a way to add multiline support inside arrays currently.

You also need to check whether CodeEdit supports multi line inserts. Last time I checked it did not, but maybe that was fixed by now idk.

Seems like it does. Here is the current state of features in this PR:

screenrecording-2026-03-04_20-14-03.mp4

@Ivorforce Ivorforce requested a review from a team March 6, 2026 08:51
@HolonProduction
Copy link
Member

You also need to check whether CodeEdit supports multi line inserts. Last time I checked it did not, but maybe that was fixed by now idk.

Seems like it does. Here is the current state of features in this PR:

I mean the video looks like it does not correctly maintain indentation 🤔

@Goldenlion5648
Copy link
Contributor Author

I wasn't sure what you meant by "support" (compared to crashing or similar), but yes you are correct that it doesn't indent as it should.

Would you say adding multiline support is blocking this PR? Or is that something that could be added in the future?

@Goldenlion5648
Copy link
Contributor Author

^ @HolonProduction while this PR does still need to fix the "escape $" and removing the correct number of "", in terms of multiline support, I think that might be out of scope since it appears that even typing multiline strings in the settings GUI might not be doable currently (the multiline snippet was added via the engine c++ code)

Do we care about making \\$ actually inserting \$, or should we just not care about \ after the first one? So in that example, \\$ would insert \\$

@HolonProduction
Copy link
Member

Do we care about making \\$ actually inserting \$

Yes. The point is that every possible string still needs to be re-presentable. (That's mostly relevant for working with those patterns in code.)

@Goldenlion5648
Copy link
Contributor Author

@HolonProduction I addressed escaping and squashed commits

here's how each of these converts now (if $0 is not shown in the output, the cursor was moved there)

zero$0end ->      zeroend
one\$0end ->      one$0end
double\\$0end ->  double\end
triple\\\$0end -> triple\$0end
quad\\\\$0end ->  quad\\end

new video of the feature in action:

screenrecording-2026-03-15_16-43-48.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement saving and loading code snippets in the script editor

6 participants