Skip to content

Commit 7cee927

Browse files
authored
Change override completion to select text after updating the buffer. (#76983)
* Change override completion to select text after updating the buffer. This (along with #76969) will allow a user to commit an override completion and easily replace the default text (and still use the expression body fixers). This required the CompletionChange class to now allow the data to represent a selection instead of just a cursor position.
1 parent bfe9d9f commit 7cee927

File tree

14 files changed

+137
-76
lines changed

14 files changed

+137
-76
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class Bar : IGoo
169169
{
170170
void IGoo.Goo()
171171
{
172-
throw new System.NotImplementedException();$$
172+
[|throw new System.NotImplementedException();|]
173173
}
174174
}
175175
""";
@@ -549,7 +549,7 @@ class Bar : IGoo
549549
{
550550
int IGoo.Generic(K key, V value)
551551
{
552-
throw new System.NotImplementedException();$$
552+
[|throw new System.NotImplementedException();|]
553553
}
554554
}
555555
""";
@@ -1214,7 +1214,7 @@ class C : IFoo
12141214
{
12151215
static bool IFoo.TryDecode(out DecodeError? decodeError, out string? errorMessage)
12161216
{
1217-
throw new System.NotImplementedException();$$
1217+
[|throw new System.NotImplementedException();|]
12181218
}
12191219
}
12201220

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ public override int goo
18781878
{
18791879
get
18801880
{
1881-
return base.goo;$$
1881+
[|return base.goo;|]
18821882
}
18831883
18841884
set
@@ -1921,7 +1921,7 @@ public override int goo
19211921
{
19221922
get
19231923
{
1924-
return base.goo;$$
1924+
[|return base.goo;|]
19251925
}
19261926
19271927
set
@@ -1963,7 +1963,7 @@ public override int goo
19631963
{
19641964
get
19651965
{
1966-
return base.goo;$$
1966+
[|return base.goo;|]
19671967
}
19681968
19691969
set
@@ -2005,7 +2005,7 @@ public override int goo
20052005
{
20062006
set
20072007
{
2008-
base.goo = value;$$
2008+
[|base.goo = value;|]
20092009
}
20102010
}
20112011
}
@@ -2041,7 +2041,7 @@ public override int goo
20412041
{
20422042
get
20432043
{
2044-
return base.goo;$$
2044+
[|return base.goo;|]
20452045
}
20462046
}
20472047
}
@@ -2102,7 +2102,7 @@ public override int this[[MyPublic] int i]
21022102
{
21032103
get
21042104
{
2105-
return base[i];$$
2105+
[|return base[i];|]
21062106
}
21072107
21082108
set
@@ -2392,7 +2392,7 @@ public override T this[int i]
23922392
{
23932393
get
23942394
{
2395-
return base[i];$$
2395+
[|return base[i];|]
23962396
}
23972397
23982398
set
@@ -2435,7 +2435,7 @@ public override T this[int i]
24352435
{
24362436
get
24372437
{
2438-
throw new System.NotImplementedException();$$
2438+
[|throw new System.NotImplementedException();|]
24392439
}
24402440
24412441
set
@@ -2624,7 +2624,7 @@ public override int @class
26242624
{
26252625
get
26262626
{
2627-
return base.@class;$$
2627+
[|return base.@class;|]
26282628
}
26292629
26302630
set
@@ -2939,7 +2939,7 @@ public override int Goo
29392939
{
29402940
get
29412941
{
2942-
return base.Goo;$$
2942+
[|return base.Goo;|]
29432943
}
29442944
}
29452945
}
@@ -3594,7 +3594,7 @@ public override required int Prop
35943594
{
35953595
get
35963596
{
3597-
return base.Prop;$$
3597+
[|return base.Prop;|]
35983598
}
35993599
}
36003600
}
@@ -3635,7 +3635,7 @@ public override required int Prop
36353635
{
36363636
get
36373637
{
3638-
return base.Prop;$$
3638+
[|return base.Prop;|]
36393639
}
36403640
}
36413641
}
@@ -3676,7 +3676,7 @@ public override required int Prop
36763676
{
36773677
get
36783678
{
3679-
return base.Prop;$$
3679+
[|return base.Prop;|]
36803680
}
36813681
}
36823682
}
@@ -3743,7 +3743,7 @@ public override int this[int i]
37433743
{
37443744
get
37453745
{
3746-
return base[i];$$
3746+
[|return base[i];|]
37473747
}
37483748
37493749
set
@@ -3885,7 +3885,7 @@ public override int goo
38853885
{
38863886
get
38873887
{
3888-
throw new System.NotImplementedException();$$
3888+
[|throw new System.NotImplementedException();|]
38893889
}
38903890
38913891
set

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class B
5151
public virtual int A { get; set; }
5252
class C : B
5353
{
54-
public override int A { get => base.A$$; set => base.A = value; }
54+
public override int A { get => [|base.A|]; set => base.A = value; }
5555
}
5656
}
5757
""";
@@ -79,7 +79,7 @@ class B
7979
public virtual int A { get; }
8080
class C : B
8181
{
82-
public override int A => base.A;$$
82+
public override int A => [|base.A|];
8383
}
8484
}
8585
""";
@@ -107,7 +107,7 @@ class B
107107
public virtual int A() => 2;
108108
class C : B
109109
{
110-
public override int A() => base.A();$$
110+
public override int A() => [|base.A()|];
111111
}
112112
}
113113
""";

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ partial class c
417417
418418
partial void goo()
419419
{
420-
throw new System.NotImplementedException();$$
420+
[|throw new System.NotImplementedException();|]
421421
}
422422
}
423423
""";
@@ -444,7 +444,7 @@ partial class c
444444
445445
public partial void goo()
446446
{
447-
throw new System.NotImplementedException();$$
447+
[|throw new System.NotImplementedException();|]
448448
}
449449
}
450450
""";
@@ -471,7 +471,7 @@ partial class c<T>
471471
472472
partial void goo(T bar)
473473
{
474-
throw new System.NotImplementedException();$$
474+
[|throw new System.NotImplementedException();|]
475475
}
476476
}
477477
""";
@@ -498,7 +498,7 @@ partial class c<T>
498498
499499
public partial void goo(T bar)
500500
{
501-
throw new System.NotImplementedException();$$
501+
[|throw new System.NotImplementedException();|]
502502
}
503503
}
504504
""";
@@ -525,7 +525,7 @@ partial class c
525525
526526
partial void goo()
527527
{
528-
throw new System.NotImplementedException();$$
528+
[|throw new System.NotImplementedException();|]
529529
}
530530
}
531531
""";
@@ -552,7 +552,7 @@ partial class c
552552
553553
private partial void goo()
554554
{
555-
throw new System.NotImplementedException();$$
555+
[|throw new System.NotImplementedException();|]
556556
}
557557
}
558558
""";
@@ -585,7 +585,7 @@ partial class c
585585
{
586586
partial void goo()
587587
{
588-
throw new System.NotImplementedException();$$
588+
[|throw new System.NotImplementedException();|]
589589
}
590590
}
591591
""";
@@ -618,7 +618,7 @@ partial class c
618618
{
619619
public partial void goo()
620620
{
621-
throw new System.NotImplementedException();$$
621+
[|throw new System.NotImplementedException();|]
622622
}
623623
}
624624
""";
@@ -645,7 +645,7 @@ partial struct c
645645
646646
partial void goo()
647647
{
648-
throw new System.NotImplementedException();$$
648+
[|throw new System.NotImplementedException();|]
649649
}
650650
}
651651
""";
@@ -744,7 +744,7 @@ partial class Bar
744744
745745
async partial void Goo()
746746
{
747-
throw new NotImplementedException();$$
747+
[|throw new NotImplementedException();|]
748748
}
749749
}
750750
""";
@@ -775,7 +775,7 @@ partial class Bar
775775
776776
public async partial void Goo()
777777
{
778-
throw new NotImplementedException();$$
778+
[|throw new NotImplementedException();|]
779779
}
780780
}
781781
""";
@@ -806,7 +806,7 @@ partial class Bar
806806
807807
partial void Goo()
808808
{
809-
throw new NotImplementedException();$$
809+
[|throw new NotImplementedException();|]
810810
}
811811
}
812812
""";
@@ -838,7 +838,7 @@ partial class PClass
838838
839839
partial void PMethod(int i)
840840
{
841-
throw new System.NotImplementedException();$$
841+
[|throw new System.NotImplementedException();|]
842842
}
843843
}
844844
}
@@ -870,7 +870,7 @@ partial class PClass
870870
871871
public partial void PMethod(int i)
872872
{
873-
throw new System.NotImplementedException();$$
873+
[|throw new System.NotImplementedException();|]
874874
}
875875
}
876876
}
@@ -903,7 +903,7 @@ partial class Bar
903903
partial class Bar
904904
{
905905
partial void Foo();
906-
partial void Foo() => throw new NotImplementedException();$$
906+
partial void Foo() => [|throw new NotImplementedException()|];
907907
}
908908
""";
909909

