Skip to content

Commit 0bf3af9

Browse files
Maximilian Wittmermickaelistria
authored andcommitted
Restore editor MultiSelection on restart
Stores the multi-selection for the text editor in the memento; and restores it on restart. Fixes #1074
1 parent 0a13daa commit 0bf3af9

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

bundles/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/AbstractTextEditor.java

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Arrays;
3030
import java.util.HashMap;
3131
import java.util.Iterator;
32+
import java.util.LinkedList;
3233
import java.util.List;
3334
import java.util.Map;
3435
import java.util.ResourceBundle;
@@ -154,6 +155,7 @@
154155
import org.eclipse.jface.text.ITextViewerExtension8.EnrichMode;
155156
import org.eclipse.jface.text.IUndoManager;
156157
import org.eclipse.jface.text.IUndoManagerExtension;
158+
import org.eclipse.jface.text.MultiTextSelection;
157159
import org.eclipse.jface.text.Position;
158160
import org.eclipse.jface.text.Region;
159161
import org.eclipse.jface.text.TabsToSpacesConverter;
@@ -291,7 +293,8 @@ public abstract class AbstractTextEditor extends EditorPart implements ITextEdit
291293
protected static final String TAG_SELECTION_OFFSET= "selectionOffset"; //$NON-NLS-1$
292294

293295
/**
294-
* Tag used in the {@link IMemento} when saving and restoring the editor's selection length.
296+
* Tag used in the {@link IMemento} when saving and restoring the editor's
297+
* selection length.
295298
*
296299
* @see #saveState(IMemento)
297300
* @see #restoreState(IMemento)
@@ -7049,9 +7052,15 @@ public void restoreState(IMemento memento) {
70497052
@Override
70507053
public void saveState(IMemento memento) {
70517054
ISelection selection= doGetSelection();
7052-
if (selection instanceof ITextSelection) {
7053-
memento.putInteger(TAG_SELECTION_OFFSET, ((ITextSelection)selection).getOffset());
7054-
memento.putInteger(TAG_SELECTION_LENGTH, ((ITextSelection)selection).getLength());
7055+
if (selection instanceof IMultiTextSelection multiSelect && multiSelect.getRegions().length > 1) {
7056+
for (int i = 0; i < multiSelect.getRegions().length; i++) {
7057+
memento.putInteger(TAG_SELECTION_OFFSET + i, multiSelect.getRegions()[i].getOffset());
7058+
memento.putInteger(TAG_SELECTION_LENGTH + i, multiSelect.getRegions()[i].getLength());
7059+
}
7060+
7061+
} else if (selection instanceof ITextSelection singleSelect) {
7062+
memento.putInteger(TAG_SELECTION_OFFSET, singleSelect.getOffset());
7063+
memento.putInteger(TAG_SELECTION_LENGTH, singleSelect.getLength());
70557064
}
70567065
final StyledText textWidget= fSourceViewer.getTextWidget();
70577066
memento.putInteger(TAG_SELECTION_TOP_PIXEL, textWidget.getTopPixel());
@@ -7068,6 +7077,9 @@ public void saveState(IMemento memento) {
70687077
* @since 3.3
70697078
*/
70707079
protected boolean containsSavedState(IMemento memento) {
7080+
if (memento.getInteger(TAG_SELECTION_OFFSET + "0") != null) { //$NON-NLS-1$
7081+
return true;
7082+
}
70717083
return memento.getInteger(TAG_SELECTION_OFFSET) != null && memento.getInteger(TAG_SELECTION_LENGTH) != null;
70727084
}
70737085

@@ -7080,15 +7092,11 @@ protected boolean containsSavedState(IMemento memento) {
70807092
* @since 3.3
70817093
*/
70827094
protected void doRestoreState(IMemento memento) {
7083-
Integer offset= memento.getInteger(TAG_SELECTION_OFFSET);
7084-
if (offset == null)
7085-
return;
7086-
7087-
Integer length= memento.getInteger(TAG_SELECTION_LENGTH);
7088-
if (length == null)
7095+
ISelection savedSelection = readSelectionFromMemento(memento);
7096+
if (savedSelection == null) {
70897097
return;
7090-
7091-
doSetSelection(new TextSelection(offset.intValue(), length.intValue()));
7098+
}
7099+
doSetSelection(savedSelection);
70927100

70937101
final StyledText textWidget= fSourceViewer.getTextWidget();
70947102

@@ -7101,6 +7109,40 @@ protected void doRestoreState(IMemento memento) {
71017109
textWidget.setHorizontalPixel(horizontalPixel.intValue());
71027110
}
71037111

7112+
@SuppressWarnings("boxing")
7113+
private ISelection readSelectionFromMemento(IMemento memento) {
7114+
if (memento.getInteger(TAG_SELECTION_OFFSET + "0") != null) { //$NON-NLS-1$
7115+
LinkedList<IRegion> regions = new LinkedList<>();
7116+
int regionCounter = 0;
7117+
while (memento.getInteger(TAG_SELECTION_OFFSET + regionCounter) != null) {
7118+
Integer offset = memento.getInteger(TAG_SELECTION_OFFSET + regionCounter);
7119+
if (offset == null) {
7120+
continue;
7121+
}
7122+
7123+
Integer length = memento.getInteger(TAG_SELECTION_LENGTH + regionCounter);
7124+
if (length == null) {
7125+
continue;
7126+
}
7127+
regions.add(new Region(offset, length));
7128+
regionCounter++;
7129+
}
7130+
return new MultiTextSelection(getDocumentProvider().getDocument(getEditorInput()),
7131+
regions.toArray(new IRegion[0]));
7132+
} else {
7133+
Integer offset = memento.getInteger(TAG_SELECTION_OFFSET);
7134+
if (offset == null) {
7135+
return null;
7136+
}
7137+
7138+
Integer length = memento.getInteger(TAG_SELECTION_LENGTH);
7139+
if (length == null) {
7140+
return null;
7141+
}
7142+
return new TextSelection(offset, length);
7143+
}
7144+
}
7145+
71047146
@Override
71057147
public Saveable[] getSaveables() {
71067148
if (fSavable == null)

0 commit comments

Comments
 (0)