Skip to content
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

Override completion can mess up based on context #1907

Closed
333fred opened this issue Aug 21, 2020 · 5 comments · Fixed by #1908
Closed

Override completion can mess up based on context #1907

333fred opened this issue Aug 21, 2020 · 5 comments · Fixed by #1908

Comments

@333fred
Copy link
Contributor

333fred commented Aug 21, 2020

[warn]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService
        Could not find the first index of the display text.
Display text: IsDefinedInSourceTree(SyntaxTree tree, TextSpan? definedWithinSpan, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)).
Completion Text: using System.Threading;

namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
    internal abstract class SourceUserDefinedOperatorSymbolBase : SourceMemberMethodSymbol
    {
        internal override bool IsDefinedInSourceTree(SyntaxTree tree, TextSpan? definedWithinSpan, CancellationToken cancellationToken = default)
        {
            return base.IsDefinedInSourceTree(tree, definedWithinSpan, cancellationToken);
        }

This can happen when the DisplayText uses fully-qualified names, but the actual change adds an import, and then minimally qualifies the result/simplifies default/plays around with nullable. This then causes an exception because we assign default(ImmutableArray<LinePositionChange> to additionalTextEdits, rather than assigning null, which causes Newtonsoft.Json to barf.

@333fred
Copy link
Contributor Author

333fred commented Aug 21, 2020

Note there are 2 bugs here:

  1. If we fail to find where to make the additional edits, we should not use a default array. https://github.com/OmniSharp/omnisharp-roslyn/blob/master/src/OmniSharp.Roslyn.CSharp/Services/Completion/CompletionService.cs#L455 needs to change to return ImmutableArray<LinePositionSpanTextChange>?.
  2. We need a workaround for this case of override completion where the displaytext is not in the change.

@filipw
Copy link
Member

filipw commented Aug 21, 2020

on that note, I think ImmutableArray should be avoided on JSON serializable contract types, we can move those to IList to be safe. I remember there were similar issues in Roslyn in the past e.g. dotnet/roslyn#21539

@333fred
Copy link
Contributor Author

333fred commented Aug 21, 2020

I'd prefer IReadonlyList, but yes. I really wanted to use ImmutableArray because it's both threadsafe and performant

@333fred
Copy link
Contributor Author

333fred commented Aug 21, 2020

What I think will work as a simple algorithm for finding the correct position in the change text for override completion is to find the last instance of the text override (including the spaces). From there, find the first instance of the display text up to but not including the first ( in that text.

@333fred
Copy link
Contributor Author

333fred commented Aug 21, 2020

Import completion shouldn't have this issue and there's nothing qualified in the DisplayText.

333fred added a commit to 333fred/omnisharp-roslyn that referenced this issue Aug 21, 2020
…sult

Override and partial method completion can have scenarios where the DisplayText of the completion item does not appear in the final text, such as when types need to be imported or when nullability differs between original declaration and generation declaration sites. We now handle these by parsing the change if we fail to find the insertion text, and using the last method in the change to find the correct location in the new text. Fixes OmniSharp#1907.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants