Skip to content

kiith-sa/DSnips

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DSnips - better UltiSnips snippets for D

Note

Changes in UltiSnips master broke compatibility with DSnips recently (August 2014). Update DSnips (re-download d.snippets) to fix this.

DSnips is a collection of UltiSnips snippets for the D programming language. UltiSnips is a snippets plugin for Vim.

A few examples of what snippets can do:


./gif/try_catch_throw.gif

try + thr + catch + if


./gif/class.gif

class + priv + pub + this + fun + fore

Getting Started

  • Install UltiSnips

  • Get DSnips: git clone https://github.com/kiith-sa/DSnips.git

  • Copy the d.snippets file to UltiSnips user snippets directory. On Linux this should be ~/.vim/UltiSnips.

    If you don't know where the user snippets directory is, open a D file and type the :UltiSnipsEdit command (after UltiSnips is installed); this will open a user d.snippets file which you can replace by the DSnips version of d.snippets.

As the sheer number of snippets here may be a bit overwhelming, here is a small(ish) list of snippets that should provide a quick improvement for most people:

assert / enforce as / enf
(wrap in) if / else if / else if / elif / else
for / foreach for / fore
writeln wr
return ret
(wrap in) scope guard scope
(wrap in) try/catch / throw / define Exception try / thr / exc
module header / import module / imp
getter / setter get / set
define a function fun
constructor this
switch / case sw / case
string mixin smix
DDoc Params: / Returns: Par / Ret

Various other snippets may save time depending on which D features you use most, see Reference.

Wrapping

Some snippets (if, try, etc.) can be used to wrap code using the visual placeholder feature of UltiSnips. To wrap code, select the code you want to wrap (using visual mode), and press the UltiSnips expand trigger. This will delete the code to insert it the next time you expand a snippet.

Background

This is a heavily modified version of default UltiSnips D snippets which I made some time ago. Unfortunately, some snippets specific for my projects as well as some indentation bugs ended up in the default snippets. This is an attempt to revamp those snippets and eventually get merged back to the default repo.

