Search in sources :

Example 26 with IParseResult

use of org.eclipse.xtext.parser.IParseResult 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 27 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project dsl-devkit by dsldevkit.

the class AbstractFastLinkingService method getUsedGrammar.

/**
 * Tries to find a grammar.
 *
 * @param resourceSet
 *          to use for loading
 * @param grammarName
 *          qualified grammar name
 * @return A singleton list containing the grammar, or an empty list if not found.
 */
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
    // copied from XtextLinkingService#getUsedGrammar()
    try {
        if (grammarName != null) {
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource) {
                    IParseResult parseResult = ((XtextResource) resource).getParseResult();
                    if (parseResult != null) {
                        rootElement = parseResult.getRootASTElement();
                    }
                } else if (!resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof Grammar) {
                    Grammar otherGrammar = (Grammar) rootElement;
                    if (grammarName.equals(otherGrammar.getName())) {
                        if (resource instanceof DerivedStateAwareResource) {
                            resource.getContents();
                        }
                        return Collections.<EObject>singletonList(otherGrammar);
                    }
                }
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
            URI normalizedURI = null;
            if (resourceSet instanceof XtextResourceSet) {
                XtextResourceSet set = (XtextResourceSet) resourceSet;
                normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
            } else {
                normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
            }
            final Resource resource = resourceSet.getResource(normalizedURI, true);
            if (!resource.getContents().isEmpty()) {
                final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
                if (grammarName.equals(usedGrammar.getName())) {
                    return Collections.<EObject>singletonList(usedGrammar);
                }
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult) Grammar(org.eclipse.xtext.Grammar) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

Example 28 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-xtend by eclipse.

the class PasteJavaCodeHandler method doPasteJavaCode.

private void doPasteJavaCode(final XtextEditor activeXtextEditor, final String javaCode, final JavaImportData javaImports) throws ExecutionException {
    ISourceViewer sourceViewer = activeXtextEditor.getInternalSourceViewer();
    final IXtextDocument xtextDocument = activeXtextEditor.getDocument();
    IJavaProject project = null;
    IEditorInput editorInput = activeXtextEditor.getEditorInput();
    if (editorInput instanceof IFileEditorInput) {
        IProject iProject = ((IFileEditorInput) editorInput).getFile().getProject();
        project = JavaCore.create(iProject);
    }
    final int selectionOffset = sourceViewer.getSelectedRange().x - 1;
    EObject targetElement = xtextDocument.readOnly(new IUnitOfWork<EObject, XtextResource>() {

        @Override
        public EObject exec(XtextResource state) throws Exception {
            IParseResult parseResult = state.getParseResult();
            if (parseResult == null) {
                return null;
            }
            ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), selectionOffset);
            return leafNode.getSemanticElement();
        }
    });
    JavaConverter javaConverter = javaConverterProvider.get();
    final String xtendCode = javaConverter.toXtend(javaCode, javaImports != null ? javaImports.getImports() : null, targetElement, project);
    if (!Strings.isEmpty(xtendCode)) {
        if (javaImports != null) {
            importsUtil.addImports(javaImports.getImports(), javaImports.getStaticImports(), new String[] {}, xtextDocument);
        }
        Point selection = sourceViewer.getSelectedRange();
        try {
            xtextDocument.replace(selection.x, selection.y, xtendCode);
        } catch (BadLocationException e) {
            throw new ExecutionException("Failed to replace content.", e);
        }
    // TODO enable formatting, when performance became better
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=457814
    // doFormat(sourceViewer, xtendCode, selection);
    }
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) Point(org.eclipse.swt.graphics.Point) IProject(org.eclipse.core.resources.IProject) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException) ExecutionException(org.eclipse.core.commands.ExecutionException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) IFileEditorInput(org.eclipse.ui.IFileEditorInput) EObject(org.eclipse.emf.ecore.EObject) JavaConverter(org.eclipse.xtend.core.javaconverter.JavaConverter) IParseResult(org.eclipse.xtext.parser.IParseResult) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) ExecutionException(org.eclipse.core.commands.ExecutionException) IEditorInput(org.eclipse.ui.IEditorInput) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 29 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-xtend by eclipse.

the class PartialParserBenchmark method doTime.

public int doTime(IParser parser, int windowSize) {
    int result = 0;
    for (int j = 0; j < contentToParse.length() - windowSize; j++) {
        ReplaceRegion replaceRegion = new ReplaceRegion(new TextRegion(j, windowSize), contentToParse.substring(j, j + windowSize));
        IParseResult newParseResult = parser.reparse(parseResult, replaceRegion);
        if (newParseResult.getRootASTElement() != null) {
            result++;
        }
    }
    return result;
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 30 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-xtend by eclipse.

the class AnnotationValueParserBenchmark method timeAnnotationValueParsing.

public int timeAnnotationValueParsing(int reps) throws Exception {
    int result = 0;
    for (int i = 0; i < reps; i++) {
        StringReader reader = new StringReader(contentToParse);
        IParseResult parseResult = parser.parse(reader);
        result += parseResult.getRootNode().getLength();
    }
    return result;
}
Also used : StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)60 EObject (org.eclipse.emf.ecore.EObject)22 StringReader (java.io.StringReader)16 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)14 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)10 INode (org.eclipse.xtext.nodemodel.INode)10 XtextResource (org.eclipse.xtext.resource.XtextResource)10 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)9 ParserRule (org.eclipse.xtext.ParserRule)8 Test (org.junit.Test)7 ITextRegion (org.eclipse.xtext.util.ITextRegion)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 URI (org.eclipse.emf.common.util.URI)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 ParseResult (org.eclipse.xtext.parser.ParseResult)3 Scope (org.yakindu.sct.model.sgraph.Scope)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2