From e38f8ada63a2fafff8db41c4a07e2d06155dff7c Mon Sep 17 00:00:00 2001 From: zepumph Date: Wed, 16 Aug 2017 16:32:36 -0800 Subject: [PATCH] add insert require statement to sublime PhET Package, https://github.com/phetsims/chipper/issues/586 --- ide/sublime-text/PhET Package/README.md | 22 +++++++++++ .../phet-commands.sublime-commands | 9 +++++ ide/sublime-text/PhET Package/phet_run_cmd.py | 38 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 ide/sublime-text/PhET Package/README.md create mode 100644 ide/sublime-text/PhET Package/phet-commands.sublime-commands create mode 100644 ide/sublime-text/PhET Package/phet_run_cmd.py diff --git a/ide/sublime-text/PhET Package/README.md b/ide/sublime-text/PhET Package/README.md new file mode 100644 index 00000000..e3be67b9 --- /dev/null +++ b/ide/sublime-text/PhET Package/README.md @@ -0,0 +1,22 @@ +# PhET Sublime Package + +This package can be used with sublime to add phet tasks to the command palette. To use these tools, +you must know how to access the `command palette`. + +To add these to your copy of sublime, locate your Packages folder. `Preferences --> Browser Packages`. Then copy this +folder into that `PhET Package` folder into the `Packages` directory. (On Windows the file path looks like: +`C:\Users\{{USER}}\AppData\Roaming\Sublime Text 3\Packages\`). + +When these commands are added into the `Packages` folder in the Sublime installation directory, then they will also be +added to the command palette, although a restart may be required. + +Usage: + +Type `PhET: ` into the command palette for a list of available scripts to be run. +You can open up the Sublime terminal (ctrl + \`) to see results from scripts that are run. + + +* PhET: Insert Require Statement + * Used to add the require statement of the type that is highlighted. + * This will also sort the list of require statements. + * Example: Highlight the word "Node"; open command palette; run command \ No newline at end of file diff --git a/ide/sublime-text/PhET Package/phet-commands.sublime-commands b/ide/sublime-text/PhET Package/phet-commands.sublime-commands new file mode 100644 index 00000000..55e82ef2 --- /dev/null +++ b/ide/sublime-text/PhET Package/phet-commands.sublime-commands @@ -0,0 +1,9 @@ +[ + { + "caption": "PhET: Insert Require Statement", + "command": "phet_run_cmd", + "args": { + "cmd": "grunt insert-require-statement --name=$selectedText --file=$file_name" + } + } +] \ No newline at end of file diff --git a/ide/sublime-text/PhET Package/phet_run_cmd.py b/ide/sublime-text/PhET Package/phet_run_cmd.py new file mode 100644 index 00000000..9f56c9bc --- /dev/null +++ b/ide/sublime-text/PhET Package/phet_run_cmd.py @@ -0,0 +1,38 @@ +import sublime +import sublime_plugin +import subprocess + +# class generally copied from http://mreq.eu/2014/10/running-custom-command/ +class RunCmd(sublime_plugin.WindowCommand): + def run(self, cmd): + + # Save the file first so you don't look working copy changes + window = self.window + view = window.active_view() + view.run_command('save') + + if "$file_name" in cmd: + view = self.window.active_view() + cmd = cmd.replace("$file_name",view.file_name()) + if "$file_dir" in cmd: + view = self.window.active_view() + cmd = cmd.replace("$file_dir",os.path.split(view.file_name())[0]) + + if "$selectedText" in cmd: + + # code section copied from https://stackoverflow.com/questions/19707727/api-how-to-get-selected-text-from-object-sublime-selection + sel = view.sel() + region1 = sel[0] + selectionText = view.substr(region1) + + cmd = cmd.replace("$selectedText", selectionText) + + + print ('Running custom command:', cmd) + + proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=os.path.split(view.file_name())[0]) + while proc.poll() is None: + line = proc.stdout.readline() + if( len(line) > 0): + print (line.decode("utf-8")) # give output from your execution/your own message + self.commandResult = proc.wait() # catch return code \ No newline at end of file