Add the zk.tag.list LSP command

pull/114/head
Mickaël Menu 3 years ago
parent 7783556aeb
commit 3662148918

@ -8,15 +8,7 @@ import (
const cmdIndex = "zk.index"
func executeCommandIndex(notebooks *core.NotebookStore, args []interface{}) (interface{}, error) {
if len(args) == 0 {
return nil, fmt.Errorf("zk.index expects a notebook path as first argument")
}
path, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf("zk.index expects a notebook path as first argument, got: %v", args[0])
}
func executeCommandIndex(notebook *core.Notebook, args []interface{}) (interface{}, error) {
opts := core.NoteIndexOpts{}
if len(args) == 2 {
options, ok := args[1].(map[string]interface{})
@ -31,10 +23,5 @@ func executeCommandIndex(notebooks *core.NotebookStore, args []interface{}) (int
}
}
notebook, err := notebooks.Open(path)
if err != nil {
return nil, err
}
return notebook.Index(opts)
}

@ -26,32 +26,19 @@ type cmdNewOpts struct {
InsertLinkAtLocation *protocol.Location `json:"insertLinkAtLocation,omitempty"`
}
func executeCommandNew(notebooks *core.NotebookStore, documents *documentStore, context *glsp.Context, args []interface{}) (interface{}, error) {
if len(args) == 0 {
return nil, fmt.Errorf("zk.index expects a notebook path as first argument")
}
wd, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf("zk.index expects a notebook path as first argument, got: %v", args[0])
}
func executeCommandNew(notebook *core.Notebook, documents *documentStore, context *glsp.Context, args []interface{}) (interface{}, error) {
var opts cmdNewOpts
if len(args) > 1 {
arg, ok := args[1].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("zk.new expects a dictionary of options as second argument, got: %v", args[1])
return nil, fmt.Errorf("%s expects a dictionary of options as second argument, got: %v", cmdNew, args[1])
}
err := unmarshalJSON(arg, &opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse zk.new args, got: %v", arg)
return nil, errors.Wrapf(err, "failed to parse %s args, got: %v", cmdNew, arg)
}
}
notebook, err := notebooks.Open(wd)
if err != nil {
return nil, err
}
date, err := dateutil.TimeFromNatural(opts.Date)
if err != nil {
return nil, errors.Wrapf(err, "%s, failed to parse the `date` option", opts.Date)

@ -0,0 +1,40 @@
package lsp
import (
"fmt"
"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util"
"github.com/mickael-menu/zk/internal/util/errors"
)
const cmdTagList = "zk.tag.list"
type cmdTagListOpts struct {
Sort []string `json:"sort,omitempty"`
}
func executeCommandTagList(logger util.Logger, notebook *core.Notebook, args []interface{}) (interface{}, error) {
var opts cmdTagListOpts
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", cmdTagList, args[1])
}
err := unmarshalJSON(arg, &opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s args, got: %v", cmdTagList, arg)
}
}
var sorters []core.CollectionSorter
var err error
if opts.Sort != nil {
sorters, err = core.CollectionSortersFromStrings(opts.Sort)
if err != nil {
return nil, err
}
}
logger.Printf("sorters: %+v\n", sorters)
return notebook.FindCollections(core.CollectionKindTag, sorters)
}

