Skip to content

Commit

Permalink
Added a snippet-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
venine committed Feb 2, 2022
1 parent 200f5dc commit a31f78d
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 2 deletions.
85 changes: 83 additions & 2 deletions doc/init.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ CONTENTS *doom-toc*
3. Filesystem |doom-fs|
4. Modules |doom-modules|
5. Keybindings |doom-keybindings|
6. Caveats |doom-caveats|
6. Snippets |doom-snippets|
7. Treesitter |doom-treesitter|
8. Caveats |doom-caveats|

==========================================================================
INTRODUCTION *doom-introduction*
Expand All @@ -20,6 +22,9 @@ This distribution has been inspired by doom-nvim and doom-emacs both. You
should check them out in order to get a feel of how this distro would fare for
you.

Please read through the ENTIRE documentation in order to understand and use
doom efficiently.

==========================================================================
PHILOSOPHY *doom-philosophy*

Expand Down Expand Up @@ -95,6 +100,9 @@ These are the system modules:
13. `vim-help`
Some keybindings for easier editing in vim help files. Planning to add
more features
14. `snippets`
Enable easy snippet creation for doom. You can think of the good ol'
YASnippet days now.

*doom-user-fs*
Doom encourages the usage of `fennel` and `lua` both for configuring the
Expand Down Expand Up @@ -1433,7 +1441,80 @@ vim-vsnip:
:key-attribs "expr"
:noremap false
:modes "i"
:help "Snippet jump to next field"
:help "Snippet jump to next field"}


==============================================================================
SNIPPETS *doom-snippets*
This section explains snippet creation in doom-evim.

Doom-evim uses vim-vsnip. As it turns out, it is not easily possible to simply
copy paste into a JSON and not throw errors. Therefore, in the spirit of
YASnippet, doom provides some keybindings to make this task easy for you.

Simply press:
`<leader>&ns` (for split window)
`<leader>&nv` (for vsplit window)

This will [v]split your current window. Now enter your snippet in the same
manner as you would in vscode or emacs and press `gx`.

Pressing `gx` will prompt you for the name, prefix and description of the
snippet. Once you are done entering all that, voila! Your snippet has been
registered.

There are other convenient keybindings too:
`<leader>&es `Edit a filetype snippets list json in a split window
`<leader>&ev `Edit a filetype snippets list json in a vsplit window

==============================================================================
TREESITTER *doom-treesitter*

This section only covers some of the additional features that users can use
and not even be aware of it. Doom ships with
`nvim-treesitter-textobjects`which make it easy to select nodes and do other
operations.

Doom creates some textobjects of its own. If you want to see the potential of
this, try using the textobjects with an REPL.

Textobjects mappings:
>
{:ib "@block.inner"
:ab "@block.outer"

:iC "@call.inner"
:aC "@call.outer"

:ic "@class.inner"
:ac "@class.outer"

"a;" "@comment.outer"

:iF "@conditional.inner"
:aF "@conditional.outer"

:if "@function.inner"
:af "@function.outer"

:il "@loop.inner"
:al "@loop.outer"

:ip "@parameter.inner"
:ap "@parameter.outer"}

Other mappings:
`]m` Go to the start of next function
`]]` Go to the start of the next class

`]M` Go to the end of next function
`][` Go to the end of next class

`[m` Go to the start of previous function
`[[` Go to the start of previous class

`[M` Go to the end of previous function
`[]` Go to the end of previous class

==============================================================================
CAVEATS *doom-caveats*
Expand Down
2 changes: 2 additions & 0 deletions fnl/init.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
(when doom.default_help_keybindings
(utils.try-require :vim-help :DOOM))

(utils.try-require :snippets :DOOM)

; Register help
; Register all help-groups in <leader>
(let [wk (require :which-key)]
Expand Down
63 changes: 63 additions & 0 deletions fnl/snippets.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
(module snippets
{autoload {utils utils
core aniseed.core}})

; All the user snippets will go in ~/.local/share/nvim/user-snippets/
(set vim.g.vsnip_snippet_dir (utils.datap "user-snippets"))

; Open a new split/vsplit window and set buffer props
(utils.define-key {:keys "<leader>&ns"
:exec (fn []
(let [current-ft vim.bo.filetype]
(vim.cmd ":split _new_snippet | :wincmd k")
(set vim.bo.buftype "nofile")
(set vim.bo.buflisted false)
(set vim.bo.filetype current-ft)))})

(utils.define-key {:keys "<leader>&nv"
:exec (fn []
(let [current-ft vim.bo.filetype]
(vim.cmd ":vsplit _new_snippet | :wincmd k")
(set vim.bo.buftype "nofile")
(set vim.bo.buflisted false)
(set vim.bo.filetype current-ft)))})

; Get the string from the buffer and save the snippet
(utils.define-key {:keys "gx"
:key-attribs "buffer"
:patterns "_new_snippet"
:events "BufEnter"
:exec (fn []
(let [s (utils.buffer-string (vim.call :winbufnr 0) [0 -1] false)]
(if (and (= 1 (length s))
(= (. s 1) ""))
false
(let [name (utils.get-user-input "Snippet name > " #(~= $1 "") true)
dest-dir (utils.datap "user-snippets")
fname (utils.datap "user-snippets" (.. vim.bo.filetype ".json"))
prefix (utils.get-user-input "Snippet prefix > " #(~= $1 "") true)
json {}]

(tset json name {:prefix [prefix] :body s})

(when (not (utils.path-exists dest-dir))
(utils.sh (.. "mkdir " dest-dir)))

(if (not (utils.path-exists fname))
(core.spit fname (vim.call :json_encode json))
(let [_file (core.slurp fname)
file (if (not (string.match _file "[{}]"))
{}
_file)
current-json (vim.call :json_decode (core.slurp fname))
new-json (vim.tbl_extend :force current-json json)]
(core.spit fname (vim.call :json_encode new-json))))))))})

; Some keybindings for vsnip
(utils.define-keys [{:keys "<leader>&ev"
:exec ":VsnipOpenVsplit<CR>"
:help "Open a snippets json in vsplit"}

{:keys "<leader>&es"
:exec ":VsnipOpenSplit<CR>"
:help "Open a snippets json in split"}])
12 changes: 12 additions & 0 deletions fnl/utils.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,15 @@
(- size 2))
new-font (.. font ":h" new)]
(set vim.go.guifont new-font)))

; Get user input
(defn get-user-input [prompt validate loop]
(let [_first-input (vim.call :input prompt)
_first-input (str.trim _first-input)]
(if (= _first-input "")
(get-user-input prompt validate loop)
(let [is-valid (validate _first-input)]
(if is-valid
_first-input
(if loop
(get-user-input prompt validate true)))))))

0 comments on commit a31f78d

Please sign in to comment.