~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Snippets/SnippetSelectionElement.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Text;
 
6
using System.Windows.Input;
 
7
using ICSharpCode.AvalonEdit.Document;
 
8
 
 
9
namespace ICSharpCode.AvalonEdit.Snippets
 
10
{
 
11
        /// <summary>
 
12
        /// Inserts the previously selected text at the selection marker.
 
13
        /// </summary>
 
14
        [Serializable]
 
15
        public class SnippetSelectionElement : SnippetElement
 
16
        {
 
17
                /// <summary>
 
18
                /// Gets/Sets the new indentation of the selected text.
 
19
                /// </summary>
 
20
                public int Indentation { get; set; }
 
21
                
 
22
                /// <inheritdoc/>
 
23
                public override void Insert(InsertionContext context)
 
24
                {
 
25
                        StringBuilder tabString = new StringBuilder();
 
26
                        
 
27
                        for (int i = 0; i < Indentation; i++) {
 
28
                                tabString.Append(context.Tab);
 
29
                        }
 
30
                        
 
31
                        string indent = tabString.ToString();
 
32
                        
 
33
                        string text = context.SelectedText.TrimStart(' ', '\t');
 
34
                        
 
35
                        text = text.Replace(context.LineTerminator,
 
36
                                                     context.LineTerminator + indent);
 
37
                        
 
38
                        context.Document.Insert(context.InsertionPosition, text);
 
39
                        context.InsertionPosition += text.Length;
 
40
                        
 
41
                        if (string.IsNullOrEmpty(context.SelectedText))
 
42
                                SnippetCaretElement.SetCaret(context);
 
43
                }
 
44
        }
 
45
}