@@ -936,7 +936,7 @@ partial class Bar
936936
partial class Bar
937937
{
938938
public partial void Foo();
939-
public partial void Foo() => throw new NotImplementedException();$$
939+
public partial void Foo() => [|throw new NotImplementedException()|];
940940
}
941941
"""
942942
;

src/EditorFeatures/Core/IntelliSense/AsyncCompletion/CommitManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ private AsyncCompletionData.CommitResult Commit(
278278
updatedCurrentSnapshot = edit.Apply();
279279
}
280280

281-
if (change.NewPosition.HasValue)
281+
if (change.NewSelection.HasValue)
282282
{
283-
// Roslyn knows how to position the caret in the snapshot we just created.
284-
// If there were more edits made by extensions, TryMoveCaretToAndEnsureVisible maps the snapshot point to the most recent one.
285-
view.TryMoveCaretToAndEnsureVisible(new SnapshotPoint(updatedCurrentSnapshot, change.NewPosition.Value));
283+
// Roslyn knows how to set the selection in the snapshot we just created.
284+
// If there were more edits made by extensions, TrySetSelectionAndEnsureVisible maps the snapshot point to the most recent one.
285+
view.TrySetSelectionAndEnsureVisible(new SnapshotSpan(updatedCurrentSnapshot, change.NewSelection.Value.ToSpan()));
286286
}
287287
else
288288
{

src/EditorFeatures/Core/Shared/Extensions/ITextViewExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ public static void SetMultiSelection(this ITextView textView, IEnumerable<Snapsh
108108
textView.GetMultiSelectionBroker().SetSelectionRange(spansInView, spansInView.Last());
109109
}
110110

111+
internal static bool TrySetSelectionAndEnsureVisible(this ITextView textView, SnapshotSpan span, IOutliningManagerService? outliningManagerService = null, EnsureSpanVisibleOptions ensureSpanVisibleOptions = EnsureSpanVisibleOptions.None)
112+
{
113+
if (!textView.TryMoveCaretToAndEnsureVisible(new VirtualSnapshotPoint(span.End), outliningManagerService, ensureSpanVisibleOptions))
114+
return false;
115+
116+
SetSelection(textView, span, isReversed: false);
117+
return true;
118+
}
119+
111120
public static bool TryMoveCaretToAndEnsureVisible(this ITextView textView, SnapshotPoint point, IOutliningManagerService? outliningManagerService = null, EnsureSpanVisibleOptions ensureSpanVisibleOptions = EnsureSpanVisibleOptions.None)
112121
=> textView.TryMoveCaretToAndEnsureVisible(new VirtualSnapshotPoint(point), outliningManagerService, ensureSpanVisibleOptions);
113122

src/EditorFeatures/TestUtilities/Completion/AbstractCompletionProviderTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Microsoft.CodeAnalysis;
1616
using Microsoft.CodeAnalysis.Completion;
1717
using Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.AsyncCompletion;
18+
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
1819
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
1920
using Microsoft.CodeAnalysis.LanguageService;
2021
using Microsoft.CodeAnalysis.Shared.Extensions;
@@ -636,7 +637,7 @@ private async Task VerifyCustomCommitWorkerAsync(
636637
{
637638
using var workspaceFixture = GetOrCreateWorkspaceFixture();
638639

639-
MarkupTestFile.GetPosition(expectedCodeAfterCommit, out var actualExpectedCode, out int expectedCaretPosition);
640+
MarkupTestFile.GetPositionAndSpan(expectedCodeAfterCommit, out var actualExpectedCode, out var expectedCaretPosition, out TextSpan? expectedSelectionSpan);
640641

641642
var options = GetCompletionOptions();
642643

@@ -661,10 +662,15 @@ private async Task VerifyCustomCommitWorkerAsync(
661662
var textBuffer = workspaceFixture.Target.CurrentDocument.GetTextBuffer();
662663

663664
var actualCodeAfterCommit = textBuffer.CurrentSnapshot.AsText().ToString();
665+
var selectionSpan = commit.NewSelection ?? textView.Selection.StreamSelectionSpan.SnapshotSpan.Span.ToTextSpan();
664666
var caretPosition = commit.NewPosition ?? textView.Caret.Position.BufferPosition.Position;
665667

666668
AssertEx.EqualOrDiff(actualExpectedCode, actualCodeAfterCommit);
667-
Assert.Equal(expectedCaretPosition, caretPosition);
669+
670+
if (expectedSelectionSpan != null)
671+
Assert.Equal(expectedSelectionSpan, selectionSpan);
672+
else if (expectedCaretPosition != null)
673+
Assert.Equal(expectedCaretPosition, caretPosition);
668674
}
669675

670676
private void VerifyCustomCommitWorker(

0 commit comments

Comments
 (0)