diff options
author | Mickael Istria | 2019-12-03 08:58:49 +0000 |
---|---|---|
committer | Mickael Istria | 2021-09-07 10:25:05 +0000 |
commit | bf5933129b8eb15b5b7cdefa781eacc1c4a5e3b9 (patch) | |
tree | df95e1fd56beedf3560e058dad6ea2278d6cf977 /org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java | |
parent | 118d3512d9b32f1219e5358a6ae5ad212ede861b (diff) | |
download | eclipse.platform.text-bf5933129b8eb15b5b7cdefa781eacc1c4a5e3b9.tar.gz eclipse.platform.text-bf5933129b8eb15b5b7cdefa781eacc1c4a5e3b9.tar.xz eclipse.platform.text-bf5933129b8eb15b5b7cdefa781eacc1c4a5e3b9.zip |
Bug 466532 -Support multipleI20210908-1800I20210907-1800
carets/cursors/selections for text manipulation
+ Made line & word start/end commands capable of multi-carets
+ Support pasting multiple tokens
+ Added command to turn block selection into multi selection
Change-Id: Ibb0c1819a197ca409fadfe2a0b8ce133762f6d9e
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/153689
Reviewed-by: Mickael Istria <mistria@redhat.com>
Tested-by: Mickael Istria <mistria@redhat.com>
Diffstat (limited to 'org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java')
-rw-r--r-- | org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java | 217 |
1 files changed, 102 insertions, 115 deletions
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java index cda1fcfdb73..01641869670 100644 --- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java +++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java @@ -33,6 +33,7 @@ import java.util.List; import java.util.Map; import java.util.ResourceBundle; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.IntStream; import java.util.stream.Stream; import org.osgi.framework.Bundle; @@ -131,6 +132,7 @@ import org.eclipse.jface.text.IFindReplaceTarget; import org.eclipse.jface.text.IFindReplaceTargetExtension; import org.eclipse.jface.text.IInformationControlCreator; import org.eclipse.jface.text.IMarkRegionTarget; +import org.eclipse.jface.text.IMultiTextSelection; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.IRewriteTarget; import org.eclipse.jface.text.ISelectionValidator; @@ -1190,72 +1192,65 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit StyledText st= fSourceViewer.getTextWidget(); if (st == null || st.isDisposed()) return; - int caretOffset= st.getCaretOffset(); - int lineNumber= st.getLineAtOffset(caretOffset); - int lineOffset= st.getOffsetAtLine(lineNumber); + boolean caretAtBeginningOfSelection = st.getCaretOffset() == st.getSelection().x; + Point firstSelection = st.getSelection(); + int[] ranges = st.getSelectionRanges(); + List<Point> newSelection = new ArrayList<>(ranges.length / 2); + for (int j = 0; j < ranges.length; j += 2) { + int offset = ranges[j]; + int length = ranges[j + 1]; + int caretOffset = caretAtBeginningOfSelection ? offset : offset + length; + int lineNumber = st.getLineAtOffset(caretOffset); + int lineOffset = st.getOffsetAtLine(lineNumber); + int lineLength; + int caretOffsetInDocument; + final IDocument document = fSourceViewer.getDocument(); - int lineLength; - int caretOffsetInDocument; - final IDocument document= fSourceViewer.getDocument(); - - try { - caretOffsetInDocument= widgetOffset2ModelOffset(fSourceViewer, caretOffset); - lineLength= document.getLineInformationOfOffset(caretOffsetInDocument).getLength(); - } catch (BadLocationException ex) { - return; - } - int lineEndOffset= lineOffset + lineLength; - - int delta= lineEndOffset - st.getCharCount(); - if (delta > 0) { - lineEndOffset -= delta; - lineLength -= delta; - } - - String line= ""; //$NON-NLS-1$ - if (lineLength > 0) - line= st.getText(lineOffset, lineEndOffset - 1); - - // Remember current selection - Point oldSelection= st.getSelection(); - - // The new caret position - int newCaretOffset= -1; - - if (isSmartHomeEndEnabled) { - // Compute the line end offset - int i= getLineEndPosition(document, line, lineLength, caretOffsetInDocument); + try { + caretOffsetInDocument = widgetOffset2ModelOffset(fSourceViewer, caretOffset); + lineLength = document.getLineInformationOfOffset(caretOffsetInDocument).getLength(); + } catch (BadLocationException ex) { + return; + } + int lineEndOffset = lineOffset + lineLength; - if (caretOffset - lineOffset == i) - // to end of line - newCaretOffset= lineEndOffset; - else - // to end of text - newCaretOffset= lineOffset + i; + int delta = lineEndOffset - st.getCharCount(); + if (delta > 0) { + lineEndOffset -= delta; + lineLength -= delta; + } + String line = ""; //$NON-NLS-1$ + if (lineLength > 0) + line = st.getText(lineOffset, lineEndOffset - 1); + + // Remember current selection + Point oldSelection = new Point(offset, offset + length); + + // The new caret position + int newCaretOffset = -1; + + if (isSmartHomeEndEnabled) { + // Compute the line end offset + int i = getLineEndPosition(document, line, lineLength, caretOffsetInDocument); + newCaretOffset = (caretOffset - lineOffset == i) ? lineEndOffset : lineOffset + i; + } else if (caretOffset < lineEndOffset) { + // to end of line + newCaretOffset = lineEndOffset; + } - } else { + if (newCaretOffset == -1) { + newCaretOffset = caretOffset; + } - if (caretOffset < lineEndOffset) - // to end of line - newCaretOffset= lineEndOffset; + newSelection.add(new Point( + fDoSelect ? (caretOffset < oldSelection.y ? oldSelection.y : oldSelection.x) : newCaretOffset, + newCaretOffset)); } - - if (newCaretOffset == -1) - newCaretOffset= caretOffset; - else - st.setCaretOffset(newCaretOffset); - - st.setCaretOffset(newCaretOffset); - if (fDoSelect) { - if (caretOffset < oldSelection.y) - st.setSelection(oldSelection.y, newCaretOffset); - else - st.setSelection(oldSelection.x, newCaretOffset); - } else - st.setSelection(newCaretOffset); - - fireSelectionChanged(oldSelection); + st.setSelectionRanges(newSelection.stream().flatMapToInt( + p -> caretAtBeginningOfSelection ? IntStream.of(p.y, p.x - p.y) : IntStream.of(p.x, p.y - p.x)) + .toArray()); + fireSelectionChanged(firstSelection); } } @@ -1326,70 +1321,60 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit StyledText st= fSourceViewer.getTextWidget(); if (st == null || st.isDisposed()) return; + boolean caretAtBeginningOfSelection = st.getCaretOffset() == st.getSelection().x; + Point firstSelection = st.getSelection(); + int[] ranges = st.getSelectionRanges(); + List<Point> newSelection = new ArrayList<>(ranges.length / 2); + for (int j = 0; j < ranges.length; j += 2) { + int offset = ranges[j]; + int length = ranges[j + 1]; + int caretOffset = caretAtBeginningOfSelection ? offset : offset + length; + int lineNumber = st.getLineAtOffset(caretOffset); + int lineOffset = st.getOffsetAtLine(lineNumber); + int lineLength; + int caretOffsetInDocument; + final IDocument document = fSourceViewer.getDocument(); - int caretOffset= st.getCaretOffset(); - int lineNumber= st.getLineAtOffset(caretOffset); - int lineOffset= st.getOffsetAtLine(lineNumber); - - int lineLength; - int caretOffsetInDocument; - final IDocument document= fSourceViewer.getDocument(); - - try { - caretOffsetInDocument= widgetOffset2ModelOffset(fSourceViewer, caretOffset); - lineLength= document.getLineInformationOfOffset(caretOffsetInDocument).getLength(); - } catch (BadLocationException ex) { - return; - } - - String line= ""; //$NON-NLS-1$ - if (lineLength > 0) { - int end= lineOffset + lineLength - 1; - end= Math.min(end, st.getCharCount() -1); - line= st.getText(lineOffset, end); - } - - // Remember current selection - Point oldSelection= st.getSelection(); + try { + caretOffsetInDocument = widgetOffset2ModelOffset(fSourceViewer, caretOffset); + lineLength = document.getLineInformationOfOffset(caretOffsetInDocument).getLength(); + } catch (BadLocationException ex) { + return; + } + int lineEndOffset = lineOffset + lineLength; - // The new caret position - int newCaretOffset= -1; + String line = ""; //$NON-NLS-1$ + if (lineLength > 0) { + int end = lineOffset + lineLength - 1; + end = Math.min(end, st.getCharCount() - 1); + line = st.getText(lineOffset, end); + } - if (isSmartHomeEndEnabled) { + // Remember current selection + Point oldSelection = new Point(offset, offset + length); - // Compute the line start offset - int index= getLineStartPosition(document, line, lineLength, caretOffsetInDocument); + // The new caret position + int newCaretOffset = -1; - if (caretOffset - lineOffset == index) + if (isSmartHomeEndEnabled) { + // Compute the line start offset + int index = getLineStartPosition(document, line, lineLength, caretOffsetInDocument); + newCaretOffset = (caretOffset - lineOffset == index) ? lineOffset : lineOffset + index; + } else if (caretOffset > lineOffset) { // to beginning of line - newCaretOffset= lineOffset; - else - // to beginning of text - newCaretOffset= lineOffset + index; - - } else { + newCaretOffset = lineOffset; + } - if (caretOffset > lineOffset) - // to beginning of line - newCaretOffset= lineOffset; + newSelection.add(new Point( + fDoSelect ? (caretOffset < oldSelection.y ? oldSelection.y : oldSelection.x) : newCaretOffset, + newCaretOffset)); } + st.setSelectionRanges(newSelection.stream().flatMapToInt( + p -> caretAtBeginningOfSelection ? IntStream.of(p.y, p.x - p.y) : IntStream.of(p.x, p.y - p.x)) + .toArray()); - if (newCaretOffset == -1) - newCaretOffset= caretOffset; - else - st.setCaretOffset(newCaretOffset); - - if (fDoSelect) { - if (caretOffset < oldSelection.y) - st.setSelection(oldSelection.y, newCaretOffset); - else - st.setSelection(oldSelection.x, newCaretOffset); - } else - st.setSelection(newCaretOffset); - - fireSelectionChanged(oldSelection); + fireSelectionChanged(firstSelection); } - } /** @@ -2944,7 +2929,9 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit * @since 2.1 */ protected void doSetSelection(ISelection selection) { - if (selection instanceof ITextSelection) { + if (selection instanceof IMultiTextSelection && ((IMultiTextSelection) selection).getRegions().length > 1) { + getSourceViewer().getSelectionProvider().setSelection(selection); + } else if (selection instanceof ITextSelection) { ITextSelection textSelection= (ITextSelection) selection; selectAndReveal(textSelection.getOffset(), textSelection.getLength()); } |