~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor/CaretMoveActions.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
                        }
49
49
                        
50
50
                        LineSegment line = data.Document.GetLine (data.Caret.Line);
51
 
                        IEnumerable<FoldSegment> foldings = data.Document.GetEndFoldings (line);
 
51
                        IEnumerable<FoldSegment > foldings = data.Document.GetEndFoldings (line);
52
52
                        FoldSegment segment = null;
53
53
                        foreach (FoldSegment folding in foldings) {
54
 
                                if (folding.IsFolded && folding.EndColumn == data.Caret.Column) {
 
54
                                if (folding.IsFolded && folding.EndColumn + 1 == data.Caret.Column) {
55
55
                                        segment = folding;
56
56
                                        break;
57
57
                                }
58
58
                        }
59
59
                        if (segment != null) {
60
 
                                data.Caret.Location = data.Document.OffsetToLocation (segment.StartLine.Offset + segment.Column); 
 
60
                                data.Caret.Location = data.Document.OffsetToLocation (segment.StartLine.Offset + segment.Column - 1); 
61
61
                                return;
62
62
                        }
63
63
                        
64
 
                        if (data.Caret.Column > 0) {
65
 
                                if (data.Caret.Column > line.EditableLength) {
66
 
                                        data.Caret.Column = line.EditableLength;
 
64
                        if (data.Caret.Column > DocumentLocation.MinColumn) {
 
65
                                if (data.Caret.Column > line.EditableLength + 1) {
 
66
                                        data.Caret.Column = line.EditableLength + 1;
67
67
                                } else {
68
68
                                        data.Caret.Column--;
69
69
                                }
70
 
                        } else if (data.Caret.Line > 0) {
 
70
                        } else if (data.Caret.Line > DocumentLocation.MinLine) {
71
71
                                LineSegment prevLine = data.Document.GetLine (data.Caret.Line - 1);
72
 
                                data.Caret.Location = new DocumentLocation (data.Caret.Line - 1, prevLine.EditableLength);
 
72
                                data.Caret.Location = new DocumentLocation (data.Caret.Line - 1, prevLine.EditableLength + 1);
73
73
                        }
74
74
                }
75
75
                
92
92
                        }
93
93
                        
94
94
                        LineSegment line = data.Document.GetLine (data.Caret.Line);
95
 
                        IEnumerable<FoldSegment> foldings = data.Document.GetStartFoldings (line);
 
95
                        IEnumerable<FoldSegment > foldings = data.Document.GetStartFoldings (line);
96
96
                        FoldSegment segment = null;
97
97
                        foreach (FoldSegment folding in foldings) {
98
98
                                if (folding.IsFolded && folding.Column == data.Caret.Column) {
104
104
                                data.Caret.Location = data.Document.OffsetToLocation (segment.EndLine.Offset + segment.EndColumn); 
105
105
                                return;
106
106
                        }
107
 
                        if (data.Caret.Column < line.EditableLength || data.Caret.AllowCaretBehindLineEnd) {
108
 
                                if (data.Caret.Column >= line.EditableLength) {
 
107
                        if (data.Caret.Column < line.EditableLength + 1 || data.Caret.AllowCaretBehindLineEnd) {
 
108
                                if (data.Caret.Column >= line.EditableLength + 1) {
109
109
                                        int nextColumn = data.GetNextVirtualColumn (data.Caret.Line, data.Caret.Column);
110
110
                                        if (data.Caret.Column != nextColumn) {
111
111
                                                data.Caret.Column = nextColumn;
112
112
                                        } else {
113
 
                                                data.Caret.Location = new DocumentLocation (data.Caret.Line + 1, 0);
 
113
                                                data.Caret.Location = new DocumentLocation (data.Caret.Line + 1, DocumentLocation.MinColumn);
114
114
                                                data.Caret.CheckCaretPosition ();
115
115
                                        }
116
116
                                } else {
117
117
                                        data.Caret.Column++;
118
118
                                }
119
 
                        } else if (data.Caret.Line + 1 < data.Document.LineCount) {
120
 
                                data.Caret.Location = new DocumentLocation (data.Caret.Line + 1, 0);
 
119
                        } else if (data.Caret.Line + 1 <= data.Document.LineCount) {
 
120
                                data.Caret.Location = new DocumentLocation (data.Caret.Line + 1, DocumentLocation.MinColumn);
121
121
                        }
122
122
                }
123
123
                
140
140
                                int col = data.MainSelection.Anchor > data.MainSelection.Lead ? data.MainSelection.Lead.Column : data.MainSelection.Anchor.Column;
141
141
                                int line = data.MainSelection.MinLine - 1;
142
142
                                data.ClearSelection ();
143
 
                                data.Caret.Location = (line >= 0) ? new DocumentLocation (line, col) : new DocumentLocation (0, 0);
 
143
                                data.Caret.Location = (line >=  DocumentLocation.MinLine) ? new DocumentLocation (line, col) : new DocumentLocation (DocumentLocation.MinLine, DocumentLocation.MinColumn);
144
144
                                data.Caret.SetToDesiredColumn (desiredColumn);
145
145
                                return;
146
146
                        }
147
147
                        
148
 
                        if (data.Caret.Line > 0) {
149
 
                                int line = data.Document.VisualToLogicalLine (data.Document.LogicalToVisualLine (data.Caret.Line) - 1);
 
148
                        if (data.Caret.Line > DocumentLocation.MinLine) {
 
149
                                int visualLine = data.Document.LogicalToVisualLine (data.Caret.Line);
 
150
                                int line = data.Document.VisualToLogicalLine (visualLine - 1);
150
151
                                int offset = data.Document.LocationToOffset (line, data.Caret.Column);
151
152
                                data.Caret.Offset = MoveCaretOutOfFolding (data, offset);
152
153
                                data.Caret.SetToDesiredColumn (desiredColumn);
174
175
                                int col = data.MainSelection.Anchor > data.MainSelection.Lead ? data.MainSelection.Lead.Column : data.MainSelection.Anchor.Column;
175
176
                                int line = data.MainSelection.MaxLine + 1;
176
177
                                data.ClearSelection ();
177
 
                                if (line < data.Document.LineCount) {
 
178
                                if (line <= data.Document.LineCount) {
178
179
                                        int offset = data.Document.LocationToOffset (line, col);
179
180
                                        data.Caret.SetToOffsetWithDesiredColumn (MoveCaretOutOfFolding (data, offset));
180
181
                                } else {
183
184
                                return;
184
185
                        }
185
186
                        
186
 
                        if (data.Caret.Line < data.Document.LineCount - 1) {
187
 
                                int line = data.Document.VisualToLogicalLine (data.Document.LogicalToVisualLine (data.Caret.Line) + 1);
 
187
                        if (data.Caret.Line < data.Document.LineCount) {
 
188
                                int nextLine = data.Document.LogicalToVisualLine (data.Caret.Line) + 1;
 
189
                                int line = data.Document.VisualToLogicalLine (nextLine);
188
190
                                int offset = data.Document.LocationToOffset (line, data.Caret.Column);
189
191
                                data.Caret.SetToOffsetWithDesiredColumn (MoveCaretOutOfFolding (data, offset));
190
192
                        } else {
197
199
                        int result;
198
200
                        for (result = 0; result < line.EditableLength; result++)
199
201
                                if (!Char.IsWhiteSpace (document.GetCharAt (line.Offset + result)))
200
 
                                        return result;
201
 
                        return result;
 
202
                                        return result + 1;
 
203
                        return result + 1;
202
204
                }
203
205
                
204
206
                static void InternalCaretMoveHome (TextEditorData data, bool firstNonWhitespace, bool hop)
205
207
                {
206
208
                        if (!data.Caret.PreserveSelection)
207
209
                                data.ClearSelection ();
208
 
                        DocumentLocation newLocation = data.Caret.Location;
 
210
                        DocumentLocation newLocation = data.Caret.Location;
 
211
 
209
212
                        LineSegment line = data.Document.GetLine (data.Caret.Line);
210
213
                        
211
 
                        if (firstNonWhitespace) {
 
214
                        if (firstNonWhitespace) {
 
215
 
212
216
                                int homeMark = GetHomeMark (data.Document, line);
213
217
                                if (hop) {
214
 
                                        newLocation.Column = data.Caret.Column == homeMark ? 0 : homeMark;
 
218
                                        newLocation.Column = data.Caret.Column == homeMark ? DocumentLocation.MinColumn : homeMark;
215
219
                                } else {
216
220
                                        newLocation.Column = homeMark;
217
221
                                }
218
222
                        } else {
219
 
                                newLocation.Column = 0;
 
223
                                newLocation.Column = DocumentLocation.MinColumn;
220
224
                        }
221
225
                        
222
226
                        // handle folding
230
234
                        }
231
235
                        if (segment != null) 
232
236
                                newLocation = data.Document.OffsetToLocation (segment.StartLine.Offset); 
233
 
                        
234
 
                        if (newLocation != data.Caret.Location) 
235
 
                                data.Caret.Location = newLocation;
 
237
                        
 
238
 
 
239
                        if (newLocation != data.Caret.Location) 
 
240
 
 
241
                                data.Caret.Location = newLocation;
 
242
 
236
243
                }
237
244
                
238
245
                public static void LineHome (TextEditorData data)
255
262
                        if (!data.Caret.PreserveSelection)
256
263
                                data.ClearSelection ();
257
264
                        DocumentLocation newLocation = data.Caret.Location;
258
 
                        LineSegment line = data.Document.GetLine (data.Caret.Line);
259
 
                        newLocation.Column = line.EditableLength;
 
265
                        LineSegment line = data.Document.GetLine (data.Caret.Line);
 
266
 
 
267
                        newLocation.Column = line.EditableLength + 1;
260
268
                        
261
269
                        // handle folding
262
270
                        IEnumerable<FoldSegment> foldings = data.Document.GetStartFoldings (line);
269
277
                        }
270
278
                        if (segment != null) 
271
279
                                newLocation = data.Document.OffsetToLocation (segment.EndLine.Offset + segment.EndColumn); 
272
 
                        if (newLocation != data.Caret.Location) {
 
280
                        if (newLocation != data.Caret.Location)
273
281
                                data.Caret.Location = newLocation;
274
 
                        }
275
282
                        
276
283
                        if (data.Caret.AllowCaretBehindLineEnd) {
277
284
                                int nextColumn = data.GetNextVirtualColumn (data.Caret.Line, data.Caret.Column);
278
285
                                if (nextColumn != data.Caret.Column)
279
286
                                        data.Caret.Column = nextColumn;
280
287
                        }
281
 
                        
 
288
                        
282
289
                }
283
290
                
284
291
                public static void ToDocumentStart (TextEditorData data)
285
292
                {
286
293
                        if (!data.Caret.PreserveSelection)
287
294
                                data.ClearSelection ();
288
 
                        data.Caret.Location = new DocumentLocation (0, 0);
 
295
                        data.Caret.Location = new DocumentLocation (DocumentLocation.MinLine, DocumentLocation.MinColumn);
289
296
                }
290
297
                
291
298
                public static void ToDocumentEnd (TextEditorData data)
295
302
                        data.Caret.Offset = data.Document.Length;
296
303
                }
297
304
                
298
 
                public static int LineHeight { get; set; }
 
305
                public static double LineHeight { get; set; }
299
306
                
300
307
                public static void PageUp (TextEditorData data)
301
308
                {
302
309
                        int pageLines = (int)((data.VAdjustment.PageSize + ((int)data.VAdjustment.Value % LineHeight)) / LineHeight);
303
310
                        int visualLine = data.Document.LogicalToVisualLine (data.Caret.Line);
304
311
                        visualLine -= pageLines;
305
 
                        int line = System.Math.Max (data.Document.VisualToLogicalLine (visualLine), 0);
 
312
                        int line = System.Math.Max (data.Document.VisualToLogicalLine (visualLine), DocumentLocation.MinLine);
306
313
                        int offset = data.Document.LocationToOffset (line, data.Caret.Column);
307
314
                        ScrollActions.PageUp (data);
308
315
                        data.Caret.Offset = MoveCaretOutOfFolding (data, offset);
314
321
                        int visualLine = data.Document.LogicalToVisualLine (data.Caret.Line);
315
322
                        visualLine += pageLines;
316
323
                        
317
 
                        int line = System.Math.Min (data.Document.VisualToLogicalLine (visualLine), data.Document.LineCount - 1);
 
324
                        int line = System.Math.Min (data.Document.VisualToLogicalLine (visualLine), data.Document.LineCount);
318
325
                        int offset = data.Document.LocationToOffset (line, data.Caret.Column);
319
326
                        ScrollActions.PageDown (data);
320
327
                        data.Caret.Offset = MoveCaretOutOfFolding (data, offset);