Skip to content

Commit 25eefa3

Browse files
committed
Merge 'justinfx/114_rename' into master
2 parents 48083da + bc8c0a3 commit 25eefa3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

GoSublime.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
"caption": "GoSublime: Fmt the current file (without saving it)",
7171
"command": "gs_fmt"
7272
},
73+
{
74+
"caption": "GoSublime: Rename the current selection (gorename)",
75+
"command": "gs_gorename"
76+
},
7377
{
7478
"caption": "GoSublime: New Go File",
7579
"command": "gs_new_go_file"

gscommands.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from gosubl import gspatch
33
from gosubl import mg9
44
import datetime
5+
import subprocess
56
import os
67
import sublime
78
import sublime_plugin
@@ -201,3 +202,61 @@ def run(self, edit, pos, content, added_path=''):
201202
gs.set_attr(k, added_path)
202203
else:
203204
gs.del_attr(k)
205+
206+
class GsGorenameCommand(sublime_plugin.TextCommand):
207+
def is_enabled(self):
208+
fn = self.view.file_name()
209+
if fn:
210+
scope_ok = fn.lower().endswith('.go')
211+
else:
212+
scope_ok = gs.is_go_source_view(self.view)
213+
214+
return scope_ok
215+
216+
def run(self, edit):
217+
view = self.view
218+
219+
# if view.is_dirty():
220+
# sublime.error_message("{0}: GoRename failure: Unsaved file".format(DOMAIN))
221+
# return
222+
223+
region = view.sel()[0]
224+
225+
# If the current selection is empty, try to expand
226+
# it to the word under the cursor
227+
if region.empty():
228+
region = view.word(region)
229+
230+
if region.empty():
231+
sublime.message_dialog('Select an identifier you would like to rename and try again.')
232+
return
233+
234+
current_selection = view.substr(region)
235+
filename = view.file_name()
236+
237+
def on_done(new_name):
238+
if new_name == current_selection:
239+
return
240+
241+
gs.println(DOMAIN, 'Requested New Name: {0}'.format(new_name))
242+
243+
offset = '{0}:#{1}'.format(filename, region.begin())
244+
command = ['gorename', '-offset', offset, '-to', new_name]
245+
246+
gs.println(DOMAIN, 'CMD: {0}'.format(' '.join(command)))
247+
248+
out = ""
249+
try:
250+
p = gs.popen(command, stderr=subprocess.STDOUT)
251+
out = p.communicate()[0]
252+
if p.returncode != 0:
253+
raise OSError("GoRename failed")
254+
255+
except Exception as e:
256+
msg = gs.tbck.format_exc()
257+
if out:
258+
msg = '{0}\n{1}'.format(msg, gs.ustr(out))
259+
gs.show_output('GsGorename', msg, replace=False, merge_domain=False)
260+
261+
view.window().show_input_panel("New name:", current_selection, on_done, None, None)
262+

0 commit comments

Comments
 (0)