use of org.eclipse.xtext.scoping.IScope in project xtext-eclipse by eclipse.
the class ArithmeticsScopeProvider method getScope.
@Override
public IScope getScope(final EObject context, final EReference reference) {
if ((reference == ArithmeticsPackage.Literals.IMPORT__MODULE)) {
return super.getGlobalScope(context.eResource(), reference);
}
final org.eclipse.xtext.example.arithmetics.arithmetics.Module module = EcoreUtil2.<org.eclipse.xtext.example.arithmetics.arithmetics.Module>getContainerOfType(context, org.eclipse.xtext.example.arithmetics.arithmetics.Module.class);
IScope result = IScope.NULLSCOPE;
EList<Import> _imports = module.getImports();
for (final Import import_ : _imports) {
boolean _eIsProxy = import_.getModule().eIsProxy();
boolean _not = (!_eIsProxy);
if (_not) {
result = this.getModuleScope(context, reference, import_.getModule(), result);
}
}
result = this.getModuleScope(context, reference, module, result);
return this.getDefinitionScope(context, reference, result);
}
use of org.eclipse.xtext.scoping.IScope in project xtext-eclipse by eclipse.
the class XbaseProposalProvider method createLocalVariableAndImplicitProposals.
protected void createLocalVariableAndImplicitProposals(EObject context, IExpressionScope.Anchor anchor, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor) {
String prefix = contentAssistContext.getPrefix();
if (prefix.length() > 0) {
if (!Character.isJavaIdentifierStart(prefix.charAt(0))) {
if (prefix.length() > 1) {
if (prefix.charAt(0) == '^' && !Character.isJavaIdentifierStart(prefix.charAt(1))) {
return;
}
}
}
}
// long time = System.currentTimeMillis();
Function<IEObjectDescription, ICompletionProposal> proposalFactory = getProposalFactory(getFeatureCallRuleName(), contentAssistContext);
IResolvedTypes resolvedTypes = context != null ? typeResolver.resolveTypes(context) : typeResolver.resolveTypes(contentAssistContext.getResource());
IExpressionScope expressionScope = resolvedTypes.getExpressionScope(context, anchor);
// TODO use the type name information
IScope scope = expressionScope.getFeatureScope();
getCrossReferenceProposalCreator().lookupCrossReference(scope, context, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, acceptor, getFeatureDescriptionPredicate(contentAssistContext), proposalFactory);
// System.out.printf("XbaseProposalProvider.createLocalVariableAndImplicitProposals = %d\n", System.currentTimeMillis() - time);
// time = System.currentTimeMillis();
// TODO use the type name information
proposeDeclaringTypeForStaticInvocation(context, null, /* ignore */
contentAssistContext, acceptor);
// System.out.printf("XbaseProposalProvider.proposeDeclaringTypeForStaticInvocation = %d\n", System.currentTimeMillis() - time);
}
use of org.eclipse.xtext.scoping.IScope 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.scoping.IScope 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.xtext.scoping.IScope in project xtext-eclipse by eclipse.
the class XbaseHoverDocumentationProvider method getResolvedDeclarator.
protected JvmDeclaredType getResolvedDeclarator(String name) {
JvmDeclaredType jvmDeclaredType = null;
if (Strings.isEmpty(name) || name.trim().length() == 0) {
jvmDeclaredType = getDeclaringType(context);
} else {
HoverReference reference = new HoverReference(TypesPackage.Literals.JVM_TYPE);
IScope scope = scopeProvider.getScope(context, reference);
IEObjectDescription declarator = scope.getSingleElement(qualifiedNameConverter.toQualifiedName(name));
if (declarator != null && EcoreUtil2.isAssignableFrom(TypesPackage.eINSTANCE.getJvmGenericType(), declarator.getEClass())) {
jvmDeclaredType = (JvmDeclaredType) context.eResource().getResourceSet().getEObject(declarator.getEObjectURI(), true);
}
}
return jvmDeclaredType;
}
Aggregations