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

lsp: add references support for links #58

Merged
merged 5 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
* `{{json title}}` prints with quotes `"An interesting note"`
* `{{json .}}` serializes the full template context as a JSON object.
* Use `--header` and `--footer` options with `zk list` to print arbitrary text at the start or end of the list.
* Support for LSP references to browse the backlinks of the link under the caret (contributed by [@pstuifzand](https://github.com/mickael-menu/zk/pull/58)).

### Fixed

Expand Down
67 changes: 67 additions & 0 deletions internal/adapter/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func NewServer(opts ServerOpts) *Server {
ResolveProvider: boolPtr(true),
}

capabilities.ReferencesProvider = &protocol.ReferenceOptions{}

return protocol.InitializeResult{
Capabilities: capabilities,
ServerInfo: &protocol.InitializeResultServerInfo{
Expand Down Expand Up @@ -389,6 +391,71 @@ func NewServer(opts ServerOpts) *Server {
return actions, nil
}

handler.TextDocumentReferences = func(context *glsp.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
doc, ok := server.documents.Get(params.TextDocument.URI)
if !ok {
return nil, nil
}

link, err := doc.DocumentLinkAt(params.Position)
if link == nil || err != nil {
return nil, err
}

notebook, err := server.notebookOf(doc)
if err != nil {
return nil, err
}

target, err := server.noteForHref(link.Href, doc, notebook)
if link == nil || target == nil || err != nil {
return nil, err
}

p, err := notebook.RelPath(target.Path)
if err != nil {
return nil, err
}

opts := core.NoteFindOpts{
LinkTo: &core.LinkFilter{Paths: []string{p}},
}

notes, err := notebook.FindNotes(opts)
if err != nil {
return nil, err
}

var locations []protocol.Location

for _, note := range notes {
pos := strings.Index(note.RawContent, target.Path[0:len(target.Path)-3])
pstuifzand marked this conversation as resolved.
Show resolved Hide resolved
var line uint32 = 0
if pos < 0 {
line = 0
} else {
linePos := strings.Count(note.RawContent[0:pos], "\n")
line = uint32(linePos)
}

locations = append(locations, protocol.Location{
URI: pathToURI(filepath.Join(notebook.Path, note.Path)),
Range: protocol.Range{
Start: protocol.Position{
Line: line,
Character: 0,
},
End: protocol.Position{
Line: line,
Character: 0,
},
},
})
}

return locations, nil
}

return server
}

Expand Down