Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Snippets/SnippetAnchorElement.cs @ 17495

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

#2077: created branch and added first version

File size: 3.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.Snippets
24{
25  /// <summary>
26  /// Creates a named anchor that can be accessed by other SnippetElements.
27  /// </summary>
28  public sealed class SnippetAnchorElement : SnippetElement
29  {
30    /// <summary>
31    /// Gets or sets the name of the anchor.
32    /// </summary>
33    public string Name { get; private set; }
34   
35    /// <summary>
36    /// Creates a SnippetAnchorElement with the supplied name.
37    /// </summary>
38    public SnippetAnchorElement(string name)
39    {
40      this.Name = name;
41    }
42   
43    /// <inheritdoc />
44    public override void Insert(InsertionContext context)
45    {
46      TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);
47      start.MovementType = AnchorMovementType.BeforeInsertion;
48      start.SurviveDeletion = true;
49      AnchorSegment segment = new AnchorSegment(start, start);
50      context.RegisterActiveElement(this, new AnchorElement(segment, Name, context));
51    }
52  }
53 
54  /// <summary>
55  /// AnchorElement created by SnippetAnchorElement.
56  /// </summary>
57  public sealed class AnchorElement : IActiveElement
58  {
59    /// <inheritdoc />
60    public bool IsEditable {
61      get { return false; }
62    }
63   
64    AnchorSegment segment;
65    InsertionContext context;
66   
67    /// <inheritdoc />
68    public ISegment Segment {
69      get { return segment; }
70    }
71   
72    /// <summary>
73    /// Creates a new AnchorElement.
74    /// </summary>
75    public AnchorElement(AnchorSegment segment, string name, InsertionContext context)
76    {
77      this.segment = segment;
78      this.context = context;
79      this.Name = name;
80    }
81   
82    /// <summary>
83    /// Gets or sets the text at the anchor.
84    /// </summary>
85    public string Text {
86      get { return context.Document.GetText(segment); }
87      set {
88        int offset = segment.Offset;
89        int length = segment.Length;
90        context.Document.Replace(offset, length, value);
91        if (length == 0) {
92          // replacing an empty anchor segment with text won't enlarge it, so we have to recreate it
93          segment = new AnchorSegment(context.Document, offset, value.Length);
94        }
95      }
96    }
97   
98    /// <summary>
99    /// Gets or sets the name of the anchor.
100    /// </summary>
101    public string Name { get; private set; }
102   
103    /// <inheritdoc />
104    public void OnInsertionCompleted()
105    {
106    }
107   
108    /// <inheritdoc />
109    public void Deactivate(SnippetEventArgs e)
110    {
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.