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);
}
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);
}
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);
}
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);
}
}
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));
}
Aggregations