zk-org / zk

A plain text note-taking assistant
https://zk-org.github.io/zk/
GNU General Public License v3.0
1.66k stars 122 forks source link

LSP Go-to-reference does not work, if notes are in a folder. #243

Closed bibor closed 2 years ago

bibor commented 2 years ago

Description

The find-references feature of the LSP (tracked in #22) does not work if the notes are organized in folders.

Expected Behaviour

find-reference should return all references to the given note.

Steps to Reproduce

  1. have a zettelkasten with the following layout
    $ tree 
    .
    └── old
    ├── eso7.md
    ├── jbxt.md
    └── vlix.md
  2. set a link from vlix.md to jbxt.md
    
    # top-note

blub

I reference another note: ref-note



3. open `jbxt.md`
4. call find-references on the language server client.

<details>
<summary>The links are recognized by `zk` as the graph output shows: </summary>
<pre>
$ zk graph --format=json | jq
{
  "notes": [
    {
      "filename": "eso7.md",
      "filenameStem": "eso7",
      "path": "old/eso7.md",
      "absPath": "/home/user/Documents/zk-test2/old/eso7.md",
      "title": "newly generated note",
      "link": "[newly generated note](old/eso7)",
      "lead": "generated with `zk new` directly in the old folder",
      "body": "generated with `zk new` directly in the old folder",
      "snippets": [
        "generated with `zk new` directly in the old folder"
      ],
      "rawContent": "# newly generated note\n\ngenerated with `zk new` directly in the old folder\n\n",
      "wordCount": 13,
      "tags": [],
      "metadata": {},
      "created": "2022-07-12T09:21:40.15040095Z",
      "modified": "2022-07-12T09:22:06.859619184Z",
      "checksum": "002388945a56d9be780edc458230055057439a5c89eb6474eb9f1490c906a1bb"
    },
    {
      "filename": "jbxt.md",
      "filenameStem": "jbxt",
      "path": "old/jbxt.md",
      "absPath": "/home/user/Documents/zk-test2/old/jbxt.md",
      "title": "ref-note",
      "link": "[ref-note](old/jbxt)",
      "lead": "Blub",
      "body": "Blub\n\nOriginal note: [top-note](vlix)",
      "snippets": [
        "Blub"
      ],
      "rawContent": "# ref-note\n\nBlub\n\nOriginal note: [top-note](vlix)\n",
      "wordCount": 6,
      "tags": [],
      "metadata": {},
      "created": "2022-07-12T09:16:54.491128191Z",
      "modified": "2022-07-12T09:18:30.498392496Z",
      "checksum": "6b54952eb7a1442bc0b778fe49ee00e307ffb23e3622f8d9cb54a1fe841ff076"
    },
    {
      "filename": "vlix.md",
      "filenameStem": "vlix",
      "path": "old/vlix.md",
      "absPath": "/home/user/Documents/zk-test2/old/vlix.md",
      "title": "top-note",
      "link": "[top-note](old/vlix)",
      "lead": "blub",
      "body": "blub\n\nI reference another note: [ref-note](jbxt)",
      "snippets": [
        "blub"
      ],
      "rawContent": "# top-note\n\nblub\n\nI reference another note: [ref-note](jbxt)\n",
      "wordCount": 8,
      "tags": [],
      "metadata": {},
      "created": "2022-07-12T09:16:54.491451242Z",
      "modified": "2022-07-12T08:33:16.160193606Z",
      "checksum": "383e4807211ba6d5f066efa09a70b6ca628c4e50fe080f4b6fb623c9e4f81fc2"
    }
  ],
  "links": [
    {
      "title": "top-note",
      "href": "old/vlix",
      "type": "markdown",
      "isExternal": false,
      "rels": [],
      "snippet": "Original note: [top-note](vlix)",
      "snippetStart": 18,
      "snippetEnd": 49,
      "sourceId": 3,
      "sourcePath": "old/jbxt.md",
      "targetId": 4,
      "targetPath": "old/vlix.md"
    },
    {
      "title": "ref-note",
      "href": "old/jbxt",
      "type": "markdown",
      "isExternal": false,
      "rels": [],
      "snippet": "I reference another note: [ref-note](jbxt)",
      "snippetStart": 18,
      "snippetEnd": 60,
      "sourceId": 4,
      "sourcePath": "old/vlix.md",
      "targetId": 3,
      "targetPath": "old/jbxt.md"
    }
  ]
}
</pre>
</details>

## my environment
`zk 0.11.0` cloned from master. Language server client: `vim-lsp`. Everything works fine, if the notes are in the root folder of a zettelkasten.
mickael-menu commented 2 years ago

@bibor Thanks for spotting that, it should be fixed in main. Let me know if you see regressions as the LSP server doesn't have automated tests yet.

bibor commented 2 years ago

Thank you very much! Unfortunatley the fix introduces a new bug, resulting in the go-to-reference feature still not working.

notebook.RelPath() is called on a relative path from the root, but it expands the path relative to the note. This causes a new, wrong relative path, because it is calculated from the note's paths.

for example an input of folder/note produces folder/folder/note.

I think it's possible to just remove the second call to RelPath(), but I'm not 100% sure, that this does not produce any unwanted side-effects. However, everything was working fine, when I tested it. I'll open a PR.