Search in sources :

Example 16 with TextSelection

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

the class JavaParserTest method canIdentifyStaticInitializerWhenInStaticClassInitializer.

/**
 * Given an IEditorPart and current selection is in a static class initializer
 * block, JavaParser should be able to figure out that we were in an static
 * initializer block.
 *
 * @throws Exception
 */
@Test
public void canIdentifyStaticInitializerWhenInStaticClassInitializer() throws Exception {
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + // create static class initializer block
    "static {\n" + "// " + OFFSET_MARKER + "\n" + "}\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Add JavaParserExampleClass.java to project
    InputStream newFileInputStream = new ByteArrayInputStream(javaSourceCode.getBytes());
    IFile javaSourceFile = project.addFileToProject("/src/org/eclipse/changelog/tests", "JavaParserExampleClass.java", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Open a source file and get the IEditorPart
    javaSourceEditorPart = openEditor(javaSourceFile);
    // make sure changelog editor content is right before merging
    assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(javaSourceEditorPart instanceof AbstractDecoratedTextEditor);
    // Select the snippet we want
    int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals("static initializer", actualFunctionName);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Example 17 with TextSelection

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

the class JavaParserTest method canIdentifyFieldWithinNestedClass.

/**
 * Given an IEditorPart and currently a field within a nested
 * class is selected, JavaParser should return a "nestedClass.fieldName"
 * construct for the current function.
 *
 * @throws Exception
 */
@Test
public void canIdentifyFieldWithinNestedClass() throws Exception {
    final String nestedClassName = "Encapsulated";
    final String currentFieldName = "variableInNestedClass";
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + "static {\n" + "// empty \n" + "}\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "private class " + nestedClassName + "{\n" + "private int " + currentFieldName + " = 10;\n" + "public String getString() throws Exception {\n" + "// return a String, yay ;-)\n" + "return \"returnString\";\n" + "}\n" + "}\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Add JavaParserExampleClass.java to project
    InputStream newFileInputStream = new ByteArrayInputStream(javaSourceCode.getBytes());
    IFile javaSourceFile = project.addFileToProject("/src/org/eclipse/changelog/tests", "JavaParserExampleClass.java", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Open a source file and get the IEditorPart
    javaSourceEditorPart = openEditor(javaSourceFile);
    // make sure changelog editor content is right before merging
    assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(javaSourceEditorPart instanceof AbstractDecoratedTextEditor);
    // Select the snippet we want
    int selectionStart = javaSourceCode.indexOf(currentFieldName);
    assertTrue(selectionStart >= 0);
    // select only a part of field name, as this should be sufficient
    int selectionLength = currentFieldName.length() - 3;
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String expectedFunctionName = nestedClassName + "." + currentFieldName;
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals(expectedFunctionName, actualFunctionName);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Example 18 with TextSelection

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

the class JavaParserTest method canIdentifyStaticInitializerWhenInStaticInstanceInitializer.

/**
 * Given an IEditorPart and current selection is in a static instance initializer
 * block, JavaParser should be able to figure out that we were in an static
 * initializer block.
 *
 * @throws Exception
 */
@Test
public void canIdentifyStaticInitializerWhenInStaticInstanceInitializer() throws Exception {
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + // create static instance initializer block
    "{\n" + "// " + OFFSET_MARKER + "\n" + "}\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Add JavaParserExampleClass.java to project
    InputStream newFileInputStream = new ByteArrayInputStream(javaSourceCode.getBytes());
    IFile javaSourceFile = project.addFileToProject("/src/org/eclipse/changelog/tests", "JavaParserExampleClass.java", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Open a source file and get the IEditorPart
    javaSourceEditorPart = openEditor(javaSourceFile);
    // make sure changelog editor content is right before merging
    assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(javaSourceEditorPart instanceof AbstractDecoratedTextEditor);
    // Select the snippet we want
    int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals("static initializer", actualFunctionName);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Example 19 with TextSelection

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

the class ExtractMethodIntegrationTest method assertAfterExtract.

protected void assertAfterExtract(final CharSequence input, final Procedure1<? super ExtractMethodRefactoring> initializer, final CharSequence expected) {
    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<Change, XtextResource> _function = (XtextResource it) -> {
                Change _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.setExplicitlyDeclareReturnType(false);
                    refactoring.setVisibility(JvmVisibility.PUBLIC);
                    refactoring.setMethodName("bar");
                    NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
                    RefactoringStatus status = refactoring.checkInitialConditions(_nullProgressMonitor);
                    Assert.assertTrue(status.toString(), status.isOK());
                    initializer.apply(refactoring);
                    NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
                    status = refactoring.checkFinalConditions(_nullProgressMonitor_1);
                    Assert.assertTrue(status.toString(), status.isOK());
                    NullProgressMonitor _nullProgressMonitor_2 = new NullProgressMonitor();
                    Change _createChange = refactoring.createChange(_nullProgressMonitor_2);
                    NullProgressMonitor _nullProgressMonitor_3 = new NullProgressMonitor();
                    _xblockexpression = _createChange.perform(_nullProgressMonitor_3);
                }
                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) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) XtextResource(org.eclipse.xtext.resource.XtextResource) Change(org.eclipse.ltk.core.refactoring.Change) ExtractMethodRefactoring(org.eclipse.xtend.ide.refactoring.ExtractMethodRefactoring) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 20 with TextSelection

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

the class XtendExpressionUtilTest method assertExpressionSelected.

protected void assertExpressionSelected(final String modelWithSelectionMarkup, final String expectedSelection) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() ");
    _builder.append(modelWithSelectionMarkup, "\t");
    _builder.newLineIfNotEmpty();
    _builder.append("}");
    _builder.newLine();
    final String model = _builder.toString();
    final String cleanedModel = model.replaceAll("\\$", "");
    final XtendFile expression = this.parse(cleanedModel);
    final int selectionOffset = model.indexOf("$");
    int _lastIndexOf = model.lastIndexOf("$");
    int _minus = (_lastIndexOf - selectionOffset);
    final int selectionLength = (_minus - 1);
    Resource _eResource = expression.eResource();
    TextSelection _textSelection = new TextSelection(selectionOffset, selectionLength);
    final XExpression selectedExpression = this.util.findSelectedExpression(((XtextResource) _eResource), _textSelection);
    final ITextRegion selectedRegion = this.locationInFileProvider.getFullTextRegion(selectedExpression);
    int _offset = selectedRegion.getOffset();
    int _offset_1 = selectedRegion.getOffset();
    int _length = selectedRegion.getLength();
    int _plus = (_offset_1 + _length);
    Assert.assertEquals(expectedSelection, cleanedModel.substring(_offset, _plus));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) TextSelection(org.eclipse.jface.text.TextSelection) ITextRegion(org.eclipse.xtext.util.ITextRegion) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XExpression(org.eclipse.xtext.xbase.XExpression) XtextResource(org.eclipse.xtext.resource.XtextResource)

Aggregations

TextSelection (org.eclipse.jface.text.TextSelection)28 Test (org.junit.Test)18 IFile (org.eclipse.core.resources.IFile)17 ByteArrayInputStream (java.io.ByteArrayInputStream)13 InputStream (java.io.InputStream)13 Path (org.eclipse.core.runtime.Path)13 IEditorPart (org.eclipse.ui.IEditorPart)8 ISelection (org.eclipse.jface.viewers.ISelection)7 AbstractDecoratedTextEditor (org.eclipse.ui.texteditor.AbstractDecoratedTextEditor)7 AbstractTextEditor (org.eclipse.ui.texteditor.AbstractTextEditor)6 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)6 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)5 XtextResource (org.eclipse.xtext.resource.XtextResource)4 XExpression (org.eclipse.xtext.xbase.XExpression)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 ITextSelection (org.eclipse.jface.text.ITextSelection)3 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)3 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)3 BadLocationException (org.eclipse.jface.text.BadLocationException)2 TreePath (org.eclipse.jface.viewers.TreePath)2