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

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

@ -57,6 +57,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
protected void SetOffset (int offset) protected void SetOffset (int offset)
{ {
Reset ();
this.offset = offset; this.offset = offset;
this.location = document.GetLocation (offset); this.location = document.GetLocation (offset);
@ -65,9 +67,74 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
#region Context helper methods #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) protected bool IsInsideComment (int offset)
@ -83,11 +150,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
return cmt != null && cmt.CommentType == CommentType.Documentation; return cmt != null && cmt.CommentType == CommentType.Documentation;
} }
protected bool IsInsideString ()
{
return IsInsideString (offset);
}
protected bool IsInsideString (int offset) protected bool IsInsideString (int offset)
{ {
@ -240,6 +302,13 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} }
} }
string cachedText = null;
protected virtual void Reset ()
{
cachedText = null;
}
protected Tuple<string, bool> GetMemberTextToCaret () protected Tuple<string, bool> GetMemberTextToCaret ()
{ {
int startOffset; int startOffset;
@ -250,9 +319,12 @@ namespace ICSharpCode.NRefactory.CSharp.Completion
} else { } else {
startOffset = 0; 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) protected Tuple<CSharpParsedFile, AstNode, CompilationUnit> GetInvocationBeforeCursor (bool afterBracket)
{ {
CompilationUnit baseUnit; CompilationUnit baseUnit;

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

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

Loading…
Cancel
Save