Skip to content

Commit 4643ff5

Browse files
Merge pull request #4117 from DustinCampbell/fix-4115
Add special case to object creation completion to not commit 'object' on '{'
2 parents eb039bc + 340e406 commit 4643ff5

File tree

11 files changed

+182
-33
lines changed

11 files changed

+182
-33
lines changed

src/EditorFeatures/CSharpTest/Completion/CompletionProviders/CrefCompletionProviderTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class C { }
247247

248248
var expected = @"
249249
using System.Collections.Generic;
250-
/// <see cref=""List{T}.Enumerator""/>
250+
/// <see cref=""List{T}.Enumerator ""/>
251251
class C { }
252252
";
253253
VerifyProviderCommit(text, "Enumerator", expected, ' ', "Enum");
@@ -314,7 +314,7 @@ class @void { }
314314
";
315315

316316
var expected = @"using System;
317-
/// <see cref=""@void""/>
317+
/// <see cref=""@void ""/>
318318
class @void { }
319319
";
320320
VerifyProviderCommit(text, "@void", expected, ' ', "@vo");
@@ -380,7 +380,7 @@ class C { }
380380

381381
var expected = @"
382382
using System.Collections.Generic;
383-
/// <see cref=""List""/>
383+
/// <see cref=""List{""/>
384384
class C { }
385385
";
386386
VerifyProviderCommit(text, "List{T}", expected, '{', "List");
@@ -401,7 +401,7 @@ public void foo(int x) { }
401401

