Search in sources :

Example 31 with TextSelection

use of org.eclipse.jface.text.TextSelection in project jbosstools-hibernate by jbosstools.

the class Utils method hasSelection.

/**
 * Checks has the editor selection or not
 * @param editor
 * @return
 */
public static boolean hasSelection(IEditorPart editor) {
    ITextEditor[] tEditors = OpenMappingUtils.getTextEditors(editor);
    boolean res = false;
    for (int i = 0; i < tEditors.length && !res; i++) {
        ITextEditor textEditor = tEditors[i];
        ISelection selection = textEditor.getSelectionProvider().getSelection();
        if (selection instanceof TextSelection) {
            TextSelection tSelection = (TextSelection) selection;
            res = tSelection.getLength() > 0;
        }
    }
    return res;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection)

Example 32 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 33 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 34 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 35 with TextSelection

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

the class NewFeatureNameUtilTest method assertDefaultName.

protected void assertDefaultName(String modelWithSelectionMarkup, String expectedName) throws Exception {
    String cleanedModel = modelWithSelectionMarkup.replaceAll("\\$", "");
    XExpression expression = parse(cleanedModel);
    int selectionOffset = modelWithSelectionMarkup.indexOf("$");
    int selectionLength = modelWithSelectionMarkup.lastIndexOf("$") - selectionOffset - 1;
    XExpression selectedExpression = util.findSelectedExpression((XtextResource) expression.eResource(), new TextSelection(selectionOffset, selectionLength));
    XExpression successor = util.findSuccessorExpressionForVariableDeclaration(selectedExpression);
    NewFeatureNameUtil nameUtil = nameUtilProvider.get();
    nameUtil.setFeatureScopeContext(successor);
    String defaultName = nameUtil.getDefaultName(selectedExpression);
    assertEquals(expectedName, defaultName);
}
Also used : NewFeatureNameUtil(org.eclipse.xtext.xbase.ui.refactoring.NewFeatureNameUtil) TextSelection(org.eclipse.jface.text.TextSelection) XExpression(org.eclipse.xtext.xbase.XExpression)

Aggregations

TextSelection (org.eclipse.jface.text.TextSelection)109 IFile (org.eclipse.core.resources.IFile)35 ISelection (org.eclipse.jface.viewers.ISelection)26 ITextSelection (org.eclipse.jface.text.ITextSelection)24 Test (org.junit.Test)23 IDocument (org.eclipse.jface.text.IDocument)21 IEditorPart (org.eclipse.ui.IEditorPart)19 InputStream (java.io.InputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Path (org.eclipse.core.runtime.Path)13 BadLocationException (org.eclipse.jface.text.BadLocationException)11 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)11 IRegion (org.eclipse.jface.text.IRegion)10 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 IResource (org.eclipse.core.resources.IResource)6