-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFollowfunctionphp.py
More file actions
150 lines (134 loc) · 4.46 KB
/
Followfunctionphp.py
File metadata and controls
150 lines (134 loc) · 4.46 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
141
142
143
144
145
146
147
148
149
150
import sublime, sublime_plugin
import tempfile, os, sys
import fnmatch
import re
class FollowfunctionphpCommand(sublime_plugin.TextCommand):
resultfiles = []
viewResultfiles = []
dbPath = ""
word = ""
# przeszukuje plik z indeksem za nazwa funkcji
def grepDb(self, filename):
isresult = None
files = []
if os.path.isfile(filename):
for n, line in enumerate(open(filename)):
if self.word.decode('utf-8') in line.decode('utf-8'):
tmpline = line.split(";")
if tmpline[1] != "":
res = []
res.append(tmpline[0])
if len(tmpline[1]) > 50:
tmplen = len(tmpline[1])-46
# res.append(tmpline[1][:7] + "..." + tmpline[1][tmplen:])
res.append("..." + tmpline[1][tmplen:])
else:
res.append(tmpline[1])
self.viewResultfiles.append(res)
self.resultfiles.append(tmpline[1])
isresult = 1
else:
print "Brak indeksu"
sublime.status_message('Please reindex files')
return isresult
# przeszukuje plik, w ktorym jest funkcja,
# aby otworzyc w odpowiednim miejscu
def grep(self, filename):
datafile = filename.replace('\\', '/').replace('\n','')
for n, line in enumerate(open(datafile)):
search = "function " + self.word
if search.decode('utf-8') in line.decode('utf-8'):
return datafile + ":" + str(n+1) + ":0"
return None
# otwiera plik z funkcja, lub wyswietla liste Plikow
# jezeli funkcja wystepuje w wiekszej ilosci plikow
def openfile(self, num):
# print "Plikow znalezionych: %d" % len(self.resultfiles)
if num > -1:
selectedFile = os.path.normpath(self.resultfiles[num])
fileWithPosition = self.grep(selectedFile)
self.saveUndo()
self.view.window().open_file(fileWithPosition,sublime.ENCODED_POSITION)
elif num == -2:
if len(self.resultfiles) == 1:
selectedFile = os.path.normpath(self.resultfiles[0])
fileWithPosition = self.grep(selectedFile)
self.saveUndo()
self.view.window().open_file(fileWithPosition,sublime.ENCODED_POSITION)
else:
self.view.window().show_quick_panel(self.viewResultfiles, self.openfile)
else:
sublime.status_message('No function found! Reindex!')
# zapisuje miejsce, z ktorego wywolano plugin, aby wrocic
def saveUndo(self):
for region in self.view.sel():
column = self.view.rowcol(region.begin())[1] + 1
row = self.view.rowcol(region.begin())[0] + 1
# (row,col) = self.view.rowcol(self.view.sel()[0].begin())
undoFilename = self.view.file_name() + ":" + str(row) + ":" + str(column)
filename = os.path.join(sublime.packages_path(), self.pathForDB, "undo")
if os.path.isfile(filename) == False:
with open(filename, 'w') as f:
f.write(undoFilename.decode('utf-8') + "\n")
f.close()
else:
with open(filename, 'r+') as f:
lineCnt = sum(1 for line in f)
if lineCnt > 4 :
f.seek(0,0)
lines = f.readlines()
# print lines
f.close
with open(filename, 'w') as f:
for (cnt, line) in enumerate(lines):
if cnt > (lineCnt-4-1):
f.write(line)
f.write(undoFilename.decode('utf-8') + "\n")
f.close()
else:
f.write(undoFilename.decode('utf-8') + "\n")
f.close()
# pobiera slowo, na ktorym jest kursor
def getword(self):
sel = self.view.sel()[0]
sel = self.view.word(sel)
string = self.view.substr(sel)
return string
# pobiera liste katalogow otwartych (lewe okno)
def getDirectories(self):
return sublime.active_window().folders()
# poiera hash pliku z baza
def getDirectoryMD5(self, dir):
import hashlib
m = hashlib.md5()
m.update(dir)
md5var = m.hexdigest()
return md5var
def checkPathForDB(self):
for root, dirs, files in os.walk(sublime.packages_path()):
for onedir in dirs:
if re.match(r'.ollow ?.unction ?php', onedir, re.IGNORECASE):
self.pathForDB = onedir
def run(self, edit):
# print 'START'
self.checkPathForDB()
self.resultfiles = []
self.viewResultfiles = []
self.word = self.getword() + "("
dirs = self.getDirectories()
found = 0
for dir in dirs:
pName = self.getDirectoryMD5(dir)
dbPath = os.path.join(sublime.packages_path(), self.pathForDB, pName)
r = self.grepDb(dbPath)
if r != None:
# print "znalezione w %s" % dir
found = 1
if found == 1:
# print "jest funkcja"
# print self.resultfiles
self.openfile(-2)
else:
print "nie ma funkcji"
sublime.status_message('No function found! Reindex!')
return