Search in sources :

Example 1 with JavaConverter

use of org.eclipse.xtend.core.javaconverter.JavaConverter in project xtext-xtend by eclipse.

the class ConvertJavaCode method doConvert.

private void doConvert(final Set<ICompilationUnit> compilationUnits, IProgressMonitor monitor, Map<ICompilationUnit, ConversionResult> conversionResults) throws ExecutionException, InterruptedException {
    for (ICompilationUnit iCompilationUnit : compilationUnits) {
        if (monitor.isCanceled()) {
            throw new InterruptedException();
        }
        JavaConverter converter = converterProvider.get();
        ConversionResult conversionResult = converter.toXtend(iCompilationUnit);
        monitor.subTask("Working with " + iCompilationUnit.getElementName());
        if (!conversionResult.getProblems().iterator().hasNext()) {
            IFile file = xtendFileToCreate(iCompilationUnit);
            Resource resource = createResource(file, conversionResult.getXtendCode());
            if (!validateResource(resource)) {
                monitor.subTask("Conversion was not successfull. Re-trying with another strategy.");
                conversionResult = converter.useRobustSyntax().toXtend(iCompilationUnit);
            }
        }
        conversionResults.put(iCompilationUnit, conversionResult);
        monitor.worked(1);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ConversionResult(org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult) IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IResource(org.eclipse.core.resources.IResource) JavaConverter(org.eclipse.xtend.core.javaconverter.JavaConverter)

Example 2 with JavaConverter

use of org.eclipse.xtend.core.javaconverter.JavaConverter 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 3 with JavaConverter

use of org.eclipse.xtend.core.javaconverter.JavaConverter in project xtext-xtend by eclipse.

the class JavaFileConverterTest method converToXtend.

public JavaConverter.ConversionResult converToXtend(final String unitName, final String javaCode) {
    try {
        final JavaConverter j2x = this.javaConverter.get();
        JavaConverter.ConversionResult result = j2x.toXtend(unitName, javaCode);
        int _size = IterableExtensions.size(result.getProblems());
        boolean _equals = (_size == 0);
        if (_equals) {
            try {
                this.file(result.getXtendCode(), true);
            } catch (final Throwable _t) {
                if (_t instanceof AssertionError) {
                    result = j2x.useRobustSyntax().toXtend(unitName, javaCode);
                } else {
                    throw Exceptions.sneakyThrow(_t);
                }
            }
        }
        return result;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JavaConverter(org.eclipse.xtend.core.javaconverter.JavaConverter)

Aggregations

JavaConverter (org.eclipse.xtend.core.javaconverter.JavaConverter)3 XtextResource (org.eclipse.xtext.resource.XtextResource)2 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 Point (org.eclipse.swt.graphics.Point)1 IEditorInput (org.eclipse.ui.IEditorInput)1 IFileEditorInput (org.eclipse.ui.IFileEditorInput)1 ConversionResult (org.eclipse.xtend.core.javaconverter.JavaConverter.ConversionResult)1 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1