use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class TextViewerJoinLinesAction method run.
@Override
public void run() {
ITextViewer viewer = getTextViewer();
if (viewer == null)
return;
if (!canModifyViewer())
return;
IDocument document = viewer.getDocument();
if (document == null)
return;
ITextSelection selection = getSelection(viewer);
if (selection == null)
return;
int startLine = selection.getStartLine();
int endLine = selection.getEndLine();
try {
int caretOffset = joinLines(document, startLine, endLine);
if (caretOffset > -1) {
StyledText widget = viewer.getTextWidget();
widget.setRedraw(false);
adjustHighlightRange(viewer, caretOffset, 0);
viewer.revealRange(caretOffset, 0);
viewer.setSelectedRange(caretOffset, 0);
widget.setRedraw(true);
}
} catch (BadLocationException e) {
// should not happen
}
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class TextViewerMoveLinesAction method runWithEvent.
@Override
public void runWithEvent(Event event) {
ITextViewer viewer = getTextViewer();
if (viewer == null)
return;
if (!canModifyViewer())
return;
// get involved objects
IDocument document = viewer.getDocument();
if (document == null)
return;
StyledText widget = viewer.getTextWidget();
if (widget == null)
return;
// get selection
ITextSelection sel = (ITextSelection) viewer.getSelectionProvider().getSelection();
if (sel.isEmpty())
return;
ITextSelection skippedLine = getSkippedLine(document, sel);
if (skippedLine == null)
return;
try {
ITextSelection movingArea = getMovingSelection(document, sel, viewer);
// visible area, bail out
if (!containedByVisibleRegion(movingArea, viewer) || !containedByVisibleRegion(skippedLine, viewer))
return;
// get the content to be moved around: the moving (selected) area and the skipped line
String moving = movingArea.getText();
String skipped = skippedLine.getText();
if (moving == null || skipped == null || document.getLength() == 0)
return;
String delim;
String insertion;
int offset, deviation;
if (fUpwards) {
delim = document.getLineDelimiter(skippedLine.getEndLine());
if (fCopy) {
delim = TextUtilities.getDefaultLineDelimiter(document);
insertion = moving + delim;
offset = movingArea.getOffset();
deviation = 0;
} else {
Assert.isNotNull(delim);
insertion = moving + delim + skipped;
offset = skippedLine.getOffset();
deviation = -skippedLine.getLength() - delim.length();
}
} else {
delim = document.getLineDelimiter(movingArea.getEndLine());
if (fCopy) {
if (delim == null) {
delim = TextUtilities.getDefaultLineDelimiter(document);
insertion = delim + moving;
} else {
insertion = moving + delim;
}
offset = skippedLine.getOffset();
deviation = movingArea.getLength() + delim.length();
} else {
Assert.isNotNull(delim);
insertion = skipped + delim + moving;
offset = movingArea.getOffset();
deviation = skipped.length() + delim.length();
}
}
// modify the document
beginCompoundEdit();
if (fCopy) {
document.replace(offset, 0, insertion);
} else {
document.replace(offset, insertion.length(), insertion);
}
// move the selection along
int selOffset = movingArea.getOffset() + deviation;
int selLength = movingArea.getLength() + (fAddDelimiter ? delim.length() : 0);
if (!(viewer instanceof ITextViewerExtension5))
selLength = Math.min(selLength, viewer.getVisibleRegion().getOffset() + viewer.getVisibleRegion().getLength() - selOffset);
else {
// TODO need to check what is necessary in the projection case
}
selectAndReveal(viewer, selOffset, selLength);
} catch (BadLocationException x) {
// won't happen without concurrent modification - bail out
return;
}
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class EditorCopyQualifiedNameHandler method getQualifiedName.
@Override
protected String getQualifiedName(ExecutionEvent event) {
XtextEditor activeXtextEditor = EditorUtils.getActiveXtextEditor(event);
if (activeXtextEditor == null) {
return null;
}
final ITextSelection selection = getTextSelection(activeXtextEditor);
return activeXtextEditor.getDocument().priorityReadOnly(new IUnitOfWork<String, XtextResource>() {
@Override
public String exec(XtextResource xTextResource) throws Exception {
EObject context = getContext(selection, xTextResource);
EObject selectedElement = getSelectedName(selection, xTextResource);
return getQualifiedName(selectedElement, context);
}
});
}
use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.
the class DefaultRenameElementHandler method isRefactoringEnabled.
protected boolean isRefactoringEnabled(IRenameElementContext renameElementContext, XtextResource resource) {
ResourceSet resourceSet = resource.getResourceSet();
if (renameElementContext != null && resourceSet != null) {
EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
if (targetElement != null && !targetElement.eIsProxy()) {
if (targetElement.eResource() == resource && renameElementContext.getTriggeringEditorSelection() instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) renameElementContext.getTriggeringEditorSelection();
ITextRegion selectedRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
INode crossReferenceNode = eObjectAtOffsetHelper.getCrossReferenceNode(resource, selectedRegion);
if (crossReferenceNode == null) {
// selection is on the declaration. make sure it's the name
ITextRegion significantRegion = locationInFileProvider.getSignificantTextRegion(targetElement);
if (!significantRegion.contains(selectedRegion))
return false;
}
}
IRenameStrategy.Provider renameStrategyProvider = globalServiceProvider.findService(targetElement, IRenameStrategy.Provider.class);
try {
if (renameStrategyProvider.get(targetElement, renameElementContext) != null) {
return true;
} else {
IRenameStrategy2 strategy2 = globalServiceProvider.findService(targetElement, IRenameStrategy2.class);
ISimpleNameProvider simpleNameProvider = globalServiceProvider.findService(targetElement, ISimpleNameProvider.class);
return strategy2 != null && simpleNameProvider.canRename(targetElement);
}
} catch (NoSuchStrategyException e) {
MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Cannot rename element", e.getMessage());
}
}
}
return false;
}
use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class QuickDiffRestoreAction method getSelection.
/**
* Returns the selection of the editor this action belongs to.
*
* @return the editor's selection, or <code>null</code>
*/
protected ITextSelection getSelection() {
if (getTextEditor() == null)
return null;
ISelectionProvider sp = getTextEditor().getSelectionProvider();
if (sp == null)
return null;
ISelection s = sp.getSelection();
if (s instanceof ITextSelection)
return (ITextSelection) s;
return null;
}
Aggregations