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

feat(lsp): add 'zk.link' LSP command #284

Merged
merged 5 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions internal/adapter/lsp/cmd_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package lsp

import (
"fmt"
"path/filepath"

"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util/errors"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)

const cmdLink = "zk.link"

type cmdLinkOpts struct {
Path *string `json:"path"`
Location *protocol.Location `json:"location"`
Title *string `json:"title"`
}

func executeCommandLink(notebook *core.Notebook, documents *documentStore, context *glsp.Context, args []interface{}) (interface{}, error) {
var opts cmdLinkOpts

if len(args) > 1 {
arg, ok := args[1].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("%s expects a dictionary of options as second argument, got: %v", cmdLink, args[1])
}
err := unmarshalJSON(arg, &opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s args, got: %v", cmdLink, arg)
}
}

if opts.Path == nil {
return nil, errors.New("'path' not provided")
}

note, err := notebook.FindByHref(*opts.Path, false)

if err != nil || note == nil {
return nil, errors.New("Requested note to link to not found!")
}
psanker marked this conversation as resolved.
Show resolved Hide resolved

info := &linkInfo{
note: note,
location: opts.Location,
title: opts.Title,
}

ok, err := linkNote(notebook, documents, context, info)

if !ok {
return nil, err
}

return map[string]interface{}{
"ok": true,
psanker marked this conversation as resolved.
Show resolved Hide resolved
"path": filepath.Join(notebook.Path, note.Path),
}, nil
}
39 changes: 10 additions & 29 deletions internal/adapter/lsp/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,37 +83,18 @@ func executeCommandNew(notebook *core.Notebook, documents *documentStore, contex
}

if !opts.DryRun && opts.InsertLinkAtLocation != nil {
doc, ok := documents.Get(opts.InsertLinkAtLocation.URI)
if !ok {
return nil, fmt.Errorf("can't insert link in %s", opts.InsertLinkAtLocation.URI)
}
linkFormatter, err := notebook.NewLinkFormatter()
if err != nil {
return nil, err
}
minNote := note.AsMinimalNote()

path := core.NotebookPath{
Path: note.Path,
BasePath: notebook.Path,
WorkingDir: filepath.Dir(doc.Path),
}
linkFormatterContext, err := core.NewLinkFormatterContext(path, note.Title, note.Metadata)
if err != nil {
return nil, err
}
info := &linkInfo{
note: &minNote,
location: opts.InsertLinkAtLocation,
title: &opts.Title,
}
ok, err := linkNote(notebook, documents, context, info)

link, err := linkFormatter(linkFormatterContext)
if err != nil {
return nil, err
}

go context.Call(protocol.ServerWorkspaceApplyEdit, protocol.ApplyWorkspaceEditParams{
Edit: protocol.WorkspaceEdit{
Changes: map[string][]protocol.TextEdit{
opts.InsertLinkAtLocation.URI: {{Range: opts.InsertLinkAtLocation.Range, NewText: link}},
},
},
}, nil)
if !ok {
return nil, err
}
}

absPath := filepath.Join(notebook.Path, note.Path)
Expand Down
7 changes: 7 additions & 0 deletions internal/adapter/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ func NewServer(opts ServerOpts) *Server {
}
return executeCommandNew(nb, server.documents, context, params.Arguments)

case cmdLink:
nb, err := openNotebook()
if err != nil {
return nil, err
}
return executeCommandLink(nb, server.documents, context, params.Arguments)

case cmdList:
nb, err := openNotebook()
if err != nil {
Expand Down
60 changes: 60 additions & 0 deletions internal/adapter/lsp/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package lsp
import (
"fmt"
"net/url"
"path/filepath"
"runtime"
"strings"

"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util/errors"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)

func pathToURI(path string) string {
Expand Down Expand Up @@ -56,3 +60,59 @@ func (b *jsonBoolean) UnmarshalJSON(data []byte) error {
}
return nil
}

type linkInfo struct {
note *core.MinimalNote
location *protocol.Location
title *string
}

func linkNote(notebook *core.Notebook, documents *documentStore, context *glsp.Context, info *linkInfo) (bool, error) {
psanker marked this conversation as resolved.
Show resolved Hide resolved
if info.location == nil {
return false, errors.New("'location' not provided")
}

// Get current document to edit
doc, ok := documents.Get(info.location.URI)
if !ok {
return false, fmt.Errorf("Cannot insert link in '%s'", info.location.URI)
}

formatter, err := notebook.NewLinkFormatter()
if err != nil {
return false, err
}

path := core.NotebookPath{
Path: info.note.Path,
BasePath: notebook.Path,
WorkingDir: filepath.Dir(doc.Path),
}

var title *string
title = info.title

if title == nil {
title = &info.note.Title
}

formatterContext, err := core.NewLinkFormatterContext(path, *title, info.note.Metadata)
if err != nil {
return false, err
}

link, err := formatter(formatterContext)
if err != nil {
return false, err
}

go context.Call(protocol.ServerWorkspaceApplyEdit, protocol.ApplyWorkspaceEditParams{
Edit: protocol.WorkspaceEdit{
Changes: map[string][]protocol.TextEdit{
info.location.URI: {{Range: info.location.Range, NewText: link}},
},
},
}, nil)

return true, nil
}