Skip to content

Commit d89042b

Browse files
committed
Typing open bracket should surround the selected text with brackets
Surround the selected text with brackets in all editors when an opening bracket is inserted. Brackets included are (,<,[,{,",',` Fixes #865
1 parent 991408d commit d89042b

File tree

4 files changed

+97
-5
lines changed

4 files changed

+97
-5
lines changed

bundles/org.eclipse.jface.text/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.jface.text
5-
Bundle-Version: 3.25.200.qualifier
5+
Bundle-Version: 3.26.0.qualifier
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin
88
Export-Package:
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.HashMap;
17+
import java.util.Map;
18+
19+
import org.eclipse.swt.SWT;
20+
21+
import org.eclipse.jface.text.source.ISourceViewer;
22+
23+
/**
24+
* @since 3.26 This strategy supports surrounding the selected text with similar opening and closing
25+
* brackets when the text is selected and an opening bracket is inserted.
26+
*/
27+
public class SurroundWithBracketsStrategy implements IAutoEditStrategy {
28+
29+
private ISourceViewer sourceViewer;
30+
31+
@SuppressWarnings("nls")
32+
private final static Map<String, String> bracketsMap= new HashMap<>(Map.of("(", ")", "[", "]", "{", "}", "<", ">", "\"", "\"", "'", "'", "`", "`"));
33+
34+
public SurroundWithBracketsStrategy(ISourceViewer sourceViewer) {
35+
this.sourceViewer= sourceViewer;
36+
}
37+
38+
@Override
39+
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
40+
if (bracketsMap.containsKey(command.text)) {
41+
try {
42+
ITextSelection selection= (ITextSelection) sourceViewer.getSelectionProvider().getSelection();
43+
if (selection != null && selection.getLength() > 0) {
44+
String selectedText= document.get(selection.getOffset(), selection.getLength());
45+
String closingBracket= bracketsMap.get(command.text);
46+
command.text= command.text + selectedText + closingBracket;
47+
command.offset= selection.getOffset();
48+
command.length= selection.getLength();
49+
50+
// Set the caret offset after the opening bracket but before the closing bracket
51+
command.caretOffset= command.offset + command.text.length() - closingBracket.length();
52+
command.shiftsCaret= false;
53+
54+
// Run this in a UI thread to ensure the selection is updated correctly
55+
sourceViewer.getTextWidget().getDisplay().asyncExec(new Runnable() {
56+
@Override
57+
public void run() {
58+
sourceViewer.setSelectedRange(command.offset + 1, selectedText.length());
59+
}
60+
});
61+
}
62+
} catch (BadLocationException e) {
63+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
64+
}
65+
}
66+
}
67+
}

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

Lines changed: 9 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,6 +14,7 @@
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

@@ -57,6 +58,7 @@
5758
import org.eclipse.jface.text.ITextViewerLifecycle;
5859
import org.eclipse.jface.text.Position;
5960
import org.eclipse.jface.text.Region;
61+
import org.eclipse.jface.text.SurroundWithBracketsStrategy;
6062
import org.eclipse.jface.text.TextViewer;
6163
import org.eclipse.jface.text.codemining.ICodeMiningProvider;
6264
import org.eclipse.jface.text.contentassist.IContentAssistant;
@@ -543,7 +545,12 @@ public void configure(SourceViewerConfiguration configuration) {
543545
String[] types= configuration.getConfiguredContentTypes(this);
544546
for (String t : types) {
545547

546-
doSetAutoEditStrategies(configuration.getAutoEditStrategies(this, t), t);
548+
IAutoEditStrategy[] autoEditStrategies= configuration.getAutoEditStrategies(this, t);
549+
IAutoEditStrategy[] newStrategies= new IAutoEditStrategy[autoEditStrategies.length + 1];
550+
System.arraycopy(autoEditStrategies, 0, newStrategies, 0, autoEditStrategies.length);
551+
newStrategies[autoEditStrategies.length]= new SurroundWithBracketsStrategy(this);
552+
553+
doSetAutoEditStrategies(newStrategies, t);
547554
setTextDoubleClickStrategy(configuration.getDoubleClickStrategy(this, t), t);
548555

549556
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

@@ -66,6 +67,7 @@
6667
import org.eclipse.jface.text.hyperlink.URLHyperlink;
6768
import org.eclipse.jface.text.hyperlink.URLHyperlinkDetector;
6869
import org.eclipse.jface.text.source.SourceViewer;
70+
import org.eclipse.jface.text.source.SourceViewerConfiguration;
6971
import org.eclipse.jface.text.tests.util.DisplayHelper;
7072

7173
/**
@@ -464,4 +466,20 @@ public void testSelectionFromViewerState() {
464466
textSelection= (ITextSelection) textViewer.getSelection();
465467
assertEquals(113, textSelection.getOffset());
466468
}
467-
}
469+
470+
@Test
471+
public void testSurroundwithBracketsStrategy() {
472+
fShell= new Shell();
473+
final SourceViewer sourceViewer= new SourceViewer(fShell, null, SWT.NONE);
474+
sourceViewer.configure(new SourceViewerConfiguration());
475+
sourceViewer.setDocument(new Document("Test sample to surround the selected text with brackets"));
476+
StyledText text= sourceViewer.getTextWidget();
477+
text.setText("Test sample to surround the selected text with brackets");
478+
text.setSelection(15, 23);
479+
assertEquals(23, text.getCaretOffset());
480+
assertEquals("surround", text.getSelectionText());
481+
text.insert("[");
482+
assertEquals("Test sample to [surround] the selected text with brackets", text.getText());
483+
assertEquals(24, text.getCaretOffset());
484+
}
485+
}

0 commit comments

Comments
 (0)