forked from eclipse/n4js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug in the computation of method names for CA
- Loading branch information
Sebastian Zarnekow
authored and
Sebastian Zarnekow
committed
Jul 11, 2017
1 parent
be7a786
commit 8485c5b
Showing
2 changed files
with
166 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...clipse.n4js.ui/src/org/eclipse/n4js/ui/contentassist/PatchedRequiredRuleNameComputer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* Copyright (c) 2017 NumberFour AG. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* NumberFour AG - Initial API and implementation | ||
*/ | ||
package org.eclipse.n4js.ui.contentassist; | ||
|
||
import static org.eclipse.xtext.xtext.ParameterConfigHelper.getAssignedArguments; | ||
import static org.eclipse.xtext.xtext.ParameterConfigHelper.getParameterConfig; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.xtext.AbstractElement; | ||
import org.eclipse.xtext.GrammarUtil; | ||
import org.eclipse.xtext.Group; | ||
import org.eclipse.xtext.Parameter; | ||
import org.eclipse.xtext.ParserRule; | ||
import org.eclipse.xtext.RuleCall; | ||
import org.eclipse.xtext.ide.editor.contentassist.antlr.RequiredRuleNameComputer; | ||
|
||
import com.google.inject.Singleton; | ||
|
||
// TODO remove with Xtext 2.13 | ||
/** | ||
* Fixes two issues with the computation of the methods that are supposed to be called on the parser. | ||
* | ||
* <ol> | ||
* <li>The parser attempts to call methods for groups that contain only one element. These grammar constructs are | ||
* normalized in the parser generator. See {@link #getEnclosingSingleElementGroup(AbstractElement)}.</li> | ||
* <li>The super impl has a bug in {@link #getRequiredRuleNames(Param)} which would return the original method name | ||
* instead of the adjusted name.</li> | ||
* </ol> | ||
* | ||
*/ | ||
@Singleton | ||
public class PatchedRequiredRuleNameComputer extends RequiredRuleNameComputer { | ||
|
||
private static final String[][] EMPTY_ARRAY = new String[0][]; | ||
|
||
@Override | ||
protected boolean isFiltered(Param param) { | ||
AbstractElement elementToParse = param.elementToParse; | ||
while (elementToParse != null) { | ||
if (isFiltered(elementToParse, param)) { | ||
return true; | ||
} | ||
elementToParse = getEnclosingSingleElementGroup(elementToParse); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Return the containing group if it contains exactly one element. | ||
*/ | ||
protected AbstractElement getEnclosingSingleElementGroup(AbstractElement elementToParse) { | ||
EObject container = elementToParse.eContainer(); | ||
if (container instanceof Group) { | ||
if (((Group) container).getElements().size() == 1) { | ||
return (AbstractElement) container; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@SuppressWarnings("restriction") | ||
@Override | ||
public String[][] getRequiredRuleNames(Param param) { | ||
if (isFiltered(param)) { | ||
return EMPTY_ARRAY; | ||
} | ||
AbstractElement elementToParse = param.elementToParse; | ||
String ruleName = param.ruleName; | ||
if (ruleName == null) { | ||
if (elementToParse instanceof RuleCall) { | ||
RuleCall call = (RuleCall) elementToParse; | ||
if (call.getRule() instanceof ParserRule) { | ||
String antlrRuleName = getRuleNames().getAntlrRuleName(call.getRule()); | ||
if (!call.getArguments().isEmpty()) { | ||
Set<Parameter> context = param.getAssignedParametes(); | ||
Set<Parameter> arguments = getAssignedArguments(call, context); | ||
int config = getParameterConfig(arguments); | ||
antlrRuleName = getRuleNames().getAntlrRuleName(call.getRule(), config); | ||
} | ||
return new String[][] { { antlrRuleName } }; | ||
} | ||
} | ||
return EMPTY_ARRAY; | ||
} | ||
String adjustedRuleName = adjustRuleName(ruleName, param); | ||
if (!(GrammarUtil.isOptionalCardinality(elementToParse) | ||
|| GrammarUtil.isOneOrMoreCardinality(elementToParse))) { | ||
return new String[][] { { adjustedRuleName } }; | ||
} | ||
if ((elementToParse.eContainer() instanceof Group)) { | ||
List<AbstractElement> tokens = getFilteredElements(((Group) elementToParse.eContainer()).getElements(), | ||
param); | ||
int idx = tokens.indexOf(elementToParse) + 1; | ||
if (idx != tokens.size()) { | ||
String secondRule = param.getBaseRuleName((AbstractElement) elementToParse.eContainer()); | ||
secondRule = secondRule.substring(0, secondRule.lastIndexOf('_') + 1) + idx; | ||
String adjustedSecondRule = adjustRuleName(secondRule, param); | ||
if (GrammarUtil.isMultipleCardinality(elementToParse)) | ||
return new String[][] { { adjustedRuleName }, { adjustedRuleName, adjustedSecondRule } }; | ||
return new String[][] { { adjustedRuleName, adjustedSecondRule } }; | ||
} | ||
} | ||
// Fix is here. Original method returns the original ruleName instead of the adjusted rule name. | ||
return new String[][] { { adjustedRuleName } }; | ||
} | ||
} |