Compared to the "old" D snippets, some snippets have been removed (project-specific, snippets that didn't really save keystrokes) and many new snippets were added, to the point where almost all D features and some very common Phobos features have a snippet. There should be less bugs and the snippets have been designed to work together (try/catch, sw/case, import chains etc.), save even more keystrokes, work with new D features etc.

The number of snippets may seem a bit overkill; a lot of that is due to operator snippets which are similar but always a bit too different to be joined, DDoc and some rather verbose interpolation code which I prefer to write in a readable way instead of clever one-liners.

Reference

Sections

All snippets

al, as, avar, bsl, case, catch, class, cont, cvar, do, doctest, elif, else, enf, enum, Ex, exc, for, fore, forever, forif, format, fsw, fun, get, gpl, if, ife, imp, in, inter, invar, ivar, main, module, new, opApply, opAssign, opB, opCall, opCmp, opDis, opDollar, opIndexAssign, opIndex, opO, opSliceAssign, opSlice, opU, out, pack, Par, priv, prot, pub, Range, ret, Ret, scope, See, set, spn, struct, supe, sw, this, thr, Thr, toStr, todo, try, tryf, tup, union, utest, wh, with, wr, wrf,


Phobos

enf, format, tup, wr, wrf


enf

std.exception.enforce

Automatically breaks the snippet into 2 lines if line length exceeds Vim textwidth (can be set in .vimrc). Can only break the snippet between the 2 parameters to enforce.

./gif/enf.gif

format

std.string.format

./gif/format.gif

Also in this sample: cvar

tup

std.typecons.tuple

./gif/tup.gif

Also in this sample: avar

wr

wrf

std.stdio.writeln, std.stdio.writefln

./gif/wr_wrf.gif

Branches

case, elif, else, fsw, if, ife, sw


if

if

Supports code wrapping.

./gif/if1.gif

./gif/if2.gif

Wrapping code in a static if.

elif

else if

Supports code wrapping.

./gif/elif.gif

else

else

Supports code wrapping.

./gif/else.gif

ife

if followed by else

./gif/ife.gif

Used to create a static if / else pair. Also in this sample: ret

sw

case

switch and case

sw generates a switch with two case's and a default branch. When done, places the cursor after the two cases so case can be used immediately to add more cases.

./gif/sw_case.gif

fsw

final switch

Similar to sw but generates a final switch without a default.

./gif/fsw.gif

Loops

do, for, fore, forever, forif, wh


do

do while loop

Supports code wrapping.

./gif/do.gif

wh

while loop

Supports code wrapping.

./gif/wh.gif

Also in this sample: if

for

for loop

Supports code wrapping.

./gif/for.gif

Also in this sample: tup, avar

fore

foreach loop

Supports code wrapping.

./gif/fore1.gif

Also in this sample: if


./gif/fore2.gif

Wrapping code in a foreach loop.

forever

infinite for loop

Supports code wrapping.

./gif/forever.gif

forif

foreach loop combined with if to filter elements

Usually, a better way to do this is to use std.algorithm.filter but it is (at least with current Phobos/DMD) sometimes easier/more readable to write performant code with a foreach/if combination.

Supports code wrapping.

./gif/forif.gif

Contracts, asserts, tests

as, doctest, in, invar, out, utest


as

assert

The 2-parameter version of assert is used by default as it's usually good practice to write that description string even if the assert may sem obvious.

Automatically breaks the snippet into 2 lines if line length exceeds Vim textwidth (can be set in .vimrc). Can only break the snippet between the 2 parameters to assert.

./gif/as.gif

in

in contract

./gif/in.gif

Also in this sample: as

out

out contract

./gif/out.gif

Also in this sample: as

invar

class/struct invariant

./gif/invar.gif

Also in this sample: as

utest

unittest block

./gif/utest.gif

doctest

a documentation unittest block

A unitest preceded by /// will add an example to the DDoc of the previous function/class/etc.

./gif/doctest.gif

Also in this sample: as

Functions

get, fun, main, Range, set, this, toStr


fun

function/method

fun is one of the more involved snippets. The parameter list is analyzed to generate a Params: DDoc section, although the parameter's descriptions need to be filled in manually. The second-to-last tabstop allows to write the one-line DDoc description of the function and to specify DDoc comment style; starting the description tabstop with /// will use /// DDoc comments while starting with /** will use /* / DDoc comments.

./gif/fun1.gif

Notice that we change the documentation comment style by typing /** in the "Description" tabstop. Also in this sample: fore


./gif/fun2.gif

Also in this sample: forif, ret

this

constructor

Like fun, generates DDoc comments.

./gif/this.gif

get

getter property

@property is not used as it's mostly considered a mistake now and may be deprecated in future.

./gif/get1.gif

By default, the generated getter returns a field with the getter's' name suffixed by '_'.


./gif/get2.gif

In this sample, the return expression is rewritten and doesn't even correspond to a field.

set

setter property

./gif/set.gif

Similarly to get, by default a setter sets a field with the setter's name suffixed by '_'. Also in this sample: as

main

the main() function

./gif/main.gif

Also in this sample: wr

Range

InputRange methods [alias trigger: InputRange]

Many D types have range-style interfaces, of which InputRange is the most common subset. Range generates InputRange API stubs which can be filled in with its tabstops.

./gif/Range.gif

toStr

toString() method

./gif/toStr.gif

Also in this sample: ret, spn

Exception handling

catch, thr, try, tryf


try

catch

thr

try/catch block, catch block, throw statement

Supports code wrapping.

./gif/try_catch_throw.gif

try is used to wrap 2 lines in a try/catch block, thr to throw exception from catch and catch to add another catch block. Also in this sample: if

tryf

try/catch/finally block

Supports code wrapping.

./gif/tryf.gif

Also in this sample: wr

Type definitions

al, class, enum, exc, inter, struct, union


class

class definiton

The default class name is the source file name with uppercased first character.

./gif/class.gif

Also in this sample: priv, pub, this, fun, fore

struct

struct definition

The default name of the enum/interface/union is determined similarly to class.

./gif/struct.gif

Also in this sample: priv, pub

enum

inter

union

enum, interface, union definitions

The default name of the enum/interface/union is determined similarly to class.

./gif/enum_union_inter.gif

al

type alias

./gif/al.gif

exc

Exception class definition

Creates a new exception type with a constructor taking a string, and implicitly taking the caller's source file and line.

./gif/exc.gif

Metaprogramming and conditional compilation

debug, mix, smix, template, version

mix

mixin statement

./gif/mix.gif

smix

string mixin expression

Creates a 'macro-like' string mixin using std.string.format to insert values into the mixin at compile-time.

./gif/smix.gif

Also in this sample: cvar

debug

debug block

Supports code wrapping.

./gif/debug.gif

version

version block

Supports code wrapping.

./gif/version.gif

template

A plain template (not a template class/function) Like fun, generates DDoc comments.

./gif/template1.gif

Also in this sample: al

Operators

opApply, opAssign, opB, opCall, opCmp, opDis, opDollar, opIndexAssign, opIndex, opO, opSliceAssign, opSlice, opU


opDis

opDispatch

Like fun, generates DDoc comments for parameters (if any).

./gif/opDis.gif

Also in this sample: ret

opAssign

opAssign (the = operator) [alias trigger: op=]

./gif/opAssign.gif

Also in this sample: spn

opDollar

opDollar (the $ operator) [alias trigger: op$]

./gif/opDollar.gif

Also in this sample: ret

opSlice

opSlice (operator to get a slice of a container) [alias trigger: op[..]]

Checks that the number of parameters is 0 or 2.

./gif/opSlice.gif

op[..] used to create both a bounded opSlice (a[1 .. 3]) and "entire container" opSlice (a[]). Also in this sample: ret, spn

opIndex

opIndex (operator to get an element of a container) [alias trigger: op[]]

Checks that there is at least 1 parameter.

./gif/opIndex.gif

op[] used to create a single-parameter and two-parameter opIndex Also in this sample: ret

opSliceAssign

opSliceAssign (operator to assign to a slice of a container) [alias trigger: op[..]=]

Checks that the number of parameters is 1 or 3.

./gif/opSliceAssign.gif

opIndexAssign

opIndexAssign (operator to set an element of a container) [alias trigger: op[]=]

Checks that there are at least 2 parameters.

./gif/opIndexAssign.gif

opCall

opCall (function call operator) [alias trigger: op()]

Like fun, generates DDoc comments for parameters.

./gif/opCall.gif

Also in this sample: ret

opB

opBinary (binary operators such as +, in and >>)

Generates an opBinary with a static if chain to overload all operators specified in a string that is the first tabstop. E.g. +-in will overload operators +, - and in, while ^^^ will overload ^ and ^^. Checks that the string only contains valid operators and contains no duplicates.

./gif/opB.gif

opB used to overload operators +, ~, ^^ and ^

opU

opUnary (unary operators such as -, ++ and ~)

Same as opB, but for unary operators.

./gif/opU.gif

opU used to overload operators -, +, -- and ++

opO

opOpAssign (operator assignments such as +=, %= and >>=)

Same as opB, but for op assignment operators.

./gif/opO.gif

opO used to overload operators +=, >>=, and >>>=

opCmp

opCmp (comparison operator)

./gif/opCmp.gif

Also in this sample: ret

opApply

opApply (foreach "operator")

opApply implementations usually contain a loop which passes individual elements to the foreach. opApply generates most of the code to pass the elements, which the user needs to wrap in a loop.

./gif/opApply.gif

Also in this sample: fore

DDoc and comments

bsl, Ex, gpl, Par, Ret, See, Thr, todo


todo

TODO comment

./gif/todo.gif

Par

Ret

DDoc Params, Returns

Must be preceded with '/// ' or '* ' (i.e. single- or multi-line DDoc comments).

Par can be suffixed by a count (1 to 5) of parameters; e.g. Par4 will create a Params section with 4 items.

./gif/docPar_docRet1.gif

Document 1 parameter with Par and the return value with Ret.


./gif/docPar2.gif

Document 2 parameters with Par2

Thr

DDoc Throws

Must be preceded with '/// ' or '* ' (i.e. single- or multi-line DDoc comments).

Thr can be suffixed by a count (1 to 2) of exception types thrown; e.g. Thr2 will create a Throws section with 2 items.

./gif/docThr.gif

Ex

DDoc Example

Must be preceded with '/// ' or '* ' (i.e. single- or multi-line DDoc comments).

Supports code wrapping.

./gif/docEx.gif

Also in this sample: struct

See

DDoc See_Also

Must be preceded with '/// ' or '* ' (i.e. single- or multi-line DDoc comments).

./gif/docSee.gif

bsl

Boost Software License header

./gif/bsl.gif

gpl

GPL2 header

./gif/gpl.gif

Various

avar, cont, cvar, imp, ivar, module, new, pack, priv, prot, pub, ret, scope, spn, supe, with


imp

import declaration

./gif/imp.gif

module

module header

Uses the file name to set the module name, but package name must be specified by the user.

Can be combined with gpl to specify license (Boost is the default as it is the most common license for D projects).

./gif/module1.gif

Using the default (Boost) license


./gif/module2.gif

Also in this sample: gpl

avar

cvar

ivar

auto / const / immutable variable definition

./gif/avar_cvar_ivar.gif

cont

continue statement

./gif/cont.gif

new

new (constructor usage) expression

./gif/new.gif

Also in this sample: avar

pub

priv

prot

pack

public / private / protected / package protection attributes

./gif/pub_priv_prot_pack.gif

ret

return statement

./gif/ret.gif

spn

builtin function attributes

Creates a sequence of function attributes (@safe, pure, nothrow, const and @nogc). Every attribute is a tabstop, making it possible to pick attributes by jumping between attributes and deleting the unwanted ones. For example, if both the ultisnips 'expand' and 'jump forward' triggers are <Tab>, pressing spn<Tab><Tab><Tab><Tab><Tab><Tab> (9 keystrokes) will result in @safe pure nothrow const @nogc while spn<Tab><Tab><BS><Tab><Tab><BS><Tab><BS><Tab> (12 keystrokes) will produce @safe nothrow.

Useful in combination with fun, this, operators and other snippets that create functions.

./gif/spn1.gif

Also in this sample: ret


./gif/spn2.gif

Also in this sample: ret

supe

parent constructor call

./gif/supe.gif

scope

scope guard

Supports code wrapping.

./gif/scope.gif

with

with block

Supports code wrapping.

./gif/with.gif

Also in this sample: if

About

Better UltiSnips snippets for D

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published