Skip to content

Commit 798c6b4

Browse files
committed
fixes #4141, closes #4154
1 parent abe03b0 commit 798c6b4

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

Rubberduck.Core/AutoComplete/AutoCompleteBase.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@ public virtual bool Execute(AutoCompleteEventArgs e, AutoCompleteSettings settin
4040
var nextChar = zSelection.StartColumn == original.Length ? string.Empty : original.Substring(zSelection.StartColumn, 1);
4141
if (input == InputToken && (input != OutputToken || nextChar != OutputToken))
4242
{
43-
var code = original.Insert(Math.Max(0, zSelection.StartColumn), InputToken + OutputToken);
43+
string code;
44+
if (!StripExplicitCallStatement(ref original, ref pSelection))
45+
{
46+
code = original.Insert(Math.Max(0, zSelection.StartColumn), InputToken + OutputToken);
47+
}
48+
else
49+
{
50+
code = original;
51+
}
4452
module.ReplaceLine(pSelection.StartLine, code);
4553

4654
var newCode = module.GetLines(pSelection);
47-
if (newCode == code)
55+
if (newCode.Equals(code, StringComparison.OrdinalIgnoreCase))
4856
{
4957
pane.Selection = new Selection(pSelection.StartLine, pSelection.StartColumn + 1);
5058
}
@@ -67,6 +75,21 @@ public virtual bool Execute(AutoCompleteEventArgs e, AutoCompleteSettings settin
6775
}
6876
}
6977

78+
private bool StripExplicitCallStatement(ref string code, ref Selection pSelection)
79+
{
80+
// VBE will "helpfully" strip empty parentheses in 'Call Something()'
81+
// ...and there's no way around it. since Call statement is optional and obsolete,
82+
// this function strips it
83+
var pattern = @"\bCall\b\s+";
84+
if (Regex.IsMatch(code, pattern, RegexOptions.IgnoreCase))
85+
{
86+
pSelection = new Selection(pSelection.StartLine, pSelection.StartColumn - "Call ".Length);
87+
code = Regex.Replace(code, pattern, string.Empty, RegexOptions.IgnoreCase);
88+
return true;
89+
}
90+
return false;
91+
}
92+
7093
private int GetPrettifiedCaretPosition(Selection pSelection, string insertedCode, string prettifiedCode)
7194
{
7295
var zSelection = pSelection.ToZeroBased();
@@ -80,7 +103,10 @@ private int GetPrettifiedCaretPosition(Selection pSelection, string insertedCode
80103
outputTokenIndices.Add(i);
81104
}
82105
}
83-
106+
if (!outputTokenIndices.Any())
107+
{
108+
return pSelection.EndColumn;
109+
}
84110
var firstAfterCaret = outputTokenIndices.Where(i => i > zSelection.StartColumn).Min();
85111

86112
var prettifiedTokenIndices = new List<int>();
@@ -93,7 +119,9 @@ private int GetPrettifiedCaretPosition(Selection pSelection, string insertedCode
93119
}
94120
}
95121

96-
return prettifiedTokenIndices[outputTokenIndices.IndexOf(firstAfterCaret)] + 1;
122+
return prettifiedTokenIndices.Any()
123+
? prettifiedTokenIndices[outputTokenIndices.IndexOf(firstAfterCaret)] + 1
124+
: prettifiedCode.Length + 2;
97125
}
98126

99127
public virtual bool IsMatch(string input) =>

0 commit comments

Comments
 (0)