PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/AvalonEdit/ICSharpCode.AvalonEdit/Indentation/CSharp/CSharpIndentationStrategy.cs

http://github.com/icsharpcode/ILSpy
C# | 95 lines | 48 code | 10 blank | 37 comment | 5 complexity | 23df775d3a39d98cbfb847bf200abdc6 MD5 | raw file
 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;
21
22namespace ICSharpCode.AvalonEdit.Indentation.CSharp
23{
24	/// <summary>
25	/// Smart indentation for C#.
26	/// </summary>
27	public class CSharpIndentationStrategy : DefaultIndentationStrategy
28	{
29		/// <summary>
30		/// Creates a new CSharpIndentationStrategy.
31		/// </summary>
32		public CSharpIndentationStrategy()
33		{
34		}
35		
36		/// <summary>
37		/// Creates a new CSharpIndentationStrategy and initializes the settings using the text editor options.
38		/// </summary>
39		public CSharpIndentationStrategy(TextEditorOptions options)
40		{
41			this.IndentationString = options.IndentationString;
42		}
43		
44		string indentationString = "\t";
45		
46		/// <summary>
47		/// Gets/Sets the indentation string.
48		/// </summary>
49		public string IndentationString {
50			get { return indentationString; }
51			set {
52				if (string.IsNullOrEmpty(value))
53					throw new ArgumentException("Indentation string must not be null or empty");
54				indentationString = value;
55			}
56		}
57		
58		/// <summary>
59		/// Performs indentation using the specified document accessor.
60		/// </summary>
61		/// <param name="document">Object used for accessing the document line-by-line</param>
62		/// <param name="keepEmptyLines">Specifies whether empty lines should be kept</param>
63		public void Indent(IDocumentAccessor document, bool keepEmptyLines)
64		{
65			if (document == null)
66				throw new ArgumentNullException("document");
67			IndentationSettings settings = new IndentationSettings();
68			settings.IndentString = this.IndentationString;
69			settings.LeaveEmptyLines = keepEmptyLines;
70			
71			IndentationReformatter r = new IndentationReformatter();
72			r.Reformat(document, settings);
73		}
74		
75		/// <inheritdoc cref="IIndentationStrategy.IndentLine"/>
76		public override void IndentLine(TextDocument document, DocumentLine line)
77		{
78			int lineNr = line.LineNumber;
79			TextDocumentAccessor acc = new TextDocumentAccessor(document, lineNr, lineNr);
80			Indent(acc, false);
81			
82			string t = acc.Text;
83			if (t.Length == 0) {
84				// use AutoIndentation for new lines in comments / verbatim strings.
85				base.IndentLine(document, line);
86			}
87		}
88		
89		/// <inheritdoc cref="IIndentationStrategy.IndentLines"/>
90		public override void IndentLines(TextDocument document, int beginLine, int endLine)
91		{
92			Indent(new TextDocumentAccessor(document, beginLine, endLine), true);
93		}
94	}
95}