Skip to content

Surround with if / for / while #2923

Closed
Closed
@kgoedert

Description

@kgoedert

It would be interesting to have a surround with option like the try/catch option, for if / while / for

Activity

rgrunber

rgrunber commented on Jun 18, 2024

@rgrunber
Member

Since we have snippets for try/catch, if / while / for, the surround with behaviour exists simply by selecting the text and immediately typing out the desired snippet. At first it looks like you've just overwritten the content, but once a snippet is selected, it is placed within it :

Screencast.from.2024-06-18.10-19-02.webm

Would this be enough ? I guess it could be made smarter if the snippet could switch between if expression / body depending on whether the selected element returns a boolean. I think this can be done though because we do modify certain snippets dynamically based on cursor!

self-assigned this
on Jun 19, 2024
moved this to 📋 Backlog in IDE Cloudaptorson Jun 19, 2024
hopehadfield

hopehadfield commented on Jun 26, 2024

@hopehadfield
Contributor

Unfortunately the snippet can't be made smarter in the way that @rgrunber suggested. Right now, a placeholder (TM_SELECTED_TEXT) is used by the language server to represent the selected text that is overwritten by the user's snippet prompt. This placeholder is then resolved by the client when the snippet is sent back. The language server does not have access to the selected text in order to evaluate whether or not it's a boolean, nor does the VS Code API support accessing this selected text from the client to send directly to the language server in the completion request.

Alternatively, I looked into adding a choice for the condition to be the selected text, but variables such as TM_SELECTED_TEXT are not considered a valid option for VS Code snippet choices.

As stated above, the surround with functionality for if / while / for is supported with snippets for body text. Postfix snippets can also be used to surround a boolean with if / while, or an array / collection type with for, specifically in the condition parentheses (see below).

postfix-if

Closing this, as I think these snippets serve the purpose outlined in the issue.

moved this from 📋 Backlog to ✅ Done in IDE Cloudaptorson Jun 26, 2024
rgrunber

rgrunber commented on Jun 26, 2024

@rgrunber
Member

... nor does the VS Code API support accessing this selected text from the client to send directly to the language server in the completion request.

I'm not entirely sure if there's no API/hack to access it. It's just becoming a very complicated feature to get working correctly, when longer term, it would be better if upstream just did it correctly for us in the form of a quick-assist. Then again, quick-assists don't seem like a nice way to do this, and I do like the snippet approach more, through completion.

In fact, if you look at the "Surround With" menu in Eclipse :

image

The "Configure Templates" is a giveaway on how they do it, but basically, Eclipse has its own snippet (template) language that defines :

if (${condition:var(boolean)}) {
	${line_selection}${cursor}
}

So our approach is not that different. The interesting part is that ${condition:var(boolean)} correctly inserts a field from a parent class! 😮 That might be a bug, or something we never correctly brought over into JDT-LS.

Screencast.from.2024-06-26.15-47-57.webm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @kgoedert@rgrunber@testforstephen@hopehadfield

      Issue actions

        Surround with if / for / while · Issue #2923 · redhat-developer/vscode-java