Browse Source

Improved comment & string context recognition.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
d461987839
  1. 12
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
  2. 90
      ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs
  3. 2
      ICSharpCode.NRefactory.CSharp/Completion/CSharpParameterCompletionEngine.cs

12
ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs

@ -150,7 +150,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -150,7 +150,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
// Magic key completion
case ':':
case '.':
if (IsInsideComment () || IsInsideString ())
if (IsInsideCommentOrString ())
return Enumerable.Empty<ICompletionData> ();
var expr = GetExpressionBeforeCursor ();
if (expr == null)
@ -172,7 +172,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -172,7 +172,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return CreateCompletionData (location, resolveResult.Item1, expr.Item2, resolveResult.Item2);
case '#':
if (IsInsideComment () || IsInsideString ())
if (IsInsideCommentOrString ())
return null;
return GetDirectiveCompletionData ();
@ -210,7 +210,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -210,7 +210,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
// Parameter completion
case '(':
if (IsInsideComment () || IsInsideString ())
if (IsInsideCommentOrString ())
return null;
var invoke = GetInvocationBeforeCursor (true);
if (invoke == null)
@ -237,7 +237,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -237,7 +237,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
// Completion on space:
case ' ':
if (IsInsideComment () || IsInsideString ())
if (IsInsideCommentOrString ())
return null;
int tokenIndex = offset;
@ -395,7 +395,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -395,7 +395,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return keywordCompletion;
// Automatic completion
default:
if (IsInsideComment () || IsInsideString ())
if (IsInsideCommentOrString ())
return null;
if (IsInLinqContext (offset)) {
tokenIndex = offset;
@ -741,7 +741,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -741,7 +741,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
IEnumerable<ICompletionData> HandleKeywordCompletion (int wordStart, string word)
{
if (IsInsideComment () || IsInsideString ())
if (IsInsideCommentOrString ())
return null;
switch (word) {
case "using":

90
ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs

@ -57,6 +57,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -57,6 +57,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
protected void SetOffset (int offset)
{
Reset ();
this.offset = offset;
this.location = document.GetLocation (offset);
@ -65,9 +67,74 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -65,9 +67,74 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
}
#region Context helper methods
protected bool IsInsideComment ()
protected bool IsInsideCommentOrString ()
{
return IsInsideComment (offset);
var text = GetMemberTextToCaret ();
bool inSingleComment = false, inString = false, inVerbatimString = false, inChar = false, inMultiLineComment = false;
for (int i = 0; i < text.Item1.Length - 1; i++) {
char ch = text.Item1[i];
char nextCh = text.Item1[i + 1];
switch (ch) {
case '/':
if (inString || inChar || inVerbatimString)
break;
if (nextCh == '/') {
i++;
inSingleComment = true;
}
if (nextCh == '*')
inMultiLineComment = true;
break;
case '*':
if (inString || inChar || inVerbatimString || inSingleComment)
break;
if (nextCh == '/') {
i++;
inMultiLineComment = false;
}
break;
case '@':
if (inString || inChar || inVerbatimString || inSingleComment || inMultiLineComment)
break;
if (nextCh == '"') {
i++;
inVerbatimString = true;
}
break;
case '\n':
case '\r':
inSingleComment = false;
inString = false;
inChar = false;
break;
case '\\':
if (inString || inChar)
i++;
break;
case '"':
if (inSingleComment || inMultiLineComment || inChar)
break;
if (inVerbatimString) {
if (nextCh == '"') {
i++;
break;
}
inVerbatimString = false;
break;
}
inString = !inString;
break;
case '\'':
if (inSingleComment || inMultiLineComment || inString || inVerbatimString)
break;
inChar = !inChar;
break;
}
}
return inSingleComment || inString || inVerbatimString || inChar || inMultiLineComment;
}
protected bool IsInsideComment (int offset)
@ -83,11 +150,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -83,11 +150,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return cmt != null && cmt.CommentType == CommentType.Documentation;
}
protected bool IsInsideString ()
{
return IsInsideString (offset);
}
protected bool IsInsideString (int offset)
{
@ -240,6 +302,13 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -240,6 +302,13 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
}
}
string cachedText = null;
protected virtual void Reset ()
{
cachedText = null;
}
protected Tuple<string, bool> GetMemberTextToCaret ()
{
int startOffset;
@ -250,9 +319,12 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -250,9 +319,12 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} else {
startOffset = 0;
}
return Tuple.Create (document.GetText (startOffset, offset - startOffset), startOffset != 0);
if (cachedText == null)
cachedText = document.GetText (startOffset, offset - startOffset);
return Tuple.Create (cachedText, startOffset != 0);
}
protected Tuple<CSharpParsedFile, AstNode, CompilationUnit> GetInvocationBeforeCursor (bool afterBracket)
{
CompilationUnit baseUnit;

2
ICSharpCode.NRefactory.CSharp/Completion/CSharpParameterCompletionEngine.cs

@ -77,7 +77,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion @@ -77,7 +77,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
char completionChar = document.GetCharAt (offset - 1);
if (completionChar != '(' && completionChar != '<' && completionChar != '[')
return null;
if (IsInsideComment () || IsInsideString ())
if (IsInsideCommentOrString ())
return null;
var invoke = GetInvocationBeforeCursor (true) ?? GetIndexerBeforeCursor ();

Loading…
Cancel
Save