Search in sources :

Example 11 with ITextSelection

use of org.eclipse.jface.text.ITextSelection 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 12 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class JavaElementDelegate method getJavaElementForXtextEditor.

protected IJavaElement getJavaElementForXtextEditor(IEditorPart editor) {
    if (!(editor instanceof XtextEditor))
        return null;
    IJavaProject jp = null;
    if (editor.getEditorInput() instanceof IFileEditorInput) {
        IFile file = ((IFileEditorInput) editor.getEditorInput()).getFile();
        if (!fileExtensionProvider.isValid(file.getFileExtension())) {
            return null;
        }
        jp = JavaCore.create(file.getProject());
        if (jp == null || !jp.exists() || !jp.isOpen())
            return null;
    } else {
        return null;
    }
    XtextEditor xtextEditor = (XtextEditor) editor;
    ISelection selection = xtextEditor.getSelectionProvider().getSelection();
    if (!(selection instanceof ITextSelection))
        return null;
    final int offset = ((ITextSelection) selection).getOffset();
    IJavaElement func = xtextEditor.getDocument().readOnly(new IUnitOfWork<IJavaElement, XtextResource>() {

        @Override
        public IJavaElement exec(XtextResource state) throws Exception {
            return findJavaElement(state, offset);
        }
    });
    return func;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) IFileEditorInput(org.eclipse.ui.IFileEditorInput) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ISelection(org.eclipse.jface.viewers.ISelection) XtextResource(org.eclipse.xtext.resource.XtextResource) ITextSelection(org.eclipse.jface.text.ITextSelection) CoreException(org.eclipse.core.runtime.CoreException)

Example 13 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractCursorHandlingTest method assertState.

protected void assertState(String string, XtextEditor editor) {
    int cursor = string.indexOf('|');
    assertEquals(string.replace("|", ""), editor.getDocument().get());
    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
    assertEquals("unexpected cursor position:", cursor, selection.getOffset());
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 14 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractAutoEditTest method selectText.

protected void selectText(XtextEditor editor, int relativeToCurrentOffset, int length) throws Exception {
    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
    editor.getInternalSourceViewer().setSelectedRange(selection.getOffset() + relativeToCurrentOffset, length);
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 15 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class TextViewerDeleteLineAction method run.

/*
	 * @see IAction#run()
	 */
@Override
public void run() {
    if (fTarget == null)
        return;
    ITextViewer viewer = getTextViewer();
    if (viewer == null)
        return;
    if (!canModifyViewer())
        return;
    IDocument document = viewer.getDocument();
    if (document == null)
        return;
    ITextSelection selection = getSelection(viewer);
    if (selection == null)
        return;
    try {
        fTarget.deleteLine(document, selection, fType, fCopyToClipboard);
    } catch (BadLocationException e) {
    // should not happen
    }
}
Also used : IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)200 ISelection (org.eclipse.jface.viewers.ISelection)64 IDocument (org.eclipse.jface.text.IDocument)50 BadLocationException (org.eclipse.jface.text.BadLocationException)45 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 IRegion (org.eclipse.jface.text.IRegion)33 Region (org.eclipse.jface.text.Region)29 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)26 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)20 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)16 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 IResource (org.eclipse.core.resources.IResource)12 IFile (org.eclipse.core.resources.IFile)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)11 Template (org.eclipse.jface.text.templates.Template)10 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)10 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)10