Skip to content

Commit

Permalink
add insert require statement to sublime PhET Package, phetsims/chippe…
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Aug 17, 2017
1 parent b9d6cbe commit e38f8ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ide/sublime-text/PhET Package/README.md
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions ide/sublime-text/PhET Package/phet-commands.sublime-commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"caption": "PhET: Insert Require Statement",
"command": "phet_run_cmd",
"args": {
"cmd": "grunt insert-require-statement --name=$selectedText --file=$file_name"
}
}
]
38 changes: 38 additions & 0 deletions ide/sublime-text/PhET Package/phet_run_cmd.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e38f8ad

Please sign in to comment.