Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 645ef5d

Browse files
nikmrward
authored andcommitted
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
1 parent 17294bb commit 645ef5d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ bool ShowCompletion(ITextEditor editor, char completionChar, bool ctrlSpace)
129129
triggerWordLength = 0;
130130
}
131131
completionData = cce.GetCompletionData(startPos, true);
132-
completionData = completionData.Concat(cce.GetImportCompletionData(startPos));
132+
if (!completionContext.OnlyTypeMembersFitAtPosition(startPos)) {
133+
completionData = completionData.Concat(cce.GetImportCompletionData(startPos));
134+
}
133135
} else {
134136
startPos = caretOffset;
135137
if (char.IsLetterOrDigit (completionChar) || completionChar == '_') {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ public static CSharpCompletionContext Get(ITextEditor editor, ICodeContext conte
8080

8181
return new CSharpCompletionContext(editor, EmptyList<string>.Instance, compilation, projectContent, document, unresolvedFile, currentLocation);
8282
}
83+
84+
/// <summary>
85+
/// Look back for the nearest char that is not a part of an identifier or a whitespace.
86+
/// (We don't stop on whitespaces because it is legal to have a piece of code like "Foo. Bar(). Baz()")
87+
/// If the char we found is a dot, then the code completion suggestions should only include type members,
88+
/// but not types from the outer scope.
89+
/// If the char we found is not a dot (e. g., a semicolon), then the code completion suggestions can include type names.
90+
/// </summary>
91+
internal bool OnlyTypeMembersFitAtPosition(int offset)
92+
{
93+
var c = '\0';
94+
var pos = offset - 1;
95+
while (pos >= 0) {
96+
c = Document.GetCharAt(pos);
97+
if (!char.IsLetterOrDigit(c) && c != '_' && !char.IsWhiteSpace(c))
98+
break;
99+
pos--;
100+
}
101+
102+
if (pos == -1 || c != '.')
103+
return false;
104+
105+
return true;
106+
}
83107

84108
private CSharpCompletionContext(ITextEditor editor, IList<string> conditionalSymbols, ICompilation compilation, IProjectContent projectContent, IDocument document, CSharpUnresolvedFile unresolvedFile, TextLocation caretLocation)
85109
{

0 commit comments

Comments
 (0)