aboutsummaryrefslogtreecommitdiff
path: root/src/scintilla_backports/6458_0a8a766722c0.patch
blob: 64bcb7480095e159d3c8309191be9bc59b42c071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# HG changeset patch
# User Mitchell
# Date 1519447387 -39600
# Node ID 0a8a766722c0bc91e1e2ecd958927a504cea5cac
# Parent  26e7749ba67ae8fc994e2f17c760a65c5d0ca479
Fix move-extends-selection mode for rectangular and line selections.

diff -r 26e7749ba67a -r 0a8a766722c0 doc/ScintillaHistory.html
--- a/doc/ScintillaHistory.html	Thu Feb 22 08:13:37 2018 +1100
+++ b/doc/ScintillaHistory.html	Sat Feb 24 15:43:07 2018 +1100
@@ -548,6 +548,9 @@
 	<a href="http://sourceforge.net/p/scintilla/bugs/1993/">Bug #1993</a>.
 	</li>
 	<li>
+	Fix move-extends-selection mode for rectangular and line selections.
+	</li>
+	<li>
 	SciTE on Windows can execute Python scripts directly by name when on path.
 	<a href="http://sourceforge.net/p/scintilla/feature-requests/1209/">Feature #1209.</a>
 	</li>
diff -r 26e7749ba67a -r 0a8a766722c0 src/Editor.cxx
--- a/src/Editor.cxx	Thu Feb 22 08:13:37 2018 +1100
+++ b/src/Editor.cxx	Sat Feb 24 15:43:07 2018 +1100
@@ -626,26 +626,31 @@
 	InvalidateSelection(sel.RangeMain(), true);
 }
 
+/* For Line selection - the anchor and caret are always
+   at the beginning and end of the region lines. */
+SelectionRange Editor::LineSelectionRange(SelectionPosition currentPos_, SelectionPosition anchor_) const {
+	if (currentPos_ > anchor_) {
+		anchor_ = SelectionPosition(static_cast<Sci::Position>(
+			pdoc->LineStart(pdoc->LineFromPosition(anchor_.Position()))));
+		currentPos_ = SelectionPosition(static_cast<Sci::Position>(
+			pdoc->LineEnd(pdoc->LineFromPosition(currentPos_.Position()))));
+	} else {
+		currentPos_ = SelectionPosition(static_cast<Sci::Position>(
+			pdoc->LineStart(pdoc->LineFromPosition(currentPos_.Position()))));
+		anchor_ = SelectionPosition(static_cast<Sci::Position>(
+			pdoc->LineEnd(pdoc->LineFromPosition(anchor_.Position()))));
+	}
+	return SelectionRange(currentPos_, anchor_);
+}
+
 void Editor::SetSelection(SelectionPosition currentPos_, SelectionPosition anchor_) {
 	currentPos_ = ClampPositionIntoDocument(currentPos_);
 	anchor_ = ClampPositionIntoDocument(anchor_);
 	Sci::Line currentLine = static_cast<Sci::Line>(pdoc->LineFromPosition(currentPos_.Position()));
-	/* For Line selection - ensure the anchor and caret are always
-	   at the beginning and end of the region lines. */
+	SelectionRange rangeNew(currentPos_, anchor_);
 	if (sel.selType == Selection::selLines) {
-		if (currentPos_ > anchor_) {
-			anchor_ = SelectionPosition(static_cast<Sci::Position>(
-				pdoc->LineStart(pdoc->LineFromPosition(anchor_.Position()))));
-			currentPos_ = SelectionPosition(static_cast<Sci::Position>(
-				pdoc->LineEnd(pdoc->LineFromPosition(currentPos_.Position()))));
-		} else {
-			currentPos_ = SelectionPosition(static_cast<Sci::Position>(
-				pdoc->LineStart(pdoc->LineFromPosition(currentPos_.Position()))));
-			anchor_ = SelectionPosition(static_cast<Sci::Position>(
-				pdoc->LineEnd(pdoc->LineFromPosition(anchor_.Position()))));
-		}
-	}
-	SelectionRange rangeNew(currentPos_, anchor_);
+		rangeNew = LineSelectionRange(currentPos_, anchor_);
+	}
 	if (sel.Count() > 1 || !(sel.RangeMain() == rangeNew)) {
 		InvalidateSelection(rangeNew);
 	}
@@ -675,6 +680,8 @@
 		sel.Rectangular() =
 			SelectionRange(SelectionPosition(currentPos_), sel.Rectangular().anchor);
 		SetRectangularRange();
