|
2 | 2 | from gosubl import gspatch |
3 | 3 | from gosubl import mg9 |
4 | 4 | import datetime |
| 5 | +import subprocess |
5 | 6 | import os |
6 | 7 | import sublime |
7 | 8 | import sublime_plugin |
@@ -201,3 +202,61 @@ def run(self, edit, pos, content, added_path=''): |
201 | 202 | gs.set_attr(k, added_path) |
202 | 203 | else: |
203 | 204 | 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