@@ -36,10 +36,48 @@ public class UseConsistentIndentation : ConfigurableRule
36
36
[ ConfigurableRuleProperty ( defaultValue : 4 ) ]
37
37
public int IndentationSize { get ; protected set ; }
38
38
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
+ } ;
40
77
41
78
// TODO make this configurable
42
- private readonly IndentationKind indentationKind = IndentationKind . Space ;
79
+ private IndentationKind indentationKind = IndentationKind . Space ;
80
+
43
81
44
82
/// <summary>
45
83
/// Analyzes the given ast to find violations.
@@ -61,6 +99,14 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
61
99
return Enumerable . Empty < DiagnosticRecord > ( ) ;
62
100
}
63
101
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
+
64
110
var tokens = Helper . Instance . Tokens ;
65
111
var diagnosticRecords = new List < DiagnosticRecord > ( ) ;
66
112
var indentationLevel = 0 ;
@@ -262,17 +308,13 @@ private int GetIndentationColumnNumber(int indentationLevel)
262
308
263
309
private int GetIndentation ( int indentationLevel )
264
310
{
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 ;
271
313
}
272
314
273
315
private string GetIndentationString ( int indentationLevel )
274
316
{
275
- return new string ( GetIndentationChar ( ) , GetIndentation ( indentationLevel ) ) ;
317
+ return new string ( indentationChar , GetIndentation ( indentationLevel ) ) ;
276
318
}
277
319
}
278
320
}
0 commit comments