Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/DocumentChangeEventArgs.cs @ 18242

Last change on this file since 18242 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.4 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 ICSharpCode.AvalonEdit.Document;
21using ICSharpCode.NRefactory.Editor;
22
23namespace ICSharpCode.AvalonEdit.Document
24{
25  /// <summary>
26  /// Describes a change of the document text.
27  /// This class is thread-safe.
28  /// </summary>
29  [Serializable]
30  public class DocumentChangeEventArgs : TextChangeEventArgs
31  {
32    volatile OffsetChangeMap offsetChangeMap;
33   
34    /// <summary>
35    /// Gets the OffsetChangeMap associated with this document change.
36    /// </summary>
37    /// <remarks>The OffsetChangeMap instance is guaranteed to be frozen and thus thread-safe.</remarks>
38    public OffsetChangeMap OffsetChangeMap {
39      get {
40        OffsetChangeMap map = offsetChangeMap;
41        if (map == null) {
42          // create OffsetChangeMap on demand
43          map = OffsetChangeMap.FromSingleElement(CreateSingleChangeMapEntry());
44          offsetChangeMap = map;
45        }
46        return map;
47      }
48    }
49   
50    internal OffsetChangeMapEntry CreateSingleChangeMapEntry()
51    {
52      return new OffsetChangeMapEntry(this.Offset, this.RemovalLength, this.InsertionLength);
53    }
54   
55    /// <summary>
56    /// Gets the OffsetChangeMap, or null if the default offset map (=single replacement) is being used.
57    /// </summary>
58    internal OffsetChangeMap OffsetChangeMapOrNull {
59      get {
60        return offsetChangeMap;
61      }
62    }
63   
64    /// <summary>
65    /// Gets the new offset where the specified offset moves after this document change.
66    /// </summary>
67    public override int GetNewOffset(int offset, AnchorMovementType movementType = AnchorMovementType.Default)
68    {
69      if (offsetChangeMap != null)
70        return offsetChangeMap.GetNewOffset(offset, movementType);
71      else
72        return CreateSingleChangeMapEntry().GetNewOffset(offset, movementType);
73    }
74   
75    /// <summary>
76    /// Creates a new DocumentChangeEventArgs object.
77    /// </summary>
78    public DocumentChangeEventArgs(int offset, string removedText, string insertedText)
79      : this(offset, removedText, insertedText, null)
80    {
81    }
82   
83    /// <summary>
84    /// Creates a new DocumentChangeEventArgs object.
85    /// </summary>
86    public DocumentChangeEventArgs(int offset, string removedText, string insertedText, OffsetChangeMap offsetChangeMap)
87      : base(offset, removedText, insertedText)
88    {
89      SetOffsetChangeMap(offsetChangeMap);
90    }
91   
92    /// <summary>
93    /// Creates a new DocumentChangeEventArgs object.
94    /// </summary>
95    public DocumentChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText, OffsetChangeMap offsetChangeMap)
96      : base(offset, removedText, insertedText)
97    {
98      SetOffsetChangeMap(offsetChangeMap);
99    }
100   
101    void SetOffsetChangeMap(OffsetChangeMap offsetChangeMap)
102    {
103      if (offsetChangeMap != null) {
104        if (!offsetChangeMap.IsFrozen)
105          throw new ArgumentException("The OffsetChangeMap must be frozen before it can be used in DocumentChangeEventArgs");
106        if (!offsetChangeMap.IsValidForDocumentChange(this.Offset, this.RemovalLength, this.InsertionLength))
107          throw new ArgumentException("OffsetChangeMap is not valid for this document change", "offsetChangeMap");
108        this.offsetChangeMap = offsetChangeMap;
109      }
110    }
111   
112    /// <inheritdoc/>
113    public override TextChangeEventArgs Invert()
114    {
115      OffsetChangeMap map = this.OffsetChangeMapOrNull;
116      if (map != null) {
117        map = map.Invert();
118        map.Freeze();
119      }
120      return new DocumentChangeEventArgs(this.Offset, this.InsertedText, this.RemovedText, map);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.