Skip to content

Commit

Permalink
Fix for #1278: convert to property: support NamedArgumentListExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jul 13, 2021
1 parent 5cfa5b6 commit 00851f9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,6 +79,34 @@ final class ConvertToPropertyActionTests extends GroovyEditorTestSuite {
assertEditorContents 'new Foo().URLEncoder = null'
}

@Test
void testSetterToProperty4() {
addGroovySource 'class Foo { void setMap(Map map) {} }', 'Foo'
convertToProperty "new Foo().set${CARET}Map(x: null)"
assertEditorContents 'new Foo().map = [x: null]'
}

@Test
void testSetterToProperty5() {
addGroovySource 'class Foo { void setMap(Map map) {} }', 'Foo'
convertToProperty "new Foo().set${CARET}Map('x' : null, y:1)"
assertEditorContents 'new Foo().map = [\'x\' : null, y:1]'
}

@Test
void testSetterToProperty6() {
addGroovySource 'class Foo { void setMap(Map map) {} }', 'Foo'
convertToProperty "new Foo().set${CARET}Map(['x' : null, y:1 ] )"
assertEditorContents 'new Foo().map = [\'x\' : null, y:1 ] '
}

@Test
void testSetterToProperty7() {
addGroovySource 'class Foo { void setMap(Map map) {} }', 'Foo'
convertToProperty "new Foo().set${CARET}Map x: null"
assertEditorContents 'new Foo().map = [x: null]'
}

@Test
void testChainedGetterProperty() {
convertToProperty "getClass().getResource('URL').g${CARET}etText()"
Expand Down Expand Up @@ -244,12 +272,4 @@ final class ConvertToPropertyActionTests extends GroovyEditorTestSuite {
convertToProperty "new Foo().set${CARET}Something()"
assertEditorContents 'new Foo().setSomething()'
}

@Test
void testNoConversion10() {
convertToProperty "new Date().set${CARET}Time(time: 1234L)"
assertEditorContents 'new Date().setTime(time: 1234L)'
}

// TODO: Convert "setX(p1: v1, p2: v2)" to "x = [p1: v1, p2: v2]"?
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2020 the original author or authors.
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,11 +25,12 @@
import java.util.regex.Matcher;

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.MethodCall;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.NamedArgumentListExpression;
import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.eclipse.GroovyPlugin;
import org.codehaus.groovy.eclipse.codebrowsing.fragments.ASTFragmentKind;
import org.codehaus.groovy.eclipse.codebrowsing.fragments.IASTFragment;
Expand All @@ -45,6 +46,7 @@
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
Expand Down Expand Up @@ -97,8 +99,8 @@ public static TextEdit createEdit(final GroovyCompilationUnit gcu, final int pos
call = (StaticMethodCallExpression) node;
}

if (call != null && call.getArguments() instanceof ArgumentListExpression) {
ArgumentListExpression args = (ArgumentListExpression) call.getArguments();
if (call != null && call.getArguments() instanceof TupleExpression) {
TupleExpression args = (TupleExpression) call.getArguments();

Matcher match; // check for accessor or mutator
if (args.getExpressions().isEmpty() && (match = compile("(?:get|is)(\\p{javaJavaIdentifierPart}+)").matcher(call.getMethodAsString())).matches()) {
Expand All @@ -124,8 +126,17 @@ public static TextEdit createEdit(final GroovyCompilationUnit gcu, final int pos
if (JavaCore.INSERT.equals(options.get(FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR))) {
replacement.append(' ');
}
if (args.getExpression(0) instanceof NamedArgumentListExpression) {
replacement.append('[');
}
edits.addChild(new ReplaceEdit(offset, length, replacement.toString()));
if (gcu.getContents()[args.getEnd()] == ')') edits.addChild(new DeleteEdit(args.getEnd(), 1));

boolean rparen = (args.getEnd() < gcu.getContents().length && gcu.getContents()[args.getEnd()] == ')');
if (args.getExpression(0) instanceof NamedArgumentListExpression) {
edits.addChild(rparen ? new ReplaceEdit(args.getEnd(), 1, "]") : new InsertEdit(args.getEnd(), "]"));
} else if (rparen) {
edits.addChild(new DeleteEdit(args.getEnd(), 1));
}

return edits;
}
Expand Down

0 comments on commit 00851f9

Please sign in to comment.