402402
var expected = @"
403403
using System.Collections.Generic;
404-
/// <see cref=""foo""/>
404+
/// <see cref=""foo(""/>
405405
class C
406406
{
407407
public void foo(int x) { }

src/EditorFeatures/CSharpTest/Completion/CompletionProviders/ExplicitInterfaceCompletionProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ interface IFoo
8787
8888
class Bar : IFoo
8989
{
90-
void IFoo.Foo
90+
void IFoo.Foo(
9191
}";
9292

9393
VerifyProviderCommit(markup, "Foo()", expected, '(', "");

src/EditorFeatures/CSharpTest/Completion/CompletionProviders/ObjectCreationCompletionProviderTests.cs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class Program
198198
{
199199
static void Main(string[] args)
200200
{
201-
D d= new D
201+
D d= new D(
202202
}
203203
}";
204204
VerifyProviderCommit(markup, "D", expected, '(', "");
@@ -262,5 +262,113 @@ static void Main(string[] args)
262262
}";
263263
VerifyItemExists(markup, "Program");
264264
}
265+
266+
[WorkItem(4115, "https://github.com/dotnet/roslyn/issues/4115")]
267+
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
268+
public void CommitObjectWithParenthesis1()
269+
{
270+
var markup = @"
271+
class C
272+
{
273+
void M1()
274+
{
275+
object o = new $$
276+
}
277+
}";
278+
279+
var expected = @"
280+
class C
281+
{
282+
void M1()
283+
{
284+
object o = new object(
285+
}
286+
}";
287+
288+
VerifyProviderCommit(markup, "object", expected, '(', "");
289+
}
290+
291+
[WorkItem(4115, "https://github.com/dotnet/roslyn/issues/4115")]
292+
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
293+
public void CommitObjectWithParenthesis2()
294+
{
295+
var markup = @"
296+
class C
297+
{
298+
void M1()
299+
{
300+
M2(new $$
301+
}
302+
303+
void M2(object o) { }
304+
}";
305+
306+
var expected = @"
307+
class C
308+
{
309+
void M1()
310+
{
311+
M2(new object(
312+
}
313+
314+
void M2(object o) { }
315+
}";
316+
317+
VerifyProviderCommit(markup, "object", expected, '(', "");
318+
}
319+
320+
[WorkItem(4115, "https://github.com/dotnet/roslyn/issues/4115")]
321+
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
322+
public void DontCommitObjectWithOpenBrace1()
323+
{
324+
var markup = @"
325+
class C
326+
{
327+
void M1()
328+
{
329+
object o = new $$
330+
}
331+
}";
332+
333+
var expected = @"
334+
class C
335+
{
336+
void M1()
337+
{
338+
object o = new {
339+
}
340+
}";
341+
342+
VerifyProviderCommit(markup, "object", expected, '{', "");
343+
}
344+
345+
[WorkItem(4115, "https://github.com/dotnet/roslyn/issues/4115")]
346+
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
347+
public void DontCommitObjectWithOpenBrace2()
348+
{
349+
var markup = @"
350+
class C
351+
{
352+
void M1()
353+
{
354+
M2(new $$
355+
}
356+
357+
void M2(object o) { }
358+
}";
359+
360+
var expected = @"
361+
class C
362+
{
363+
void M1()
364+
{
365+
M2(new {
366+
}
367+
368+
void M2(object o) { }
369+
}";
370+
371+
VerifyProviderCommit(markup, "object", expected, '{', "");
372+
}
265373
}
266374
}

src/EditorFeatures/Test/Completion/AbstractCompletionProviderTests.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ protected virtual void VerifyProviderCommitWorker(string codeBeforeCommit, int p
354354
}
355355
}
356356

357-
private void VerifyProviderCommitCheckResults(Document document, int position, string itemToCommit, string expectedCodeAfterCommit, char? commitChar, string textTypedSoFar)
357+
private void VerifyProviderCommitCheckResults(Document document, int position, string itemToCommit, string expectedCodeAfterCommit, char? commitCharOpt, string textTypedSoFar)
358358
{
359359
var textBuffer = WorkspaceFixture.Workspace.Documents.Single().TextBuffer;
360360
var textSnapshot = textBuffer.CurrentSnapshot.AsText();
@@ -363,13 +363,30 @@ private void VerifyProviderCommitCheckResults(Document document, int position, s
363363
var firstItem = items.First(i => CompareItems(i.DisplayText, itemToCommit));
364364

365365
var completionRules = GetCompletionRules(document);
366-
var textChange = completionRules.IsCommitCharacter(firstItem, commitChar.HasValue ? commitChar.Value : ' ', textTypedSoFar)
367-
? completionRules.GetTextChange(firstItem, commitChar, textTypedSoFar)
368-
: new TextChange();
366+
var commitChar = commitCharOpt ?? '\t';
369367

370-
var oldText = document.GetTextAsync().Result;
371-
var newText = oldText.WithChanges(textChange);
372-
Assert.Equal(expectedCodeAfterCommit, newText.ToString());
368+
var text = document.GetTextAsync().Result;
369+
370+
if (commitChar == '\t' || completionRules.IsCommitCharacter(firstItem, commitChar, textTypedSoFar))
371+
{
372+
var textChange = completionRules.GetTextChange(firstItem, commitChar, textTypedSoFar);
373+
374+
// Adjust TextChange to include commit character, so long as it isn't TAB.
375+
if (commitChar != '\t')
376+
{
377+
textChange = new TextChange(textChange.Span, textChange.NewText.TrimEnd(commitChar) + commitChar);
378+
}
379+
380+
text = text.WithChanges(textChange);
381+
}
382+
else
383+
{
384+
// nothing was commited, but we should insert the commit character.
385+
var textChange = new TextChange(new TextSpan(firstItem.FilterSpan.End, 0), commitChar.ToString());
386+
text = text.WithChanges(textChange);
387+
}
388+
389+
Assert.Equal(expectedCodeAfterCommit, text.ToString());
373390
}
374391

375392
protected void VerifyItemInEditorBrowsableContexts(string markup, string referencedCode, string item, int expectedSymbolsSameSolution, int expectedSymbolsMetadataReference,

src/EditorFeatures/VisualBasicTest/Completion/CompletionProviders/CrefCompletionProviderTests.vb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ End Class
339339

340340
Dim expected = <File><![CDATA[
341341
''' <summary>
342-
''' <see cref="C."/>
342+
''' <see cref="C.("/>
343343
''' </summary>
344344
Class C
345345
Sub bar(x As Integer, y As Integer)
@@ -366,7 +366,7 @@ End Class
366366
Dim expected = <File><![CDATA[
367367
Imports System.Collections.Generic
368368
''' <summary>
369-
''' <see cref=""/>
369+
''' <see cref=" "/>
370370
''' </summary>
371371
Class C
372372
Sub bar(x As Integer, y As Integer)

src/EditorFeatures/VisualBasicTest/Completion/CompletionProviders/EnumCompletionProviderTests.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ End Enum
446446

447447
Class C
448448
Sub M()
449-
Const e As E = E.A
449+
Const e As E = E.A,
450450
End Sub
451451
End Class
452452
]]></Text>.Value

src/EditorFeatures/VisualBasicTest/Completion/CompletionProviders/ImplementsClauseCompletionProviderTests.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ End Interface
500500

501501
Class C
502502
Implements [Interface]
503-
Public Sub test Implements [Interface]
503+
Public Sub test Implements [Interface].
504504
End Class</text>.Value
505505

506506
VerifyProviderCommit(text, "Interface", expected, "."c, "")

src/EditorFeatures/VisualBasicTest/Completion/CompletionProviders/NamedParameterCompletionProviderTests.vb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ End Module
300300
Dim expected = <Text>
301301
Module Program
302302
Sub Main(args As String())
303-
Main(args:
303+
Main(args:=
304304
End Sub
305305
End Module
306306

@@ -323,7 +323,7 @@ End Module
323323
Dim expected = <Text>
324324
Module Program
325325
Sub Main(args As String())
326-
Main(args
326+
Main(args:
327327
End Sub
328328
End Module
329329

@@ -346,7 +346,7 @@ End Module
346346
Dim expected = <Text>
347347
Module Program
348348
Sub Main(args As String())
349-
Main(args:=
349+
Main(args:=
350350
End Sub
351351
End Module
352352

src/EditorFeatures/VisualBasicTest/Completion/CompletionProviders/PartialTypeCompletionProviderTests.vb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Partial Class $$</text>
4747

4848
<Fact, Trait(Traits.Feature, Traits.Features.Completion)>
4949
Public Sub PartialGenericClassCommitOnParen()
50+
' TODO(DustinCa): This is testing the wrong behavior and will need to be updated to the commented expected
51+
' result when https://github.com/dotnet/roslyn/issues/4137 is fixed.
52+
5053
Dim text = <text>Class Bar
5154
End Class
5255

@@ -61,9 +64,17 @@ End Class
6164
Partial Class C(Of Bar)
6265
End Class
6366

64-
Partial Class C(Of Bar)</text>
67+
Partial Class C(Of Bar)(</text>
68+
69+
' Dim expected = <text>Class Bar
70+
'End Class
71+
72+
'Partial Class C(Of Bar)
73+
'End Class
74+
75+
'Partial Class C(</text>
6576

66-
VerifyProviderCommit(text.Value, "C(Of Bar)", expected.Value, "("c, "", Microsoft.CodeAnalysis.SourceCodeKind.Regular)
77+
VerifyProviderCommit(text.Value, "C(Of Bar)", expected.Value, "("c, "", SourceCodeKind.Regular)
6778
End Sub
6879

6980
<Fact, Trait(Traits.Feature, Traits.Features.Completion)>
@@ -84,7 +95,7 @@ End Class
8495

8596
Partial Class C(Of Bar)</text>
8697

87-
VerifyProviderCommit(text.Value, "C(Of Bar)", expected.Value, Nothing, "", Microsoft.CodeAnalysis.SourceCodeKind.Regular)
98+
VerifyProviderCommit(text.Value, "C(Of Bar)", expected.Value, Nothing, "", SourceCodeKind.Regular)
8899
End Sub
89100

90101
<Fact, Trait(Traits.Feature, Traits.Features.Completion)>

src/EditorFeatures/VisualBasicTest/Completion/CompletionProviders/SymbolCompletionProviderTests.vb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ End Class
25292529
Imports System
25302530
Class classattribute : Inherits Attribute
25312531
End Class
2532-
&lt;[class]
2532+
&lt;[class](
25332533
Class C
25342534
End Class
25352535
</Text>.Value
@@ -5141,7 +5141,7 @@ Class C
51415141
Public Class [Inherits]
51425142
End Class
51435143
Class C
5144-
Inherits [Inherits]
5144+
Inherits [Inherits].
51455145
"
51465146

51475147
VerifyProviderCommit(markup, "Inherits", expected, "."c, "")
@@ -5345,7 +5345,7 @@ Class G
53455345
End Class
53465346

53475347
Class DG
5348-
Inherits G
5348+
Inherits G(
53495349
End Class
53505350
"
53515351
VerifyProviderCommit(markup, "G", expected, "("c, "")
@@ -5547,10 +5547,10 @@ Class G(Of T)
55475547
End Class
55485548

55495549
Class DG
5550-
Function Bar() as G(Of
5550+
Function Bar() as G(
55515551
End Class</code>.Value
55525552

5553-
VerifyProviderCommit(text, "G(Of …)", expected, Nothing, "")
5553+
VerifyProviderCommit(text, "G(Of …)", expected, "("c, "")
55545554
End Sub
55555555

55565556
<WorkItem(668159)>
@@ -5670,7 +5670,7 @@ End Class]]></code>.Value
56705670
<code><![CDATA[
56715671
Class Await
56725672
Sub Foo()
5673-
Dim x = new [Await
5673+
Dim x = new [Await]
56745674
End Sub
56755675
End Class]]></code>.Value
56765676

0 commit comments

Comments
 (0)