Skip to content

Instantly share code, notes, and snippets.

@DCzajkowski
Last active February 18, 2019 18:12
Show Gist options
  • Save DCzajkowski/86fb11e6e5c4aee6b9157364772799ce to your computer and use it in GitHub Desktop.
Save DCzajkowski/86fb11e6e5c4aee6b9157364772799ce to your computer and use it in GitHub Desktop.
# Create a file in Plugins/
# You can run this via Sublime's console with: view.run_command("php_unit_test_method")
# You can bind it to ctrl+e as a regular command
import sublime
import sublime_plugin
class PhpUnitTestMethodCommand(sublime_plugin.TextCommand):
def run(self, edit):
line = self.view.substr(self.view.line(self.view.sel()[0]))
lineSplit = line.split('it', 1)
self.view.replace(edit, self.view.line(self.view.sel()[0]), lineSplit[0] + '/** @test */\n' + lineSplit[0] + 'function ' + 'it' + lineSplit[1].replace(' ', '_') + '() {\n' + lineSplit[0] + ' \n' + lineSplit[0] + '}')
@JeffreyWay
Copy link

I'm sure there's a much simpler way, but this is what I ended up with.

# Create a file in Plugins/
# You can run this via Sublime's console with: view.run_command("php_unit_test_method")
# You can bind it to ctrl+e as a regular command

import sublime
import re
import sublime_plugin

class PhpUnitTestMethodCommand(sublime_plugin.TextCommand):
    def run(self, edit, lines = 10):
        line = self.view.substr(self.view.line(self.view.sel()[0]))
        lineSplit = re.compile(r"(\s{2,})").split(line)

        self.view.replace(edit, self.view.line(self.view.sel()[0]), lineSplit[1] + '/** @test */\n' + lineSplit[1] + 'public function ' + lineSplit[2].strip().replace(' ', '_') + '() \n\t{\n' + lineSplit[1] + '\t\n' + lineSplit[1] + '}')

        (row,col) = self.view.rowcol(self.view.sel()[0].begin())

        self.view.run_command("goto_line", {"line": row})
        self.view.run_command("move_to", {"to": "eol"})

@JeffreyWay
Copy link

Probably still a better approach to calculate the snake_case version of the text, and then programmatically trigger a snippet, like the one you sent me originally. That way, we could add any number of tab stops, but I have no clue how to do that.

@DCzajkowski
Copy link
Author

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment