Permalink
Browse files

GRECLIPSE-1502 DSLD support for noParens

  • Loading branch information...
1 parent 2c611cd commit 066f4cd4701e4bd7a7ce286f34958752a19c9495 @aeisenberg aeisenberg committed Sep 25, 2012
@@ -32,6 +32,7 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.tests.util.GroovyUtils;
+import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
/**
@@ -41,6 +42,19 @@
*/
public class DSLContentAssistTests extends CompletionTestCase {
+ private static final String COMMAND_CHAIN_NO_ARGS =
+ "contribute (currentType('Inner')) {\n" +
+ " method name:'flart', noParens:true, type: 'Inner'\n" +
+ "}";
+ private static final String COMMAND_CHAIN_ONE_ARG =
+ "contribute (currentType('Inner')) {\n" +
+ " method name:'flart', noParens:true, type: 'Inner', params:[a:Integer]\n" +
+ "}";
+
+ private static final String COMMAND_CHAIN_TWO_ARGS =
+ "contribute (currentType('Inner')) {\n" +
+ " method name:'flart', noParens:true, type: 'Inner', params:[a:Integer, b:String]\n" +
+ "}";
private static final String SET_DELEGATE_ON_INT = "contribute(currentType(Integer) & enclosingCallName(\"foo\")) {\n" +
" setDelegateType(String)\n" +
"}";
@@ -143,6 +157,76 @@ public void testEmptyClosure2() throws Exception {
proposalExists(proposals, "toHexString()", 0);
}
+ public void testCommandChain1() throws Exception {
+ createDsls(COMMAND_CHAIN_NO_ARGS);
+ String contents =
+ "class Inner { }\n" +
+ "def val = new Inner()\n" +
+ "val.fla";
+
+ Document doc = new Document(contents);
+ ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, ".fla"));
+ proposalExists(proposals, "flart", 1);
+ ICompletionProposal proposal = findFirstProposal(proposals, "flart", false);
+ applyProposalAndCheck(doc, proposal, contents.replace("val.fla", "val.flart"));
+ }
+
+ public void testCommandChain2() throws Exception {
+ createDsls(COMMAND_CHAIN_NO_ARGS);
+ String contents =
+ "class Inner { }\n" +
+ "def val = new Inner()\n" +
+ "val.flart foo fl";
+
+ Document doc = new Document(contents);
+ ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, " fl"));
+ proposalExists(proposals, "flart", 1);
+ ICompletionProposal proposal = findFirstProposal(proposals, "flart", false);
+ applyProposalAndCheck(doc, proposal, contents.replace(" fl", " flart"));
+ }
+
+ public void testCommandChain3() throws Exception {
+ createDsls(COMMAND_CHAIN_NO_ARGS);
+ String contents =
+ "class Inner { }\n" +
+ "def val = new Inner()\n" +
+ "val.flart foo, baz fl";
+
+ Document doc = new Document(contents);
+ ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, " fl"));
+ proposalExists(proposals, "flart", 1);
+ ICompletionProposal proposal = findFirstProposal(proposals, "flart", false);
+ applyProposalAndCheck(doc, proposal, contents.replace(" fl", " flart"));
+ }
+
+ public void testCommandChain4() throws Exception {
+ createDsls(COMMAND_CHAIN_ONE_ARG);
+ String contents =
+ "class Inner { }\n" +
+ "def val = new Inner()\n" +
+ "val.flart foo, baz fl";
+
+ Document doc = new Document(contents);
+ ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, " fl"));
+ proposalExists(proposals, "flart", 1);
+ ICompletionProposal proposal = findFirstProposal(proposals, "flart", false);
+ applyProposalAndCheck(doc, proposal, contents.replace(" fl", " flart 0 "));
+ }
+
+ public void testCommandChain5() throws Exception {
+ createDsls(COMMAND_CHAIN_TWO_ARGS);
+ String contents =
+ "class Inner { }\n" +
+ "def val = new Inner()\n" +
+ "val.flart foo, baz fl";
+
+ Document doc = new Document(contents);
+ ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, " fl"));
+ proposalExists(proposals, "flart", 1);
+ ICompletionProposal proposal = findFirstProposal(proposals, "flart", false);
+ applyProposalAndCheck(doc, proposal, contents.replace(" fl", " flart 0, \"\" "));
+ }
+
protected void addJarToProject(String jarName) throws JavaModelException, IOException {
String externalFilePath = findExternalFilePath(jarName);
env.addExternalJar(getDefaultProject().getFullPath(), externalFilePath);
@@ -236,8 +236,27 @@ private StyledString getStyledGroovy() {
@Override
protected String computeReplacementString() {
- if (!hasParameters() || !hasArgumentList())
- return super.computeReplacementString();
+ if (!hasParameters() || !hasArgumentList()) {
+ if (proposalOptions.noParens) {
+ // command chain expression with no known arguments
+ char[] proposalName = fProposal.getName();
+ boolean hasWhitespace = false;
+ for (int i = 0; i < proposalName.length; i++) {
+ if (CharOperation.isWhitespace(proposalName[i])) {
+ hasWhitespace = true;
+ }
+ }
+ String newProposalName;
+ if (hasWhitespace) {
+ newProposalName = "\"" + String.valueOf(proposalName) + "\"";
+ } else {
+ newProposalName = String.valueOf(proposalName);
+ }
+ return newProposalName;
+ } else {
+ return super.computeReplacementString();
+ }
+ }
long millis = DEBUG ? System.currentTimeMillis() : 0;
String replacement;
@@ -284,6 +303,12 @@ private String computeGuessingCompletion() throws JavaModelException {
fProposal.setName(proposalName);
FormatterPrefs prefs = getFormatterPrefs();
+ if (proposalOptions.noParens) {
+ // eat the opening paren replace with a space if there isn't one
+ // already
+ buffer.replace(buffer.length() - 1, buffer.length(), prefs.beforeOpeningParen ? "" : " ");
+ }
+
setCursorPosition(buffer.length());
@@ -361,10 +386,12 @@ private String computeGuessingCompletion() throws JavaModelException {
// check what to add after argument
if (i == allCount - 1 || (i == allCount - 2 && i == indexOfLastClosure - 1)) {
- if (prefs.beforeClosingParen) {
+ if (prefs.beforeClosingParen || proposalOptions.noParens) {
buffer.append(SPACE);
}
- buffer.append(RPAREN);
+ if (!proposalOptions.noParens) {
+ buffer.append(RPAREN);
+ }
} else if (i < allCount - 1) {
if (prefs.beforeComma)
buffer.append(SPACE);
@@ -65,11 +65,14 @@
private IType cachedDeclaringType;
+ private boolean noParens;
+
public GroovyMethodProposal(MethodNode method) {
super();
this.method = method;
contributor = "Groovy";
useNamedArguments = false;
+ noParens = false;
}
public GroovyMethodProposal(MethodNode method, String contributor) {
this(method);
@@ -85,6 +88,10 @@ public void setUseNamedArguments(boolean useNamedArguments) {
this.useNamedArguments = useNamedArguments;
}
+ public void setNoParens(boolean noParens) {
+ this.noParens = noParens;
+ }
+
@Override
public AnnotatedNode getAssociatedNode() {
return method;
@@ -174,7 +181,7 @@ private ProposalFormattingOptions getGroovyProposalOptions() {
if (options == null) {
options = ProposalFormattingOptions.newFromOptions();
}
- return options.newFromExisting(useNamedArguments, method);
+ return options.newFromExisting(useNamedArguments, noParens, method);
}
/**
@@ -86,7 +86,7 @@ private ProposalFormattingOptions getGroovyProposalOptions() {
if (options == null) {
options = ProposalFormattingOptions.newFromOptions();
}
- return options.newFromExisting(true, null);
+ return options.newFromExisting(true, false, null);
}
protected StyledString createDisplayString() {
@@ -28,7 +28,7 @@ public static ProposalFormattingOptions newFromOptions() {
return new ProposalFormattingOptions(prefs.getBoolean(PreferenceConstants.GROOVY_CONTENT_ASSIST_NOPARENS),
prefs.getBoolean(PreferenceConstants.GROOVY_CONTENT_ASSIST_BRACKETS),
prefs.getBoolean(PreferenceConstants.GROOVY_CONTENT_NAMED_ARGUMENTS),
- prefs.getBoolean(PreferenceConstants.GROOVY_CONTENT_PARAMETER_GUESSING));
+ prefs.getBoolean(PreferenceConstants.GROOVY_CONTENT_PARAMETER_GUESSING), false);
}
public final boolean noParensAroundClosures;
@@ -39,22 +39,29 @@ public static ProposalFormattingOptions newFromOptions() {
public final boolean doParameterGuessing;
+ // used for DSL command expressions
+ public final boolean noParens;
+
public ProposalFormattingOptions(boolean noParensAroundArgs,
boolean useBracketsForClosures, boolean useNamedArguments,
- boolean doParameterGuessing) {
+ boolean doParameterGuessing, boolean noParens) {
this.noParensAroundClosures = noParensAroundArgs;
this.useBracketsForClosures = useBracketsForClosures;
this.useNamedArguments = useNamedArguments;
this.doParameterGuessing = doParameterGuessing;
+ this.noParens = noParens;
}
- public ProposalFormattingOptions newFromExisting(boolean overrideUseNamedArgs, MethodNode method) {
- // if overridden, always use named args
+ public ProposalFormattingOptions newFromExisting(boolean overrideUseNamedArgs, boolean overrideNoParens, MethodNode method) {
+ // For named args if overridden, always use named args
// if not a constructor and not overridden, never use named args
- if (overrideUseNamedArgs) {
- return new ProposalFormattingOptions(noParensAroundClosures, useBracketsForClosures, true, doParameterGuessing);
+ if (overrideUseNamedArgs || overrideNoParens) {
+ return new ProposalFormattingOptions(noParensAroundClosures, useBracketsForClosures, overrideUseNamedArgs,
+ doParameterGuessing,
+ overrideNoParens);
} else if (useNamedArguments && !(method instanceof ConstructorNode)) {
- return new ProposalFormattingOptions(noParensAroundClosures, useBracketsForClosures, false, doParameterGuessing);
+ return new ProposalFormattingOptions(noParensAroundClosures, useBracketsForClosures, false, doParameterGuessing,
+ overrideNoParens);
} else {
return this;
}
@@ -188,6 +188,7 @@ void method(Map<String, Object> args) {
String doc = value == null ? null : asString(value); // might be null
boolean useNamedArgs = asBoolean(args.get("useNamedArgs"));
+ boolean noParens = asBoolean(args.get("noParens"));
ParameterContribution[] params = extractParams(args, "params");
ParameterContribution[] namedParams = extractParams(args, "namedParams");
@@ -197,7 +198,7 @@ void method(Map<String, Object> args) {
boolean isDeprecated = isDeprecated(args);
if (!staticScope || (staticScope && isStatic)) {
contributions.add(new MethodContributionElement(name == null ? NO_NAME : name, params, namedParams, optionalParams, returnType == null ? NO_TYPE
- : returnType, declaringType, isStatic, provider == null ? this.provider : provider, doc, useNamedArgs, isDeprecated, DEFAULT_RELEVANCE_MULTIPLIER));
+ : returnType, declaringType, isStatic, provider == null ? this.provider : provider, doc, useNamedArgs, noParens, isDeprecated, DEFAULT_RELEVANCE_MULTIPLIER));
}
}
@@ -72,12 +72,15 @@
private ProposalFormattingOptions options = ProposalFormattingOptions.newFromOptions();
private final int relevanceMultiplier;
private final boolean isDeprecated;
+ private final boolean noParens;
public MethodContributionElement(String methodName, ParameterContribution[] params, String returnType, String declaringType, boolean isStatic, String provider, String doc, boolean useNamedArgs, boolean isDeprecated, int relevanceMultiplier) {
- this(methodName, params, NO_PARAMETER_CONTRIBUTION, NO_PARAMETER_CONTRIBUTION, returnType, declaringType, isStatic, provider, doc, useNamedArgs, isDeprecated, relevanceMultiplier);
+ this(methodName, params, NO_PARAMETER_CONTRIBUTION, NO_PARAMETER_CONTRIBUTION, returnType, declaringType, isStatic, provider, doc, useNamedArgs, false, isDeprecated, relevanceMultiplier);
}
- public MethodContributionElement(String methodName, ParameterContribution[] params, ParameterContribution[] namedParams, ParameterContribution[] optionalParams, String returnType, String declaringType, boolean isStatic, String provider, String doc, boolean useNamedArgs, boolean isDeprecated, int relevanceMultiplier) {
+ public MethodContributionElement(String methodName, ParameterContribution[] params, ParameterContribution[] namedParams,
+ ParameterContribution[] optionalParams, String returnType, String declaringType, boolean isStatic, String provider,
+ String doc, boolean useNamedArgs, boolean noParens, boolean isDeprecated, int relevanceMultiplier) {
this.methodName = methodName;
this.params = params;
this.namedParams = namedParams;
@@ -86,6 +89,7 @@ public MethodContributionElement(String methodName, ParameterContribution[] para
this.isStatic = isStatic;
this.declaringType = declaringType;
this.useNamedArgs = useNamedArgs;
+ this.noParens = noParens;
this.isDeprecated = isDeprecated;
this.relevanceMultiplier = relevanceMultiplier;
@@ -105,6 +109,7 @@ public TypeAndDeclaration lookupType(String name, ClassNode declaringType, Resol
public IGroovyProposal toProposal(ClassNode declaringType, ResolverCache resolver) {
GroovyMethodProposal groovyMethodProposal = new GroovyMethodProposal(toMethod(declaringType.redirect(), resolver), provider, options);
groovyMethodProposal.setUseNamedArguments(useNamedArgs);
+ groovyMethodProposal.setNoParens(noParens);
groovyMethodProposal.setRelevanceMultiplier(relevanceMultiplier);
return groovyMethodProposal;
}

0 comments on commit 066f4cd

Please sign in to comment.