Skip to content

Commit 056e7e8

Browse files
committed
Fix a few error cases
1 parent 44ba3cf commit 056e7e8

File tree

5 files changed

+71
-29
lines changed

5 files changed

+71
-29
lines changed

Source/ExcelDna.IntelliSense.Host/MyFunctions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ public static string jDummyFunc()
2222
return "Howzit !";
2323
}
2424

25+
[ExcelFunction]
26+
public static object AnotherFunction( [Description("In and out")] object inout)
27+
{
28+
return inout;
29+
}
30+
31+
[ExcelFunction]
32+
public static object ANonDescriptFunction(object inout)
33+
{
34+
return inout;
35+
}
36+
2537
}
2638
}
2739
#endif

Source/ExcelDna.IntelliSense/ExcelDna.IntelliSense.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
</ItemGroup>
5858
<ItemGroup>
5959
<Compile Include="FormattedText.cs" />
60+
<Compile Include="FormulaParser.cs" />
6061
<Compile Include="IntelliSenseDisplay.cs" />
6162
<Compile Include="IntelliSenseHelper.cs" />
6263
<Compile Include="IntelliSenseProvider.cs" />

Source/ExcelDna.IntelliSense/FormattedText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public FormattedText()
1818

1919
public void Add(TextLine line) { _lines.Add(line); }
2020

21-
public void Add(IEnumerable<TextLine> lines) { _lines.AddRange(lines); }
21+
public void Add(IEnumerable<TextLine> lines) { if (lines != null) _lines.AddRange(lines); }
2222

2323
public IEnumerator<TextLine> GetEnumerator()
2424
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Linq;
2+
using System.Text.RegularExpressions;
3+
4+
namespace ExcelDna.IntelliSense
5+
{
6+
static class FormulaParser
7+
{
8+
// TODO: This needs a proper implementation, considering subformulae
9+
internal static bool TryGetFormulaInfo(string formulaPrefix, out string functionName, out int currentArgIndex)
10+
{
11+
var match = Regex.Match(formulaPrefix, @"^=(?<functionName>\w*)\(");
12+
if (match.Success)
13+
{
14+
functionName = match.Groups["functionName"].Value;
15+
currentArgIndex = formulaPrefix.Count(c => c == ',');
16+
return true;
17+
}
18+
functionName = null;
19+
currentArgIndex = -1;
20+
return false;
21+
}
22+
}
23+
}

