Skip to content
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

Conversation

lathapatil
Copy link
Contributor

@lathapatil lathapatil commented Jul 8, 2024

Surround the selected text with brackets in all editors when an opening bracket is inserted. Brackets included are (,<,[,{,",',`

Fixes #865
#1141

Linked to #1718

command.shiftsCaret= false;

// Run this in a UI thread to ensure the selection is updated correctly
sourceViewer.getTextWidget().getDisplay().asyncExec(new Runnable() {
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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?

Copy link
Contributor Author

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 ?

Copy link
Contributor

github-actions bot commented Jul 8, 2024

Test Results

 1 818 files  +  483   1 818 suites  +483   1h 53m 11s ⏱️ + 50m 49s
 7 712 tests +    1   7 483 ✅ ±    0  228 💤 ±  0  1 ❌ +1 
24 297 runs  +4 430  23 549 ✅ +4 197  747 💤 +232  1 ❌ +1 

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.

@lathapatil lathapatil force-pushed the Enhancements/865_SurroundTextByBrackets branch from d89042b to 6698b75 Compare July 11, 2024 08:31
@lathapatil
Copy link
Contributor Author

@mickaelistria @laeubi is there an issue with this approach or this issue fix is not required now?

@mickaelistria
Copy link
Contributor

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.
Another thing I noticed, which is not related and is deeper in the editor, is that this very convenient feature wouldn't work for multiple or block text selection. But this seems out of the scope for this change.

So IMO, please just rebase to get the MANIFEST.MF compatible with current master branch and we can merge.

@akurtakov
Copy link
Member

Content of this PR is plain wrong as it contains many other unrelated changes.

@lathapatil
Copy link
Contributor Author

Content of this PR is plain wrong as it contains many other unrelated changes.

Its my mistake. I am checking on it.

@lathapatil lathapatil force-pushed the Enhancements/865_SurroundTextByBrackets branch from c555d19 to 6698b75 Compare October 21, 2024 09:03
@lathapatil lathapatil force-pushed the Enhancements/865_SurroundTextByBrackets branch from 6698b75 to 284107e Compare October 21, 2024 09:07
@lathapatil
Copy link
Contributor Author

lathapatil commented Oct 21, 2024

this very convenient feature wouldn't work for multiple or block text selection

@mickaelistria It works in generic editor for all cases but works in Java editor only if text blocks surrounds with { and `
Is it expected or is further analysis required to fix this in java editor as well?

2024-10-21_15h12_54.mp4

@mickaelistria
Copy link
Contributor

The Java editor has some extra configuration here and there to tweak the edition experience.
I don't mind merging it if it works for the generic editor and most text editors. The JDT part will probably have to be investigated in JDT directly anyway, without need to fix anything here.
I'm sorry we missed to merge this change earlier, since we're in RC phase, it's most likely better to wait until next stream starts (so we have time to get feedback and improve it if necessary).

@mickaelistria mickaelistria added this to the 4.35 M1 milestone Nov 19, 2024
@MohananRahul MohananRahul removed this from the 4.35 M1 milestone Nov 22, 2024
@mickaelistria
Copy link
Contributor

Now is a good time to merge. Thank you!
Please add a note to the noteworthy documentation about this new feature.

@mickaelistria mickaelistria added this to the 4.35 M1 milestone Dec 6, 2024
@mickaelistria mickaelistria added the noteworthy Noteworthy feature label Dec 6, 2024
@mickaelistria mickaelistria merged commit 7adb4ef into eclipse-platform:master Dec 6, 2024
9 of 14 checks passed
@HannesWell
Copy link
Member

Thank you for this PR!
@mickaelistria the next time please make sure that such long standing PRs are rebased so that the version-bump bot can do it's duty. This is now missing, but I have created #2598, to apply it.

@mickaelistria
Copy link
Contributor

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

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?

Copy link
Contributor

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)) {
Copy link
Contributor

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()?

Copy link
Contributor Author

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();
Copy link
Contributor

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?

Copy link
Contributor Author

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));
Copy link
Contributor

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?

Copy link
Contributor

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 ?

@szarnekow
Copy link
Contributor

@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?

@jukzi
Copy link
Contributor

jukzi commented Dec 9, 2024

please fix eclipse-jdt/eclipse.jdt.ui#1843

@iloveeclipse
Copy link
Member

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!

@iloveeclipse
Copy link
Member

Just discovered that this change also broke source hover functionality in Java editor (Shift + Hover over method to see implementation) => #2600

@lathapatil
Copy link
Contributor Author

I will check the root cause and fix it ASAP . Thanks for reviewing .

lathapatil added a commit to lathapatil/eclipse.platform.ui that referenced this pull request Dec 10, 2024
lathapatil added a commit to lathapatil/eclipse.platform.ui that referenced this pull request Dec 12, 2024
lathapatil added a commit to lathapatil/eclipse.platform.ui that referenced this pull request Dec 12, 2024
merks pushed a commit that referenced this pull request Dec 13, 2024
Fixes #2600
Review points implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
noteworthy Noteworthy feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typing open bracket should surround the selected text element with brackets
10 participants