下面列出了org.eclipse.jface.text.ITextSelection#getEndLine ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void removeLineComments(IDocument document, ITextSelection selection, String comment, ITextEditor editor)
throws BadLocationException {
int lineNumber = selection.getStartLine();
int endLineNumber = selection.getEndLine();
String oldText = document.get();
int deletedChars = 0;
Boolean isStartBeforeComment = false;
while (lineNumber <= endLineNumber) {
int commentOffset = oldText.indexOf(comment, document.getLineOffset(lineNumber) + deletedChars);
document.replace(commentOffset - deletedChars, comment.length(), "");
if (deletedChars == 0) {
isStartBeforeComment = commentOffset > selection.getOffset();
}
if (lineNumber != endLineNumber) {
deletedChars += comment.length();
}
lineNumber++;
}
ITextSelection newSelection = new TextSelection(
selection.getOffset() - (isStartBeforeComment ? 0 : comment.length()),
selection.getLength() - deletedChars);
editor.selectAndReveal(newSelection.getOffset(), newSelection.getLength());
}
private void addLineComments(IDocument document, ITextSelection selection, String comment, ITextEditor editor)
throws BadLocationException {
int lineNumber = selection.getStartLine();
int endLineNumber = selection.getEndLine();
int insertedChars = 0;
while (lineNumber <= endLineNumber) {
document.replace(document.getLineOffset(lineNumber), 0, comment);
if (lineNumber != endLineNumber) {
insertedChars += comment.length();
}
lineNumber++;
}
ITextSelection newSelection = new TextSelection(selection.getOffset() + comment.length(),
selection.getLength() + insertedChars);
editor.selectAndReveal(newSelection.getOffset(), newSelection.getLength());
}
/**
* Given a selection on a document, computes the lines fully or partially covered by
* <code>selection</code>. A line in the document is considered covered if
* <code>selection</code> comprises any characters on it, including the terminating delimiter.
* <p>Note that the last line in a selection is not considered covered if the selection only
* comprises the line delimiter at its beginning (that is considered part of the second last
* line).
* As a special case, if the selection is empty, a line is considered covered if the caret is
* at any position in the line, including between the delimiter and the start of the line. The
* line containing the delimiter is not considered covered in that case.
* </p>
*
* @param document the document <code>selection</code> refers to
* @param selection a selection on <code>document</code>
* @param viewer the <code>ISourceViewer</code> displaying <code>document</code>
* @return a selection describing the range of lines (partially) covered by
* <code>selection</code>, without any terminating line delimiters
* @throws BadLocationException if the selection is out of bounds (when the underlying document has changed during the call)
*/
private ITextSelection getMovingSelection(IDocument document, ITextSelection selection, ITextViewer viewer) throws BadLocationException {
int low= document.getLineOffset(selection.getStartLine());
int endLine= selection.getEndLine();
int high= document.getLineOffset(endLine) + document.getLineLength(endLine);
// get everything up to last line without its delimiter
String delim= document.getLineDelimiter(endLine);
if (delim != null)
high -= delim.length();
// the new selection will cover the entire lines being moved, except for the last line's
// delimiter. The exception to this rule is an empty last line, which will stay covered
// including its delimiter
if (delim != null && document.getLineLength(endLine) == delim.length())
fAddDelimiter= true;
else
fAddDelimiter= false;
return new TextSelection(document, low, high - low);
}
protected boolean isSelectionCommented(IDocument document, ITextSelection selection, String commentPrefix)
{
try {
for (int lineNum = selection.getStartLine(); lineNum <= selection.getEndLine(); ++lineNum) {
IRegion r = document.getLineInformation(lineNum);
String str = document.get(r.getOffset(), r.getLength()).trim();
if (!str.startsWith(commentPrefix)) {
return false;
}
}
return true;
} catch (Exception x) {
}
return false;
}
private boolean areLinesCommented(IDocument document, ITextSelection selection, String comment)
throws BadLocationException {
int lineNumber = selection.getStartLine();
while (lineNumber <= selection.getEndLine()) {
IRegion lineRegion = document.getLineInformation(lineNumber);
if (!document.get(lineRegion.getOffset(), lineRegion.getLength()).trim().startsWith(comment)) {
return false;
}
lineNumber++;
}
return true;
}
/**
* Checks if <code>selection</code> is contained by the visible region of <code>viewer</code>.
* As a special case, a selection is considered contained even if it extends over the visible
* region, but the extension stays on a partially contained line and contains only white space.
*
* @param selection the selection to be checked
* @param viewer the viewer displaying a visible region of <code>selection</code>'s document.
* @return <code>true</code>, if <code>selection</code> is contained, <code>false</code> otherwise.
*/
private boolean containedByVisibleRegion(ITextSelection selection, ITextViewer viewer) {
int min= selection.getOffset();
int max= min + selection.getLength();
IDocument document= viewer.getDocument();
IRegion visible;
if (viewer instanceof ITextViewerExtension5)
visible= ((ITextViewerExtension5) viewer).getModelCoverage();
else
visible= viewer.getVisibleRegion();
int visOffset= visible.getOffset();
try {
if (visOffset > min) {
if (document.getLineOfOffset(visOffset) != selection.getStartLine())
return false;
if (!isWhitespace(document.get(min, visOffset - min))) {
return false;
}
}
int visEnd= visOffset + visible.getLength();
if (visEnd < max) {
if (document.getLineOfOffset(visEnd) != selection.getEndLine())
return false;
if (!isWhitespace(document.get(visEnd, max - visEnd))) {
return false;
}
}
return true;
} catch (BadLocationException e) {
}
return false;
}
/**
* Computes the region of the skipped line given the text block to be moved. If
* <code>fUpwards</code> is <code>true</code>, the line above <code>selection</code>
* is selected, otherwise the line below.
*
* @param document the document <code>selection</code> refers to
* @param selection the selection on <code>document</code> that will be moved.
* @return the region comprising the line that <code>selection</code> will be moved over, without its terminating delimiter.
*/
private ITextSelection getSkippedLine(IDocument document, ITextSelection selection) {
int skippedLineN= (fUpwards ? selection.getStartLine() - 1 : selection.getEndLine() + 1);
if (skippedLineN > document.getNumberOfLines() || (!fCopy && (skippedLineN < 0 || skippedLineN == document.getNumberOfLines())))
return null;
try {
if (fCopy && skippedLineN == -1)
skippedLineN= 0;
IRegion line= document.getLineInformation(skippedLineN);
return new TextSelection(document, line.getOffset(), line.getLength());
} catch (BadLocationException e) {
// only happens on concurrent modifications
return null;
}
}
@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
}
}
private IRegion copiedGetTextBlockFromSelection(ITextSelection selection) {
try {
IDocument document= getDocument();
int start= document.getLineOffset(selection.getStartLine());
int endLine= selection.getEndLine();
IRegion endLineInfo= document.getLineInformation(endLine);
int end= endLineInfo.getOffset() + endLineInfo.getLength();
return new Region(start, end - start);
} catch (BadLocationException x) {
}
return null;
}
public void run()
{
ITextEditor editor = getTextEditor();
ISelection selection = editor.getSelectionProvider().getSelection();
if (selection instanceof ITextSelection)
{
ITextSelection textSelection = (ITextSelection) selection;
if (textSelection.getLength() != 0)
{
IAnnotationModel model = getAnnotationModel(editor);
if (model != null)
{
int start = textSelection.getStartLine();
int end = textSelection.getEndLine();
try
{
IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
int offset = document.getLineOffset(start);
int endOffset = document.getLineOffset(end + 1);
Position position = new Position(offset, endOffset - offset);
model.addAnnotation(new ProjectionAnnotation(), position);
} catch (BadLocationException x)
{
// ignore
}
}
}
}
}
/**
* Checks if <code>selection</code> is contained by the visible region of <code>viewer</code>.
* As a special case, a selection is considered contained even if it extends over the visible
* region, but the extension stays on a partially contained line and contains only white space.
*
* @param selection the selection to be checked
* @param viewer the viewer displaying a visible region of <code>selection</code>'s document.
* @return <code>true</code>, if <code>selection</code> is contained, <code>false</code> otherwise.
*/
private boolean containedByVisibleRegion(ITextSelection selection, ISourceViewer viewer) {
int min= selection.getOffset();
int max= min + selection.getLength();
IDocument document= viewer.getDocument();
IRegion visible;
if (viewer instanceof ITextViewerExtension5)
visible= ((ITextViewerExtension5) viewer).getModelCoverage();
else
visible= viewer.getVisibleRegion();
int visOffset= visible.getOffset();
try {
if (visOffset > min) {
if (document.getLineOfOffset(visOffset) != selection.getStartLine())
return false;
if (!isWhitespace(document.get(min, visOffset - min))) {
showStatus();
return false;
}
}
int visEnd= visOffset + visible.getLength();
if (visEnd < max) {
if (document.getLineOfOffset(visEnd) != selection.getEndLine())
return false;
if (!isWhitespace(document.get(visEnd, max - visEnd))) {
showStatus();
return false;
}
}
return true;
} catch (BadLocationException e) {
}
return false;
}
/**
* Computes the region of the skipped line given the text block to be moved. If
* <code>fUpwards</code> is <code>true</code>, the line above <code>selection</code>
* is selected, otherwise the line below.
*
* @param document the document <code>selection</code> refers to
* @param selection the selection on <code>document</code> that will be moved.
* @return the region comprising the line that <code>selection</code> will be moved over, without its terminating delimiter.
*/
private ITextSelection getSkippedLine(IDocument document, ITextSelection selection) {
int skippedLineN= (fUpwards ? selection.getStartLine() - 1 : selection.getEndLine() + 1);
if (skippedLineN > document.getNumberOfLines() || (!fCopy && (skippedLineN < 0 || skippedLineN == document.getNumberOfLines())))
return null;
try {
if (fCopy && skippedLineN == -1)
skippedLineN= 0;
IRegion line= document.getLineInformation(skippedLineN);
return new TextSelection(document, line.getOffset(), line.getLength());
} catch (BadLocationException e) {
// only happens on concurrent modifications
return null;
}
}
private boolean isMultilineSelection() {
ISelection selection= getSelectionProvider().getSelection();
if (selection instanceof ITextSelection) {
ITextSelection ts= (ITextSelection) selection;
return ts.getStartLine() != ts.getEndLine();
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
ITextSelection selection = WorkbenchUtils.getActiveTextSelection();
IDocument doc = WorkbenchUtils.getActiveDocument();
IEditorInput input = WorkbenchUtils.getActiveInput();
IEditorPart editor = WorkbenchUtils.getActiveEditor(false);
boolean isTextOperationAllowed = (selection != null) && (doc != null)
&& (input != null) && (editor != null)
&& (editor instanceof ModulaEditor);
if (isTextOperationAllowed) {
ITextEditor iTextEditor = (ITextEditor)editor;
final ITextOperationTarget operationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
String commentPrefix = ((SourceCodeTextEditor)editor).getEOLCommentPrefix();
isTextOperationAllowed = (operationTarget != null)
&& (operationTarget instanceof TextViewer)
&& (validateEditorInputState(iTextEditor))
&& (commentPrefix != null);
if ((isTextOperationAllowed)) {
int startLine = selection.getStartLine();
int endLine = selection.getEndLine();
int selOffset = selection.getOffset();
int selLen = selection.getLength();
int realEndLine = doc.getLineOfOffset(selOffset + selLen); // for selection end at pos=0 (endLine is line before here)
// Are cursor and anchor at 0 positions?
boolean is0pos = false;
if (doc.getLineOffset(startLine) == selOffset) {
if ((doc.getLineOffset(endLine) + doc.getLineLength(endLine) == selOffset + selLen)) {
is0pos = true;
}
}
ArrayList<ReplaceEdit> edits = null;
int offsAfter[] = {0};
if (is0pos || selLen == 0) {
edits = commentWholeLines(startLine, (selLen == 0) ? startLine : endLine, realEndLine, doc, offsAfter);
} else {
edits = commentRange(selOffset, selLen, "(*", "*)", offsAfter); //$NON-NLS-1$ //$NON-NLS-2$
}
if (edits != null && !edits.isEmpty()) {
DocumentRewriteSession drws = null;
try {
if (doc instanceof IDocumentExtension4) {
drws = ((IDocumentExtension4)doc).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
}
MultiTextEdit edit= new MultiTextEdit(0, doc.getLength());
edit.addChildren((TextEdit[]) edits.toArray(new TextEdit[edits.size()]));
edit.apply(doc, TextEdit.CREATE_UNDO);
iTextEditor.getSelectionProvider().setSelection(new TextSelection(offsAfter[0], 0));
}
finally {
if (doc instanceof IDocumentExtension4 && drws != null) {
((IDocumentExtension4)doc).stopRewriteSession(drws);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static IRegion convertRegion(int offset, int length, String text,
String originalDelimiter) {
try {
Document document = new Document(text);
String delimiter = document.getLineDelimiter(0);
// If the document's line delimiter is the same as that used to originally
// calculate this offset/length, then just return the original region.
if (delimiter == null || delimiter.equals(originalDelimiter)) {
return new Region(offset, length);
}
// If we're running a platform other than the one this offset/length was
// calculated on, we'll need to adjust the values. We start by creating
// a text selection containing the original region and the text with the
// original line endings.
String originalText = text.replaceAll(delimiter, originalDelimiter);
ITextSelection originalSelection = new TextSelection(new Document(
originalText), offset, length);
int delimiterLengthCorrection = originalDelimiter.length()
- delimiter.length();
// Adjust the offset by the delimiter length difference for each line
// that came before it.
int newOffset = originalSelection.getOffset()
- (delimiterLengthCorrection * originalSelection.getStartLine());
// Adjust the length by the delimiter length difference for each line
// between the start and the end of the original region
// TODO: account for case where the selection ends with a line break;
// currently this will not update the length since the selection starts
// and ends on the same line.
int regionLineBreaks = originalSelection.getEndLine()
- originalSelection.getStartLine();
int newLength = originalSelection.getLength()
- (delimiterLengthCorrection * regionLineBreaks);
return new Region(newOffset, newLength);
} catch (BadLocationException e) {
return null;
}
}
/**
* Given a selection on a document, computes the lines fully or partially covered by
* <code>selection</code>. A line in the document is considered covered if
* <code>selection</code> comprises any characters on it, including the terminating delimiter.
* <p>Note that the last line in a selection is not considered covered if the selection only
* comprises the line delimiter at its beginning (that is considered part of the second last
* line).
* As a special case, if the selection is empty, a line is considered covered if the caret is
* at any position in the line, including between the delimiter and the start of the line. The
* line containing the delimiter is not considered covered in that case.
* </p>
*
* @param document the document <code>selection</code> refers to
* @param selection a selection on <code>document</code>
* @param viewer the <code>ISourceViewer</code> displaying <code>document</code>
* @return a selection describing the range of lines (partially) covered by
* <code>selection</code>, without any terminating line delimiters
* @throws BadLocationException if the selection is out of bounds (when the underlying document has changed during the call)
*/
private ITextSelection getMovingSelection(IDocument document, ITextSelection selection, ISourceViewer viewer) throws BadLocationException {
int low= document.getLineOffset(selection.getStartLine());
int endLine= selection.getEndLine();
int high= document.getLineOffset(endLine) + document.getLineLength(endLine);
// get everything up to last line without its delimiter
String delim= document.getLineDelimiter(endLine);
if (delim != null)
high -= delim.length();
return new TextSelection(document, low, high - low);
}