Search in sources :

Example 26 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.

the class AbstractPolymorphicScopeProvider method getScope.

/**
 * Returns a scope for a given context object and EReference, using a named scope definition. If none is found
 * tries to find a scope (using the same name) for the type of the reference.
 *
 * @param context
 *          the context
 * @param reference
 *          the reference
 * @param scopeName
 *          the scope name
 * @param originalResource
 *          the original resource
 * @return the scope
 */
public IScope getScope(final EObject context, final EReference reference, final String scopeName, final Resource originalResource) {
    if (context.eResource() != originalResource && context.eResource() instanceof XtextResource) {
        registerForeignObject(context, (XtextResource) context.eResource(), originalResource);
    }
    String nameToFind = scopeName == null ? SCOPE_STRING : scopeName;
    IScope scope = internalGetScope(context, reference, nameToFind, originalResource);
    if (scope == null) {
        scope = internalGetScope(context, reference.getEReferenceType(), nameToFind, originalResource);
    }
    return (scope == null) ? IScope.NULLSCOPE : scope;
}
Also used : IScope(org.eclipse.xtext.scoping.IScope) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 27 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project statecharts by Yakindu.

the class AsyncXtextContentAssistProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
    if (getContentProposalProvider() == null)
        return null;
    IXtextDocument document = (IXtextDocument) viewer.getDocument();
    final CancelableCompletionProposalComputer computer = createCompletionProposalComputer(viewer, offset);
    ICompletionProposal[] result = document.readOnly(new CancelableUnitOfWork<ICompletionProposal[], XtextResource>() {

        @Override
        public ICompletionProposal[] exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
            computer.setCancelIndicator(cancelIndicator);
            try {
                return computer.exec(state);
            } catch (Throwable t) {
                return new ICompletionProposal[] {};
            }
        }
    });
    Arrays.sort(result, getCompletionProposalComparator());
    result = getCompletionProposalPostProcessor().postProcess(result);
    return result;
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) XtextResource(org.eclipse.xtext.resource.XtextResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 28 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project statecharts by Yakindu.

the class AbstractSGenTest method parseExpression.

protected EObject parseExpression(String expression, String ruleName) {
    XtextResource resource = resourceProvider.get();
    resource.setURI(URI.createPlatformPluginURI("path", true));
    ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
    parserRule.setName(ruleName);
    IParseResult result = parser.parse(parserRule, new StringReader(expression));
    EObject rootASTElement = result.getRootASTElement();
    resource.getContents().add(rootASTElement);
    ListBasedDiagnosticConsumer diagnosticsConsumer = new ListBasedDiagnosticConsumer();
    linker.linkModel(result.getRootASTElement(), diagnosticsConsumer);
    if (result.hasSyntaxErrors()) {
        StringBuilder errorMessages = new StringBuilder();
        Iterable<INode> syntaxErrors = result.getSyntaxErrors();
        for (INode iNode : syntaxErrors) {
            errorMessages.append(iNode.getSyntaxErrorMessage());
            errorMessages.append("\n");
        }
        throw new RuntimeException("Could not parse expression, syntax errors: " + errorMessages);
    }
    if (diagnosticsConsumer.hasConsumedDiagnostics(Severity.ERROR)) {
        throw new RuntimeException("Error during linking: " + diagnosticsConsumer.getResult(Severity.ERROR));
    }
    return rootASTElement;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) StringReader(java.io.StringReader) ListBasedDiagnosticConsumer(org.eclipse.xtext.resource.impl.ListBasedDiagnosticConsumer) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 29 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project statecharts by Yakindu.

the class XtextStyledTextSelectionProvider method getSelection.

public ISelection getSelection() {
    if (styledText.isDisposed())
        return StructuredSelection.EMPTY;
    int offset = styledText.getCaretOffset() - 1;
    XtextResource fakeResource = xtextResource;
    IParseResult parseResult = fakeResource.getParseResult();
    if (parseResult == null)
        return StructuredSelection.EMPTY;
    ICompositeNode rootNode = parseResult.getRootNode();
    ILeafNode selectedNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
    final EObject selectedObject = NodeModelUtils.findActualSemanticObjectFor(selectedNode);
    if (selectedObject == null) {
        return StructuredSelection.EMPTY;
    }
    return new StructuredSelection(selectedObject);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 30 with XtextResource

use of org.eclipse.xtext.resource.XtextResource in project dsl-devkit by dsldevkit.

the class CatalogFromExtensionPointScope method getAllLocalElements.

@Override
protected Iterable<IEObjectDescription> getAllLocalElements() {
    loadDefinitions();
    if (loadedModel == null) {
        return ImmutableList.of();
    }
    XtextResource resource = (XtextResource) loadedModel.eResource();
    IQualifiedNameProvider nameProvider = resource.getResourceServiceProvider().get(IQualifiedNameProvider.class);
    return Scopes.scopedElementsFor(ImmutableList.of(loadedModel), nameProvider);
}
Also used : IQualifiedNameProvider(org.eclipse.xtext.naming.IQualifiedNameProvider) XtextResource(org.eclipse.xtext.resource.XtextResource)

Aggregations

XtextResource (org.eclipse.xtext.resource.XtextResource)627 Test (org.junit.Test)367 Resource (org.eclipse.emf.ecore.resource.Resource)107 EObject (org.eclipse.emf.ecore.EObject)99 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)67 StringInputStream (org.eclipse.xtext.util.StringInputStream)67 URI (org.eclipse.emf.common.util.URI)62 Diagnostic (org.eclipse.emf.common.util.Diagnostic)55 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)55 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)46 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)40 Grammar (org.eclipse.xtext.Grammar)32 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)31 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)30 IFile (org.eclipse.core.resources.IFile)29 Issue (org.eclipse.xtext.validation.Issue)29 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)28 List (java.util.List)26 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)26 INode (org.eclipse.xtext.nodemodel.INode)23