forked from alvarolm/GoRename
/
goRename.py
457 lines (357 loc) · 15.1 KB
/
goRename.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# Copyright (c) 2014 Jesse Meek <https://github.com/waigani>
# Copyright (c) 2016 Alvaro Leiva <https://github.com/alvarolm>
# This program is Free Software see LICENSE file for details.
"""
GoRename is a Go gorename plugin for Sublime Text 3.
It depends on the gorename tool being installed:
go get -u golang.org/x/tools/cmd/gorename
"""
# TODO: review & clean
import sublime, sublime_plugin, subprocess, time, re, os, subprocess, sys, time, hashlib
DEBUG = False
VERSION = ''
use_golangconfig = False
# holds renaming parameters
renameMe = {}
runningTool = False
def log(*msg):
print("GoRename:", msg[0:])
def debug(*msg):
if DEBUG:
print("GoRename [DEBUG]:", msg[0:])
def error(*msg):
print("GoRename [ERROR]:", msg[0:])
def plugin_loaded():
global DEBUG
global VERSION
global use_golangconfig
DEBUG = get_setting("gorename_debug", False)
use_golangconfig = get_setting("gorename_use_golangconfig", False)
# load shellenv
def load_shellenv():
global shellenv
from .dep import shellenv
# try golangconfig
if use_golangconfig:
try:
global golangconfig
import golangconfig
except:
error("couldn't import golangconfig:", sys.exc_info()[0])
log("using shellenv instead of golangconfig")
use_golangconfig = False
load_shellenv()
else:
load_shellenv()
log("debug:", DEBUG)
log("use_golangconfig", use_golangconfig)
# keep track of the version if possible (pretty nasty workaround, any other ideas ?)
try:
PluginPath = os.path.dirname(os.path.realpath(__file__))
p = subprocess.Popen(["git", "describe", "master", "--tags"], stdout=subprocess.PIPE, cwd=PluginPath)
GITVERSION = p.communicate()[0].decode("utf-8").rstrip()
if p.returncode != 0:
debug("git return code", p.returncode)
raise Exception("git return code", p.returncode)
defsettings = os.path.join(PluginPath, 'Default.sublime-settings')
f = open(defsettings,'r')
filedata = f.read()
f.close()
newdata = filedata.replace(get_setting('gorename_version'), GITVERSION+'_')
f = open(defsettings,'w')
f.write(newdata)
f.close()
except:
debug("couldn't get git tag:", sys.exc_info()[0])
# read version
VERSION = sublime.load_settings('Default.sublime-settings').get('gorename_version')
log("version:", VERSION)
# check if user setting exists and creates it
us = sublime.load_settings("GoRename.sublime-settings")
if (not us.has('gorename_debug')):
us.set('gorename_debug', DEBUG)
sublime.save_settings("GoRename.sublime-settings")
class GoRenameCommand(sublime_plugin.TextCommand):
def __init__(self, view):
self.view = view
# ...
def run(self, edit, simulate=False, force=False, verbose=False):
try:
current_selection = self.view.sel()
region = current_selection[0]
text = self.view.substr(sublime.Region(0, region.end()))
cb_map = self.get_map(text)
byte_end = cb_map[sorted(cb_map.keys())[-1]]
byte_begin = None
if not region.empty():
byte_begin = cb_map[region.begin()-1]
else:
byte_begin = byte_end
except:
sublime.error_message('GoRename:\nCouldn\'t get cursor positon, make sure that the Go source file is saved and the cursor is over the identifier (variable, function ...) you want to query.')
error("couldn't cursor position: ", sys.exc_info()[0])
word = self.view.substr(self.view.word(region.begin())).rstrip()
position = self.view.rowcol(region.begin())
line_number = position[0]+1
del position
line_string = self.view.substr(self.view.line(region))
# TODO: improve preliminary identifier validation
if len(word) == 0:
self.view.show_popup('<b>Gorename</b>:<br/> Invalid identifier:\nno identifier here.')
return
message = 'Running GoRename %s:\nFrom %s to %s\n[Line Number: %s][Byte Offset: %s]\nFlags: %s\nReference:\n%s'
global s, f, v, flags
s = simulate
f = force
v = verbose
flags = ''
def compile_flags(only_enabled=False): # and construct flags argument
compiled_flags_array = []
enabledTitle = 'ENABLED: '
if only_enabled:
enabledTitle = ''
global flags
# reset
flags = ''
if s:
compiled_flags_array.append(enabledTitle+'Simulate (-d)')
flags = '-d '
elif not only_enabled:
compiled_flags_array.append('DISABLED: Simulate (-d)')
if f:
compiled_flags_array.append(enabledTitle+'force (-force)')
flags = flags + '-force '
elif not only_enabled:
compiled_flags_array.append('DISABLED: force (-force)')
if v:
compiled_flags_array.append(enabledTitle+'verbose (-v)')
flags = flags + '-v'
elif not only_enabled:
compiled_flags_array.append('DISABLED: verbose (-v)')
return compiled_flags_array
def rename_name_input(name):
debug('flags:', flags)
global renameMe
renameMe['compiled_message'] = message % ('%s',word, name, line_number, byte_begin, compile_flags(True), line_string)
self.write_running(renameMe['compiled_message'] % ('[press ENTER to continue]'), True, True)
renameMe['offset'] = byte_begin
renameMe['name'] = name
renameMe['flags'] = flags
renameMe['file_path'] = self.view.file_name()
renameMe['checksum'] = hashlib.sha256(open(renameMe['file_path'],'rb').read()).hexdigest()
def popup_menu_callback(flag_opt):
global s,f,v
if flag_opt == 0:
s = not s
elif flag_opt == 1:
f = not f
elif flag_opt == 2:
v = not v
if flag_opt != -1:
pop_menu()
else:
self.view.window().show_input_panel('GoRename: rename "%s" (from line %s) to' % (word, line_number), '', rename_name_input, on_change=None, on_cancel=None)
def pop_menu():
self.view.show_popup_menu(compile_flags(), popup_menu_callback)
pop_menu()
def gorename_complete(self, out, err, focus=False):
self.write_out(out, err)
def write_running(self, content, readonly=False, focus=False):
""" Write the "Running..." header to a new file and focus it to get results
"""
#window = self.view.window()
window = sublime.active_window()
view = get_output_view(window)
view.set_read_only(False)
# Run a new command to use the edit object for this view.
view.run_command('go_rename_write_running', {'content': content})
if get_setting("gorename_output", "buffer") == "output_panel":
window.run_command('show_panel', {'panel': "output." + view.name() })
else:
window.focus_view(view)
view.set_read_only(readonly)
# focus no matter what
if focus:
window.focus_view(view)
def write_out(self, result, err):
""" Write the gorename output to a new file.
"""
#window = self.view.window()
window = sublime.active_window()
view = get_output_view(window)
# Run a new command to use the edit object for this view.
view.run_command('go_rename_write_results', {
'result': result,
'err': err})
if get_setting("gorename_output", "buffer") == "output_panel":
window.run_command('show_panel', {'panel': "output." + view.name() })
else:
window.focus_view(view)
def get_map(self, chars):
""" Generate a map of character offset to byte offset for the given string 'chars'.
"""
byte_offset = 0
cb_map = {}
for char_offset, char in enumerate(chars):
cb_map[char_offset] = byte_offset
byte_offset += len(char.encode('utf-8'))
if char == '\n' and self.view.line_endings() == "Windows":
byte_offset += 1
return cb_map
def gorename(self, file_path, begin_offset=None, flags=None, name=None, callback=None):
""" Builds the gorename shell command and calls it, returning it's output as a string.
"""
global runningTool
runningTool = True
pos = "#" + str(begin_offset)
# golangconfig or shellenv ?
cmd_env = ''
if use_golangconfig:
try:
toolpath, cmd_env = golangconfig.subprocess_info('gorename', ['GOPATH', 'PATH'], view=self.view)
toolpath = os.path.realpath(toolpath)
except:
error("golangconfig:", sys.exc_info())
return
else:
toolpath = 'gorename'
cmd_env = shellenv.get_env(for_subprocess=True)[1]
cmd_env.update(get_setting("gorename_env", {}))
debug("env", cmd_env)
gorename_json = ""
if get_setting("gorename_json", False):
gorename_json = "-json"
# Build gorename cmd.
cmd = "%(toolpath)s -offset %(file_path)s:%(pos)s -to %(name)s %(flags)s" % {
"toolpath": toolpath,
"file_path": file_path,
"pos": pos,
"name": name,
"flags": flags}
debug("cmd", cmd)
sublime.set_timeout_async(lambda: self.runInThread(cmd, callback, cmd_env), 0)
def runInThread(self, cmd, callback, env):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True, env=env)
out, err = proc.communicate()
callback(out.decode('utf-8'), err.decode('utf-8'))
global runningTool
runningTool = False
class GoRenameConfirmCommand(sublime_plugin.TextCommand):
""" Writes the gorename output to the current view.
"""
def run(self, edit):
global renameMe
#view = self.view
debug('Stored rename parameters:', renameMe)
# check that the referenced file hasn't changed
if (len(renameMe)==0):
sublime.error_message("Invalid GoRename parameters")
if (runningTool == False):
if ((hashlib.sha256(open(renameMe['file_path'],'rb').read()).hexdigest() != renameMe['checksum']) and (get_setting('gorename_rename_modified_files', False) == False)):
sublime.error_message("Couldn't execute gorename, the referenced file has changed, please start over.")
# reset renameMe
renameMe = {}
else:
GR = GoRenameCommand(self)
GR.write_running(renameMe['compiled_message'] % ('[Running...]'), True, True)
GR.gorename(file_path=renameMe['file_path'] ,begin_offset=renameMe['offset'], name=renameMe['name'], flags=renameMe['flags'], callback=GR.gorename_complete)
# reset renameMe
renameMe = {}
else:
sublime.message_dialog("GoRename tool already executing")
class GoRenameWriteResultsCommand(sublime_plugin.TextCommand):
""" Writes the gorename output to the current view.
"""
def run(self, edit, result, err):
view = self.view
view.set_read_only(False)
if result:
view.insert(edit, view.size(), result)
if err:
errLen = view.insert(edit, view.size(), err)
view.set_read_only(True)
# reset
global renameMe
renameMe = {}
class GoRenameWriteRunningCommand(sublime_plugin.TextCommand):
""" Writes the gorename output to the current view.
"""
def run(self, edit, content):
view = self.view
view.set_viewport_position(view.text_to_layout(view.size() - 1))
view.insert(edit, view.size(), content)
class GoRenameShowResultsCommand(sublime_plugin.TextCommand):
def run(self, edit):
if get_setting("gorename_output", "buffer") == "output_panel":
self.view.window().run_command('show_panel', {'panel': "output.GoRename Output" })
else:
output_view = get_output_view(self.view.window())
self.view.window().focus_view(output_view)
class GoRenameOpenResultCommand(sublime_plugin.EventListener):
'''
def on_modification(self, view):
if view.name() == "GoRename Output":
log("on modif")
'''
def on_selection_modified(self, view):
if view.name() == "GoRename Output":
if len(view.sel()) != 1:
return
if view.sel()[0].size() == 0:
return
lines = view.lines(view.sel()[0])
if len(lines) != 1:
return
line = view.full_line(lines[0])
text = view.substr(line)
format = get_setting("gorename_format")
# "filename:line:col" pattern for json
m = re.search("\"([^\"]+):([0-9]+):([0-9]+)\"", text)
# >filename:line:col< pattern for xml
if m == None:
m = re.search(">([^<]+):([0-9]+):([0-9]+)<", text)
# filename:line.col-line.col: pattern for plain
if m == None:
m = re.search("^([^:]+):([0-9]+).([0-9]+)[-: ]", text)
if m:
w = view.window()
new_view = w.open_file(m.group(1) + ':' + m.group(2) + ':' + m.group(3), sublime.ENCODED_POSITION)
group, index = w.get_view_index(new_view)
if group != -1:
w.focus_group(group)
def get_output_view(window):
view = None
buff_name = 'GoRename Output'
if get_setting("gorename_output", "buffer") == "output_panel":
view = window.create_output_panel(buff_name)
else:
# If the output file is already open, use that.
for v in window.views():
if v.name() == buff_name:
view = v
break
# Otherwise, create a new one.
if view is None:
view = window.new_file()
view.set_name(buff_name)
view.set_scratch(True)
view_settings = view.settings()
view_settings.set('line_numbers', False)
view.set_syntax_file('Packages/GoRename/GoRenameResults.tmLanguage')
return view
def get_setting(key, default=None):
""" Returns the setting in the following hierarchy: project setting, user setting,
default setting. If none are set the 'default' value passed in is returned.
"""
val = None
try:
val = sublime.active_window().active_view().settings().get('GoRename', {}).get(key)
except AttributeError:
pass
if not val:
val = sublime.load_settings("GoRename.sublime-settings").get(key)
if not val:
val = sublime.load_settings("Default.sublime-settings").get(key)
if not val:
val = default
return val