Skip to content

Commit 7adb4ef

Browse files
lathapatilmickaelistria
authored andcommitted
Typing open bracket should surround the selected text with brackets
Review points incorporated
1 parent f082218 commit 7adb4ef

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*******************************************************************************
2+
* Copyright (c) ETAS GmbH 2024, all rights reserved.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* ETAS GmbH - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jface.text;
15+
16+
import java.util.Map;
17+
18+
import org.eclipse.swt.SWT;
19+
20+
import org.eclipse.jface.text.source.ISourceViewer;
21+
22+
/**
23+
* @since 3.26 This strategy supports surrounding the selected text with similar opening and closing
24+
* brackets when the text is selected and an opening bracket is inserted.
25+
*/
26+
public class SurroundWithBracketsStrategy implements IAutoEditStrategy {
27+
28+
private ISourceViewer sourceViewer;
29+
30+
@SuppressWarnings("nls")
31+
private final Map<String, String> bracketsMap= Map.of("(", ")", "[", "]", "{", "}", "<", ">", "\"", "\"", "'", "'", "`", "`");
32+
33+
public SurroundWithBracketsStrategy(ISourceViewer sourceViewer) {
34+
this.sourceViewer= sourceViewer;
35+
}
36+
37+
@Override
38+
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
39+
if (bracketsMap.containsKey(command.text)) {
40+
try {
41+
ITextSelection selection= (ITextSelection) sourceViewer.getSelectionProvider().getSelection();
42+
if (selection != null && selection.getLength() > 0) {
43+
String selectedText= document.get(selection.getOffset(), selection.getLength());
44+
String closingBracket= bracketsMap.get(command.text);
45+
command.text= command.text + selectedText + closingBracket;
46+
command.offset= selection.getOffset();
47+
command.length= selection.getLength();
48+
49+
// Set the caret offset after the opening bracket but before the closing bracket
50+
command.caretOffset= command.offset + command.text.length() - closingBracket.length();
51+
command.shiftsCaret= false;
52+
53+
// Run this in a UI thread asynchronously to ensure the selection is updated correctly
54+
sourceViewer.getTextWidget().getDisplay().asyncExec(new Runnable() {
55+
@Override
56+
public void run() {
57+
sourceViewer.setSelectedRange(command.offset + 1, selectedText.length());
58+
}
59+
});
60+
}
61+
} catch (BadLocationException e) {
62+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
63+
}
64+
}
65+
}
66+
}

bundles/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 IBM Corporation and others.
2+
* Copyright (c) 2000, 2024 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,10 +14,12 @@
1414
* Tom Hofmann (Perspectix AG) - bug 297572
1515
* Sergey Prigogin (Google) - bug 441448
1616
* Angelo Zerr <[email protected]> - [CodeMining] Add CodeMining support in SourceViewer - Bug 527515
17+
* Latha Patil (ETAS GmbH) - Issue 865 - Surround the selected text with brackets on inserting a opening bracket
1718
*******************************************************************************/
1819
package org.eclipse.jface.text.source;
1920

2021
import java.util.ArrayList;
22+
import java.util.Arrays;
2123
import java.util.Iterator;
2224
import java.util.List;
2325
import java.util.Stack;
@@ -57,6 +59,7 @@
5759
import org.eclipse.jface.text.ITextViewerLifecycle;
5860
import org.eclipse.jface.text.Position;
5961
import org.eclipse.jface.text.Region;
62+
import org.eclipse.jface.text.SurroundWithBracketsStrategy;
6063
import org.eclipse.jface.text.TextViewer;
6164
import org.eclipse.jface.text.codemining.ICodeMiningProvider;
6265
import org.eclipse.jface.text.contentassist.IContentAssistant;
@@ -543,7 +546,12 @@ public void configure(SourceViewerConfiguration configuration) {
543546
String[] types= configuration.getConfiguredContentTypes(this);
544547
for (String t : types) {
545548

546-
doSetAutoEditStrategies(configuration.getAutoEditStrategies(this, t), t);
549+
IAutoEditStrategy[] autoEditStrategies= configuration.getAutoEditStrategies(this, t);
550+
List<IAutoEditStrategy> autoEditStrategiesList= new ArrayList<>(Arrays.asList(autoEditStrategies));
551+
autoEditStrategiesList.add(new SurroundWithBracketsStrategy(this));
552+
IAutoEditStrategy[] newStrategies= autoEditStrategiesList.toArray(new IAutoEditStrategy[0]);
553+
554+
doSetAutoEditStrategies(newStrategies, t);
547555
setTextDoubleClickStrategy(configuration.getDoubleClickStrategy(this, t), t);
548556

549557
int[] stateMasks= configuration.getConfiguredTextHoverStateMasks(this, t);

tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2014, 2019 Google, Inc and others.
2+
* Copyright (c) 2014, 2024 Google, Inc and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -12,6 +12,7 @@
1212
* Sergey Prigogin (Google) - initial API and implementation
1313
* Mickael Istria (Red Hat Inc.) - [Bug 544708] Ctrl+Home
1414
* Paul Pazderski - [Bug 545530] Test for TextViewer's default IDocumentAdapter implementation.
15+
* Latha Patil (ETAS GmbH) - Issue 865 - Test for Surround the selected text with brackets
1516
*******************************************************************************/
1617
package org.eclipse.jface.text.tests;
1718

@@ -69,6 +70,7 @@
6970
import org.eclipse.jface.text.hyperlink.URLHyperlink;
7071
import org.eclipse.jface.text.hyperlink.URLHyperlinkDetector;
7172
import org.eclipse.jface.text.source.SourceViewer;
73+
import org.eclipse.jface.text.source.SourceViewerConfiguration;
7274
import org.eclipse.jface.text.tests.util.DisplayHelper;
7375

7476
/**
@@ -454,4 +456,20 @@ public void testSelectionFromViewerState() {
454456
textSelection= (ITextSelection) textViewer.getSelection();
455457
assertEquals(113, textSelection.getOffset());
456458
}
457-
}
459+
460+
@Test
461+
public void testSurroundwithBracketsStrategy() {
462+
fShell= new Shell();
463+
final SourceViewer sourceViewer= new SourceViewer(fShell, null, SWT.NONE);
464+
sourceViewer.configure(new SourceViewerConfiguration());
465+
sourceViewer.setDocument(new Document("Test sample to surround the selected text with brackets"));
466+
StyledText text= sourceViewer.getTextWidget();
467+
text.setText("Test sample to surround the selected text with brackets");
468+
text.setSelection(15, 23);
469+
assertEquals(23, text.getCaretOffset());
470+
assertEquals("surround", text.getSelectionText());
471+
text.insert("[");
472+
assertEquals("Test sample to [surround] the selected text with brackets", text.getText());
473+
assertEquals(24, text.getCaretOffset());
474+
}
475+
}

0 commit comments

Comments
 (0)