Skip to content

Commit

Permalink
Change override completion to select text after updating the buffer. (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
ToddGrun authored Feb 23, 2025
1 parent bfe9d9f commit 7cee927
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class Bar : IGoo
{
void IGoo.Goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -549,7 +549,7 @@ class Bar : IGoo
{
int IGoo.Generic(K key, V value)
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -1214,7 +1214,7 @@ class C : IFoo
{
static bool IFoo.TryDecode(out DecodeError? decodeError, out string? errorMessage)
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}
set
Expand Down Expand Up @@ -1921,7 +1921,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}
set
Expand Down Expand Up @@ -1963,7 +1963,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}
set
Expand Down Expand Up @@ -2005,7 +2005,7 @@ public override int goo
{
set
{
base.goo = value;$$
[|base.goo = value;|]
}
}
}
Expand Down Expand Up @@ -2041,7 +2041,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}
}
}
Expand Down Expand Up @@ -2102,7 +2102,7 @@ public override int this[[MyPublic] int i]
{
get
{
return base[i];$$
[|return base[i];|]
}
set
Expand Down Expand Up @@ -2392,7 +2392,7 @@ public override T this[int i]
{
get
{
return base[i];$$
[|return base[i];|]
}
set
Expand Down Expand Up @@ -2435,7 +2435,7 @@ public override T this[int i]
{
get
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
set
Expand Down Expand Up @@ -2624,7 +2624,7 @@ public override int @class
{
get
{
return base.@class;$$
[|return base.@class;|]
}
set
Expand Down Expand Up @@ -2939,7 +2939,7 @@ public override int Goo
{
get
{
return base.Goo;$$
[|return base.Goo;|]
}
}
}
Expand Down Expand Up @@ -3594,7 +3594,7 @@ public override required int Prop
{
get
{
return base.Prop;$$
[|return base.Prop;|]
}
}
}
Expand Down Expand Up @@ -3635,7 +3635,7 @@ public override required int Prop
{
get
{
return base.Prop;$$
[|return base.Prop;|]
}
}
}
Expand Down Expand Up @@ -3676,7 +3676,7 @@ public override required int Prop
{
get
{
return base.Prop;$$
[|return base.Prop;|]
}
}
}
Expand Down Expand Up @@ -3743,7 +3743,7 @@ public override int this[int i]
{
get
{
return base[i];$$
[|return base[i];|]
}
set
Expand Down Expand Up @@ -3885,7 +3885,7 @@ public override int goo
{
get
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class B
public virtual int A { get; set; }
class C : B
{
public override int A { get => base.A$$; set => base.A = value; }
public override int A { get => [|base.A|]; set => base.A = value; }
}
}
""";
Expand Down Expand Up @@ -79,7 +79,7 @@ class B
public virtual int A { get; }
class C : B
{
public override int A => base.A;$$
public override int A => [|base.A|];
}
}
""";
Expand Down Expand Up @@ -107,7 +107,7 @@ class B
public virtual int A() => 2;
class C : B
{
public override int A() => base.A();$$
public override int A() => [|base.A()|];
}
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ partial class c
partial void goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand All @@ -444,7 +444,7 @@ partial class c
public partial void goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand All @@ -471,7 +471,7 @@ partial class c<T>
partial void goo(T bar)
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand All @@ -498,7 +498,7 @@ partial class c<T>
public partial void goo(T bar)
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand All @@ -525,7 +525,7 @@ partial class c
partial void goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand All @@ -552,7 +552,7 @@ partial class c
private partial void goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -585,7 +585,7 @@ partial class c
{
partial void goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -618,7 +618,7 @@ partial class c
{
public partial void goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand All @@ -645,7 +645,7 @@ partial struct c
partial void goo()
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -744,7 +744,7 @@ partial class Bar
async partial void Goo()
{
throw new NotImplementedException();$$
[|throw new NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -775,7 +775,7 @@ partial class Bar
public async partial void Goo()
{
throw new NotImplementedException();$$
[|throw new NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -806,7 +806,7 @@ partial class Bar
partial void Goo()
{
throw new NotImplementedException();$$
[|throw new NotImplementedException();|]
}
}
""";
Expand Down Expand Up @@ -838,7 +838,7 @@ partial class PClass
partial void PMethod(int i)
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
}
Expand Down Expand Up @@ -870,7 +870,7 @@ partial class PClass
public partial void PMethod(int i)
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}
}
}
Expand Down Expand Up @@ -903,7 +903,7 @@ partial class Bar
partial class Bar
{
partial void Foo();
partial void Foo() => throw new NotImplementedException();$$
partial void Foo() => [|throw new NotImplementedException()|];
}
""";

Expand Down Expand Up @@ -936,7 +936,7 @@ partial class Bar
partial class Bar
{
public partial void Foo();
public partial void Foo() => throw new NotImplementedException();$$
public partial void Foo() => [|throw new NotImplementedException()|];
}
"""
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ private AsyncCompletionData.CommitResult Commit(
updatedCurrentSnapshot = edit.Apply();
}

if (change.NewPosition.HasValue)
if (change.NewSelection.HasValue)
{
// Roslyn knows how to position the caret in the snapshot we just created.
// If there were more edits made by extensions, TryMoveCaretToAndEnsureVisible maps the snapshot point to the most recent one.
view.TryMoveCaretToAndEnsureVisible(new SnapshotPoint(updatedCurrentSnapshot, change.NewPosition.Value));
// Roslyn knows how to set the selection in the snapshot we just created.
// If there were more edits made by extensions, TrySetSelectionAndEnsureVisible maps the snapshot point to the most recent one.
view.TrySetSelectionAndEnsureVisible(new SnapshotSpan(updatedCurrentSnapshot, change.NewSelection.Value.ToSpan()));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public static void SetMultiSelection(this ITextView textView, IEnumerable<Snapsh
textView.GetMultiSelectionBroker().SetSelectionRange(spansInView, spansInView.Last());
}

internal static bool TrySetSelectionAndEnsureVisible(this ITextView textView, SnapshotSpan span, IOutliningManagerService? outliningManagerService = null, EnsureSpanVisibleOptions ensureSpanVisibleOptions = EnsureSpanVisibleOptions.None)
{
if (!textView.TryMoveCaretToAndEnsureVisible(new VirtualSnapshotPoint(span.End), outliningManagerService, ensureSpanVisibleOptions))
return false;

SetSelection(textView, span, isReversed: false);
return true;
}

public static bool TryMoveCaretToAndEnsureVisible(this ITextView textView, SnapshotPoint point, IOutliningManagerService? outliningManagerService = null, EnsureSpanVisibleOptions ensureSpanVisibleOptions = EnsureSpanVisibleOptions.None)
=> textView.TryMoveCaretToAndEnsureVisible(new VirtualSnapshotPoint(point), outliningManagerService, ensureSpanVisibleOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Editor.Implementation.IntelliSense.AsyncCompletion;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand Down Expand Up @@ -636,7 +637,7 @@ private async Task VerifyCustomCommitWorkerAsync(
{
using var workspaceFixture = GetOrCreateWorkspaceFixture();

MarkupTestFile.GetPosition(expectedCodeAfterCommit, out var actualExpectedCode, out int expectedCaretPosition);
MarkupTestFile.GetPositionAndSpan(expectedCodeAfterCommit, out var actualExpectedCode, out var expectedCaretPosition, out TextSpan? expectedSelectionSpan);

var options = GetCompletionOptions();

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

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

AssertEx.EqualOrDiff(actualExpectedCode, actualCodeAfterCommit);
Assert.Equal(expectedCaretPosition, caretPosition);

if (expectedSelectionSpan != null)
Assert.Equal(expectedSelectionSpan, selectionSpan);
else if (expectedCaretPosition != null)
Assert.Equal(expectedCaretPosition, caretPosition);
}

private void VerifyCustomCommitWorker(
Expand Down
Loading

0 comments on commit 7cee927

Please sign in to comment.