From ae3a86dbfaf9e3fa5d979e06f0435087d68dbf0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Menu?= Date: Mon, 19 Dec 2022 14:28:15 +0100 Subject: [PATCH] Add new options for LSP command `zk.new` (#276) --- CHANGELOG.md | 8 ++++++- docs/editors-integration.md | 33 +++++++++++++++------------ internal/adapter/lsp/cmd_new.go | 40 +++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd0cc28..76f8373 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ All notable changes to this project will be documented in this file. - +## Unreleased + +### Added + +* LSP: `zk.new` now returns the created note's content in its output (`content`), and has two new options: + * `dryRun` will prevent `zk.new` from creating the note on the file system. + * `insertContentAtLocation` can be used to insert the created note's content into an arbitrary location. ## 0.12.0 diff --git a/docs/editors-integration.md b/docs/editors-integration.md index 6506631..beb79de 100644 --- a/docs/editors-integration.md +++ b/docs/editors-integration.md @@ -150,19 +150,21 @@ This LSP command calls `zk new` to create a new note. It can be useful to quickl 1. A path to any file or directory in the notebook, to locate it. 2.
(Optional) A dictionary of additional options (click to expand) - | Key | Type | Description | - |------------------------|----------------------|-------------------------------------------------------------------------------------------| - | `title` | string | Title of the new note | - | `content` | string | Initial content of the note | - | `dir` | string | Parent directory, relative to the root of the notebook | - | `group` | string | [Note configuration group](config-group.md) | - | `template` | string | [Custom template used to render the note](template-creation.md) | - | `extra` | dictionary | A dictionary of extra variables to expand in the template | - | `date` | string | A date of creation for the note in natural language, e.g. "tomorrow" | - | `edit` | boolean | When true, the editor will open the newly created note (**not supported by all editors**) | - | `insertLinkAtLocation` | location1 | A location in another note where a link to the new note will be inserted | - - The `location` type is an [LSP Location object](https://microsoft.github.io/language-server-protocol/specification#location), for example: + | Key | Type | Description | + |---------------------------|----------------------|----------------------------------------------------------------------------------------------------------------------| + | `title` | string | Title of the new note | + | `content` | string | Initial content of the note | + | `dir` | string | Parent directory, relative to the root of the notebook | + | `group` | string | [Note configuration group](config-group.md) | + | `template` | string | [Custom template used to render the note](template-creation.md) | + | `extra` | dictionary | A dictionary of extra variables to expand in the template | + | `date` | string | A date of creation for the note in natural language, e.g. "tomorrow" | + | `edit` | boolean | When true, the editor will open the newly created note (**not supported by all editors**) | + | `dryRun` | boolean | When true, `zk` will not actually create the note on the file system, but will return its generated content and path | + | `insertLinkAtLocation` | location1 | A location in another note where a link to the new note will be inserted | + | `insertContentAtLocation` | location1 | A location in another note where the content of the new note will be inserted | + + 1. The `location` type is an [LSP Location object](https://microsoft.github.io/language-server-protocol/specification#location), for example: ```json { @@ -175,7 +177,10 @@ This LSP command calls `zk new` to create a new note. It can be useful to quickl ```
-`zk.new` returns a dictionary with the key `path` containing the absolute path to the newly created file. +`zk.new` returns a dictionary with two properties: + +* `path` containing the absolute path to the created note. +* `content` containing the raw content of the created note. #### `zk.list` diff --git a/internal/adapter/lsp/cmd_new.go b/internal/adapter/lsp/cmd_new.go index 23dfdc4..66b3a3e 100644 --- a/internal/adapter/lsp/cmd_new.go +++ b/internal/adapter/lsp/cmd_new.go @@ -15,15 +15,17 @@ import ( const cmdNew = "zk.new" type cmdNewOpts struct { - Title string `json:"title"` - Content string `json:"content"` - Dir string `json:"dir"` - Group string `json:"group"` - Template string `json:"template"` - Extra map[string]string `json:"extra"` - Date string `json:"date"` - Edit jsonBoolean `json:"edit"` - InsertLinkAtLocation *protocol.Location `json:"insertLinkAtLocation"` + Title string `json:"title"` + Content string `json:"content"` + Dir string `json:"dir"` + Group string `json:"group"` + Template string `json:"template"` + Extra map[string]string `json:"extra"` + Date string `json:"date"` + Edit jsonBoolean `json:"edit"` + DryRun jsonBoolean `json:"dryRun"` + InsertLinkAtLocation *protocol.Location `json:"insertLinkAtLocation"` + InsertContentAtLocation *protocol.Location `json:"insertContentAtLocation"` } func executeCommandNew(notebook *core.Notebook, documents *documentStore, context *glsp.Context, args []interface{}) (interface{}, error) { @@ -51,6 +53,7 @@ func executeCommandNew(notebook *core.Notebook, documents *documentStore, contex Group: opt.NewNotEmptyString(opts.Group), Template: opt.NewNotEmptyString(opts.Template), Extra: opts.Extra, + DryRun: bool(opts.DryRun), Date: date, }) if err != nil { @@ -69,7 +72,17 @@ func executeCommandNew(notebook *core.Notebook, documents *documentStore, contex return nil, errors.New("zk.new could not generate a new note") } - if opts.InsertLinkAtLocation != nil { + if opts.InsertContentAtLocation != nil { + go context.Call(protocol.ServerWorkspaceApplyEdit, protocol.ApplyWorkspaceEditParams{ + Edit: protocol.WorkspaceEdit{ + Changes: map[string][]protocol.TextEdit{ + opts.InsertContentAtLocation.URI: {{Range: opts.InsertContentAtLocation.Range, NewText: note.RawContent}}, + }, + }, + }, nil) + } + + 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) @@ -104,12 +117,15 @@ func executeCommandNew(notebook *core.Notebook, documents *documentStore, contex } absPath := filepath.Join(notebook.Path, note.Path) - if opts.Edit { + if !opts.DryRun && opts.Edit { go context.Call(protocol.ServerWindowShowDocument, protocol.ShowDocumentParams{ URI: pathToURI(absPath), TakeFocus: boolPtr(true), }, nil) } - return map[string]interface{}{"path": absPath}, nil + return map[string]interface{}{ + "path": absPath, + "content": note.RawContent, + }, nil }