Skip to content

Commit

Permalink
Refactoring, and added settings file that lets you configure your cof…
Browse files Browse the repository at this point in the history
…fee path.
  • Loading branch information
surjikal committed Sep 15, 2012
1 parent 4fc8028 commit 57cd06c
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 58 deletions.
3 changes: 3 additions & 0 deletions CoffeeCompile.sublime-settings
@@ -0,0 +1,3 @@
{
"coffee_executable": ""
}
5 changes: 0 additions & 5 deletions Default (Linux).sublime-keymap

This file was deleted.

5 changes: 0 additions & 5 deletions Default (OSX).sublime-keymap

This file was deleted.

5 changes: 0 additions & 5 deletions Default (Windows).sublime-keymap

This file was deleted.

3 changes: 3 additions & 0 deletions Keymaps/Default (Linux).sublime-keymap
@@ -0,0 +1,3 @@
[
{"keys": ["ctrl+shift+c"], "command": "coffee_compile"}
]
3 changes: 3 additions & 0 deletions Keymaps/Default (OSX).sublime-keymap
@@ -0,0 +1,3 @@
[
{"keys": ["ctrl+shift+c"], "command": "coffee_compile"}
]
3 changes: 3 additions & 0 deletions Keymaps/Default (Windows).sublime-keymap
@@ -0,0 +1,3 @@
[
{"keys": ["ctrl+shift+c"], "command": "coffee_compile"}
]
File renamed without changes.
83 changes: 83 additions & 0 deletions Menus/Main.sublime-menu
@@ -0,0 +1,83 @@
[
{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "CoffeeCompile",
"children":
[
{
"command": "open_file",
"args": {"file": "${packages}/CoffeeCompile/CoffeeCompile.sublime-settings"},
"caption": "Settings – Default"
},
{
"command": "open_file",
"args": {"file": "${packages}/User/CoffeeCompile.sublime-settings"},
"caption": "Settings – User"
},
{
"command": "open_file",
"args": {
"file": "${packages}/CoffeeCompile/Keymaps/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file",
"args": {
"file": "${packages}/CoffeeCompile/Keymaps/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file",
"args": {
"file": "${packages}/CoffeeCompile/Keymaps/Default (Linux).sublime-keymap",
"platform": "Linux"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings – User"
},
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings – User"
},
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Linux).sublime-keymap",
"platform": "Linux"
},
"caption": "Key Bindings – User"
},
{ "caption": "-" }
]
}
]
}
]
}
]
13 changes: 4 additions & 9 deletions README.md
Expand Up @@ -3,15 +3,6 @@
This package allows you to compile some or all of your CoffeeScript right from the editor.
The JavaScript output will even have syntax highlighting!

![CoffeeCompile Screenshot](http://i.imgur.com/2J49Q.png)


### Mountain Lions
There is a known issue with OSX 10.8.1 (i.e. the latest Mountain Lion). It should be fixed soon!
Please accept this kitten as a small token of apology:

![Kitten Bribe](http://cityrag.com/wp-content/uploads/2012/06/kitten-closeup-1.jpg)


##Install

Expand Down Expand Up @@ -48,3 +39,7 @@ Just highlight some CoffeeScript code, right click and select the _Coffee Compil
To compile the whole file, don't highlight any text.

This package assumes that the _coffee_ command is on your path (it probably is).


##Screenshot
![CoffeeCompile Screenshot](http://i.imgur.com/2J49Q.png)
83 changes: 49 additions & 34 deletions coffee_compile.py
Expand Up @@ -5,60 +5,60 @@
import sublime


PLATFORM = platform.system()
PLATFORM_IS_WINDOWS = PLATFORM is 'Windows'

CONFIG = {
'Darwin': {
'executable': '/usr/local/share/npm/lib/node_modules/coffee-script/bin/coffee'
},
'Windows': {
'executable': 'coffee.cmd'
},
'default': {
'executable': 'coffee'
}
}
PLATFORM_IS_WINDOWS = platform.system() is 'Windows'


class CoffeeCompileCommand(sublime_plugin.TextCommand):

PANEL_NAME = 'coffeecompile_output'
COFFEE_COMMAND = CONFIG.get(PLATFORM, 'default')['executable']
DEFAULT_COFFEE_EXECUTABLE = 'coffee.cmd' if PLATFORM_IS_WINDOWS else 'coffee'
SETTINGS = sublime.load_settings("CoffeeCompile.sublime-settings")

def run(self, edit):
text = self._get_text_to_compile()
window = self.view.window()

javascript, error = self._compile(text, window)
self._write_output_to_panel(window, javascript, error)


def _compile(self, text, window):
args = [self.COFFEE_COMMAND, '--stdio', '--print', '--lint']
startupinfo = self._get_startupinfo() if PLATFORM_IS_WINDOWS else None
args = self._get_coffee_args()

process = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
startupinfo=startupinfo)
try:
process = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
startupinfo=self._get_startupinfo())
return process.communicate(text)

return process.communicate(text)
except OSError as e:
error_message = 'CoffeeCompile error: '
if e.errno is 2:
error_message += 'Could not find your "coffee" executable. '
error_message += str(e)

sublime.status_message(error_message)
return ('', error_message)

def _write_output_to_panel(self, window, javascript, error):
panel = window.get_output_panel(self.PANEL_NAME)
panel.set_syntax_file('Packages/JavaScript/JavaScript.tmLanguage')

output_view = window.get_output_panel(self.PANEL_NAME)
output_view.set_read_only(False)
edit = output_view.begin_edit()
output_view.insert(edit, 0, javascript)
output_view.end_edit(edit)
output_view.sel().clear()
output_view.set_read_only(True)
text = javascript or str(error)
self._write_to_panel(panel, text)

window.run_command('show_panel', {'panel': 'output.%s' % self.PANEL_NAME})

def _write_to_panel(self, panel, text):
panel.set_read_only(False)
edit = panel.begin_edit()
panel.insert(edit, 0, text)
panel.end_edit(edit)
panel.sel().clear()
panel.set_read_only(True)

def _get_text_to_compile(self):
region = self._get_selected_region() if self._editor_contains_selected_text() \
else self._get_region_for_entire_file()
Expand All @@ -76,7 +76,22 @@ def _editor_contains_selected_text(self):
return True
return False

def _get_coffee_args(self):
coffee_executable = self._get_coffee_executable()

args = [coffee_executable, '--stdio', '--print', '--lint']
if self.SETTINGS.get('bare'):
args.append('--bare')

return args

def _get_coffee_executable(self):
return self.SETTINGS.get('coffee_executable') or self.DEFAULT_COFFEE_EXECUTABLE

def _get_startupinfo(self):
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
if PLATFORM_IS_WINDOWS:
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
return info
return None
1 change: 1 addition & 0 deletions package-metadata.json
@@ -0,0 +1 @@
{"url": "https://github.com/surjikal/sublime-coffee-compile", "version": "2012.09.14.16.51.38", "description": "Preview compiled CoffeeScript in your editor!"}

0 comments on commit 57cd06c

Please sign in to comment.