-
Notifications
You must be signed in to change notification settings - Fork 194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing open bracket should surround the selected text with brackets #2051
Typing open bracket should surround the selected text with brackets #2051
Conversation
bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/SurroundWithBracketsStrategy.java
Outdated
Show resolved
Hide resolved
bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java
Outdated
Show resolved
Hide resolved
command.shiftsCaret= false; | ||
|
||
// Run this in a UI thread to ensure the selection is updated correctly | ||
sourceViewer.getTextWidget().getDisplay().asyncExec(new Runnable() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Display#execute
to ensure it is run in the UI-Thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's wrong with the current approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says to "Run this in a UI thread to ensure the selection is updated correctly" but in fact it schedules an async execution in the UI thread, this might be okay / desired but the comment does not reflect that.
In general such "patterns" have several problems for example if you are already in an UI thread, this will possibly execute an arbitrary number of other UI actions that possibly alter your current state. Display#execute
on the other hand will execute your command in-place if you are already on an UI thread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@laeubi
If I do not run this in async execution, the selected text is unselected as soon as the text is surrounded by braces, making the selection unavailable for further operations of surrounding with braces.
Maybe I will update the comment to: 'Run this in a UI thread asynchronously to ensure the selection is updated correctly.
}); | ||
} | ||
} catch (BadLocationException e) { | ||
SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we really raise an SWT error here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@laeubi I referred DefaultDocumentAdapter
class raising SWT error in methods getTextRange
and replaceTextRange
for BadLocationException
I even see catch block is empty in many other cases .
What do you Suggest ?
Test Results 1 818 files + 483 1 818 suites +483 1h 53m 11s ⏱️ + 50m 49s For more details on these failures, see this check. Results for commit 284107e. ± Comparison against base commit 54479d7. ♻️ This comment has been updated with latest results. |
d89042b
to
6698b75
Compare
@mickaelistria @laeubi is there an issue with this approach or this issue fix is not required now? |
I have some concern that changing selection that way is likely to conflict with some other auto editor strategies that would run after this one for the same event and alter the command changing the size of the inserted text, or that would implement a similar strategy to also change the selection. But both are minor and I think the best way to figure it out is to merge. So IMO, please just rebase to get the MANIFEST.MF compatible with current master branch and we can merge. |
Content of this PR is plain wrong as it contains many other unrelated changes. |
Its my mistake. I am checking on it. |
c555d19
to
6698b75
Compare
Review points incorporated
6698b75
to
284107e
Compare
@mickaelistria It works in generic editor for all cases but works in Java editor only if text blocks surrounds with { and ` 2024-10-21_15h12_54.mp4 |
The Java editor has some extra configuration here and there to tweak the edition experience. |
Now is a good time to merge. Thank you! |
Thank you for this PR! |
Oops, sorry and thank you for addressing it! |
* @since 3.26 This strategy supports surrounding the selected text with similar opening and closing | ||
* brackets when the text is selected and an opening bracket is inserted. | ||
*/ | ||
public class SurroundWithBracketsStrategy implements IAutoEditStrategy { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work with block selections, too? E.g., when the user has multiple selections in the current document?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@szarnekow please refer video in this comment.
Are you also asking for the same thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's a different thing (which is likely unsupported yet). For example, in a text abababa
, try to search for a
and click "Select All", then you get a multi-selection. In such case, the expectation would be that if someone hits (
the resulting text becomes (a)b(a)b(a)b(a)
.
|
||
@Override | ||
public void customizeDocumentCommand(IDocument document, DocumentCommand command) { | ||
if (bracketsMap.containsKey(command.text)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see quite a bunch of null-checks when command.text is accessed in other context.
Wouldn't this throw an NPE since bracketsMap is null-hostile cause it's created via Map.of()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A null check will be added.
public void customizeDocumentCommand(IDocument document, DocumentCommand command) { | ||
if (bracketsMap.containsKey(command.text)) { | ||
try { | ||
ITextSelection selection= (ITextSelection) sourceViewer.getSelectionProvider().getSelection(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DocumentCommand has a package visible field fSelection
- is that the same or something else then the selection obtained here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works for both the ITextSelection
obtained from the sourceViewer
and the fSelection
from the DocumentCommand
doSetAutoEditStrategies(configuration.getAutoEditStrategies(this, t), t); | ||
IAutoEditStrategy[] autoEditStrategies= configuration.getAutoEditStrategies(this, t); | ||
List<IAutoEditStrategy> autoEditStrategiesList= new ArrayList<>(Arrays.asList(autoEditStrategies)); | ||
autoEditStrategiesList.add(new SurroundWithBracketsStrategy(this)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding the strategy here makes it incredibly hard to disable. Why did you opt for this solution instead of returning it from org.eclipse.jface.text.source.SourceViewerConfiguration.getAutoEditStrategies(ISourceViewer, String)
as a default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good suggestion.
@lathapatil can you please consider the suggested change in a next PR ?
@mickaelistria you suggest that JDT can tweak this, but the way it is configured doesn't allow subclasses to override the behavior. What did you have in mind when you gave green light to merge? |
please fix eclipse-jdt/eclipse.jdt.ui#1843 |
Please also consider comments from @szarnekow and do not fix JDT UI, but improve platform code - the change here shouldn't break clients! |
Just discovered that this change also broke source hover functionality in Java editor (Shift + Hover over method to see implementation) => #2600 |
I will check the root cause and fix it ASAP . Thanks for reviewing . |
Fixes eclipse-platform#2600 Review points implementation
Fixes eclipse-platform#2600 Review points implementation
Fixes #2600 Review points implementation
Surround the selected text with brackets in all editors when an opening bracket is inserted. Brackets included are (,<,[,{,",',`
Fixes #865
#1141
Linked to #1718