Skip to content

Instantly share code, notes, and snippets.

@stecman
Created February 21, 2015 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stecman/bd23f382ae1304fc1e73 to your computer and use it in GitHub Desktop.
Save stecman/bd23f382ae1304fc1e73 to your computer and use it in GitHub Desktop.
Hack lang: Sublime Text 3 plugin to copy fully qualified class name to clipboard using hh_client
from subprocess import Popen, PIPE, STDOUT
import json
import sublime, sublime_plugin
"""
hhclient_use: copy fully qualified class name to clipboard
Copy the full class name at the current cursor position (or first class in the file)
to the clipboard. Requires the hh_client command line program to provide a JSON formatted
outline of the code in the focused buffer.
"""
class HhclientUseCommand(sublime_plugin.TextCommand):
def run(self, edit):
command = ['/usr/bin/hh_client', '--outline', '--json']
content = self.view.substr(sublime.Region(0, self.view.size()))
with Popen(command, stdout=PIPE, stdin=PIPE, stderr=STDOUT) as proc:
client_stdout = proc.communicate(input=str.encode(content))[0]
raw = client_stdout.decode()
outline = json.loads(raw)
cursor_line = self.get_cursor_line()
name = None
# loop through all returned symbols until we go past the cursor position
for index,item in enumerate(reversed(outline)):
if (item['type'] == 'class'):
name = item['name']
if (cursor_line >= item['line']):
break
sublime.set_clipboard(name)
def get_cursor_line(self):
region = self.view.sel()[0]
cursor = self.view.rowcol(region.a)
return cursor[0] + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment