Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 36aacb9

Browse files
author
Mikhail Arkhipov
committed
Handle cls in addition to self in completions (#1794)
* Remove stale reference * Don't suppress LHS diagnostics on augmented assign * Revert "Don't suppress LHS diagnostics on augmented assign" This reverts commit 6109ac7. * Handle cls * Remove temp test (cherry picked from commit 398a75c)
1 parent f3dba25 commit 36aacb9

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/LanguageServer/Impl/Completion/ExpressionCompletion.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ private static IEnumerable<CompletionItem> GetClassItems(IPythonClassType cls, E
5858
// See if we are completing on self. Note that we may be inside inner function
5959
// that does not necessarily have 'self' argument so we are looking beyond local
6060
// scope. We then check that variable type matches the class type, if any.
61-
var selfVariable = eval.LookupNameInScopes("self");
62-
var completingOnSelf = cls.Equals(selfVariable?.GetPythonType()) && e is NameExpression nex && nex.Name == "self";
61+
var classVariable = eval.LookupNameInScopes("self") ?? eval.LookupNameInScopes("cls");
62+
var completingOnClass = cls.Equals(classVariable?.GetPythonType()) && e is NameExpression nex && (nex.Name == "self" || nex.Name == "cls");
6363

6464
var items = new List<CompletionItem>();
6565
var names = cls.GetMemberNames().ToArray();
@@ -74,7 +74,7 @@ private static IEnumerable<CompletionItem> GetClassItems(IPythonClassType cls, E
7474
var unmangledName = cls.UnmangleMemberName(t);
7575
if (!string.IsNullOrEmpty(unmangledName)) {
7676
// Hide private variables outside of the class scope.
77-
if (!completingOnSelf && cls.IsPrivateMember(t)) {
77+
if (!completingOnClass && cls.IsPrivateMember(t)) {
7878
continue;
7979
}
8080
items.Add(context.ItemSource.CreateCompletionItem(unmangledName, m, cls));

src/LanguageServer/Test/CompletionTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,5 +1486,30 @@ public async Task ImportDotMembers() {
14861486
var result = cs.GetCompletions(analysis, new SourceLocation(1, 16));
14871487
result.Should().OnlyHaveLabels("m1", "m2");
14881488
}
1489+
1490+
[TestMethod, Priority(0)]
1491+
public async Task ClassPrivateMembers() {
1492+
const string code = @"
1493+
class Op:
1494+
__EQ = '0'
1495+
__NOT_EQ = '1'
1496+
__OP_LIST = [__EQ]
1497+
1498+
def print_EQ1(self):
1499+
self.
1500+
1501+
@classmethod
1502+
def print_EQ2(cls):
1503+
cls.
1504+
";
1505+
var analysis = await GetAnalysisAsync(code);
1506+
var cs = new CompletionSource(new PlainTextDocumentationSource(), ServerSettings.completion, Services);
1507+
1508+
var result = cs.GetCompletions(analysis, new SourceLocation(8, 14));
1509+
result.Should().HaveLabels("__EQ", "__NOT_EQ", "__OP_LIST");
1510+
1511+
result = cs.GetCompletions(analysis, new SourceLocation(12, 13));
1512+
result.Should().HaveLabels("__EQ", "__NOT_EQ", "__OP_LIST");
1513+
}
14891514
}
14901515
}

0 commit comments

Comments
 (0)