3
3
using System . Linq ;
4
4
using Rubberduck . VBEditor . ComManagement ;
5
5
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
6
+ using System . Reflection ;
6
7
7
8
namespace Rubberduck . VBEditor . SourceCodeHandling
8
9
{
@@ -165,42 +166,51 @@ private static CodeString GetPrettifiedCodeString(CodeString original, Selection
165
166
return result ;
166
167
}
167
168
169
+ private const string LineContinuation = " _" ;
170
+
168
171
public CodeString GetCurrentLogicalLine ( ICodeModule module )
169
172
{
170
- const string lineContinuation = " _" ;
171
-
172
173
Selection pSelection ;
173
174
using ( var pane = module . CodePane )
174
175
{
175
176
pSelection = pane . Selection ;
176
177
}
177
178
178
- var currentLineIndex = pSelection . StartLine ;
179
- var currentLine = module . GetLines ( currentLineIndex , 1 ) ;
179
+ var selectedLines = module . GetLines ( pSelection . StartLine , pSelection . LineCount ) . Replace ( " \r " , string . Empty ) . Split ( ' \n ' ) ;
180
+ var currentLine = selectedLines [ 0 ] ;
180
181
181
- var caretLine = ( currentLineIndex , currentLine ) ;
182
- var lines = new List < ( int Line , string Content ) > { caretLine } ;
182
+ var caretStartLine = ( pSelection . StartLine , currentLine ) ;
183
+ var lines = new List < ( int pLine , string Content ) > { caretStartLine } ;
183
184
184
- while ( currentLineIndex >= 1 )
185
+ // selection line may not be the only physical line in the complete logical line; accounts for line continuations.
186
+ InsertPhysicalLinesAboveSelectionStart ( lines , module , pSelection . StartLine ) ;
187
+ AppendPhysicalLinesBelowSelectionStart ( lines , module , pSelection . StartLine , currentLine ) ;
188
+
189
+ var logicalLine = string . Join ( "\r \n " , lines . Select ( e => e . Content ) ) ;
190
+
191
+ var zCaretLine = lines . IndexOf ( caretStartLine ) ;
192
+ var zCaretColumn = pSelection . StartColumn - 1 ;
193
+ var caretPosition = new Selection (
194
+ zCaretLine , zCaretColumn , zCaretLine + pSelection . LineCount - 1 , pSelection . EndColumn - 1 ) ;
195
+
196
+ var pStartLine = lines [ 0 ] . pLine ;
197
+ var pEndLine = lines [ lines . Count - 1 ] . pLine ;
198
+ var snippetPosition = new Selection ( pStartLine , 1 , pEndLine , 1 ) ;
199
+
200
+ if ( lines [ 0 ] . pLine < pSelection . StartLine || lines . Last ( ) . pLine > pSelection . EndLine )
185
201
{
186
- currentLineIndex -- ;
187
- if ( currentLineIndex >= 1 )
188
- {
189
- currentLine = module . GetLines ( currentLineIndex , 1 ) ;
190
- if ( currentLine . EndsWith ( lineContinuation ) )
191
- {
192
- lines . Insert ( 0 , ( currentLineIndex , currentLine ) ) ;
193
- }
194
- else
195
- {
196
- break ;
197
- }
198
- }
202
+ // selection spans more than a single logical line
203
+ return null ;
199
204
}
200
205
201
- currentLineIndex = pSelection . StartLine ;
202
- currentLine = caretLine . currentLine ;
203
- while ( currentLineIndex <= module . CountOfLines && currentLine . EndsWith ( lineContinuation ) )
206
+ var result = new CodeString ( logicalLine , caretPosition , snippetPosition ) ;
207
+ return result ;
208
+ }
209
+
210
+ private void AppendPhysicalLinesBelowSelectionStart ( ICollection < ( int Line , string Content ) > lines , ICodeModule module , int currentLineIndex , string currentLine )
211
+ {
212
+ // assumes caret line is already in the list.
213
+ while ( currentLineIndex <= module . CountOfLines && currentLine . EndsWith ( LineContinuation ) )
204
214
{
205
215
currentLineIndex ++ ;
206
216
if ( currentLineIndex <= module . CountOfLines )
@@ -213,21 +223,27 @@ public CodeString GetCurrentLogicalLine(ICodeModule module)
213
223
break ;
214
224
}
215
225
}
226
+ }
216
227
217
- var logicalLine = string . Join ( "\r \n " , lines . Select ( e => e . Content ) ) ;
218
- var zCaretLine = lines . IndexOf ( caretLine ) ;
219
- var zCaretColumn = pSelection . StartColumn - 1 ;
220
-
221
- var startLine = lines [ 0 ] . Line ;
222
- var endLine = lines [ lines . Count - 1 ] . Line ;
223
-
224
- var result = new CodeString (
225
- logicalLine ,
226
- new Selection ( zCaretLine , zCaretColumn ) ,
227
- new Selection ( startLine , 1 , endLine , 1 ) ) ;
228
-
229
- return result ;
230
-
228
+ private void InsertPhysicalLinesAboveSelectionStart ( IList < ( int Line , string Content ) > lines , ICodeModule module , int currentLineIndex )
229
+ {
230
+ // assumes caret line is already in the list.
231
+ while ( currentLineIndex >= 1 )
232
+ {
233
+ currentLineIndex -- ;
234
+ if ( currentLineIndex >= 1 )
235
+ {
236
+ var currentLine = module . GetLines ( currentLineIndex , 1 ) ;
237
+ if ( currentLine . EndsWith ( LineContinuation ) )
238
+ {
239
+ lines . Insert ( 0 , ( currentLineIndex , currentLine ) ) ;
240
+ }
241
+ else
242
+ {
243
+ break ;
244
+ }
245
+ }
246
+ }
231
247
}
232
248
233
249
public CodeString GetCurrentLogicalLine ( QualifiedModuleName module )
0 commit comments