slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.93k stars 568 forks source link

VS Code: extra spaces & braces when completing auto-imported types #3714

Closed jpnurmi closed 11 months ago

jpnurmi commented 11 months ago

In VS Code, auto-completion behaves differently for built-in types and std-widgets. Both place the cursor between the curly braces but when auto-completing std-widgets, there are extra spaces around.

The problem is that after hitting enter, the extra whitespace is left around i.e. there's a trailing space after the opening brace, and the closing brace is indented off by one space.

built-in std-widgets
VerticalLayout·{
····|
}
VerticalBox·{·
····|
·}

When wrapping in elements and auto-completing the element type in snippet mode, built-in elements behave correctly but for std-widgets, it inserts extra braces.

With the following starting point:

Vert|·{
····Rectangle·{}
}
built-in std-widgets
VerticalLayout·{
····Rectangle·{}
}
VerticalBox·{··}·{
····Rectangle·{}
}
jpnurmi commented 11 months ago

https://github.com/slint-ui/slint/assets/140617/1a51262f-e771-45eb-89b6-5e0c3e99ed67

jpnurmi commented 11 months ago

I thought it was somehow related to the special handling of std-widgets.slint but actually, it happens with any type that gets auto-imported from another .slint file. If the import already exists, then it completes fine without extra spaces and braces.

https://github.com/slint-ui/slint/assets/140617/ca6a36fe-4145-472f-9d49-9c05c4dde01c

ogoffart commented 11 months ago

This is the difference between https://github.com/slint-ui/slint/blob/f546d22861af3199b801c77185b59b2a52906c3d/tools/lsp/language/completion.rs#L76 and https://github.com/slint-ui/slint/blob/f546d22861af3199b801c77185b59b2a52906c3d/tools/lsp/language/completion.rs#L667

It used to be consistant but the spaces were removed in one case: https://github.com/slint-ui/slint/commit/839b5e8ee66de3ace208ac1b3bde9156599b2c38

jpnurmi commented 11 months ago

Thanks, that helped with the indentation. It should also check if the token is already followed by a brace to avoid inserting extra braces when using the "Wrap in element" action:

VerticalBox·{}·{
····Rectangle·{}
}