use of org.eclipse.jface.text.ITextViewer 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.ITextViewer 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.ITextViewer in project xtext-eclipse by eclipse.
the class TextViewerOperationAction method run.
/**
* The <code>TextOperationAction</code> implementation of this <code>IAction</code> method runs the operation with
* the current operation code.
*/
@Override
public void run() {
if (fOperationCode == -1 || fOperationTarget == null)
return;
ITextViewer viewer = getTextViewer();
if (viewer == null)
return;
if (!fRunsOnReadOnly && !canModifyViewer())
return;
Display display = null;
Shell shell = viewer.getTextWidget().getShell();
if (shell != null && !shell.isDisposed())
display = shell.getDisplay();
BusyIndicator.showWhile(display, new Runnable() {
@Override
public void run() {
fOperationTarget.doOperation(fOperationCode);
}
});
}
use of org.eclipse.jface.text.ITextViewer in project xtext-eclipse by eclipse.
the class TextViewerOperationAction method update.
/**
* The <code>TextOperationAction</code> implementation of this <code>IUpdate</code> method discovers the operation
* through the current editor's <code>ITextOperationTarget</code> adapter, and sets the enabled state accordingly.
*/
@Override
public void update() {
if (!fAllowUpdate)
return;
super.update();
if (!fRunsOnReadOnly && !canModifyViewer()) {
setEnabled(false);
return;
}
ITextViewer viewer = getTextViewer();
if (fOperationTarget == null && viewer != null && fOperationCode != -1)
fOperationTarget = viewer.getTextOperationTarget();
boolean isEnabled = (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
setEnabled(isEnabled);
}
use of org.eclipse.jface.text.ITextViewer in project xtext-eclipse by eclipse.
the class TextViewerShiftAction method run.
/**
* The <code>TextOperationAction</code> implementation of this <code>IAction</code> method runs the operation with
* the current operation code.
*/
@Override
public void run() {
if (fOperationCode == -1 || fOperationTarget == null)
return;
ITextViewer viewer = getTextViewer();
if (viewer == null)
return;
if (!canModifyViewer())
return;
Display display = null;
Shell shell = viewer.getTextWidget().getShell();
if (shell != null && !shell.isDisposed())
display = shell.getDisplay();
BusyIndicator.showWhile(display, new Runnable() {
@Override
public void run() {
fOperationTarget.doOperation(fOperationCode);
}
});
}
Aggregations