+	} else if (sel.selType == Selection::selLines) {
+		sel.RangeMain() = LineSelectionRange(currentPos_, sel.RangeMain().anchor);
 	} else {
 		sel.RangeMain() =
 			SelectionRange(SelectionPosition(currentPos_), sel.RangeMain().anchor);
@@ -3120,7 +3127,7 @@
 
 void Editor::CursorUpOrDown(int direction, Selection::selTypes selt) {
 	if ((selt == Selection::noSel) && sel.MoveExtends()) {
-		selt = Selection::selStream;
+		selt = !sel.IsRectangular() ? Selection::selStream : Selection::selRectangle;
 	}
 	SelectionPosition caretToUse = sel.Range(sel.Main()).caret;
 	if (sel.IsRectangular()) {
@@ -3142,6 +3149,11 @@
 		sel.Rectangular() = SelectionRange(posNew, rangeBase.anchor);
 		SetRectangularRange();
 		MovedCaret(posNew, caretToUse, true);
+	} else if (sel.selType == Selection::selLines && sel.MoveExtends()) {
+		// Calculate new caret position and call SetSelection(), which will ensure whole lines are selected.
+		const SelectionPosition posNew = MovePositionSoVisible(
+			PositionUpOrDown(caretToUse, direction, -1), direction);
+		SetSelection(posNew, sel.Range(sel.Main()).anchor);
 	} else {
 		InvalidateWholeSelection();
 		if (!additionalSelectionTyping || (sel.IsRectangular())) {
@@ -3263,7 +3275,7 @@
 	}
 }
 
-bool IsRectExtend(unsigned int iMessage) {
+bool IsRectExtend(unsigned int iMessage, bool isRectMoveExtends) {
 	switch (iMessage) {
 	case SCI_CHARLEFTRECTEXTEND:
 	case SCI_CHARRIGHTRECTEXTEND:
@@ -3272,6 +3284,19 @@
 	case SCI_LINEENDRECTEXTEND:
 		return true;
 	default:
+		if (isRectMoveExtends) {
+			// Handle SCI_SETSELECTIONMODE(SC_SEL_RECTANGLE) and subsequent movements.
+			switch (iMessage) {
+			case SCI_CHARLEFTEXTEND:
+			case SCI_CHARRIGHTEXTEND:
+			case SCI_HOMEEXTEND:
+			case SCI_VCHOMEEXTEND:
+			case SCI_LINEENDEXTEND:
+				return true;
+			default:
+				return false;
+			}
+		}
 		return false;
 	}
 }
@@ -3307,6 +3332,9 @@
 }
 
 int Editor::HorizontalMove(unsigned int iMessage) {
+	if (sel.selType == Selection::selLines) {
+		return 0; // horizontal moves with line selection have no effect
+	}
 	if (sel.MoveExtends()) {
 		iMessage = WithExtends(iMessage);
 	}
@@ -3319,7 +3347,7 @@
 	// Invalidate each of the current selections
 	InvalidateWholeSelection();
 
-	if (IsRectExtend(iMessage)) {
+	if (IsRectExtend(iMessage, sel.IsRectangular() && sel.MoveExtends())) {
 		const SelectionRange rangeBase = sel.IsRectangular() ? sel.Rectangular() : sel.RangeMain();
 		if (!sel.IsRectangular()) {
 			sel.DropAdditionalRanges();
@@ -3328,6 +3356,7 @@
 		SelectionPosition spCaret = rangeBase.caret;
 		switch (iMessage) {
 		case SCI_CHARLEFTRECTEXTEND:
+		case SCI_CHARLEFTEXTEND: // only when sel.IsRectangular() && sel.MoveExtends()
 			if (pdoc->IsLineEndPosition(spCaret.Position()) && spCaret.VirtualSpace()) {
 				spCaret.SetVirtualSpace(spCaret.VirtualSpace() - 1);
 			} else if ((virtualSpaceOptions & SCVS_NOWRAPLINESTART) == 0 || pdoc->GetColumn(spCaret.Position()) > 0) {
@@ -3335,6 +3364,7 @@
 			}
 			break;
 		case SCI_CHARRIGHTRECTEXTEND:
+		case SCI_CHARRIGHTEXTEND: // only when sel.IsRectangular() && sel.MoveExtends()
 			if ((virtualSpaceOptions & SCVS_RECTANGULARSELECTION) && pdoc->IsLineEndPosition(sel.MainCaret())) {
 				spCaret.SetVirtualSpace(spCaret.VirtualSpace() + 1);
 			} else {
@@ -3342,13 +3372,16 @@
 			}
 			break;
 		case SCI_HOMERECTEXTEND:
+		case SCI_HOMEEXTEND: // only when sel.IsRectangular() && sel.MoveExtends()
 			spCaret = SelectionPosition(
 				static_cast<Sci::Position>(pdoc->LineStart(pdoc->LineFromPosition(spCaret.Position()))));
 			break;
 		case SCI_VCHOMERECTEXTEND:
+		case SCI_VCHOMEEXTEND: // only when sel.IsRectangular() && sel.MoveExtends()
 			spCaret = SelectionPosition(pdoc->VCHomePosition(spCaret.Position()));
 			break;
 		case SCI_LINEENDRECTEXTEND:
+		case SCI_LINEENDEXTEND: // only when sel.IsRectangular() && sel.MoveExtends()
 			spCaret = SelectionPosition(pdoc->LineEndPosition(spCaret.Position()));
 			break;
 		}
@@ -7615,10 +7648,12 @@
 			case SC_SEL_RECTANGLE:
 				sel.SetMoveExtends(!sel.MoveExtends() || (sel.selType != Selection::selRectangle));
 				sel.selType = Selection::selRectangle;
+				sel.Rectangular() = sel.RangeMain(); // adjust current selection
 				break;
 			case SC_SEL_LINES:
 				sel.SetMoveExtends(!sel.MoveExtends() || (sel.selType != Selection::selLines));
 				sel.selType = Selection::selLines;
+				SetSelection(sel.RangeMain().caret, sel.RangeMain().anchor); // adjust current selection
 				break;
 			case SC_SEL_THIN:
 				sel.SetMoveExtends(!sel.MoveExtends() || (sel.selType != Selection::selThin));
diff -r 26e7749ba67a -r 0a8a766722c0 src/Editor.h
--- a/src/Editor.h	Thu Feb 22 08:13:37 2018 +1100
+++ b/src/Editor.h	Sat Feb 24 15:43:07 2018 +1100
@@ -312,6 +312,7 @@
 	void ThinRectangularRange();
 	void InvalidateSelection(SelectionRange newMain, bool invalidateWholeSelection=false);
 	void InvalidateWholeSelection();
+	SelectionRange LineSelectionRange(SelectionPosition currentPos_, SelectionPosition anchor_) const;
 	void SetSelection(SelectionPosition currentPos_, SelectionPosition anchor_);
 	void SetSelection(Sci::Position currentPos_, Sci::Position anchor_);
 	void SetSelection(SelectionPosition currentPos_);
diff -r 26e7749ba67a -r 0a8a766722c0 test/simpleTests.py
--- a/test/simpleTests.py	Thu Feb 22 08:13:37 2018 +1100
+++ b/test/simpleTests.py	Sat Feb 24 15:43:07 2018 +1100
@@ -1466,6 +1466,88 @@
 		self.ed.DropSelectionN(0)
 		self.assertEquals(self.ed.MainSelection, 2)
 
+class TestModalSelection(unittest.TestCase):
+
+	def setUp(self):
+		self.xite = Xite.xiteFrame
+		self.ed = self.xite.ed
+		self.ed.ClearAll()
+		self.ed.EmptyUndoBuffer()
+		# 3 lines of 3 characters
+		t = b"xxx\nxxx\nxxx"
+		self.ed.AddText(len(t), t)
+
+	def testCharacterSelection(self):
+		self.ed.SetSelection(1, 1)
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 1)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.SelectionMode = self.ed.SC_SEL_STREAM
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 1)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.CharRight()
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 2)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.LineDown()
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 6)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.ClearSelections()
+
+	def testRectangleSelection(self):
+		self.ed.SetSelection(1, 1)
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 1)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.SelectionMode = self.ed.SC_SEL_RECTANGLE
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 1)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.CharRight()
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 2)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.LineDown()
+		self.assertEquals(self.ed.Selections, 2)
+		self.assertEquals(self.ed.MainSelection, 1)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 2)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.assertEquals(self.ed.GetSelectionNCaret(1), 6)
+		self.assertEquals(self.ed.GetSelectionNAnchor(1), 5)
+		self.ed.ClearSelections()
+
+	def testLinesSelection(self):
+		self.ed.SetSelection(1, 1)
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 1)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 1)
+		self.ed.SelectionMode = self.ed.SC_SEL_LINES
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 0)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 3)
+		self.ed.CharRight()
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 0)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 3)
+		self.ed.LineDown()
+		self.assertEquals(self.ed.Selections, 1)
+		self.assertEquals(self.ed.MainSelection, 0)
+		self.assertEquals(self.ed.GetSelectionNCaret(0), 7)
+		self.assertEquals(self.ed.GetSelectionNAnchor(0), 0)
+		self.ed.ClearSelections()
+
 class TestStyleAttributes(unittest.TestCase):
 	""" These tests are just to ensure that the calls set and retrieve values.
 	They do not check the visual appearance of the style attributes.