Search in sources :

Example 51 with TextSelection

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

the class ExtractVariableIntegrationTest method assertAfterExtract.

protected void assertAfterExtract(final CharSequence input, final CharSequence expected, final boolean isFinal) {
    try {
        final String inputString = input.toString();
        final String model = inputString.replace("$", "");
        final IFile file = this.workbenchTestHelper.createFile("Foo", model);
        final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
        try {
            final IUnitOfWork<Change, XtextResource> _function = (XtextResource it) -> {
                Change _xblockexpression = null;
                {
                    final int offset = inputString.indexOf("$");
                    int _lastIndexOf = inputString.lastIndexOf("$");
                    int _minus = (_lastIndexOf - 1);
                    final int length = (_minus - offset);
                    final TextSelection textSelection = new TextSelection(offset, length);
                    final XExpression selection = this.util.findSelectedExpression(it, textSelection);
                    final ExtractVariableRefactoring refactoring = this.refactoringProvider.get();
                    refactoring.setFinal(isFinal);
                    refactoring.initialize(editor, selection);
                    NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                    final RefactoringStatus status = refactoring.checkAllConditions(_nullProgressMonitor);
                    Assert.assertTrue(status.toString(), status.isOK());
                    NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
                    Change _createChange = refactoring.createChange(_nullProgressMonitor_1);
                    NullProgressMonitor _nullProgressMonitor_2 = new NullProgressMonitor();
                    _xblockexpression = _createChange.perform(_nullProgressMonitor_2);
                }
                return _xblockexpression;
            };
            editor.getDocument().<Change>readOnly(_function);
            Assert.assertEquals(expected.toString(), editor.getDocument().get());
        } finally {
            editor.close(false);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TextSelection(org.eclipse.jface.text.TextSelection) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ExtractVariableRefactoring(org.eclipse.xtext.xbase.ui.refactoring.ExtractVariableRefactoring) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) XtextResource(org.eclipse.xtext.resource.XtextResource) Change(org.eclipse.ltk.core.refactoring.Change) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 52 with TextSelection

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

the class ExtractMethodIntegrationTest method assertExtractForbidden.

protected void assertExtractForbidden(final CharSequence input, final Procedure1<? super ExtractMethodRefactoring> initializer, final String messageFragment) {
    try {
        final String inputString = input.toString();
        final IFile file = this.workbenchTestHelper.createFile("Foo", inputString.replace("$", ""));
        final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
        try {
            final IUnitOfWork<String, XtextResource> _function = (XtextResource it) -> {
                String _xblockexpression = null;
                {
                    int _indexOf = inputString.indexOf("$");
                    int _lastIndexOf = inputString.lastIndexOf("$");
                    int _indexOf_1 = inputString.indexOf("$");
                    int _minus = (_lastIndexOf - _indexOf_1);
                    int _minus_1 = (_minus - 1);
                    TextSelection _textSelection = new TextSelection(_indexOf, _minus_1);
                    final List<XExpression> selection = this.util.findSelectedSiblingExpressions(it, _textSelection);
                    final ExtractMethodRefactoring refactoring = this.refactoringProvider.get();
                    refactoring.initialize(editor, selection, true);
                    refactoring.setMethodName("bar");
                    NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                    final RefactoringStatus status = refactoring.checkInitialConditions(_nullProgressMonitor);
                    initializer.apply(refactoring);
                    NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
                    status.merge(refactoring.checkFinalConditions(_nullProgressMonitor_1));
                    Assert.assertTrue(status.toString(), status.hasError());
                    final String message = status.getMessageMatchingSeverity(RefactoringStatus.ERROR);
                    Assert.assertTrue(message, message.toLowerCase().contains(messageFragment.toLowerCase()));
                    _xblockexpression = "";
                }
                return _xblockexpression;
            };
            editor.getDocument().<String>readOnly(_function);
        } finally {
            editor.close(false);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TextSelection(org.eclipse.jface.text.TextSelection) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) XtextResource(org.eclipse.xtext.resource.XtextResource) ExtractMethodRefactoring(org.eclipse.xtend.ide.refactoring.ExtractMethodRefactoring) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 53 with TextSelection

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

the class XtendQuickfixProvider method fixWrongFileRenameClass.

@Fix(IssueCodes.WRONG_FILE)
public void fixWrongFileRenameClass(final Issue issue, final IssueResolutionAcceptor acceptor) {
    URI uri = issue.getUriToProblem();
    String className = uri.trimFileExtension().lastSegment();
    String label = String.format("Rename class to '%s'", className);
    acceptor.accept(issue, label, label, null, (element, context) -> {
        context.getXtextDocument().modify(resource -> {
            IRenameElementContext renameContext = renameContextFactory.createRenameElementContext(element, null, new TextSelection(context.getXtextDocument(), issue.getOffset(), issue.getLength()), resource);
            final ProcessorBasedRefactoring refactoring = renameRefactoringProvider.getRenameRefactoring(renameContext);
            ((RenameElementProcessor) refactoring.getProcessor()).setNewName(className);
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, true, monitor -> {
                try {
                    if (!refactoring.checkFinalConditions(monitor).isOK())
                        return;
                    Change change = refactoring.createChange(monitor);
                    change.initializeValidationData(monitor);
                    PerformChangeOperation performChangeOperation = new PerformChangeOperation(change);
                    performChangeOperation.setUndoManager(RefactoringCore.getUndoManager(), refactoring.getName());
                    performChangeOperation.setSchedulingRule(ResourcesPlugin.getWorkspace().getRoot());
                    performChangeOperation.run(monitor);
                } catch (CoreException e) {
                    logger.error(e);
                }
            });
            return null;
        });
    });
}
Also used : PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) CoreException(org.eclipse.core.runtime.CoreException) TextSelection(org.eclipse.jface.text.TextSelection) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) Change(org.eclipse.ltk.core.refactoring.Change) URI(org.eclipse.emf.common.util.URI) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) RenameElementProcessor(org.eclipse.xtext.ui.refactoring.impl.RenameElementProcessor) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 54 with TextSelection

