1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections.Generic;
7
using System.Windows.Input;
9
using ICSharpCode.AvalonEdit.Document;
10
using ICSharpCode.AvalonEdit.Editing;
11
using ICSharpCode.AvalonEdit.Utils;
13
namespace ICSharpCode.AvalonEdit.Snippets
16
/// Represents the context of a snippet insertion.
18
public class InsertionContext : IWeakEventListener
23
RaisingInsertionCompleted,
29
Status currentStatus = Status.Insertion;
32
/// Creates a new InsertionContext instance.
34
public InsertionContext(TextArea textArea, int insertionPosition)
37
throw new ArgumentNullException("textArea");
38
this.TextArea = textArea;
39
this.Document = textArea.Document;
40
this.SelectedText = textArea.Selection.GetText(textArea.Document);
41
this.InsertionPosition = insertionPosition;
42
this.startPosition = insertionPosition;
44
DocumentLine startLine = this.Document.GetLineByOffset(insertionPosition);
45
ISegment indentation = TextUtilities.GetWhitespaceAfter(this.Document, startLine.Offset);
46
this.Indentation = Document.GetText(indentation.Offset, Math.Min(indentation.EndOffset, insertionPosition) - indentation.Offset);
47
this.Tab = textArea.Options.IndentationString;
49
this.LineTerminator = TextUtilities.GetNewLineFromDocument(this.Document, startLine.LineNumber);
53
/// Gets the text area.
55
public TextArea TextArea { get; private set; }
58
/// Gets the text document.
60
public TextDocument Document { get; private set; }
63
/// Gets the text that was selected before the insertion of the snippet.
65
public string SelectedText { get; private set; }
68
/// Gets the indentation at the insertion position.
70
public string Indentation { get; private set; }
73
/// Gets the indentation string for a single indentation level.
75
public string Tab { get; private set; }
78
/// Gets the line terminator at the insertion position.
80
public string LineTerminator { get; private set; }
83
/// Gets/Sets the insertion position.
85
public int InsertionPosition { get; set; }
87
readonly int startPosition;
88
AnchorSegment wholeSnippetAnchor;
89
bool deactivateIfSnippetEmpty;
92
/// Gets the start position of the snippet insertion.
94
public int StartPosition {
96
if (wholeSnippetAnchor != null)
97
return wholeSnippetAnchor.Offset;
104
/// Inserts text at the insertion position and advances the insertion position.
105
/// This method will add the current indentation to every line in <paramref name="text"/> and will
106
/// replace newlines with the expected newline for the document.
108
public void InsertText(string text)
111
throw new ArgumentNullException("text");
112
if (currentStatus != Status.Insertion)
113
throw new InvalidOperationException();
115
text = text.Replace("\t", this.Tab);
117
using (this.Document.RunUpdate()) {
119
SimpleSegment segment;
120
while ((segment = NewLineFinder.NextNewLine(text, textOffset)) != SimpleSegment.Invalid) {
121
string insertString = text.Substring(textOffset, segment.Offset - textOffset)
122
+ this.LineTerminator + this.Indentation;
123
this.Document.Insert(InsertionPosition, insertString);
124
this.InsertionPosition += insertString.Length;
125
textOffset = segment.EndOffset;
127
string remainingInsertString = text.Substring(textOffset);
128
this.Document.Insert(InsertionPosition, remainingInsertString);
129
this.InsertionPosition += remainingInsertString.Length;
133
Dictionary<SnippetElement, IActiveElement> elementMap = new Dictionary<SnippetElement, IActiveElement>();
134
List<IActiveElement> registeredElements = new List<IActiveElement>();
137
/// Registers an active element. Elements should be registered during insertion and will be called back
138
/// when insertion has completed.
140
/// <param name="owner">The snippet element that created the active element.</param>
141
/// <param name="element">The active element.</param>
142
public void RegisterActiveElement(SnippetElement owner, IActiveElement element)
145
throw new ArgumentNullException("owner");
147
throw new ArgumentNullException("element");
148
if (currentStatus != Status.Insertion)
149
throw new InvalidOperationException();
150
elementMap.Add(owner, element);
151
registeredElements.Add(element);
155
/// Returns the active element belonging to the specified snippet element, or null if no such active element is found.
157
public IActiveElement GetActiveElement(SnippetElement owner)
160
throw new ArgumentNullException("owner");
161
IActiveElement element;
162
if (elementMap.TryGetValue(owner, out element))
169
/// Gets the list of active elements.
171
public IEnumerable<IActiveElement> ActiveElements {
172
get { return registeredElements; }
176
/// Calls the <see cref="IActiveElement.OnInsertionCompleted"/> method on all registered active elements
177
/// and raises the <see cref="InsertionCompleted"/> event.
179
/// <param name="e">The EventArgs to use</param>
180
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
181
Justification="There is an event and this method is raising it.")]
182
public void RaiseInsertionCompleted(EventArgs e)
184
if (currentStatus != Status.Insertion)
185
throw new InvalidOperationException();
189
currentStatus = Status.RaisingInsertionCompleted;
190
int endPosition = this.InsertionPosition;
191
this.wholeSnippetAnchor = new AnchorSegment(Document, startPosition, endPosition - startPosition);
192
TextDocumentWeakEventManager.UpdateFinished.AddListener(Document, this);
193
deactivateIfSnippetEmpty = (endPosition != startPosition);
195
foreach (IActiveElement element in registeredElements) {
196
element.OnInsertionCompleted();
198
if (InsertionCompleted != null)
199
InsertionCompleted(this, e);
200
currentStatus = Status.Interactive;
201
if (registeredElements.Count == 0) {
202
// deactivate immediately if there are no interactive elements
203
Deactivate(new SnippetEventArgs(DeactivateReason.NoActiveElements));
205
myInputHandler = new SnippetInputHandler(this);
206
// disable existing snippet input handlers - there can be only 1 active snippet
207
foreach (TextAreaStackedInputHandler h in TextArea.StackedInputHandlers) {
208
if (h is SnippetInputHandler)
209
TextArea.PopStackedInputHandler(h);
211
TextArea.PushStackedInputHandler(myInputHandler);
215
SnippetInputHandler myInputHandler;
218
/// Occurs when the all snippet elements have been inserted.
220
public event EventHandler InsertionCompleted;
223
/// Calls the <see cref="IActiveElement.Deactivate"/> method on all registered active elements.
225
/// <param name="e">The EventArgs to use</param>
226
public void Deactivate(SnippetEventArgs e)
228
if (currentStatus == Status.Deactivated || currentStatus == Status.RaisingDeactivated)
230
if (currentStatus != Status.Interactive)
231
throw new InvalidOperationException("Cannot call Deactivate() until RaiseInsertionCompleted() has finished.");
233
e = new SnippetEventArgs(DeactivateReason.Unknown);
235
TextDocumentWeakEventManager.UpdateFinished.RemoveListener(Document, this);
236
currentStatus = Status.RaisingDeactivated;
237
TextArea.PopStackedInputHandler(myInputHandler);
238
foreach (IActiveElement element in registeredElements) {
239
element.Deactivate(e);
241
if (Deactivated != null)
242
Deactivated(this, e);
243
currentStatus = Status.Deactivated;
247
/// Occurs when the interactive mode is deactivated.
249
public event EventHandler<SnippetEventArgs> Deactivated;
251
bool IWeakEventListener.ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
253
return ReceiveWeakEvent(managerType, sender, e);
256
/// <inheritdoc cref="IWeakEventListener.ReceiveWeakEvent"/>
257
protected virtual bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
259
if (managerType == typeof(TextDocumentWeakEventManager.UpdateFinished)) {
260
// Deactivate if snippet is deleted. This is necessary for correctly leaving interactive
261
// mode if Undo is pressed after a snippet insertion.
262
if (wholeSnippetAnchor.Length == 0 && deactivateIfSnippetEmpty)
263
Deactivate(new SnippetEventArgs(DeactivateReason.Deleted));