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
Changes from 2 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
75 changes: 75 additions & 0 deletions internal/adapter/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func NewServer(opts ServerOpts) *Server {
ResolveProvider: boolPtr(true),
}

capabilities.ReferencesProvider = &protocol.ReferenceOptions{
WorkDoneProgressOptions: protocol.WorkDoneProgressOptions{
WorkDoneProgress: boolPtr(false),
},
}
pstuifzand marked this conversation as resolved.
Show resolved Hide resolved

return protocol.InitializeResult{
Capabilities: capabilities,
ServerInfo: &protocol.InitializeResultServerInfo{
Expand Down Expand Up @@ -389,6 +395,75 @@ 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
}

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

opts.LinkTo = &core.LinkFilter{
Paths: []string{p},
Negate: false,
Recursive: true,
MaxDistance: 1,
}
pstuifzand marked this conversation as resolved.
Show resolved Hide resolved

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: "file://" + filepath.Join(notebook.Path, note.Path),
pstuifzand marked this conversation as resolved.
Show resolved Hide resolved
Range: protocol.Range{
Start: protocol.Position{
Line: line,
Character: 0,
},
End: protocol.Position{
Line: line,
Character: 0,
},
},
})
}

return locations, nil
}

return server
}

Expand Down