Skip to content

Instantly share code, notes, and snippets.

@jmondo
Last active May 2, 2022 20:33
Show Gist options
  • Save jmondo/53b68e9465ffa090a0fe7b1dd9108474 to your computer and use it in GitHub Desktop.
Save jmondo/53b68e9465ffa090a0fe7b1dd9108474 to your computer and use it in GitHub Desktop.
Copy ruby class sublime plugin
# Installation
# Go to Tools > Developer > New Plugin, copy this code, save it.
# Then open the key bindings file and add something like
# { "keys": ["super+k", "super+c"], "command": "copy_ruby_class_window" }
# Usage
# With any ruby file open call this command to copy the class name (including all the nested modules) to clipboard.
# Shoutout to https://github.com/astrauka/TestRSpec plugin for the parsing/re logic
import sublime
import sublime_plugin
import re
def text_to_class_name(text):
matches = re.findall("(class|module)[ ]+([a-zA-Z0-9:]+)", text)
names = []
for descriptor, name in matches:
if name == '': break
names.append(name)
if descriptor == 'class': break
return "::".join(names)
class CopyRubyClassWindowCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
# If there's a highlighted selection, only use that
if view.sel()[0]:
region = view.sel()[0]
else: # Use the whole window
region = sublime.Region(0, view.size())
class_name = text_to_class_name(view.substr(region))
sublime.set_clipboard(class_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment