Skip to content

Commit 0197465

Browse files
committed
closes #4505
1 parent 3746fdb commit 0197465

File tree

3 files changed

+67
-38
lines changed

3 files changed

+67
-38
lines changed

Rubberduck.Core/AutoComplete/Service/SelfClosingPairHandler.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settin
4141
}
4242

4343
var original = CodePaneHandler.GetCurrentLogicalLine(e.Module);
44+
if (original == null)
45+
{
46+
// selection spans more than a single logical line
47+
return false;
48+
}
4449

4550
if (pair != null)
4651
{
@@ -74,6 +79,13 @@ public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settin
7479

7580
private bool HandleInternal(AutoCompleteEventArgs e, CodeString original, SelfClosingPair pair, out CodeString result)
7681
{
82+
if (!original.CaretPosition.IsSingleCharacter)
83+
{
84+
// todo: WrapSelection?
85+
result = null;
86+
return false;
87+
}
88+
7789
var isPresent = original.CaretLine.EndsWith($"{pair.OpeningChar}{pair.ClosingChar}");
7890

7991
if (!_scpService.Execute(pair, original, e.Character, out result))

Rubberduck.Core/AutoComplete/Service/SmartConcatenationHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settin
2525
}
2626

2727
var currentContent = CodePaneHandler.GetCurrentLogicalLine(e.Module);
28-
if (!currentContent.IsInsideStringLiteral)
28+
if (!currentContent?.IsInsideStringLiteral ?? true)
2929
{
30+
// selection spans more than a single logical line
3031
return false;
3132
}
3233

Rubberduck.VBEEditor/SourceCodeHandling/CodePaneSourceCodeHandler.cs

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using Rubberduck.VBEditor.ComManagement;
55
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
6+
using System.Reflection;
67

78
namespace Rubberduck.VBEditor.SourceCodeHandling
89
{
@@ -165,42 +166,51 @@ private static CodeString GetPrettifiedCodeString(CodeString original, Selection
165166
return result;
166167
}
167168

169+
private const string LineContinuation = " _";
170+
168171
public CodeString GetCurrentLogicalLine(ICodeModule module)
169172
{
170-
const string lineContinuation = " _";
171-
172173
Selection pSelection;
173174
using (var pane = module.CodePane)
174175
{
175176
pSelection = pane.Selection;
176177
}
177178

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];
180181

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};
183184

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)
185201
{
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;
199204
}
200205

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))
204214
{
205215
currentLineIndex++;
206216
if (currentLineIndex <= module.CountOfLines)
@@ -213,21 +223,27 @@ public CodeString GetCurrentLogicalLine(ICodeModule module)
213223
break;
214224
}
215225
}
226+
}
216227

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+
}
231247
}
232248

233249
public CodeString GetCurrentLogicalLine(QualifiedModuleName module)

0 commit comments

Comments
 (0)