-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSearchFile.py
More file actions
160 lines (130 loc) · 4.14 KB
/
SearchFile.py
File metadata and controls
160 lines (130 loc) · 4.14 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
151
152
153
154
155
156
157
158
159
160
import sublime, sublime_plugin, os
def initConstant():
global Setttings
global Options
global SplitFlag
Setttings = sublime.load_settings('SearchFile.sublime-settings')
Options = { 'ROOT': Setttings.get('root'), 'ENHANCE': Setttings.get('enhance') }
if sublime.platform() == 'osx':
SplitFlag = '/'
else:
SplitFlag = '\\'
initConstant()
def findFile(findpath, filename, tmplist):
if findpath == '':
print( 'empty file find ' )
return True
if findpath[0] == '/':
if Options['ROOT'] == '' or Options['ROOT'] == None:
for root in sublime.active_window().folders():
if os.path.exists(root + SplitFlag.join(findpath.split('/'))):
tmplist = root
break
else:
tmplist = Options['ROOT']
findpath = tmplist + SplitFlag.join(findpath.split('/'))
if os.path.exists(findpath):
try:
sublime.active_window().open_file(findpath)
except BaseException:
print( "Error on except" )
return False
else:
message = "Sorry, I cannot find the %s file."
print( message % findpath )
return False
return True
def parseSel(self, findpath):
srcpath = self.view.file_name()
tmplist = srcpath.split(SplitFlag)
tmplist.pop()
tmplist = SplitFlag.join(tmplist) + SplitFlag
sels = []
FindResult = []
if findpath.find('\n') != -1:
allpath = findpath.split('\n')
for index in range(len(allpath)):
allpath[index] = allpath[index].replace('\t','').replace(' ','')
FindResult = []
for path in allpath:
filename = path.split('/').pop()
if findFile(path, filename, tmplist) == False:
FindResult.append(filename)
else:
filename = findpath.split('/').pop()
if findFile(findpath, filename, tmplist) == False:
FindResult.append(filename)
return FindResult
class SearchfileCommand(sublime_plugin.TextCommand):
def run(self, edit):
print( "\n\n\n" )
print( "SeachFile Start" )
print( "===============================================" )
sels = self.view.sel()
notFindSel = []
FindResult = []
for index in range(len(sels)):
findpath = self.view.substr(sels[index]).lstrip().rstrip()
if findpath == '':
notFindSel.append(index)
else:
selresult = parseSel(self, findpath)
if len(selresult) > 0:
FindResult.append(','.join(selresult))
if len(notFindSel) > 0 and Options['ENHANCE']:
#self.view.run_command("expand_selection", {"to": "scope"})
self.view.run_command("expand_selection", {"to": "brackets"})
#print( Options['ENHANCE'] )
sels = self.view.sel()
for i in notFindSel:
findpath = self.view.substr(sels[i]).replace("'","").replace('"','').lstrip().rstrip()
if findpath != '':
selresult = parseSel(self, findpath)
if len(selresult) > 0:
FindResult.append(','.join(selresult))
if len(FindResult) > 0:
message = "Sorry, I cannot find the %s file."
sublime.message_dialog(message % ','.join(FindResult))
print( "===============================================" )
print( "SeachFile End" )
print( "\n\n\n" )
#class SearchfileCommand(sublime_plugin.EventListener):
#def on_load(self, view):
#print "new view loadding"
#return
#def on_modified(self, view):
#print "modify view"
#return
#sett = sublime.load_settings('SearchFile.sublime-settings')
#
#if findpath.find('/') != -1:
# tmplist.append('\\'.join(findpath.split('/')))
#else:
# tmplist.append(findpath)
#
#
# if os.path.exists(findpath):
# try:
# sublime.active_window().open_file(findpath)
# except BaseException:
# print "Error on except"
# else:
# message = "Sorry, I cannot find the %s file."
# print message % findpath
# sublime.message_dialog(message % self.view.substr(sels[0]))
#
#
#
#globals()['settings'] = sublime.load_settings('Emmet.sublime-settings')
# if self.view.substr(sel).find('{') != -1:
#print sublime.active_window().active_view().file_name()
#print self.view.file_name()
#self.view.insert(edit, 0, "")
# try:
# fh = open("testfile", "w")
# fh.write("This is my test file for exception handling!!")
# except IOError:
# print "Error: can\'t find file or read data"
# else:
# print "Written content in the file successfully"
# fh.close()