1- using System . Linq ;
1+ using System ;
2+ using System . Linq ;
23using System . Text . RegularExpressions ;
34
45namespace ExcelDna . IntelliSense
@@ -8,16 +9,28 @@ static class FormulaParser
89 // Set from IntelliSenseDisplay.Initialize
910 public static char ListSeparator = ',' ;
1011
11- // TODO: This needs a proper implementation, considering subformulae
1212 internal static bool TryGetFormulaInfo ( string formulaPrefix , out string functionName , out int currentArgIndex )
1313 {
14- var match = Regex . Match ( formulaPrefix , @"^=(?<functionName>[\w|.]*)\(" ) ;
15- if ( match . Success )
14+ formulaPrefix = Regex . Replace ( formulaPrefix , "(\" [^\" ]*\" )|(\\ ([^\\ (\\ )]*\\ ))| " , string . Empty ) ;
15+
16+ while ( Regex . IsMatch ( formulaPrefix , "\\ ([^\\ (\\ )]*\\ )" ) )
17+ {
18+ formulaPrefix = Regex . Replace ( formulaPrefix , "\\ ([^\\ (\\ )]*\\ )" , string . Empty ) ;
19+ }
20+
21+ int lastOpeningParenthesis = formulaPrefix . LastIndexOf ( "(" , formulaPrefix . Length - 1 , StringComparison . Ordinal ) ;
22+
23+ if ( lastOpeningParenthesis > - 1 )
1624 {
17- functionName = match . Groups [ "functionName" ] . Value ;
18- currentArgIndex = formulaPrefix . Count ( c => c == ListSeparator ) ;
19- return true ;
25+ var match = Regex . Match ( formulaPrefix . Substring ( 0 , lastOpeningParenthesis ) , @"[^\w](?<functionName>\w*)$" ) ;
26+ if ( match . Success )
27+ {
28+ functionName = match . Groups [ "functionName" ] . Value ;
29+ currentArgIndex = formulaPrefix . Substring ( lastOpeningParenthesis , formulaPrefix . Length - lastOpeningParenthesis ) . Count ( c => c == ListSeparator ) ;
30+ return true ;
31+ }
2032 }
33+
2134 functionName = null ;
2235 currentArgIndex = - 1 ;
2336 return false ;
0 commit comments