@ -99,6 +99,7 @@ func NewServer(opts ServerOpts) *Server {
Commands: []string{
cmdIndex,
cmdNew,
cmdTagList,
},
}
capabilities.CompletionProvider = &protocol.CompletionOptions{
@ -336,11 +337,42 @@ func NewServer(opts ServerOpts) *Server {
}
handler.WorkspaceExecuteCommand = func(context *glsp.Context, params *protocol.ExecuteCommandParams) (interface{}, error) {
openNotebook := func() (*core.Notebook, error) {
args := params.Arguments
if len(args) == 0 {
return nil, fmt.Errorf("%s expects a notebook path as first argument", params.Command)
}
path, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf("%s expects a notebook path as first argument, got: %v", params.Command, args[0])
}
return server.notebooks.Open(path)
}
switch params.Command {
case cmdIndex:
return executeCommandIndex(server.notebooks, params.Arguments)
nb, err := openNotebook()
if err != nil {
return nil, err
}
return executeCommandIndex(nb, params.Arguments)
case cmdNew:
return executeCommandNew(server.notebooks, server.documents, context, params.Arguments)
nb, err := openNotebook()
if err != nil {
return nil, err
}
return executeCommandNew(nb, server.documents, context, params.Arguments)
case cmdTagList:
nb, err := openNotebook()
if err != nil {
return nil, err
}
return executeCommandTagList(server.logger, nb, params.Arguments)
default:
return nil, fmt.Errorf("unknown zk LSP command: %s", params.Command)
}

@ -79,7 +79,7 @@ func (d *CollectionDAO) FindOrCreate(kind core.CollectionKind, name string) (cor
func (d *CollectionDAO) FindAll(kind core.CollectionKind, sorters []core.CollectionSorter) ([]core.Collection, error) {
query := `
SELECT c.name, COUNT(nc.id) as count
SELECT c.id, c.name, COUNT(nc.id) as count
FROM collections c
INNER JOIN notes_collections nc ON nc.collection_id = c.id
WHERE kind = ?
@ -104,14 +104,16 @@ func (d *CollectionDAO) FindAll(kind core.CollectionKind, sorters []core.Collect
collections := []core.Collection{}
for rows.Next() {
var id sql.NullInt64
var name string
var count int
err := rows.Scan(&name, &count)
err := rows.Scan(&id, &name, &count)
if err != nil {
return collections, err
}
collections = append(collections, core.Collection{
ID: core.CollectionID(id.Int64),
Kind: kind,
Name: name,
NoteCount: count,

@ -43,11 +43,11 @@ func TestCollectionDaoFindAll(t *testing.T) {
cs, err = dao.FindAll("tag", nil)
assert.Nil(t, err)
assert.Equal(t, cs, []core.Collection{
{Kind: "tag", Name: "adventure", NoteCount: 2},
{Kind: "tag", Name: "fantasy", NoteCount: 1},
{Kind: "tag", Name: "fiction", NoteCount: 1},
{Kind: "tag", Name: "history", NoteCount: 1},
{Kind: "tag", Name: "science", NoteCount: 3},
{ID: 2, Kind: "tag", Name: "adventure", NoteCount: 2},
{ID: 4, Kind: "tag", Name: "fantasy", NoteCount: 1},
{ID: 1, Kind: "tag", Name: "fiction", NoteCount: 1},
{ID: 5, Kind: "tag", Name: "history", NoteCount: 1},
{ID: 7, Kind: "tag", Name: "science", NoteCount: 3},
})
})
}
@ -59,11 +59,11 @@ func TestCollectionDaoFindAllSortedByName(t *testing.T) {
})
assert.Nil(t, err)
assert.Equal(t, cs, []core.Collection{
{Kind: "tag", Name: "science", NoteCount: 3},
{Kind: "tag", Name: "history", NoteCount: 1},
{Kind: "tag", Name: "fiction", NoteCount: 1},
{Kind: "tag", Name: "fantasy", NoteCount: 1},
{Kind: "tag", Name: "adventure", NoteCount: 2},
{ID: 7, Kind: "tag", Name: "science", NoteCount: 3},
{ID: 5, Kind: "tag", Name: "history", NoteCount: 1},
{ID: 1, Kind: "tag", Name: "fiction", NoteCount: 1},
{ID: 4, Kind: "tag", Name: "fantasy", NoteCount: 1},
{ID: 2, Kind: "tag", Name: "adventure", NoteCount: 2},
})
})
}
@ -75,11 +75,11 @@ func TestCollectionDaoFindAllSortedByNoteCount(t *testing.T) {
})
assert.Nil(t, err)
assert.Equal(t, cs, []core.Collection{
{Kind: "tag", Name: "science", NoteCount: 3},
{Kind: "tag", Name: "adventure", NoteCount: 2},
{Kind: "tag", Name: "fantasy", NoteCount: 1},
{Kind: "tag", Name: "fiction", NoteCount: 1},
{Kind: "tag", Name: "history", NoteCount: 1},
{ID: 7, Kind: "tag", Name: "science", NoteCount: 3},
{ID: 2, Kind: "tag", Name: "adventure", NoteCount: 2},
{ID: 4, Kind: "tag", Name: "fantasy", NoteCount: 1},
{ID: 1, Kind: "tag", Name: "fiction", NoteCount: 1},
{ID: 5, Kind: "tag", Name: "history", NoteCount: 1},
})
})
}

@ -9,13 +9,13 @@ import (
// Collection represents a collection, such as a tag.
type Collection struct {
// Unique ID of this collection in the Notebook.
ID CollectionID
ID CollectionID `json:"id"`
// Kind of this note collection, such as a tag.
Kind CollectionKind
Kind CollectionKind `json:"kind"`
// Name of this collection.
Name string
Name string `json:"name"`
// Number of notes associated with this collection.
NoteCount int
NoteCount int `json:"note_count"`
}
// CollectionID represents the unique ID of a collection relative to a given

Loading…
Cancel
Save