Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit d75e340

Browse files
author
Mikhail Arkhipov
authored
Fix addBrackets setting (microsoft#1241)
* Fix addBrackets * Remove unrelated change * Usings
1 parent 0472503 commit d75e340

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

src/LanguageServer/Impl/Completion/CompletionItemSource.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ internal class CompletionItemSource {
2828
public static readonly CompletionItem Star = CreateCompletionItem("*", CompletionItemKind.Keyword);
2929

3030
private readonly IDocumentationSource _docSource;
31-
private readonly ServerSettings.PythonCompletionOptions _options;
3231

3332
public CompletionItemSource(IDocumentationSource docSource, ServerSettings.PythonCompletionOptions options) {
3433
_docSource = docSource;
35-
_options = options;
34+
Options = options;
3635
}
3736

37+
public ServerSettings.PythonCompletionOptions Options { get; set; }
38+
3839
public CompletionItem CreateCompletionItem(string text, IMember member, IPythonType self = null, string label = null)
3940
=> CreateCompletionItem(text, ToCompletionItemKind(member?.MemberType ?? PythonMemberType.Class), member, self, label);
4041

4142
public CompletionItemEx CreateCompletionItem(string text, CompletionItemKind kind, IMember member, IPythonType self = null, string label = null) {
4243
var t = member?.GetPythonType();
4344
var docFormat = _docSource.DocumentationFormat;
4445

45-
if (_options.addBrackets && (kind == CompletionItemKind.Constructor || kind == CompletionItemKind.Function || kind == CompletionItemKind.Method)) {
46+
if (Options.addBrackets && (kind == CompletionItemKind.Constructor || kind == CompletionItemKind.Function || kind == CompletionItemKind.Method)) {
47+
label = text;
4648
text += "($0)";
4749
docFormat = InsertTextFormat.Snippet;
4850
}

src/LanguageServer/Impl/Completion/CompletionSource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public CompletionSource(IDocumentationSource docSource, ServerSettings.PythonCom
2929
_itemSource = new CompletionItemSource(docSource, completionSettings);
3030
}
3131

32+
public ServerSettings.PythonCompletionOptions Options {
33+
get => _itemSource.Options;
34+
set => _itemSource.Options = value;
35+
}
36+
3237
public CompletionResult GetCompletions(IDocumentAnalysis analysis, SourceLocation location) {
3338
if(analysis.Document.ModuleType != ModuleType.User) {
3439
return CompletionResult.Empty;

src/LanguageServer/Impl/Completion/ExpressionCompletion.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ private static IEnumerable<CompletionItem> GetClassItems(IPythonClassType cls, E
7676
if (m is IVariable v && v.Source != VariableSource.Declaration) {
7777
continue;
7878
}
79+
7980
// If this is class member completion, unmangle private member names.
8081
var unmangledName = cls.UnmangleMemberName(t);
8182
if (!string.IsNullOrEmpty(unmangledName)) {

src/LanguageServer/Impl/Implementation/Server.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ private bool HandleConfigurationChanges(ServerSettings newSettings) {
209209
Settings = newSettings;
210210

211211
_symbolHierarchyMaxSymbols = Settings.analysis.symbolsHierarchyMaxSymbols;
212+
_completionSource.Options = Settings.completion;
212213

213214
if (oldSettings == null) {
214215
return true;

src/LanguageServer/Test/CompletionTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,5 +1194,25 @@ def test(x: Foo = func()):
11941194
var comps = cs.GetCompletions(analysis, new SourceLocation(13, 7));
11951195
comps.Should().HaveLabels("name", "z");
11961196
}
1197+
1198+
[TestMethod, Priority(0)]
1199+
public async Task AddBrackets() {
1200+
const string code = @"prin";
1201+
var analysis = await GetAnalysisAsync(code, PythonVersions.LatestAvailable3X);
1202+
1203+
ServerSettings.completion.addBrackets = true;
1204+
var cs = new CompletionSource(new PlainTextDocumentationSource(), ServerSettings.completion);
1205+
1206+
var comps = cs.GetCompletions(analysis, new SourceLocation(1, 5));
1207+
var print = comps.Completions.FirstOrDefault(x => x.label == "print");
1208+
print.Should().NotBeNull();
1209+
print.insertText.Should().Be("print($0)");
1210+
1211+
cs.Options.addBrackets = false;
1212+
comps = cs.GetCompletions(analysis, new SourceLocation(1, 5));
1213+
print = comps.Completions.FirstOrDefault(x => x.label == "print");
1214+
print.Should().NotBeNull();
1215+
print.insertText.Should().Be("print");
1216+
}
11971217
}
11981218
}

0 commit comments

Comments
 (0)