Source/ExcelDna.IntelliSense/IntelliSenseDisplay.cs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,19 @@ void FormulaEditMove(Rect editWindowBounds, Rect excelTooltipBounds)
235235
void FormulaEditTextChange(string formulaPrefix, Rect editWindowBounds, Rect excelTooltipBounds)
236236
{
237237
Debug.Print($"^^^ FormulaEditStateChanged. CurrentPrefix: {formulaPrefix}, Thread {Thread.CurrentThread.ManagedThreadId}");
238-
var match = Regex.Match(formulaPrefix, @"^=(?<functionName>\w*)\(");
239-
if (match.Success)
238+
string functionName;
239+
int currentArgIndex;
240+
if (FormulaParser.TryGetFormulaInfo(formulaPrefix, out functionName, out currentArgIndex))
240241
{
241-
string functionName = match.Groups["functionName"].Value;
242-
243242
IntelliSenseFunctionInfo functionInfo;
244243
if (_functionInfoMap.TryGetValue(functionName, out functionInfo))
245244
{
246-
// TODO: Fix this: Need to consider subformulae
247-
int currentArgIndex = formulaPrefix.Count(c => c == ',');
248245
_argumentsToolTip.ShowToolTip(
249246
GetFunctionIntelliSense(functionInfo, currentArgIndex),
250247
(int)editWindowBounds.Left, (int)editWindowBounds.Bottom + 5);
251248
return;
252249
}
250+
253251
}
254252

255253
// All other paths, we hide the box
@@ -288,15 +286,19 @@ void FunctionListSelectedItemChange(string selectedItemText, Rect selectedItemBo
288286
if (_functionInfoMap.TryGetValue(selectedItemText, out functionInfo))
289287
{
290288
// It's ours!
291-
_descriptionToolTip.ShowToolTip(
292-
text: new FormattedText { GetFunctionDescription(functionInfo) },
293-
left: (int)selectedItemBounds.Right + 25,
294-
top: (int)selectedItemBounds.Top);
295-
}
296-
else
297-
{
298-
_descriptionToolTip.Hide();
289+
var descriptionLines = GetFunctionDescriptionOrNull(functionInfo);
290+
if (descriptionLines != null)
291+
{
292+
_descriptionToolTip.ShowToolTip(
293+
text: new FormattedText { GetFunctionDescriptionOrNull(functionInfo) },
294+
left: (int)selectedItemBounds.Right + 25,
295+
top: (int)selectedItemBounds.Top);
296+
return;
297+
}
299298
}
299+
300+
// Not ours or no description
301+
_descriptionToolTip.Hide();
300302
}
301303

302304
void FunctionListMove(Rect selectedItemBounds)
@@ -307,18 +309,20 @@ void FunctionListMove(Rect selectedItemBounds)
307309
// TODO: Performance / efficiency - cache these somehow
308310
// TODO: Probably not a good place for LINQ !?
309311
static readonly string[] s_newLineStringArray = new string[] { Environment.NewLine };
310-
IEnumerable<TextLine> GetFunctionDescription(IntelliSenseFunctionInfo functionInfo)
312+
IEnumerable<TextLine> GetFunctionDescriptionOrNull(IntelliSenseFunctionInfo functionInfo)
311313
{
312-
return
313-
functionInfo.Description
314-
.Split(s_newLineStringArray, StringSplitOptions.None)
315-
.Select(line =>
316-
new TextLine {
317-
new TextRun
318-
{
319-
Style = System.Drawing.FontStyle.Regular,
320-
Text = line
321-
}});
314+
var description = functionInfo.Description;
315+
if (string.IsNullOrEmpty(description))
316+
return null;
317+
318+
return description.Split(s_newLineStringArray, StringSplitOptions.None)
319+
.Select(line =>
320+
new TextLine {
321+
new TextRun
322+
{
323+
Style = System.Drawing.FontStyle.Regular,
324+
Text = line
325+
}});
322326
}
323327

324328
FormattedText GetFunctionIntelliSense(IntelliSenseFunctionInfo functionInfo, int currentArgIndex)
@@ -357,12 +361,13 @@ FormattedText GetFunctionIntelliSense(IntelliSenseFunctionInfo functionInfo, int
357361
}
358362
nameLine.Add(new TextRun { Text = ")" });
359363

360-
var descriptionLines = GetFunctionDescription(functionInfo);
364+
var descriptionLines = GetFunctionDescriptionOrNull(functionInfo);
361365

362366
var formattedText = new FormattedText { nameLine, descriptionLines };
363367
if (functionInfo.ArgumentList.Count > currentArgIndex)
364368
{
365-
formattedText.Add(GetArgumentDescription(functionInfo.ArgumentList[currentArgIndex]));
369+
var description = GetArgumentDescription(functionInfo.ArgumentList[currentArgIndex]);
370+
formattedText.Add(description);
366371
}
367372

368373
return formattedText;
@@ -379,7 +384,7 @@ TextLine GetArgumentDescription(IntelliSenseFunctionInfo.ArgumentInfo argumentIn
379384
new TextRun
380385
{
381386
Style = System.Drawing.FontStyle.Italic,
382-
Text = argumentInfo.Description
387+
Text = argumentInfo.Description ?? ""
383388
},
384389
};
385390
}
@@ -399,6 +404,7 @@ public void Dispose()
399404
_argumentsToolTip.Dispose();
400405
_argumentsToolTip = null;
401406
}
407+
_uiMonitor.Dispose();
402408
}, null);
403409

404410
_syncContextMain = null;

0 commit comments

Comments
 (0)