Search in sources :

Example 86 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument 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)

Example 87 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.

the class CodeBuilderQuickfix method getXtendModification.

protected IModification getXtendModification(final ICodeBuilder.Xtend builder) {
    final IModification _function = (IModificationContext it) -> {
        final XtendTypeDeclaration xtendClass = builder.getXtendType();
        final IEditorPart editor = this.editorOpener.open(EcoreUtil.getURI(xtendClass), false);
        if ((!(editor instanceof XtextEditor))) {
            return;
        }
        final XtextEditor xtextEditor = ((XtextEditor) editor);
        final IXtextDocument document = xtextEditor.getDocument();
        final Wrapper<Integer> wrapper = Wrapper.<Integer>forType(Integer.class);
        final IUnitOfWork<ReplacingAppendable, XtextResource> _function_1 = (XtextResource resource) -> {
            ReplacingAppendable _xblockexpression = null;
            {
                int offset = builder.getInsertOffset(resource);
                wrapper.set(Integer.valueOf(offset));
                final int typeIndentation = this.getTypeIndentation(resource, document, xtendClass);
                DocumentSourceAppender.Factory.OptionalParameters _optionalParameters = new DocumentSourceAppender.Factory.OptionalParameters();
                final Procedure1<DocumentSourceAppender.Factory.OptionalParameters> _function_2 = (DocumentSourceAppender.Factory.OptionalParameters it_1) -> {
                    int _indentationLevel = builder.getIndentationLevel();
                    int _plus = (_indentationLevel + typeIndentation);
                    it_1.baseIndentationLevel = _plus;
                    it_1.ensureEmptyLinesAround = true;
                };
                DocumentSourceAppender.Factory.OptionalParameters _doubleArrow = ObjectExtensions.<DocumentSourceAppender.Factory.OptionalParameters>operator_doubleArrow(_optionalParameters, _function_2);
                _xblockexpression = this.appendableFactory.create(document, resource, offset, 0, _doubleArrow);
            }
            return _xblockexpression;
        };
        final ReplacingAppendable appendable = document.<ReplacingAppendable>readOnly(_function_1);
        Integer offset = wrapper.get();
        builder.build(appendable);
        appendable.commitChanges();
        xtextEditor.setHighlightRange(((offset).intValue() + 1), appendable.length(), true);
    };
    return _function;
}
Also used : Wrapper(org.eclipse.xtext.util.Wrapper) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) XtextResource(org.eclipse.xtext.resource.XtextResource) IEditorPart(org.eclipse.ui.IEditorPart) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) DocumentSourceAppender(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 88 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.

the class FieldInitializerUtil method getSelectedResource.

public IJavaElement getSelectedResource(IStructuredSelection selection) {
    IJavaElement elem = null;
    if (selection != null && !selection.isEmpty()) {
        Object o = selection.getFirstElement();
        elem = Adapters.adapt(o, IJavaElement.class);
        if (elem == null) {
            elem = getPackage(o);
        }
    }
    if (elem == null) {
        IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IWorkbenchPart part = activePage.getActivePart();
        if (part instanceof ContentOutline) {
            part = activePage.getActiveEditor();
        }
        if (part instanceof XtextEditor) {
            IXtextDocument doc = ((XtextEditor) part).getDocument();
            IFile file = Adapters.adapt(doc, IFile.class);
            elem = getPackage(file);
        }
    }
    if (elem == null || elem.getElementType() == IJavaElement.JAVA_MODEL) {
        try {
            IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
            if (projects.length == 1) {
                elem = projects[0];
            }
        } catch (JavaModelException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    return elem;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ContentOutline(org.eclipse.ui.views.contentoutline.ContentOutline) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 89 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class ContentAssistProcessorTestBuilder method assertMatchString.

public ContentAssistProcessorTestBuilder assertMatchString(String matchString) throws Exception {
    String currentModelToParse = getModel();
    final XtextResource xtextResource = loadHelper.getResourceFor(new StringInputStream(currentModelToParse));
    final IXtextDocument xtextDocument = getDocument(xtextResource, currentModelToParse);
    XtextSourceViewerConfiguration configuration = get(XtextSourceViewerConfiguration.class);
    Shell shell = new Shell();
    try {
        ISourceViewer sourceViewer = getSourceViewer(shell, xtextDocument, configuration);
        IContentAssistant contentAssistant = configuration.getContentAssistant(sourceViewer);
        String contentType = xtextDocument.getContentType(currentModelToParse.length());
        if (contentAssistant.getContentAssistProcessor(contentType) != null) {
            ContentAssistContext.Factory factory = get(ContentAssistContext.Factory.class);
            ContentAssistContext[] contexts = factory.create(sourceViewer, currentModelToParse.length(), xtextResource);
            for (ContentAssistContext context : contexts) {
                Assert.assertTrue("matchString = '" + matchString + "', actual: '" + context.getPrefix() + "'", "".equals(context.getPrefix()) || matchString.equals(context.getPrefix()));
            }
        } else {
            Assert.fail("No content assistant for content type " + contentType);
        }
        return this;
    } finally {
        shell.dispose();
    }
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) Shell(org.eclipse.swt.widgets.Shell) IContentAssistant(org.eclipse.jface.text.contentassist.IContentAssistant) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) XtextResource(org.eclipse.xtext.resource.XtextResource) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) XtextSourceViewerConfiguration(org.eclipse.xtext.ui.editor.XtextSourceViewerConfiguration)

Example 90 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class ContentAssistProcessorTestBuilder method applyProposal.

public ContentAssistProcessorTestBuilder applyProposal(int position, String proposalString) throws Exception {
    IXtextDocument document = getDocument(getModel());
    Shell shell = new Shell();
    try {
        ICompletionProposal[] proposals = computeCompletionProposals(document, position, shell);
        ICompletionProposal proposal = findProposal(proposalString, proposals);
        return applyProposal(proposal, position, document);
    } finally {
        shell.dispose();
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)147 XtextResource (org.eclipse.xtext.resource.XtextResource)51 Test (org.junit.Test)46 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)38 IFile (org.eclipse.core.resources.IFile)37 BadLocationException (org.eclipse.jface.text.BadLocationException)26 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)26 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)25 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)23 EObject (org.eclipse.emf.ecore.EObject)22 DefaultFoldingRegionProvider (org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider)18 FoldedPosition (org.eclipse.xtext.ui.editor.folding.FoldedPosition)18 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)16 List (java.util.List)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 ArrayList (java.util.ArrayList)11 Issue (org.eclipse.xtext.validation.Issue)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 URI (org.eclipse.emf.common.util.URI)9 IRegion (org.eclipse.jface.text.IRegion)9