Skip to content

Commit

Permalink
Fix for #1093: don't smart insert '{' after comma or colon
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 5, 2020
1 parent 04b3545 commit 02f56b5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,23 @@ final class SmartInsertTests extends GroovyEditorTestSuite {

send('{')

assertEditorContents("new C(1, 2, ) {${CARET}") // TODO
assertEditorContents("new C(1, 2, {${CARET})")
}

@Test
void testInsertCurlyBrace3() {
addGroovySource '''\
|class C {
| def setX(x) {
| }
|}
|'''.stripMargin()

makeEditor("new C(x: ${CARET})")

send('{')

assertEditorContents("new C(x: {${CARET})")
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.jdt.ui.text.IJavaPartitions;
import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ContentAssistant;
Expand All @@ -58,7 +59,7 @@ public class GroovyConfiguration extends JavaSourceViewerConfiguration {
public GroovyConfiguration(final GroovyColorManager colorManager, final IPreferenceStore preferenceStore, final ITextEditor editor) {
super(colorManager, preferenceStore, editor, IJavaPartitions.JAVA_PARTITIONING);

// replace Java's string scanner to enable Groovy's color choice
// replace Java's string scanner to enable Groovy's color choices
AbstractJavaScanner stringScanner = new SingleTokenJavaScanner(colorManager,
PreferenceConstants.getPreferenceStore(), PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_STRINGS_COLOR);
ReflectionUtils.setPrivateField(JavaSourceViewerConfiguration.class, "fStringScanner", this, stringScanner);
Expand Down Expand Up @@ -109,6 +110,21 @@ public IAutoEditStrategy[] getAutoEditStrategies(final ISourceViewer sourceViewe
};
strategies = (IAutoEditStrategy[]) ArrayUtils.add(strategies, (IAutoEditStrategy) (document, command) -> {
if ("{".equals(command.text)) {
//int offset = SmartSemicolonAutoEditStrategy.firstNonWhitespaceBackward(document, command.offset-1, partitioning, -1);
Integer prev = ReflectionUtils.executePrivateMethod(SmartSemicolonAutoEditStrategy.class, "firstNonWhitespaceBackward",
new Class[] {IDocument.class, int.class, String.class, int.class}, SmartSemicolonAutoEditStrategy.class,
new Object[] {document, command.offset - 1, partitioning, -1});
try {
if (prev != null && prev != -1) {
switch (document.getChar(prev)) {
case ',':
case ':':
return;
}
}
} catch (BadLocationException ignore) {
}

IAutoEditStrategy delegate = new SmartSemicolonAutoEditStrategy(partitioning);
delegate.customizeDocumentCommand(document, command);
}
Expand Down

0 comments on commit 02f56b5

Please sign in to comment.