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);
}
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();
}
}
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);
}
}
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;
}
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;
}
Aggregations