You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!
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).
Closing this, as I think these snippets serve the purpose outlined in the issue.
... 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 :
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.
Activity
rgrunber commentedon Jun 18, 2024
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!
hopehadfield commentedon Jun 26, 2024
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 withfor
, specifically in the condition parentheses (see below).Closing this, as I think these snippets serve the purpose outlined in the issue.
rgrunber commentedon Jun 26, 2024
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 :
The "Configure Templates" is a giveaway on how they do it, but basically, Eclipse has its own snippet (template) language that defines :
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