summaryrefslogtreecommitdiff
path: root/config/essentials/vis/vis-ultisnips/testlpeg-snipmate.lua
blob: 997365fb3b763834fb839ca322b9e1013e7876ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
local lpeg = require("lpeg")

--------------------------------------------------------------------------------

-- Base definitions
-- local tws = lpeg.S(" ") ^ 1
local tnewline = lpeg.S("\n")
-- local tlowcasedword = lpeg.R("az") ^ 1
local tdigit = lpeg.locale()["digit"]
local talphanum = lpeg.locale()["alnum"]
local tanyprintable = lpeg.locale()["print"]
-- local tcontrol = lpeg.locale()["cntrl"]
local ttabtrigger = tanyprintable ^ 1
local ttag = lpeg.Cg(lpeg.Cp(), "selstart")
	* lpeg.P("${")
	* lpeg.Cg(tdigit ^ 1, "tag-order")
	* ((lpeg.S(":") * lpeg.Cg(talphanum ^ 1, "default-value") * lpeg.S("}")) + lpeg.S("}"))
	* lpeg.Cg(lpeg.Cp(), "selend")
local tsnippetdecl = lpeg.P("snippet") * lpeg.S(" ") * lpeg.Cg(ttabtrigger, "tabtrigger") * tnewline
local tsnippetcontent = lpeg.C(lpeg.Cp() * (lpeg.S("\t ") ^ 1 * (lpeg.Ct(ttag) + tanyprintable) ^ 1 * tnewline) ^ 1)

-- Constructs
local tsnippet = tsnippetdecl * tsnippetcontent
local tcomment = lpeg.S("#") * tanyprintable ^ 0 * tnewline

-- The way grammar captures:
-- Every snippet gets its own table, and every table has:
-- 'tabtrigger' - the tabtrigger
-- [1]          - full content
-- [2..n]       - tags
local tsnippetsfile = lpeg.Ct((tcomment + lpeg.Ct(tsnippet) + tnewline) ^ 1)
--------------------------------------------------------------------------------

-- local testsingle = [[
-- snippet sim
--         ${1:public} static int Main(string[] args)
--         {
--                 ${0}
--                 return 0;
--         }
-- ]]

-- local testmulti = [[
-- snippet sim
--         ${1:public} static int Main(string[] args)
--         {
--                 ${0}
--                 return 0;
--         }
-- snippet simc
--         public class Application
--         {
--                 ${1:public} static int Main(string[] args)
--                 {
--                         ${0}
--                         return 0;
--                 }
--         }
-- snippet svm
--         ${1:public} static void Main(string[] args)
--         {
--                 ${0}
--         }
-- ]]

local testfile = [[
# I'll most propably add more stuff in here like
# * List/Array constructio
# * Mostly used generics
# * Linq
# * Funcs, Actions, Predicates
# * Lambda
# * Events
#
# Feedback is welcome!
#
# Main
snippet sim
        ${1:public} static int Main(string[] args)
        {
                ${0}
                return 0;
        }
snippet simc
        public class Application
        {
                ${1:public} static int Main(string[] args)
                {
                        ${0}
                        return 0;
                }
        }
snippet svm
        ${1:public} static void Main(string[] args)
        {
                ${0}
        }
# if condition
snippet if
        if (${1:true})
        {
                ${0:${VISUAL}}
        }
snippet el
        else
        {
                ${0:${VISUAL}}
        }
]]

--------------------------------------------------------------------------------
-- Test

local function print_table(tableau, tabwidth)
	if tabwidth == nil then
		tabwidth = 0
	end

	-- Iterate
	for k, v in pairs(tableau) do
		local tabs = ("\t"):rep(tabwidth)

		print(tabs .. k .. ':"' .. tostring(v) .. '"')
		if type(v) == "table" then
			print_table(v, tabwidth + 1)
		end
	end
end

--print("------------ header ------------------------------------")
--p = lpeg.Ct(tsnippetdecl)
--t = p:match([[
--snippet classy
--]])
--print_table(t)
--print("--------------------------------------------------------------")

--print("------------ tag ------------------------------------")
--print_table(
--  lpeg.Ct(ttag):match('${0:VISUAL}')
--)
--print_table(
--  lpeg.Ct(ttag):match('${12:Badonkadong}')
--)
--print_table(
--  lpeg.Ct(ttag):match('${1}')
--)
--print("--------------------------------------------------------------")

--print("------------ single snippet test ------------------------------------")
--print_table(lpeg.Ct(tsnippet):match(testsingle))
--print("--------------------------------------------------------------")

--print("------------ multi snippet test ------------------------------------")
--print_table(lpeg.Ct(tsnippetsfile):match(testmulti))
--print("--------------------------------------------------------------")

print("------------ file with comments -------------------------------------")
print_table(tsnippetsfile:match(testfile))
print("--------------------------------------------------------------")