@@ -63,8 +63,8 @@ public bool MatchPattern(SearchPattern pattern, Boundary boundary, SearchConditi
63
63
Boundary scope = ParseSearchBoundary ( boundary , condition . SearchIn ) ;
64
64
65
65
string text = _content . Substring ( scope . Index , scope . Length ) ;
66
- List < Boundary > macthes = MatchPattern ( pattern , text ) ;
67
- if ( macthes . Count > 0 )
66
+ List < Boundary > matches = MatchPattern ( pattern , text ) ;
67
+ if ( matches . Count > 0 )
68
68
result = true ;
69
69
70
70
return result ;
@@ -121,7 +121,33 @@ public Boundary GetLineBoundary(int index)
121
121
}
122
122
123
123
return result ;
124
- }
124
+ }
125
+
126
+ /// <summary>
127
+ /// Returns the Boundary of a specified line number
128
+ /// </summary>
129
+ /// <param name="lineNumber">The line number to return the boundary for</param>
130
+ /// <returns></returns>
131
+ public Boundary GetBoundaryFromLine ( int lineNumber )
132
+ {
133
+ Boundary result = new Boundary ( ) ;
134
+
135
+ if ( lineNumber >= _lineEnds . Count )
136
+ {
137
+ return result ;
138
+ }
139
+
140
+ // Fine when the line number is 0
141
+ var start = 0 ;
142
+ if ( lineNumber > 0 )
143
+ {
144
+ start = _lineEnds [ lineNumber - 1 ] + 1 ;
145
+ }
146
+ result . Index = start ;
147
+ result . Length = _lineEnds [ lineNumber ] - result . Index + 1 ;
148
+
149
+ return result ;
150
+ }
125
151
126
152
/// <summary>
127
153
/// Return content of the line
@@ -159,7 +185,6 @@ private List<Boundary> MatchPattern(SearchPattern pattern, string text)
159
185
foreach ( Match m in matches )
160
186
{
161
187
Boundary bound = new Boundary ( ) { Index = m . Index , Length = m . Length } ;
162
- if ( ScopeMatch ( pattern , bound , text ) )
163
188
matchList . Add ( bound ) ;
164
189
}
165
190
}
@@ -174,7 +199,7 @@ private List<Boundary> MatchPattern(SearchPattern pattern, string text)
174
199
/// <param name="boundary">Boundary in a text</param>
175
200
/// <param name="text">Text</param>
176
201
/// <returns>True if boundary is matching the pattern scope</returns>
177
- private bool ScopeMatch ( SearchPattern pattern , Boundary boundary , string text )
202
+ public bool ScopeMatch ( SearchPattern pattern , Boundary boundary )
178
203
{
179
204
string prefix = DevSkim . Language . GetCommentPrefix ( Language ) ;
180
205
string suffix = DevSkim . Language . GetCommentSuffix ( Language ) ;
@@ -183,8 +208,8 @@ private bool ScopeMatch(SearchPattern pattern, Boundary boundary, string text)
183
208
if ( pattern . Scopes . Contains ( PatternScope . All ) || string . IsNullOrEmpty ( prefix ) )
184
209
return true ;
185
210
186
- bool isInComment = ( IsBetween ( text , boundary . Index , prefix , suffix , inline )
187
- || IsBetween ( text , boundary . Index , inline , "\n " ) ) ;
211
+ bool isInComment = ( IsBetween ( Content , boundary . Index , prefix , suffix , inline )
212
+ || IsBetween ( Content , boundary . Index , inline , "\n " ) ) ;
188
213
189
214
return ! ( isInComment && ! pattern . Scopes . Contains ( PatternScope . Comment ) ) ;
190
215
}
@@ -201,23 +226,22 @@ private bool IsBetween(string text, int index, string prefix, string suffix, str
201
226
{
202
227
bool result = false ;
203
228
string preText = string . Concat ( text . Substring ( 0 , index ) ) ;
204
- int lastPreffix = preText . LastIndexOf ( prefix , StringComparison . Ordinal ) ;
205
- if ( ! string . IsNullOrEmpty ( inline ) )
229
+ int lastPrefix = preText . LastIndexOf ( prefix , StringComparison . InvariantCulture ) ;
230
+ if ( ! string . IsNullOrEmpty ( inline ) && lastPrefix >= 0 )
206
231
{
207
- int lastInline = preText . Substring ( 0 , lastPreffix ) . LastIndexOf ( inline , StringComparison . Ordinal ) ;
208
- for ( int i = lastInline ; i < lastPreffix ; i ++ )
232
+ int lastInline = preText . Substring ( 0 , lastPrefix ) . LastIndexOf ( inline , StringComparison . InvariantCulture ) ;
233
+
234
+ // For example in C#, If this /* is actually commented out by a //
235
+ if ( lastInline < lastPrefix && ! preText . Substring ( lastInline , lastPrefix - lastInline ) . Contains ( '\n ' ) )
209
236
{
210
- if ( Environment . NewLine . Contains ( preText [ i ] ) )
211
- {
212
- lastPreffix = 0 ;
213
- }
237
+ lastPrefix = 0 ;
214
238
}
215
239
}
216
- if ( lastPreffix >= 0 )
240
+ if ( lastPrefix >= 0 )
217
241
{
218
- preText = preText . Substring ( lastPreffix ) ;
242
+ preText = text . Substring ( lastPrefix ) ;
219
243
int lastSuffix = preText . IndexOf ( suffix , StringComparison . Ordinal ) ;
220
- if ( lastSuffix < 0 )
244
+ if ( lastSuffix + lastPrefix > index )
221
245
result = true ;
222
246
}
223
247
@@ -254,7 +278,7 @@ private int BoundaryByLine(int line, int offset)
254
278
/// <returns>Boundary</returns>
255
279
private Boundary ParseSearchBoundary ( Boundary boundary , string searchIn )
256
280
{
257
- // Default baundary is the fidning line
281
+ // Default boundary is the finding line
258
282
Boundary result = GetLineBoundary ( boundary . Index ) ;
259
283
string srch = ( string . IsNullOrEmpty ( searchIn ) ) ? string . Empty : searchIn . ToLower ( ) ;
260
284
@@ -310,9 +334,17 @@ private bool ParseSearchIn(string searchIn, out int[] args)
310
334
return result ;
311
335
}
312
336
313
- private string _content ;
314
337
private List < int > _lineEnds ;
315
-
338
+ private string _content ;
339
+ public string Content {
340
+ get
341
+ {
342
+ return _content ;
343
+ }
344
+ private set {
345
+ _content = value ;
346
+ }
347
+ }
316
348
public string Language { get ; set ; }
317
349
}
318
350
}
0 commit comments