Skip to content

Commit

Permalink
Bracket support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinesh0723 authored and jukzi committed Mar 8, 2024
1 parent 7572bd4 commit 6dcad12
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -220,7 +221,26 @@ public String getTextRange(int offset, int length) {
@Override
public void replaceTextRange(int pos, int length, String text) {
try {
fDocument.replace(pos, length, text);
String[][] bracketTypes= {
{ SpecialCharacterConstants.openParentheses, SpecialCharacterConstants.openSquareBracket, SpecialCharacterConstants.openCurlyBracket, SpecialCharacterConstants.openAngleBracket,
SpecialCharacterConstants.singleQuote, SpecialCharacterConstants.doubleQuotes },
{ SpecialCharacterConstants.closeParentheses, SpecialCharacterConstants.closeSquareBracket, SpecialCharacterConstants.closeCurlyBracket,
SpecialCharacterConstants.closeAngleBracket, SpecialCharacterConstants.singleQuote, SpecialCharacterConstants.doubleQuotes }
};
boolean containsSpecialBrackets= Arrays.asList(bracketTypes[0]).contains(text);
TextSelection textSelection= new TextSelection(fDocument, pos, length);
//Checks if the text contains special type of brackets, the length of the selected text is greater than 0 & the selected text is not empty.
if (containsSpecialBrackets && textSelection.getLength() > 0 && !textSelection.getText().trim().isEmpty()) {
int index= Arrays.asList(bracketTypes[0]).indexOf(text);
if (index >= 0) {
String currentOpeningBracket= bracketTypes[0][index];
String currentClosingBracket= bracketTypes[1][index];
text= currentOpeningBracket + textSelection.getText() + currentClosingBracket;
fDocument.replace(pos, length, text);
}
} else {
fDocument.replace(pos, length, text);
}
} catch (BadLocationException x) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2023 ETAS GmbH and others, all rights reserved.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ETAS GmbH - initial API and implementation
*******************************************************************************/

package org.eclipse.jface.text;

/**
* @since 3.25
*/
public class SpecialCharacterConstants {

public static final String openParentheses= "("; //$NON-NLS-1$

public static final String closeParentheses= ")"; //$NON-NLS-1$

public static final String openSquareBracket= "["; //$NON-NLS-1$

public static final String closeSquareBracket= "]"; //$NON-NLS-1$

public static final String openCurlyBracket= "{"; //$NON-NLS-1$

public static final String closeCurlyBracket= "}"; //$NON-NLS-1$

public static final String openAngleBracket= "<"; //$NON-NLS-1$

public static final String closeAngleBracket= ">"; //$NON-NLS-1$

public static final String singleQuote= "'"; //$NON-NLS-1$

public static final String doubleQuotes= "\""; //$NON-NLS-1$


}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ protected boolean condition() {
}.waitForCondition(textViewer.getControl().getDisplay(), 3000));
}

@Test
public void testSpecialCharacterSelection() {
fShell= new Shell();
final TextViewer textViewer= new TextViewer(fShell, SWT.NONE);
StyledText textWidget= textViewer.getTextWidget();
textViewer.setDocument(new Document());
StyledTextContent content= textViewer.getTextWidget().getContent();
assumeNotNull(content);
content.setText("Hello");
content.replaceTextRange(0, 5, "(");
assertEquals("Expected text after replacement", "(Hello)", textWidget.getText());
}

/**
* Test if {@link TextViewer}s default {@link IDocumentAdapter} implementation adhere to
* {@link IDocumentAdapter}s JavaDoc.
Expand Down

0 comments on commit 6dcad12

Please sign in to comment.