-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_snippets.py
96 lines (83 loc) · 3.27 KB
/
parse_snippets.py
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
import glob
import os
import re
from sys import platform
startPath = ''
local_snippets_file = ''
base_pawn_snippets = ''
if platform == "linux" or platform == "linux2":
startPath = os.path.dirname(__file__) + '/scripts'
local_snippets_file = open("/home/ciro/.config/Code/User/snippets/pawn.json", "w")
base_pawn_snippets = open(os.path.dirname(__file__) + '/base_snippets.txt', "r")
elif platform == "win32":
local_snippets_file = open("C:\\Users\\Ciro\\AppData\\Roaming\\Code\\User\\snippets\\pawn.json", "w")
base_pawn_snippets = open(os.path.dirname(__file__) + '\\base_snippets.txt', "r")
startPath = os.path.dirname(__file__) + '\\scripts'
# print(os.listdir(startPath))
local_snippets_file.write('{\n')
for line in base_pawn_snippets:
local_snippets_file.write(line)
for root, dirs, files in os.walk(startPath):
for file in files:
if file.endswith('.inc') or file.endswith('.pwn') or file.endswith('.p'):
file_name = root.replace('scripts\\', '')
f = open(root + '/' + file)
local_snippets_file.write(
'\t// ==========[' + file_name + '/' + file + ']==========\n')
lastComments = []
for line in f:
if(line.startswith('//')):
line = line.replace('//', '')
lastComments.append(line)
elif(line.startswith('forward') or line.startswith('ptask') or line.startswith('task') or line.startswith('timer') or line.startswith('static stock') or line.startswith('stock') or line.startswith('native')):
func_name = ''
params = ''
description = ''
line = line.replace('stock', '')
line = line.replace('static', '')
line = line.replace('forward', '')
line = line.replace('native', '')
line = line.replace('{', '')
line = line.replace(';', '')
line = line.replace('\"', '\'')
line = line.replace('ptask', '')
line = line.replace('task', '')
line = line.replace('timer', '')
# line = re.sub(r'\[.*\]', '', line)
T = line.split('//', 1)
line = T[0]
line = line.split('return', 1)[0]
if(len(T) > 1):
lastComments.append(T[1].replace('//', ''))
# Porcamadonna? That should be faster than the previous
params = line[line.find('(')+len('('):line.rfind(')')]
# Is strip enough? Should I strip the whole line before?
func_name = line.replace(
'(' + params + ')', '').replace('\n', '').strip()
S = func_name.split(':')
if(len(S) > 1):
func_name = S[1]
split = params.split(',')
params = ''
# print(func_name)
# Params
index = 1
for s in split:
params = params + \
'${' + str(index) + ':' + s.strip() + '}'
if(index != len(split)):
params = params + ', '
index = index+1
# Description
for v in lastComments:
description = description + v.strip() + '\\n'
# print('Name: ' + func_name + ' Params: ' + params + ' Desc: ' + description)
local_snippets_file.write('\t"lsarp_' + func_name + '": {\n\t\t"scope": "pawn",\n\t\t"prefix": "'+func_name +
'",\n\t\t"body": "'+func_name+'(' + params + ')$0",\n\t\t"description": "' + description + '"\n\t},\n')
else:
lastComments = []
local_snippets_file.write(
'\t// ==================================================\n')
local_snippets_file.write('}')
print('Pawn files parsed.')
#input("Press Enter to exit")