@@ -36,10 +36,48 @@ public class UseConsistentIndentation : ConfigurableRule
3636 [ ConfigurableRuleProperty ( defaultValue : 4 ) ]
3737 public int IndentationSize { get ; protected set ; }
3838
39- private enum IndentationKind { Space , Tab } ;
39+
40+ // Cannot name to IndentationKind due to the enum type of the same name.
41+ /// <summary>
42+ /// Represents the kind of indentation to be used.
43+ ///
44+ /// Possible values are: `space`, `tab`. If any invalid value is given, the
45+ /// property defaults to `space`.
46+ ///
47+ /// `space` means `IndentationSize` number of `space` characters are used to provide one level of indentation.
48+ /// `tab` means a tab character, `\t`.
49+ ///</summary>
50+ [ ConfigurableRuleProperty ( defaultValue : "space" ) ]
51+ public string Kind
52+ {
53+ get
54+ {
55+ return indentationKind . ToString ( ) ;
56+ }
57+ set
58+ {
59+ if ( String . IsNullOrWhiteSpace ( value ) ||
60+ ! Enum . TryParse < IndentationKind > ( value , true , out indentationKind ) )
61+ {
62+ indentationKind = IndentationKind . Space ;
63+ }
64+ }
65+ }
66+
67+ private bool insertSpaces ;
68+ private char indentationChar ;
69+ private int indentationLevelMultiplier ;
70+
71+ // TODO Enable auto when the rule is able to detect indentation
72+ private enum IndentationKind {
73+ Space ,
74+ Tab ,
75+ // Auto
76+ } ;
4077
4178 // TODO make this configurable
42- private readonly IndentationKind indentationKind = IndentationKind . Space ;
79+ private IndentationKind indentationKind = IndentationKind . Space ;
80+
4381
4482 /// <summary>
4583 /// Analyzes the given ast to find violations.
@@ -61,6 +99,14 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6199 return Enumerable . Empty < DiagnosticRecord > ( ) ;
62100 }
63101
102+ // It is more efficient to initialize these fields in ConfigurRule method
103+ // but when the rule will enable `Auto` IndentationKind, we will anyways need to move
104+ // the setting of these variables back here after the rule detects the indentation kind for
105+ // each invocation.
106+ insertSpaces = indentationKind == IndentationKind . Space ;
107+ indentationChar = insertSpaces ? ' ' : '\t ' ;
108+ indentationLevelMultiplier = insertSpaces ? IndentationSize : 1 ;
109+
64110 var tokens = Helper . Instance . Tokens ;
65111 var diagnosticRecords = new List < DiagnosticRecord > ( ) ;
66112 var indentationLevel = 0 ;
@@ -262,17 +308,13 @@ private int GetIndentationColumnNumber(int indentationLevel)
262308
263309 private int GetIndentation ( int indentationLevel )
264310 {
265- return indentationLevel * this . IndentationSize ;
266- }
267-
268- private char GetIndentationChar ( )
269- {
270- return indentationKind == IndentationKind . Space ? ' ' : '\t ' ;
311+ // todo if condition can be evaluated during rule configuration
312+ return indentationLevel * indentationLevelMultiplier ;
271313 }
272314
273315 private string GetIndentationString ( int indentationLevel )
274316 {
275- return new string ( GetIndentationChar ( ) , GetIndentation ( indentationLevel ) ) ;
317+ return new string ( indentationChar , GetIndentation ( indentationLevel ) ) ;
276318 }
277319 }
278320}
0 commit comments