Home

Awesome

snippets plugin for lite-xl

Installation

Place the following files in the editor's plugins directory (e.g ~/.config/lite-xl/plugins, not ~/.config/lite-xl/plugins/lite-xl-snippets):

Usage

Adding snippets

Snippets may be added through lua configuration:

local snippets = require 'plugins.snippets'
snippets.add {
    trigger  = 'fori',
    files    = '%.lua$',
    info     = 'ipairs',             -- optional, used by the autocomplete menu
    desc     = 'array iterator',     -- optional, used by the autocomplete menu
    format   = 'lsp',                -- 'lsp' must be lowercase
    template = [[
for ${1:i}, ${2:v} in ipairs($3) do
    $0
end
]]
}

Other possible and optional fields: nodes, defaults, transforms, choices, matches, p_args. See docs.md for details.

For snippets to be automatically loaded on startup, they should be placed somewhere the editor will load them by itself. The easiest way to do that is the user module (init.lua); another is to create a new plugin which contains the snippets, which has the advantage of not cluttering the config file. E.g, in plugins/my_snippets.lua or plugins/my_snippets/init.lua:

-- modversion:3
local snippets = require 'plugins.snippets'

snippets.add { ... }
snippets.add { ... }

A last option, if the snippets are placed at a location that the editor does not automatically load, is to load them manually using dofile. E.g in init.lua:

dofile '/path/to/my_snippets.lua'

An alternative to lua configuration is to use json files, which is done by adding paths to the lsp_snippets plugin:

local lsp_snippets = require 'plugins.lsp_snippets'
lsp_snippets.add_paths {
    'snippets',                   -- relative paths are prefixed with the userdir
    '/path/to/snippets/folder',
    '/specific/snippet/file.json'
}

The add_paths function takes as argument either a single path or an array of paths and loads them according to the following rules:

For details on these json files, refer to the vscode spec (project scope is not supported). Existing snippets may be found at rafamadriz/friendly-snippets or in vscode extensions (e.g most extensions which add support for a given language will contain snippets).

For example, adding rafamadriz/friendly-snippets:

  1. git clone https://github.com/rafamadriz/friendly-snippets.git in the userdir
  2. add lsp_snippets.add_paths 'friendly-snippets/snippets' to init.lua

Using snippets

A snippet may be expanded either with the autocomplete suggestions or manually:

local snippets = require 'plugins.snippets'
snippets.execute {
    format   = 'lsp',
    template = [[
local function $1($2)
    $0
end
]]
}

This will expand the given snippet at each cursor in the current document.

Once expanded, a snippet may be navigated through using these commands:

snippets:next: sets selections for the next tabstop. (wraps around to 1)

snippets:previous (shift+tab): sets selections for the previous tabstop. (wraps around to the last tabstop)

snippets:select-current: selects the values of the current tabstop.

snippets:exit (escape): sets selections for either the end tabstop (e.g $0) or after the snippet.

snippets:exit-all: exits all snippets, as opposed to only the innermost level.

snippets:next-or-exit (tab): if the current tabstop is the last one, exits; otherwise, next.

Configuration

config.plugins.snippets:

Advanced

See docs.md.

Notes