norcalli / snippets.nvim

GNU General Public License v3.0
277 stars 13 forks source link

Questions on snippets and completion with completion-nvim #13

Open Th3Whit3Wolf opened 4 years ago

Th3Whit3Wolf commented 4 years ago

I have a couple things I would like to do with snippets nvim

  1. Organize snippets in another directory and preferably be able to lazy load based on filetype
     .
    ├─ lua
    │  ├─ init.lua
    │  └─ plugins 
    │     ├─ init.lua
    │     └─ snippets
    │        ├─ init.lua
    │        └─ lang            # Language directory
    │            ├─ c.lua       # C snippets
    │            └─ rust.lua    # Rust snippets
    └ init.vim
  1. When snippets get selected from completion-nvim expand (and maybe map to expansion to / without interfering with mapping to pumvisible menu selection if that's possible)
Th3Whit3Wolf commented 4 years ago

For part one lets say you have a verbose directory structure that looks something like this.

     .
    ├─ lua
    │  ├─ init.lua
    │  └─ plugins 
    │     ├─ init.lua
    │     └─ snippets
    │        ├─ init.lua
    │        └─ lang            # Language directory
    │            ├─ c.lua       # C snippets
    │            └─ rust.lua    # Rust snippets
    └ init.vim

in lua/plugins/snippets/lang/rust.lua you'd have something like this.

local U = require 'snippets.utils'

local snippets = {}

snippets = {
    macro = U.match_indentation [[
macro_rules! ${1:name} { 
  ($2) => {
    $0
  }
}
]],
    type = [[type $1 = $2;]],
    struct = U.match_indentation [[
struct $1 {
  $0
}]],
    enum = U.match_indentation [[
enum $1 {
  $0
}]],
    -- TODO(ashkan, 2020-08-19 05:33:54+0900) case change from TitleCase to snake_case for last element of ::
    field = [[$1: $2,]],
    -- field = [[${2=R.case_change.S[1]..}: $1,]];
    impl = U.match_indentation [[
impl $1 {
  $0
}
]],
    hashmap = [[use std::collections::HashMap;]],
    hashset = [[use std::collections::HashSet;]],
    collections = [[use std::collections::$1;]],
    match = U.match_indentation [[
match $1 {
  $0
}]],
    bcase = U.match_indentation [[
$1 => {
  $0
}]],
    case = U.match_indentation [[$1 => $0,]]
}

return snippets

in lua/plugins/snippets/init.lua you'd have something like this.

local snippets = require 'snippets'
local U = require 'snippets.utils'

snippets.snippets = {
    rust = require 'plugins/snippets/lang/rust',
    _global = {
        -- If you aren't inside of a comment, make the line a comment.
        copyright = U.force_comment [[Copyright (C) Ashkan Kiani ${=os.date("%Y")}]]
    }
}

Not sure how you could tie in lazy loading, maybe set an autocmd and wrap this in a function that checks for filetype.