use of org.eclipse.xtext.resource.IEObjectDescription in project xtext-eclipse by eclipse.
the class AbstractTypeScopeTest method testGetElementByInstance_01.
@Test
public void testGetElementByInstance_01() {
JvmVoid voidType = TypesFactory.eINSTANCE.createJvmVoid();
IEObjectDescription element = getTypeScope().getSingleElement(voidType);
assertNotNull(element);
assertEquals(voidType.getIdentifier(), element.getName().toString());
}
use of org.eclipse.xtext.resource.IEObjectDescription in project xtext-eclipse by eclipse.
the class EObjectDescriptionBasedStubGeneratorTest method testNested.
@Test
public void testNested() {
final EObjectDescription _top = new EObjectDescription(QualifiedName.create("foo", "Bar"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.<String, String>emptyMap());
final EObjectDescription _nested0 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz0"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
final EObjectDescription _nested1 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz1"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
final EObjectDescription _nested10 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz1$FooBar0"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
final EObjectDescription _nested11 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz1$FooBar0"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
IResourceDescription resourceDescription = new AbstractResourceDescription() {
@Override
public URI getURI() {
return null;
}
@Override
public Iterable<IReferenceDescription> getReferenceDescriptions() {
return Collections.emptyList();
}
@Override
public Iterable<QualifiedName> getImportedNames() {
return Collections.emptyList();
}
@Override
protected List<IEObjectDescription> computeExportedObjects() {
return Arrays.asList(new IEObjectDescription[] { _top, _nested0, _nested1, _nested10, _nested11 });
}
};
assertEquals("package foo;\n" + "public class Bar{\n" + "public static class Baz0{\n" + "}\n" + "public static class Baz1{\n" + "public static class FooBar0{\n" + "}\n" + "public static class FooBar0{\n" + "}\n" + "}\n" + "}", gen.getJavaStubSource(_top, resourceDescription));
}
use of org.eclipse.xtext.resource.IEObjectDescription 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.xtext.resource.IEObjectDescription in project xtext-eclipse by eclipse.
the class DefaultReferenceFinder method createExportedElementsMap.
@Deprecated
protected Map<EObject, URI> createExportedElementsMap(final Resource resource) {
return new ForwardingMap<EObject, URI>() {
private Map<EObject, URI> delegate;
@Override
protected Map<EObject, URI> delegate() {
if (delegate != null) {
return delegate;
}
URI uri = EcoreUtil2.getPlatformResourceOrNormalizedURI(resource);
IResourceServiceProvider resourceServiceProvider = getServiceProviderRegistry().getResourceServiceProvider(uri);
if (resourceServiceProvider == null) {
return delegate = Collections.emptyMap();
}
IResourceDescription.Manager resourceDescriptionManager = resourceServiceProvider.getResourceDescriptionManager();
if (resourceDescriptionManager == null) {
return delegate = Collections.emptyMap();
}
IResourceDescription resourceDescription = resourceDescriptionManager.getResourceDescription(resource);
Map<EObject, URI> exportedElementMap = newIdentityHashMap();
if (resourceDescription != null) {
for (IEObjectDescription exportedEObjectDescription : resourceDescription.getExportedObjects()) {
EObject eObject = resource.getEObject(exportedEObjectDescription.getEObjectURI().fragment());
if (eObject != null)
exportedElementMap.put(eObject, exportedEObjectDescription.getEObjectURI());
}
}
return delegate = exportedElementMap;
}
};
}
use of org.eclipse.xtext.resource.IEObjectDescription 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));
}
});
}
Aggregations