@@ -9,6 +9,7 @@ namespace Rubberduck.SmartIndenter
9
9
{
10
10
internal class AbsoluteCodeLine
11
11
{
12
+ private const string StupidLineEnding = ": _" ;
12
13
private const string StringPlaceholder = "\a " ;
13
14
private static readonly Regex StringLiteralRegex = new Regex ( "\" (?:[^\" ]+|\" \" )*\" " ) ;
14
15
private static readonly Regex StringReplaceRegex = new Regex ( StringPlaceholder ) ;
@@ -21,7 +22,7 @@ internal class AbsoluteCodeLine
21
22
private static readonly Regex TypeEnumEndRegex = new Regex ( @"^End\s(Enum|Type)" ) ;
22
23
private static readonly Regex InProcedureInRegex = new Regex ( @"^(Else)?If\s.*\sThen$|^Else$|^Case\s|^With|^For\s|^Do$|^Do\s|^While$|^While\s|^Select Case" ) ;
23
24
private static readonly Regex InProcedureOutRegex = new Regex ( @"^Else(If)?|^Case\s|^End With|^Next\s|^Next$|^Loop$|^Loop\s|^Wend$|^End If$|^End Select" ) ;
24
- private static readonly Regex DeclarationRegex = new Regex ( @"^(Dim|Const|Static|Public|Private)\s.* \sAs\s" ) ;
25
+ private static readonly Regex DeclarationRegex = new Regex ( @"^(Dim|Const|Static|Public|Private)\s(.*( \sAs\s)?|_) " ) ;
25
26
private static readonly Regex PrecompilerInRegex = new Regex ( @"^#(Else)?If\s.+Then$|^#Else$" ) ;
26
27
private static readonly Regex PrecompilerOutRegex = new Regex ( @"^#ElseIf\s.+Then|^#Else$|#End\sIf$" ) ;
27
28
private static readonly Regex SingleLineElseIfRegex = new Regex ( @"^ElseIf\s.*\sThen\s.*" ) ;
@@ -30,13 +31,24 @@ internal class AbsoluteCodeLine
30
31
private uint _lineNumber ;
31
32
private bool _numbered ;
32
33
private string _code ;
34
+ private bool _stupidLineEnding ;
33
35
private readonly string [ ] _segments ;
34
36
private List < string > _strings ;
35
37
36
38
public AbsoluteCodeLine ( string code , IIndenterSettings settings )
37
39
{
38
40
_settings = settings ;
39
- _code = code ;
41
+
42
+ if ( code . EndsWith ( StupidLineEnding ) )
43
+ {
44
+ _code = code . Substring ( 0 , code . Length - StupidLineEnding . Length ) ;
45
+ _stupidLineEnding = true ;
46
+ }
47
+ else
48
+ {
49
+ _code = code ;
50
+ }
51
+
40
52
Original = code ;
41
53
42
54
ExtractStringLiterals ( ) ;
@@ -110,7 +122,21 @@ public bool ContainsOnlyComment
110
122
111
123
public bool IsDeclaration
112
124
{
113
- get { return ! IsEmpty && DeclarationRegex . IsMatch ( _code ) ; }
125
+ get { return ! IsEmpty && ( ! IsProcedureStart && ! ProcedureStartIgnoreRegex . IsMatch ( _code ) ) && DeclarationRegex . IsMatch ( _code ) ; }
126
+ }
127
+
128
+ public bool IsDeclarationContinuation { get ; set ; }
129
+
130
+ public bool HasDeclarationContinuation
131
+ {
132
+ get
133
+ {
134
+ return ( ! IsProcedureStart && ! ProcedureStartIgnoreRegex . IsMatch ( _code ) ) &&
135
+ ! ContainsOnlyComment &&
136
+ string . IsNullOrEmpty ( EndOfLineComment ) &&
137
+ HasContinuation &&
138
+ ( ( IsDeclarationContinuation && Segments . Count ( ) == 1 ) || DeclarationRegex . IsMatch ( Segments . Last ( ) ) ) ;
139
+ }
114
140
}
115
141
116
142
public bool HasContinuation
@@ -205,14 +231,17 @@ public string Indent(int indents, bool atProcStart, bool absolute = false)
205
231
if ( ( IsPrecompilerDirective && _settings . ForceCompilerDirectivesInColumn1 ) ||
206
232
( IsBareDebugStatement && _settings . ForceDebugStatementsInColumn1 ) ||
207
233
( atProcStart && ! _settings . IndentFirstCommentBlock && ContainsOnlyComment ) ||
208
- ( atProcStart && ! _settings . IndentFirstDeclarationBlock && IsDeclaration ) )
234
+ ( atProcStart && ! _settings . IndentFirstDeclarationBlock && ( IsDeclaration || IsDeclarationContinuation ) ) )
209
235
{
210
236
indents = 0 ;
211
237
}
212
238
213
239
var number = _numbered ? _lineNumber . ToString ( CultureInfo . InvariantCulture ) : string . Empty ;
214
240
var gap = Math . Max ( ( absolute ? indents : _settings . IndentSpaces * indents ) - number . Length , number . Length > 0 ? 1 : 0 ) ;
215
- AlignDims ( gap ) ;
241
+ if ( _settings . AlignDims && ( IsDeclaration || IsDeclarationContinuation ) )
242
+ {
243
+ AlignDims ( gap ) ;
244
+ }
216
245
217
246
var code = string . Join ( ": " , _segments ) ;
218
247
if ( _strings . Any ( ) )
@@ -223,22 +252,26 @@ public string Indent(int indents, bool atProcStart, bool absolute = false)
223
252
code = string . Join ( string . Empty , number , new string ( ' ' , gap ) , code ) ;
224
253
if ( string . IsNullOrEmpty ( EndOfLineComment ) )
225
254
{
226
- return code ;
255
+ return code + ( _stupidLineEnding ? StupidLineEnding : string . Empty ) ;
227
256
}
228
257
229
258
var position = Original . LastIndexOf ( EndOfLineComment , StringComparison . Ordinal ) ;
230
259
switch ( _settings . EndOfLineCommentStyle )
231
260
{
232
261
case EndOfLineCommentStyle . Absolute :
233
- return string . Format ( "{0}{1}{2}" , code , new string ( ' ' , Math . Max ( position - code . Length , 1 ) ) , EndOfLineComment ) ;
262
+ return string . Format ( "{0}{1}{2}{3}" , code , new string ( ' ' , Math . Max ( position - code . Length , 1 ) ) ,
263
+ EndOfLineComment , _stupidLineEnding ? StupidLineEnding : string . Empty ) ;
234
264
case EndOfLineCommentStyle . SameGap :
235
265
var uncommented = Original . Substring ( 0 , position - 1 ) ;
236
- return string . Format ( "{0}{1}{2}" , code , new string ( ' ' , uncommented . Length - uncommented . TrimEnd ( ) . Length + 1 ) , EndOfLineComment ) ;
266
+ return string . Format ( "{0}{1}{2}{3}" , code , new string ( ' ' , uncommented . Length - uncommented . TrimEnd ( ) . Length + 1 ) ,
267
+ EndOfLineComment , _stupidLineEnding ? StupidLineEnding : string . Empty ) ;
237
268
case EndOfLineCommentStyle . StandardGap :
238
- return string . Format ( "{0}{1}{2}" , code , new string ( ' ' , _settings . IndentSpaces * 2 ) , EndOfLineComment ) ;
269
+ return string . Format ( "{0}{1}{2}{3}" , code , new string ( ' ' , _settings . IndentSpaces * 2 ) , EndOfLineComment ,
270
+ _stupidLineEnding ? StupidLineEnding : string . Empty ) ;
239
271
case EndOfLineCommentStyle . AlignInColumn :
240
272
var align = _settings . EndOfLineCommentColumnSpaceAlignment - code . Length ;
241
- return string . Format ( "{0}{1}{2}" , code , new string ( ' ' , Math . Max ( align - 1 , 1 ) ) , EndOfLineComment ) ;
273
+ return string . Format ( "{0}{1}{2}{3}" , code , new string ( ' ' , Math . Max ( align - 1 , 1 ) ) , EndOfLineComment ,
274
+ _stupidLineEnding ? StupidLineEnding : string . Empty ) ;
242
275
default :
243
276
throw new InvalidEnumArgumentException ( ) ;
244
277
}
@@ -250,13 +283,10 @@ public override string ToString()
250
283
}
251
284
252
285
private void AlignDims ( int postition )
253
- {
254
- if ( ! DeclarationRegex . IsMatch ( _segments [ 0 ] ) || IsProcedureStart ) return ;
286
+ {
255
287
var alignTokens = _segments [ 0 ] . Split ( new [ ] { " As " } , StringSplitOptions . None ) ;
256
288
var gap = Math . Max ( _settings . AlignDimColumn - postition - alignTokens [ 0 ] . Length - 2 , 0 ) ;
257
- _segments [ 0 ] = string . Format ( "{0}{1} As {2}" , alignTokens [ 0 ] . Trim ( ) ,
258
- ( ! _settings . AlignDims ) ? string . Empty : new string ( ' ' , gap ) ,
259
- string . Join ( " As " , alignTokens . Skip ( 1 ) ) ) ;
289
+ _segments [ 0 ] = string . Format ( "{0}{1} As {2}" , alignTokens [ 0 ] . Trim ( ) , new string ( ' ' , gap ) , string . Join ( " As " , alignTokens . Skip ( 1 ) ) ) ;
260
290
}
261
291
}
262
292
}
0 commit comments