use of org.eclipse.jface.text.TextSelection in project eclipse.platform.text by eclipse.

the class GotoLastEditPositionAction method run.

@Override
public void run() {
    EditPosition editPosition = TextEditorPlugin.getDefault().getLastEditPosition();
    if (editPosition == null)
        return;
    final Position pos = editPosition.getPosition();
    if (pos == null || pos.isDeleted)
        return;
    IWorkbenchWindow window = getWindow();
    if (window == null)
        return;
    IWorkbenchPage page = window.getActivePage();
    IEditorPart editor;
    try {
        editor = page.openEditor(editPosition.getEditorInput(), editPosition.getEditorId());
    } catch (PartInitException ex) {
        // $NON-NLS-1$
        IStatus status = new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, "Go to Last Edit Location failed", ex);
        TextEditorPlugin.getDefault().getLog().log(status);
        return;
    }
    // Optimization - could also use else branch
    if (editor instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) editor;
        textEditor.selectAndReveal(pos.offset, pos.length);
        return;
    }
    /*
		 * Workaround: send out a text selection
		 * XXX: Needs to be improved, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=32214
		 */
    if (editor != null) {
        IEditorSite site = editor.getEditorSite();
        if (site == null)
            return;
        ISelectionProvider provider = editor.getEditorSite().getSelectionProvider();
        if (provider == null)
            return;
        provider.setSelection(new TextSelection(pos.offset, pos.length));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IStatus(org.eclipse.core.runtime.IStatus) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) EditPosition(org.eclipse.ui.internal.texteditor.EditPosition) EditPosition(org.eclipse.ui.internal.texteditor.EditPosition) Position(org.eclipse.jface.text.Position) TextSelection(org.eclipse.jface.text.TextSelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IEditorSite(org.eclipse.ui.IEditorSite)

Example 55 with TextSelection

use of org.eclipse.jface.text.TextSelection in project linuxtools by eclipse.

the class DoubleClickTest method testDoubleClickFunction.

@Test
public void testDoubleClickFunction() throws Exception {
    ILaunchConfiguration config = createConfiguration(proj.getProject());
    // $NON-NLS-1$
    doLaunch(config, "testDoubleClickFunction");
    CachegrindViewPart view = (CachegrindViewPart) ValgrindUIPlugin.getDefault().getView().getDynamicView();
    CachegrindOutput output = view.getOutputs()[0];
    // $NON-NLS-1$
    CachegrindFile file = getFileByName(output, "cpptest.cpp");
    // $NON-NLS-1$
    CachegrindFunction func = getFunctionByName(file, "main");
    TreePath path = new TreePath(new Object[] { output, file, func });
    doDoubleClick(path);
    // check file in editor
    IEditorPart editor = checkFile(file);
    // check line number
    ITextEditor textEditor = (ITextEditor) editor;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    TextSelection textSelection = (TextSelection) selection;
    // zero-indexed
    int line = textSelection.getStartLine() + 1;
    int expectedLine = ((IFunction) func.getModel()).getSourceRange().getStartLine();
    assertEquals(expectedLine, line);
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TreePath(org.eclipse.jface.viewers.TreePath) CachegrindOutput(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindOutput) CachegrindFile(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFile) CachegrindFunction(org.eclipse.linuxtools.internal.valgrind.cachegrind.model.CachegrindFunction) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) CachegrindViewPart(org.eclipse.linuxtools.internal.valgrind.cachegrind.CachegrindViewPart) IEditorPart(org.eclipse.ui.IEditorPart) Test(org.junit.Test)

Aggregations

TextSelection (org.eclipse.jface.text.TextSelection)105 IFile (org.eclipse.core.resources.IFile)35 ITextSelection (org.eclipse.jface.text.ITextSelection)24 ISelection (org.eclipse.jface.viewers.ISelection)24 Test (org.junit.Test)23 IDocument (org.eclipse.jface.text.IDocument)21 IEditorPart (org.eclipse.ui.IEditorPart)19 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Path (org.eclipse.core.runtime.Path)13 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)11 BadLocationException (org.eclipse.jface.text.BadLocationException)10 IRegion (org.eclipse.jface.text.IRegion)10 XtextResource (org.eclipse.xtext.resource.XtextResource)9 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)8 Module (org.eclipse.titan.designer.AST.Module)8 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)8 XExpression (org.eclipse.xtext.xbase.XExpression)8 TTCN3Editor (org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor)7 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)7