Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Editing/SimpleSelection.cs

Last change on this file was 11743, checked in by jkarder, 10 years ago

#2077: improved line moving

File size: 5.7 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using ICSharpCode.AvalonEdit.Document;
22using ICSharpCode.AvalonEdit.Utils;
23#if NREFACTORY
24using ICSharpCode.NRefactory.Editor;
25#endif
26
27namespace ICSharpCode.AvalonEdit.Editing
28{
29  /// <summary>
30  /// A simple selection.
31  /// </summary>
32  public sealed class SimpleSelection : Selection
33  {
34    readonly TextViewPosition start, end;
35    readonly int startOffset, endOffset;
36   
37    /// <summary>
38    /// Creates a new SimpleSelection instance.
39    /// </summary>
40    public SimpleSelection(TextArea textArea, TextViewPosition start, TextViewPosition end)
41      : base(textArea)
42    {
43      this.start = start;
44      this.end = end;
45      this.startOffset = textArea.Document.GetOffset(start.Location);
46      this.endOffset = textArea.Document.GetOffset(end.Location);
47    }
48   
49    /// <inheritdoc/>
50    public override IEnumerable<SelectionSegment> Segments {
51      get {
52        return ExtensionMethods.Sequence<SelectionSegment>(new SelectionSegment(startOffset, start.VisualColumn, endOffset, end.VisualColumn));
53      }
54    }
55   
56    /// <inheritdoc/>
57    public override ISegment SurroundingSegment {
58      get {
59        return new SelectionSegment(startOffset, endOffset);
60      }
61    }
62   
63    /// <inheritdoc/>
64    public override void ReplaceSelectionWithText(string newText)
65    {
66      if (newText == null)
67        throw new ArgumentNullException("newText");
68      using (textArea.Document.RunUpdate()) {
69        ISegment[] segmentsToDelete = textArea.GetDeletableSegments(this.SurroundingSegment);
70        for (int i = segmentsToDelete.Length - 1; i >= 0; i--) {
71          if (i == segmentsToDelete.Length - 1) {
72            if (segmentsToDelete[i].Offset == SurroundingSegment.Offset && segmentsToDelete[i].Length == SurroundingSegment.Length) {
73              newText = AddSpacesIfRequired(newText, start, end);
74            }
75            if (string.IsNullOrEmpty(newText)) {
76              // place caret at the beginning of the selection
77              if (start.CompareTo(end) <= 0)
78                textArea.Caret.Position = start;
79              else
80                textArea.Caret.Position = end;
81            } else {
82              // place caret so that it ends up behind the new text
83              textArea.Caret.Offset = segmentsToDelete[i].EndOffset;
84            }
85            textArea.Document.Replace(segmentsToDelete[i], newText);
86          } else {
87            textArea.Document.Remove(segmentsToDelete[i]);
88          }
89        }
90        if (segmentsToDelete.Length != 0) {
91          textArea.ClearSelection();
92        }
93      }
94    }
95   
96    public override TextViewPosition StartPosition {
97      get { return start; }
98    }
99   
100    public override TextViewPosition EndPosition {
101      get { return end; }
102    }
103   
104    /// <inheritdoc/>
105    public override Selection UpdateOnDocumentChange(DocumentChangeEventArgs e)
106    {
107      if (e == null)
108        throw new ArgumentNullException("e");
109      int newStartOffset, newEndOffset;
110      if (startOffset <= endOffset) {
111        newStartOffset = e.GetNewOffset(startOffset, AnchorMovementType.Default);
112        newEndOffset = Math.Max(newStartOffset, e.GetNewOffset(endOffset, AnchorMovementType.BeforeInsertion));
113      } else {
114        newEndOffset = e.GetNewOffset(endOffset, AnchorMovementType.Default);
115        newStartOffset = Math.Max(newEndOffset, e.GetNewOffset(startOffset, AnchorMovementType.BeforeInsertion));
116      }
117      return Selection.Create(
118        textArea,
119        new TextViewPosition(textArea.Document.GetLocation(newStartOffset), start.VisualColumn),
120        new TextViewPosition(textArea.Document.GetLocation(newEndOffset), end.VisualColumn)
121      );
122    }
123   
124    /// <inheritdoc/>
125    public override bool IsEmpty {
126      get { return startOffset == endOffset && start.VisualColumn == end.VisualColumn; }
127    }
128   
129    /// <inheritdoc/>
130    public override int Length {
131      get {
132        return Math.Abs(endOffset - startOffset);
133      }
134    }
135   
136    /// <inheritdoc/>
137    public override Selection SetEndpoint(TextViewPosition endPosition)
138    {
139      return Create(textArea, start, endPosition);
140    }
141   
142    public override Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition)
143    {
144      var document = textArea.Document;
145      if (document == null)
146        throw ThrowUtil.NoDocumentAssigned();
147      return Create(textArea, start, endPosition);
148    }
149   
150    /// <inheritdoc/>
151    public override int GetHashCode()
152    {
153      unchecked {
154        return startOffset * 27811 + endOffset + textArea.GetHashCode();
155      }
156    }
157   
158    /// <inheritdoc/>
159    public override bool Equals(object obj)
160    {
161      SimpleSelection other = obj as SimpleSelection;
162      if (other == null) return false;
163      return this.start.Equals(other.start) && this.end.Equals(other.end)
164        && this.startOffset == other.startOffset && this.endOffset == other.endOffset
165        && this.textArea == other.textArea;
166    }
167   
168    /// <inheritdoc/>
169    public override string ToString()
170    {
171      return "[SimpleSelection Start=" + start + " End=" + end + "]";
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.