Browse Source

Fix scoping issue with the code completion suggestions list

Fixes the following issue with code completion.

class Foo
{
    public void Bar() {}
}

var foo = new Foo();
foo.
    ^ Pressing Ctrl+Space here makes the code completion window show
itself. The completions list contains the type names from the 'global'
scope whereas it should contain only the list of Foo's members.

Cherry-picked from pull request #730
pull/734/head
nik 10 years ago committed by Matt Ward
parent
commit
645ef5d6cb
  1. 4
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CSharpCompletionBinding.cs
  2. 24
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CSharpCompletionContext.cs

4
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CSharpCompletionBinding.cs

@ -129,7 +129,9 @@ namespace CSharpBinding.Completion @@ -129,7 +129,9 @@ namespace CSharpBinding.Completion
triggerWordLength = 0;
}
completionData = cce.GetCompletionData(startPos, true);
completionData = completionData.Concat(cce.GetImportCompletionData(startPos));
if (!completionContext.OnlyTypeMembersFitAtPosition(startPos)) {
completionData = completionData.Concat(cce.GetImportCompletionData(startPos));
}
} else {
startPos = caretOffset;
if (char.IsLetterOrDigit (completionChar) || completionChar == '_') {

24
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CSharpCompletionContext.cs

@ -80,6 +80,30 @@ namespace CSharpBinding.Completion @@ -80,6 +80,30 @@ namespace CSharpBinding.Completion
return new CSharpCompletionContext(editor, EmptyList<string>.Instance, compilation, projectContent, document, unresolvedFile, currentLocation);
}
/// <summary>
/// Look back for the nearest char that is not a part of an identifier or a whitespace.
/// (We don't stop on whitespaces because it is legal to have a piece of code like "Foo. Bar(). Baz()")
/// If the char we found is a dot, then the code completion suggestions should only include type members,
/// but not types from the outer scope.
/// If the char we found is not a dot (e. g., a semicolon), then the code completion suggestions can include type names.
/// </summary>
internal bool OnlyTypeMembersFitAtPosition(int offset)
{
var c = '\0';
var pos = offset - 1;
while (pos >= 0) {
c = Document.GetCharAt(pos);
if (!char.IsLetterOrDigit(c) && c != '_' && !char.IsWhiteSpace(c))
break;
pos--;
}
if (pos == -1 || c != '.')
return false;
return true;
}
private CSharpCompletionContext(ITextEditor editor, IList<string> conditionalSymbols, ICompilation compilation, IProjectContent projectContent, IDocument document, CSharpUnresolvedFile unresolvedFile, TextLocation caretLocation)
{

Loading…
Cancel
Save