nvim/UltiSnips/c.snippets

186 lines
3.2 KiB
Plaintext

extends cpp
priority 200
global !p
def complete(t, opts):
if t:
opts = [m[len(t):] for m in opts if m.startswith(t)]
if len(opts) == 1:
return opts[0]
return '(' + '|'.join(opts) + ')'
endglobal
snippet helloworld "Hello World"
#include <stdio.h>
int main(void){(void)printf("Hello World!\n");return 0;}
endsnippet
snippet for "For-loop head"
for (${1:int ${2:i}} = ${3:0}; ${4:$2 < $5}; ${6:$2++}) {
$7
}
$0
endsnippet
snippet while "while loop head"
for (;$1;) {
$2
}
$0
endsnippet
snippet fun "New Function"
${1:int} ${2:MyFunc} (${3:void}) {
`!p
if t[1].replace('static ', '') != "void":
snip.rv = f"{t[1].replace('static ', '')} rv = 0;"
else:
snip.rv = ""`
$5
`!p
if t[1].replace('static ', '') != "void":
snip.rv = f"return rv;"
else:
snip.rv = ""`
}
$0
endsnippet
snippet pfun "prototype for function" bA
/**
* @name $2
* @return $1
* @brief ${4: Description}
* `!p
params = t[3].replace(", ", ",").split(",")
rval = ""
for param in params:
if len(param.split(' ')) >= 2:
rval += f"\n *\t@param {param}"
snip.rv = rval`
*/
${1:int} ${2:MyFunc} (${3:void});
endsnippet
snippet exfun "New Function with Documentation"
/**
* @name $2
* @return $1
* @brief ${4: Description}
* `!p
params = t[3].replace(", ", ",").split(",")
rval = ""
for param in params:
if len(param.split(' ')) >= 2:
rval += f"\n *\t@param {param}"
snip.rv = rval`
*/
${1:int} ${2:MyFunc} (${3:void}) {
/* Returns ${5:void} */
`!p
if t[1].replace('static ', '') != "void":
snip.rv = f"{t[1].replace('static ', '')} {t[5]} = 0;"
else:
snip.rv = ""`
$6
`!p
if t[1].replace('static ', '') != "void":
snip.rv = f"return {t[5]};"
else:
snip.rv = ""`
}
$0
endsnippet
snippet "(\w+) = malloc" "Automativ malloc error implementation" rA
/* Allocate memory for `!p snip.rv = match.group(1)` */
`!p snip.rv = match.group(1)` = ($1*)malloc(sizeof(${1:int}) * $2);
if (!`!p snip.rv = match.group(1)`) {
/* Error */
(void)printf("ERROR: malloc error on memory allocation of: `!p snip.rv = match.group(1)`!\n");
exit(EXIT_FAILURE);
}
$0
endsnippet
snippet swap "Swap two integer or numerical variables"
/* Swap Variables $1 and $2 */
${1:Var1} = $1 + ${2:Var2};
$2 = $1 - $2;
$1 = $1 - $2;
$0
endsnippet
snippet defstruct "define a new type with a struct" A
/* define struct $1 ${2:Explaination} */
typedef struct $1 $1;
struct $1 {
$3
};
endsnippet
snippet docstring "Meta Comment for Documenation" A
/*
* Filename: `!p snip.rv = fn`
* Author: ${1:Yannick Reiss}
* Date: `date`
* Project: ${2:Project Name}
* Copyright: ${3:None}
* Description: ${4:Funny module}
*
*/$0
endsnippet
snippet templatehsrm "Program templte from HSRM" A
/* include */
$1
/* makros */
$2
/* typ definitions */
$3
/* local function prototypes */
$4
/* global constants */
$5
/* global variables */
$6
/* local constants */
$7
/* local variables */
$8
/* global and local function implementations */
$0
endsnippet
snippet /* "comment" A
/* $1 */$0
endsnippet
snippet fflush "flush stdout" iA
/* flush stdout */
if ( fflush(stdout) ) {
perror("Could not flush stdout");
exit(EXIT_FAILURE);
}
endsnippet
snippet switch "Classic C switch statement" b
switch(${1:Expression}) {
case $3:
$4
break;$5
default:
$2
}$0
endsnippet