use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.
the class XtextGrammarRefactoringIntegrationTest method testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier.
@Test
public void testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier() throws Exception {
ResourceSet rs = resourceSetProvider.get();
EcoreFactory eInstance = EcoreFactory.eINSTANCE;
Resource ecoreModelResource = createEcoreModel(rs, ecoreURI, initialModelRoot);
EClass greetingClass = getGreetingClass(ecoreModelResource);
EReference greetingRefLocal = getReferenceoGreeting(ecoreModelResource, greetingClass);
String greetingClassFragment = EcoreUtil.getURI(greetingClass).fragment();
String greetingRefFragment = EcoreUtil.getURI(greetingRefLocal).fragment();
EPackage refPackage = eInstance.createEPackage();
refPackage.setName("myDsl2");
refPackage.setNsPrefix("myDsl2");
refPackage.setNsURI("http://testrefactoring2");
EClass modelRefClass = eInstance.createEClass();
refPackage.getEClassifiers().add(modelRefClass);
modelRefClass.setName("ModelRef");
EReference reference = eInstance.createEReference();
reference.setName("ref");
reference.setLowerBound(0);
reference.setUpperBound(-1);
reference.setEType(greetingClass);
modelRefClass.getEStructuralFeatures().add(reference);
Resource refToGreetingResource = createEcoreModel(rs, URI.createPlatformResourceURI(TEST_PROJECT + "/src/org/xtext/example/mydsl/" + "MyDsl2.ecore", true), refPackage);
refToGreetingResource.unload();
ecoreModelResource.unload();
waitForBuild();
waitForDisplay();
XtextEditor editor = openEditor(grammarFile);
doRefactoring(editor);
waitForBuild();
checkConsistenceOfGrammar(editor);
ecoreModelResource.load(null);
String renamedGreetingClassFragment = greetingClassFragment.replaceFirst(CLASSIFIERNAME, REFACTOREDCLASSIFIERNAME);
EObject renamedGreetingClass = ecoreModelResource.getEObject(renamedGreetingClassFragment);
assertNotNull(renamedGreetingClass);
assertEquals(REFACTOREDCLASSIFIERNAME, SimpleAttributeResolver.NAME_RESOLVER.apply(renamedGreetingClass));
EReference greetingReference = (EReference) ecoreModelResource.getEObject(greetingRefFragment);
EClassifier eType = greetingReference.getEType();
assertFalse(eType.eIsProxy());
assertEquals(REFACTOREDCLASSIFIERNAME, eType.getName());
refToGreetingResource.load(null);
EReference externalReferenceToGreeting = getReferenceoGreeting(refToGreetingResource, eType);
assertFalse(externalReferenceToGreeting.getEType().eIsProxy());
assertEquals(REFACTOREDCLASSIFIERNAME, externalReferenceToGreeting.getEType().getName());
}
use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.
the class XtextGrammarRefactoringIntegrationTest method setUp.
@SuppressWarnings("static-access")
@Override
public void setUp() throws Exception {
super.setUp();
EcoreRefactoringParticipant.setDisableWarning(true);
project = createProject(TEST_PROJECT);
IJavaProject javaProject = makeJavaProject(project);
addNature(javaProject.getProject(), XtextProjectHelper.NATURE_ID);
Injector injector = Activator.getInstance().getInjector(getEditorId());
injector.injectMembers(this);
grammar = "grammar org.xtext.example.mydsl.MyDsl\n hidden(WS) generate myDsl\n 'http://testrefactoring'\n import 'http://www.eclipse.org/emf/2002/Ecore'\n as ecore \nModel: greetings+=" + CLASSIFIERNAME + "*; \n" + CLASSIFIERNAME + ": 'Hello' name=ID '!';\n terminal ID : '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;\n terminal WS: (' '|'\t'|'\r'|'\n')+;";
grammarFile = IResourcesSetupUtil.createFile(GRAMMAR_FILE, grammar);
grammarUri = URI.createPlatformResourceURI(GRAMMAR_FILE, true);
EcoreFactory eInstance = EcoreFactory.eINSTANCE;
initialModelRoot = eInstance.createEPackage();
initialModelRoot.setName("myDsl");
initialModelRoot.setNsPrefix("myDsl");
initialModelRoot.setNsURI("http://testrefactoring");
EClass modelClass = eInstance.createEClass();
modelClass.setName("Model");
EClass greetingClass = eInstance.createEClass();
EAttribute nameAttribute = eInstance.createEAttribute();
nameAttribute.setEType(EcorePackage.Literals.ESTRING);
nameAttribute.setName("name");
greetingClass.getEStructuralFeatures().add(nameAttribute);
greetingClass.setName(CLASSIFIERNAME);
EReference reference = eInstance.createEReference();
reference.setName("greetings");
reference.setLowerBound(0);
reference.setUpperBound(-1);
reference.setEType(greetingClass);
modelClass.getEStructuralFeatures().add(reference);
initialModelRoot.getEClassifiers().add(modelClass);
initialModelRoot.getEClassifiers().add(greetingClass);
ecoreURI = URI.createPlatformResourceURI(ECORE_FILE, true);
Resource grammarResource = resourceSetProvider.get().getResource(grammarUri, true);
ParserRule greetingsParserRule = Iterables.filter(EcoreUtil2.getAllContentsOfType(grammarResource.getContents().get(0), ParserRule.class), new Predicate<ParserRule>() {
@Override
public boolean apply(ParserRule input) {
return input.getName().equals(CLASSIFIERNAME);
}
}).iterator().next();
ParserRule modelParserRule = Iterables.filter(EcoreUtil2.getAllContentsOfType(grammarResource.getContents().get(0), ParserRule.class), new Predicate<ParserRule>() {
@Override
public boolean apply(ParserRule input) {
return input.getName().equals("Model");
}
}).iterator().next();
greetingParserRuleUri = EcoreUtil.getURI(greetingsParserRule);
modelParserRuleUri = EcoreUtil.getURI(modelParserRule);
grammarResource.unload();
}
use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.
the class CrossReferenceTemplateVariableResolver method resolveValues.
@Override
public List<String> resolveValues(TemplateVariable variable, XtextTemplateContext castedContext) {
String abbreviatedCrossReference = (String) variable.getVariableType().getParams().iterator().next();
int dotIndex = abbreviatedCrossReference.lastIndexOf('.');
if (dotIndex <= 0) {
// $NON-NLS-1$ //$NON-NLS-2$
log.error("CrossReference '" + abbreviatedCrossReference + "' could not be resolved.");
return Collections.emptyList();
}
String[] classReferencePair = new String[] { abbreviatedCrossReference.substring(0, dotIndex), abbreviatedCrossReference.substring(dotIndex + 1) };
Grammar grammar = getGrammar(castedContext);
if (grammar == null) {
return Collections.emptyList();
}
EReference reference = getReference(classReferencePair[0], classReferencePair[1], grammar);
if (reference == null) {
log.debug(// $NON-NLS-1$ //$NON-NLS-2$
"CrossReference to class '" + classReferencePair[0] + "' and reference '" + classReferencePair[1] + // $NON-NLS-1$
"' could not be resolved.");
return Collections.emptyList();
}
IScope scope = null;
EObject currentModel = castedContext.getContentAssistContext().getCurrentModel();
if (currentModel == null) {
scope = globalScopeProvider.getScope(castedContext.getContentAssistContext().getResource(), reference, null);
} else {
scope = castedContext.getScopeProvider().getScope(currentModel, reference);
}
Iterable<IEObjectDescription> linkingCandidates = queryScope(scope);
List<String> names = new ArrayList<String>();
for (IEObjectDescription eObjectDescription : linkingCandidates) {
names.add(qualifiedNameConverter.toString(eObjectDescription.getName()));
}
return names;
}
use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.
the class DefaultQuickfixProvider method createLinkingIssueResolutions.
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
final IModificationContext modificationContext = modificationContextFactory.createModificationContext(issue);
final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
if (xtextDocument == null)
return;
xtextDocument.readOnly(new CancelableUnitOfWork<Void, XtextResource>() {
IssueResolutionAcceptor myAcceptor = null;
@Override
public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
myAcceptor = getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator);
EObject target = state.getEObject(issue.getUriToProblem().fragment());
EReference reference = getUnresolvedEReference(issue, target);
if (reference == null)
return null;
fixUnresolvedReference(issue, xtextDocument, target, reference);
return null;
}
protected void fixUnresolvedReference(final Issue issue, final IXtextDocument xtextDocument, EObject target, EReference reference) throws BadLocationException {
boolean caseInsensitive = caseInsensitivityHelper.isIgnoreCase(reference);
EObject crossReferenceTerminal = getCrossReference(issue, target);
String ruleName = null;
Keyword keyword = null;
if (crossReferenceTerminal instanceof RuleCall) {
RuleCall ruleCall = (RuleCall) crossReferenceTerminal;
ruleName = ruleCall.getRule().getName();
} else if (crossReferenceTerminal instanceof Keyword) {
keyword = (Keyword) crossReferenceTerminal;
}
String issueString = xtextDocument.get(issue.getOffset(), issue.getLength());
IScope scope = scopeProvider.getScope(target, reference);
List<IEObjectDescription> discardedDescriptions = Lists.newArrayList();
Set<String> qualifiedNames = Sets.newHashSet();
int addedDescriptions = 0;
int checkedDescriptions = 0;
for (IEObjectDescription referableElement : queryScope(scope)) {
String referableElementQualifiedName = qualifiedNameConverter.toString(referableElement.getQualifiedName());
if (similarityMatcher.isSimilar(issueString, qualifiedNameConverter.toString(referableElement.getName()))) {
addedDescriptions++;
createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
qualifiedNames.add(referableElementQualifiedName);
} else {
if (qualifiedNames.add(referableElementQualifiedName))
discardedDescriptions.add(referableElement);
}
checkedDescriptions++;
if (checkedDescriptions > 100)
break;
}
if (discardedDescriptions.size() + addedDescriptions <= 5) {
for (IEObjectDescription referableElement : discardedDescriptions) {
createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
}
}
}
protected AbstractElement getCrossReference(final Issue issue, EObject target) {
final ICompositeNode node = NodeModelUtils.getNode(target);
if (node == null)
throw new IllegalStateException("Cannot happen since we found a reference");
ICompositeNode rootNode = node.getRootNode();
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, issue.getOffset());
CrossReference crossReference = findCrossReference(target, leaf);
return crossReference.getTerminal();
}
public void createResolution(String issueString, IEObjectDescription solution, String ruleName, Keyword keyword, boolean caseInsensitive) {
String replacement = qualifiedNameConverter.toString(solution.getName());
String replaceLabel = fixCrossReferenceLabel(issueString, replacement);
if (keyword != null) {
if (caseInsensitive && !replacement.equalsIgnoreCase(keyword.getValue()))
return;
if (!caseInsensitive && !replacement.equals(keyword.getValue()))
return;
} else if (ruleName != null) {
replacement = converter.convertToString(replacement, ruleName);
if (replacement == null) {
return;
}
} else {
logger.error("either keyword or ruleName have to present", new IllegalStateException());
}
myAcceptor.accept(issue, replaceLabel, replaceLabel, fixCrossReferenceImage(issueString, replacement), new ReplaceModification(issue, replacement));
}
});
}
use of org.eclipse.emf.ecore.EReference in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method createLinkingIssueResolutions.
/**
* Filter quickfixes for types and constructors.
*/
@Override
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
final IModificationContext modificationContext = getModificationContextFactory().createModificationContext(issue);
final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
if (xtextDocument != null) {
xtextDocument.readOnly(new CancelableUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
try {
EObject target = state.getEObject(issue.getUriToProblem().fragment());
EReference reference = getUnresolvedEReference(issue, target);
if (reference != null && reference.getEReferenceType() != null) {
createLinkingIssueQuickfixes(issue, getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator), xtextDocument, state, target, reference);
}
} catch (WrappedException e) {
// issue information seems to be out of sync, e.g. there is no
// EObject with the given fragment
}
return null;
}
});
}
}
Aggregations