Search in sources :

Example 1 with RefactoringWizardOpenOperation_NonForking

use of org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking in project xtext-eclipse by eclipse.

the class ExtractVariableHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    try {
        syncUtil.totalSync(false);
        final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
        if (editor != null) {
            final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
            final IXtextDocument document = editor.getDocument();
            XtextResource resource = document.priorityReadOnly(new IUnitOfWork<XtextResource, XtextResource>() {

                @Override
                public XtextResource exec(XtextResource state) throws Exception {
                    return resourceCopier.loadIntoNewResourceSet(state);
                }
            });
            XExpression expression = expressionUtil.findSelectedExpression(resource, selection);
            if (expression != null) {
                ExtractVariableRefactoring introduceVariableRefactoring = refactoringProvider.get();
                if (introduceVariableRefactoring.initialize(editor, expression)) {
                    ITextRegion region = locationInFileProvider.getFullTextRegion(expression);
                    editor.selectAndReveal(region.getOffset(), region.getLength());
                    ExtractVariableWizard wizard = new ExtractVariableWizard(introduceVariableRefactoring);
                    RefactoringWizardOpenOperation_NonForking openOperation = new RefactoringWizardOpenOperation_NonForking(wizard);
                    openOperation.run(editor.getSite().getShell(), "Extract Local Variable");
                }
            }
        }
    } catch (InterruptedException e) {
        return null;
    } catch (Exception exc) {
        LOG.error("Error during refactoring", exc);
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error during refactoring", exc.getMessage() + "\nSee log for details");
    }
    return null;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) ExecutionException(org.eclipse.core.commands.ExecutionException) RefactoringWizardOpenOperation_NonForking(org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking) ITextRegion(org.eclipse.xtext.util.ITextRegion) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 2 with RefactoringWizardOpenOperation_NonForking

use of org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking in project xtext-xtend by eclipse.

the class ExtractMethodHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    try {
        syncUtil.totalSync(false);
        final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
        if (editor != null) {
            final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
            final IXtextDocument document = editor.getDocument();
            XtextResource copiedResource = document.priorityReadOnly(new IUnitOfWork<XtextResource, XtextResource>() {

                @Override
                public XtextResource exec(XtextResource state) throws Exception {
                    return resourceCopier.loadIntoNewResourceSet(state);
                }
            });
            List<XExpression> expressions = expressionUtil.findSelectedSiblingExpressions(copiedResource, selection);
            if (!expressions.isEmpty()) {
                ExtractMethodRefactoring extractMethodRefactoring = refactoringProvider.get();
                if (extractMethodRefactoring.initialize(editor, expressions, true)) {
                    updateSelection(editor, expressions);
                    ExtractMethodWizard wizard = wizardFactory.create(extractMethodRefactoring);
                    RefactoringWizardOpenOperation_NonForking openOperation = new RefactoringWizardOpenOperation_NonForking(wizard);
                    openOperation.run(editor.getSite().getShell(), "Extract Method");
                }
            }
        }
    } catch (InterruptedException e) {
        return null;
    } catch (Exception exc) {
        LOG.error("Error during refactoring", exc);
        MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error during refactoring", exc.getMessage() + "\nSee log for details");
    }
    return null;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) ExecutionException(org.eclipse.core.commands.ExecutionException) RefactoringWizardOpenOperation_NonForking(org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking) XExpression(org.eclipse.xtext.xbase.XExpression) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

ExecutionException (org.eclipse.core.commands.ExecutionException)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)2 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)2 RefactoringWizardOpenOperation_NonForking (org.eclipse.xtext.ui.refactoring.ui.RefactoringWizardOpenOperation_NonForking)2 XExpression (org.eclipse.xtext.xbase.XExpression)2 ITextRegion (org.eclipse.xtext.util.ITextRegion)1