nix-community / rnix-lsp

WIP Language Server for Nix! [maintainer=@aaronjanse]
MIT License
701 stars 41 forks source link

autocompletion is not working in vscode: no suggestions #103

Open milahu opened 1 year ago

milahu commented 1 year ago

Describe the bug

vscode says "no suggestions"

autocomplete works for javascript files, so i blame rnix-lsp

Steps to reproduce

edit this nix file

# autocomplete
let pkgs = {
  hello = "echo hello";
  a.b.c = "hello nested";
}; in pkgs.
#12345678901
#          ^ 4:11 = cursor
#         ^ write a dot, press Ctrl+Space

response from rnix-lsp looks ok

jsonrpc: request + response ```json { "jsonrpc": "2.0", "id": 4, "method": "textDocument/completion", "params": { "textDocument": { "uri": "file:///tmp/test-lsp.nix" }, "position": { "line": 4, "character": 11 }, "context": { "triggerKind": 1 } } } ``` ```json { "jsonrpc": "2.0", "id": 4, "result": [ { "deprecated": false, "detail": "Attribute", "label": "hello", "textEdit": { "newText": "hello", "range": { "end": { "character": 10, "line": 4 }, "start": { "character": 6, "line": 4 } } } }, { "deprecated": false, "detail": "Attribute", "label": "a", "textEdit": { "newText": "a", "range": { "end": { "character": 10, "line": 4 }, "start": { "character": 6, "line": 4 } } } } ] } ```

jsonrpc traffic was captured with

rnix-lsp-wrapper.sh ```sh #! /bin/sh # rnix-lsp-wrapper.sh cd "$(dirname "$0")" tee -a rnix-lsp.in.txt | ./rnix-lsp | tee -a rnix-lsp.out.txt ```

Expected behavior

show suggestions

Additional context

vscodium

Version: 1.70.0
Commit: da76f93349a72022ca4670c1b84860304616aaa2
Date: 2022-08-05T11:26:33.437Z
Electron: 18.3.5
Chromium: 100.0.4896.160
Node.js: 16.13.2
V8: 10.0.139.17-electron.0
OS: Linux x64 5.15.67

Editor configuration

{
  "nix.enableLanguageServer": true,
  "nix.serverPath": "/tmp/rnix-lsp/target/debug/rnix-lsp-wrapper.sh"
}
milahu commented 1 year ago

on closer look, the textEdit range is wrong

actual: range of pkgs. in line 4

}; in pkgs.
01234567890
{
        "range": {
          "end": {
            "character": 10,
            "line": 4
          },
          "start": {
            "character": 6,
            "line": 4
          }
        }
}

expected: start == end == 4:11

{
        "range": {
          "end": {
            "character": 11,
            "line": 4
          },
          "start": {
            "character": 11,
            "line": 4
          }
        }
}

spec: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit

interface TextEdit {
    /**
     * The range of the text document to be manipulated. To insert
     * text into a document create a range where start === end.
     */
    range: Range;

    /**
     * The string to be inserted. For delete operations use an
     * empty string.
     */
    newText: string;
}

to compare, this works: completion from prefix

```nix # autocomplete let pkgs = { hello = "echo hello"; a.b.c = "hello nested"; }; in pkgs.hel #12345678901234 # ^ 4:14 ``` ```json { "jsonrpc": "2.0", "id": 8, "method": "textDocument/completion", "params": { "textDocument": { "uri": "file:///tmp/test-lsp.nix" }, "position": { "line": 4, "character": 14 }, "context": { "triggerKind": 1 } } } ``` ```json { "jsonrpc": "2.0", "id": 8, "result": [ { "deprecated": false, "detail": "Attribute", "label": "hello", "textEdit": { "newText": "hello", "range": { "end": { "character": 14, "line": 4 }, "start": { "character": 11, "line": 4 } } } } ] } ```