Skip to content

Commit

Permalink
initial code import
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutierrez committed Jan 14, 2012
0 parents commit 8bbfe4d
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
*.pyc
5 changes: 5 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"keys": ["ctrl+alt+m"], "command": "minify"
}
]
5 changes: 5 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"keys": ["ctrl+alt+m"], "command": "minify"
}
]
5 changes: 5 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"keys": ["ctrl+alt+m"], "command": "minify"
}
]
6 changes: 6 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"caption": "Minify Javascript",
"command": "minify"
}
]
72 changes: 72 additions & 0 deletions GoogleClosure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import sublime, sublime_plugin
from GoogleClosureCall import GoogleClosureCall

class Minify(sublime_plugin.TextCommand):
def run(self, edit):
print edit
selections = self.view.sel()

# check if the user has any actual selections
has_selections = False
for sel in selections:
if sel.empty() == False:
has_selections = True

# if not, add the entire file as a selection
if not has_selections:
full_region = sublime.Region(0, self.view.size())
selections.add(full_region)


threads = []
for sel in selections:
selbody = self.view.substr(sel)
thread = GoogleClosureCall(sel, selbody, 5)
threads.append(thread)
thread.start()

selections.clear()
editgroup = self.view.begin_edit('minify')
self.handle_threads(edit, threads, selections, offset = 0, i = 0, dir = 1)

def handle_threads(self, edit, threads, selections, offset = 0, i = 0, dir = 1):
print "handling threads"
next_threads = []
for thread in threads:
if thread.is_alive():
next_threads.append(thread)
continue
if thread.result == False:
continue
offset = self.replace(edit, thread, selections, offset)
threads = next_threads

if len(threads):
before = i % 8
after = (7) - before

if not after:
dir = -1
if not before:
dir = 1

i += dir

self.view.set_status('minify', '[%s=%s]' % (' ' * before, ' ' * after))

sublime.set_timeout(lambda: self.handle_threads(edit, threads, selections, offset, i, dir), 100)
return

self.view.end_edit(edit)
self.view.erase_status('minify')
sublime.status_message('Successfully minified')

def replace(self, edit, thread, selections, offset):
sel = thread.sel
original = thread.original
result = thread.result

if offset:
sel = sublime.Region(sel.begin() + offset, sel.end() + offset)

self.view.replace(edit, sel, result)
32 changes: 32 additions & 0 deletions GoogleClosureCall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sublime, sublime_plugin
import urllib
import urllib2
import threading

class GoogleClosureCall(threading.Thread):
def __init__(self, sel, string, timeout):
self.sel = sel
self.original = string
self.timeout = timeout
self.result = None
threading.Thread.__init__(self)

def run(self):
try:
data = urllib.urlencode({
'js_code': self.original,
'compilation_level': "WHITESPACE_ONLY",
'output_info': "compiled_code" })

ua = 'Sublime Text - Google Closure'
req = urllib2.Request("http://closure-compiler.appspot.com/compile", data, headers = { 'User-Agent': ua })
file = urllib2.urlopen(req, timeout = self.timeout)
self.result = file.read()
return
except (urllib2.HTTPError) as (e):
err = '%s: HTTP error %s contacting API' % (__name__, str(e.code))
except (urllib2.URLError) as (e):
err = '%s: URL error %s contacting API' % (__name__, str(e.code))

sublime.error_message(err)
self.result = False

0 comments on commit 8bbfe4d

Please sign in to comment.