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