-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets.py
More file actions
140 lines (119 loc) · 4 KB
/
snippets.py
File metadata and controls
140 lines (119 loc) · 4 KB
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
from __future__ import print_function
import json
import os
import sys
from settings import folders
PY3 = sys.version_info.major == 3
def S(s):
if PY3:
return s.decode('utf-8')
return s
def B(s):
if PY3:
return s.encode('utf-8')
return s
def file_put(filename, text):
with open(filename, 'wb') as fp:
fp.write(B(text))
def file_get(filename):
with open(filename, 'rb') as fp:
return S(fp.read())
sn = file_get('snippets.txt')
# Visual Studio
# https://docs.microsoft.com/en-us/visualstudio/ide/code-snippets?view=vs-2019
# May support placeholders like $Width$ by
# <Declarations>
# <Literal>
# <ID>Width</ID>
# <Type>Integer</Type>
# <ToolTip>Replace by width</ToolTip>
# <Default>150</Default>
# </Literal>
# </Declarations>
VS_fmt = '''\
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>%s</Title>
<Author>Myself</Author>
<Description></Description>
<Shortcut>%s</Shortcut>
</Header>
<Snippet>
<Code Language="cpp">
<![CDATA[%s]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
'''
# VSCode
# https://code.visualstudio.com/docs/editor/userdefinedsnippets
'''
// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
'''
# Sublime
# https://docs.sublimetext.io/guide/extensibility/snippets.html
# ${1:this} like in VS Code are supported
Sublime_fmt = '''\
<snippet>
<tabTrigger>%s</tabTrigger>
<content><![CDATA[%s]]></content>
</snippet>
'''
# TODO:
# IDEA: File | Settings | Live Templates; https://www.jetbrains.com/help/idea/using-live-templates.html
# Notepad++: No support yet https://github.com/ffes/nppsnippets/issues/33
result = {}
for note in sn.split('=' * 80 + '\r\n'):
head = note.splitlines()[0]
body = '\r\n'.join(note.splitlines()[1:])
if head.startswith('// '):
short = head[3:]
print('[%s]' % short)
print(note)
print('-' * 80)
result[short[1:]] = body
if 'VS' in folders:
folder = os.path.expanduser(folders['VS'])
if folder:
for k, v in result.items():
fn = os.path.join(folder, '%s.snippet' % k)
content = VS_fmt % ('#' + k, '#' + k, v)
if not os.path.exists(fn) or file_get(fn) != content:
file_put(fn, content)
if 'VSCODE' in folders:
folder = os.path.expanduser(folders['VSCODE'])
if folder:
fn = os.path.join(folder, 'cpp.json')
content = {}
for k, v in result.items():
content[k] = {
'prefix': '#' + k,
'body': v.replace('$end$', '$0').replace('$selected$', '$TM_SELECTED_TEXT').splitlines(),
'description': k,
}
content = json.dumps(content)
if not os.path.exists(fn) or file_get(fn) != content:
file_put(fn, content)
if 'SUBLIME' in folders:
folder = os.path.expanduser(folders['SUBLIME'])
if folder:
for k, v in result.items():
fn = os.path.join(folder, '%s.sublime-snippet' % k)
content = Sublime_fmt % ('#' + k, v.replace('$end$', '$0').replace('$selected$', '$SELECTION'))
if not os.path.exists(fn) or file_get(fn) != content:
file_put(fn, content)