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

ArgumentOutOfRangeException from completion/resolve on the 1st line of top level program #2123

Closed
filipw opened this issue Mar 26, 2021 · 2 comments · Fixed by #2126
Closed

Comments

@filipw
Copy link
Member

filipw commented Mar 26, 2021

This reproduces for me with an empty .cs file + a regular project file, when trying to use the top level statements.

As I start typing Console I see the following in the logs (using OmniSharp 1.37.6):

[warn]: OmniSharp.Stdio.Host
        ************ Request ************
{
  "Type": "request",
  "Seq": 581,
  "Command": "/completion/resolve",
  "Arguments": {
    "Item": {
      "Label": "Console",
      "Kind": 7,
      "Tags": null,
      "Detail": "System",
      "Documentation": null,
      "Preselect": false,
      "SortText": "1Console System",
      "FilterText": "Console",
      "InsertTextFormat": 1,
      "TextEdit": {
        "NewText": "Console",
        "StartLine": 0,
        "StartColumn": 0,
        "EndLine": 0,
        "EndColumn": 1
      },
      "CommitCharacters": [
        " ",
        "{",
        "}",
        "[",
        "]",
        "(",
        ")",
        ".",
        ",",
        ":",
        ";",
        "+",
        "-",
        "*",
        "/",
        "%",
        "&",
        "|",
        "^",
        "!",
        "~",
        "=",
        "<",
        ">",
        "?",
        "@",
        "#",
        "'",
        "\"",
        "\\"
      ],
      "AdditionalTextEdits": null,
      "Data": 455
    }
  }
}
[fail]: OmniSharp.Stdio.Host
        ************  Response ************ 
{
  "Request_seq": 581,
  "Command": "/completion/resolve",
  "Running": true,
  "Success": false,
  "Message": "\"System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.\\nParameter name: position\\n  at Microsoft.CodeAnalysis.Text.SourceText+LineInfo.IndexOf (System.Int32 position) [0x00012] in <02f1a35137194cc3b7bab26f0cb540aa>:0 \\n  at Microsoft.CodeAnalysis.Text.SourceText+LineInfo.GetLineFromPosition (System.Int32 position) [0x00000] in <02f1a35137194cc3b7bab26f0cb540aa>:0 \\n  at Microsoft.CodeAnalysis.Text.TextLineCollection.GetLinePosition (System.Int32 position) [0x00000] in <02f1a35137194cc3b7bab26f0cb540aa>:0 \\n  at OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService.GetAdditionalTextEdits (Microsoft.CodeAnalysis.Completion.CompletionChange change, Microsoft.CodeAnalysis.Text.SourceText sourceText, Microsoft.CodeAnalysis.CSharp.CSharpParseOptions parseOptions, Microsoft.CodeAnalysis.Text.TextSpan typedSpan, System.String completionDisplayText, System.String providerName) [0x00030] in <c8591cb9164d4c0c9d9cc0ca8a5741e8>:0 \\n  at OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService.Handle (OmniSharp.Models.v1.Completion.CompletionResolveRequest request) [0x004c3] in <c8591cb9164d4c0c9d9cc0ca8a5741e8>:0 \\n  at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].GetFirstNotEmptyResponseFromHandlers (OmniSharp.Endpoint.Exports.ExportHandler`2[TRequest,TResponse][] handlers, TRequest request) [0x00099] in <f1c1a4d731e249dab8abb3b16a7202fe>:0 \\n  at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].HandleRequestForLanguage (System.String language, TRequest request, OmniSharp.Protocol.RequestPacket packet) [0x00163] in <f1c1a4d731e249dab8abb3b16a7202fe>:0 \\n  at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].Process (OmniSharp.Protocol.RequestPacket packet, OmniSharp.Endpoint.LanguageModel model, Newtonsoft.Json.Linq.JToken requestObject) [0x002d8] in <f1c1a4d731e249dab8abb3b16a7202fe>:0 \\n  at OmniSharp.Stdio.Host.HandleRequest (System.String json, Microsoft.Extensions.Logging.ILogger logger) [0x000e8] in <438371f941214689ad1ed79c148e9213>:0 \"",
  "Body": null,
  "Seq": 470,
  "Type": "response"
}

The completion works but because resolve doesn't work, the details are empty:

Bildschirmfoto 2021-03-26 um 20 21 52

This doesn't happen on line 2 anymore:

Bildschirmfoto 2021-03-26 um 20 22 23

@filipw
Copy link
Member Author

filipw commented Mar 26, 2021

looks like it's happening here

var additionalEditEndPosition = sourceText.Lines.GetLinePosition(typedSpan.Start - 1);
and it ends up being -1 since we are on line 0.

@333fred do you remember, is this subtraction intentional? I thought we are now on zero based indices

@333fred
Copy link
Contributor

333fred commented Mar 26, 2021

@333fred do you remember, is this subtraction intentional? I thought we are now on zero based indices

Yes, it was intentional. This is because we don't want the current cursor location to intersect with the additional text edit. I'm not entirely sure whether we can make it just work without the -1, it'll need to be tested in vscode to make sure it doesn't get overwritten.

333fred added a commit to 333fred/omnisharp-roslyn that referenced this issue Apr 2, 2021
Recently, Roslyn undeprecated the CompletionChange.TextChanges API. This API gives changes in a significantly simpler manner for us to deal with, as it splits up things like imports and the actual main change element, so we can remove a whole bunch of code dedicated to finding the changed elements and mapping original source locations to the new document. The new pattern is much simpler: except for import completion, we always resolve the change up front. For most providers, this is an extremely quick call, as most providers just return the label, and for the ones that don't we can't do much about it anyway. We can then loop through the individual changes, putting changes that are not touching the current cursor location into AdditionalTextChanges. This shrinks the completion payload pretty significantly for many scenarios, and gets rid of a bunch of special handling around it. The only remaining special handling is adjusting the filter texts and snippitizing completions that want to move the cursor. I've also aligned the behavior of the Preselect flag with Roslyn's completion handler, and removed additional filtering of completion items so that they can be filtered by the client instead.

Fixes OmniSharp#2123 as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants