Skip to content

Change override completion to select text after updating the buffer. #76983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1443,7 +1443,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}

set
Expand Down Expand Up @@ -1482,7 +1482,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}

set
Expand Down Expand Up @@ -1520,7 +1520,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}

set
Expand Down Expand Up @@ -1558,7 +1558,7 @@ public override int goo
{
set
{
base.goo = value;$$
[|base.goo = value;|]
}
}
}";
Expand Down Expand Up @@ -1590,7 +1590,7 @@ public override int goo
{
get
{
return base.goo;$$
[|return base.goo;|]
}
}
}";
Expand Down Expand Up @@ -1647,7 +1647,7 @@ public override int this[[MyPublic] int i]
{
get
{
return base[i];$$
[|return base[i];|]
}

set
Expand Down Expand Up @@ -1915,7 +1915,7 @@ public override T this[int i]
{
get
{
return base[i];$$
[|return base[i];|]
}

set
Expand Down Expand Up @@ -1954,7 +1954,7 @@ public override T this[int i]
{
get
{
throw new System.NotImplementedException();$$
[|throw new System.NotImplementedException();|]
}

set
Expand Down Expand Up @@ -2123,7 +2123,7 @@ public override int @class
{
get
{
return base.@class;$$
[|return base.@class;|]
}

set
Expand Down Expand Up @@ -2406,7 +2406,7 @@ public override int Goo
{
get
{
return base.Goo;$$
[|return base.Goo;|]
}
}
}
Expand Down Expand Up @@ -2822,7 +2822,7 @@ public override required int Prop
{
get
{
return base.Prop;$$
[|return base.Prop;|]
}
}
}
Expand Down Expand Up @@ -2861,7 +2861,7 @@ public override required int Prop
{
get
{
return base.Prop;$$
[|return base.Prop;|]
}
}
}
Expand Down Expand Up @@ -2900,7 +2900,7 @@ public override required int Prop
{
get
{
return base.Prop;$$
[|return base.Prop;|]
}
}
}
Expand Down Expand Up @@ -2967,7 +2967,7 @@ public override int this[int i]
{
get
{
return base[i];$$
[|return base[i];|]
}

set
Expand Down Expand Up @@ -3098,7 +3098,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
Loading