Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

split class to seperate py file. add fixjsstyle support.

  • Loading branch information...
commit e76a48f57b3af0a1008cad8f75113b907a3342a5 1 parent 5c2fdd4
@fbzhong authored
Showing with 205 additions and 154 deletions.
  1. +9 −6 README.md
  2. +4 −0 const.py
  3. +127 −0 fixjsstyle.py
  4. +3 −148 gjslint.py
  5. +62 −0 listener.py
View
15 README.md
@@ -14,6 +14,8 @@ Features
- Closure Linter: Show Closure Linter Result
+- Closure Linter: Fix JavaScript Style
+
- Highlight error line by click on the result view
- Cross platform: support Windows, Linux and Mac OS X
@@ -27,23 +29,24 @@ Installation
-------------
- Using Package Control http://wbond.net/sublime_packages/package_control
- > Add Repository: https://github.com/fbzhong/sublime-closure-linter
+ - Add Repository: https://github.com/fbzhong/sublime-closure-linter
- Download and extract to Sublime Text 2 Packages folder
- > Windows: %APPDATA%\Sublime Text 2\Packages
- > Mac OS X: ~/Library/Application Support/Sublime Text 2/Packages
- > Linux: ~/.config/sublime-text-2/Packages
+ - Windows: %APPDATA%\Sublime Text 2\Packages
+ - Mac OS X: ~/Library/Application Support/Sublime Text 2/Packages
+ - Linux: ~/.config/sublime-text-2/Packages
How to use?
-------------
- Using the Command Palette (Windows and Linux: Ctrl+Shift+P, OSX: Command+Shift+P) then search for:
-
- - Closure Linter: Run Closure Linter (ctrl+shift+j)
+ - Closure Linter: Run Closure Linter (ctrl+shift+j)
+ - Closure Linter: Fix JavaScript Style (ctrl+alt+shift+j)
- Closure Linter: Show Closure Linter Result
- Find them in Tools menu:
- Tools -> Lint -> Run Closure Linter
+ - Tools -> Lint -> Fix JavaScript Style
- Tools -> Lint -> Show Closure Linter Result
Open up a .js file and hit ctrl+shift+j to run Closure Linter. An new output panel will appear giving you the Closure Linter results:
View
4 const.py
@@ -0,0 +1,4 @@
+
+RESULT_VIEW_NAME = 'gjslint_result_view'
+
+SETTINGS_FILE = "sublime-closure-linter.sublime-settings"
View
127 fixjsstyle.py
@@ -0,0 +1,127 @@
+import os
+import re
+import shutil
+import tempfile
+import sublime
+import sublime_plugin
+from const import *
+from listener import *
+from statusprocess import *
+from asyncprocess import *
+
+class FixJsStyleCommand(sublime_plugin.WindowCommand):
+ def run(self):
+ file_path = self.window.active_view().file_name()
+ file_name = os.path.basename(file_path)
+
+ self.buffered_data = ''
+ self.file_path = file_path
+ self.file_name = file_name
+ self.is_running = True
+ self.tests_panel_showed = False
+ self.file_view = self.window.active_view()
+
+ self.init_tests_panel()
+
+ if file_name.endswith('.js') == False:
+ self.append_data(None, "fixjslint can only be used for JavaScript file.", True)
+ return
+
+ # save file.
+ self.file_view.run_command('save')
+
+ # make a copy.
+ self.target_file_path = os.path.join(tempfile.gettempdir(), self.file_name)
+ shutil.copyfile(self.file_path, self.target_file_path)
+
+ s = sublime.load_settings(SETTINGS_FILE)
+ cmd = s.get('fixjsstyle_path', 'fixjsstyle') + ' ' + s.get('fixjsstyle_flags', '') + ' "' + self.target_file_path + '"'
+
+ if s.get('debug', False) == True:
+ print "DEBUG: " + str(cmd)
+
+ AsyncProcess(cmd, self)
+ StatusProcess('Starting Fix Javascript Style for file ' + file_name, self)
+
+ ClosureLinterEventListener.disabled = True
+
+ def init_tests_panel(self):
+ if not hasattr(self, 'output_view'):
+ self.output_view = self.window.get_output_panel(RESULT_VIEW_NAME)
+ self.output_view.set_name(RESULT_VIEW_NAME)
+ self.clear_test_view()
+ self.output_view.settings().set("file_path", self.file_path)
+
+ def show_tests_panel(self):
+ if self.tests_panel_showed:
+ return
+ self.window.run_command("show_panel", {"panel": "output."+RESULT_VIEW_NAME})
+ self.tests_panel_showed = True
+
+ def clear_test_view(self):
+ self.output_view.set_read_only(False)
+ edit = self.output_view.begin_edit()
+ self.output_view.erase(edit, sublime.Region(0, self.output_view.size()))
+ self.output_view.end_edit(edit)
+ self.output_view.set_read_only(True)
+
+ def append_data(self, proc, data, flush=False):
+ self.buffered_data = self.buffered_data + data.decode("utf-8")
+ str = self.buffered_data.replace(self.target_file_path, self.file_name).replace('\r\n', '\n').replace('\r', '\n')
+
+ if flush == False:
+ rsep_pos = str.rfind('\n')
+ if rsep_pos == -1:
+ # not found full line.
+ return
+ self.buffered_data = str[rsep_pos+1:]
+ str = str[:rsep_pos+1]
+
+ self.show_tests_panel()
+ selection_was_at_end = (len(self.output_view.sel()) == 1 and self.output_view.sel()[0] == sublime.Region(self.output_view.size()))
+ self.output_view.set_read_only(False)
+ edit = self.output_view.begin_edit()
+ self.output_view.insert(edit, self.output_view.size(), str)
+ if selection_was_at_end:
+ self.output_view.show(self.output_view.size())
+ self.output_view.end_edit(edit)
+ self.output_view.set_read_only(True)
+
+ if flush:
+ self.output_view.run_command("goto_line", {"line": 1})
+
+ def update_status(self, msg, progress):
+ sublime.status_message(msg + " " + progress)
+
+ def proc_terminated(self, proc):
+ if proc.returncode == 0:
+ msg = self.file_name + ' fixjsstyle done!'
+ self.replace_file_content()
+ else:
+ msg = ''
+ self.append_data(proc, msg, True)
+
+ # remove file.
+ os.remove(self.target_file_path)
+
+ ClosureLinterEventListener.disabled = False
+
+ def replace_file_content(self):
+ # open target file.
+ f = open(self.target_file_path, 'r')
+
+ # start to replace
+ sel_region = self.file_view.sel()[0]
+
+ edit = self.file_view.begin_edit()
+ self.file_view.erase(edit, sublime.Region(0, self.file_view.size()))
+ self.file_view.insert(edit, self.file_view.size(), f.read())
+ self.file_view.end_edit(edit)
+
+ self.file_view.show_at_center(sel_region)
+
+ self.file_view.run_command('save')
+
+ # done.
+ f.close()
+
View
151 gjslint.py
@@ -1,13 +1,13 @@
import os
import re
+import shutil
import sublime
import sublime_plugin
+from const import *
+from listener import *
from statusprocess import *
from asyncprocess import *
-RESULT_VIEW_NAME = 'gjslint_result_view'
-SETTINGS_FILE = "sublime-closure-linter.sublime-settings"
-
class ShowClosureLinterResultCommand(sublime_plugin.WindowCommand):
"""show closure linter result"""
def run(self):
@@ -93,148 +93,3 @@ def proc_terminated(self, proc):
self.append_data(proc, msg, True)
ClosureLinterEventListener.disabled = False
-
-class FixJsStyleCommand(sublime_plugin.WindowCommand):
- def run(self):
- file_path = self.window.active_view().file_name()
- file_name = os.path.basename(file_path)
-
- self.buffered_data = ''
- self.file_path = file_path
- self.file_name = file_name
- self.is_running = True
- self.tests_panel_showed = False
- self.file_view = self.window.active_view()
-
- self.init_tests_panel()
-
- if file_name.endswith('.js') == False:
- self.append_data(None, "fixjslint can only be used for JavaScript file.", True)
- return
-
- # save file.
- self.file_view.run_command('save')
-
- s = sublime.load_settings(SETTINGS_FILE)
- cmd = s.get('fixjsstyle_path', 'fixjsstyle') + ' ' + s.get('fixjsstyle_flags', '') + ' "' + file_path + '"'
-
- if s.get('debug', False) == True:
- print "DEBUG: " + str(cmd)
-
- AsyncProcess(cmd, self)
- StatusProcess('Starting Fix Javascript Style for file ' + file_name, self)
-
- ClosureLinterEventListener.disabled = True
-
- def init_tests_panel(self):
- if not hasattr(self, 'output_view'):
- self.output_view = self.window.get_output_panel(RESULT_VIEW_NAME)
- self.output_view.set_name(RESULT_VIEW_NAME)
- self.clear_test_view()
- self.output_view.settings().set("file_path", self.file_path)
-
- def show_tests_panel(self):
- if self.tests_panel_showed:
- return
- self.window.run_command("show_panel", {"panel": "output."+RESULT_VIEW_NAME})
- self.tests_panel_showed = True
-
- def clear_test_view(self):
- self.output_view.set_read_only(False)
- edit = self.output_view.begin_edit()
- self.output_view.erase(edit, sublime.Region(0, self.output_view.size()))
- self.output_view.end_edit(edit)
- self.output_view.set_read_only(True)
-
- def append_data(self, proc, data, flush=False):
- self.buffered_data = self.buffered_data + data.decode("utf-8")
- str = self.buffered_data.replace(self.file_path, self.file_name).replace('\r\n', '\n').replace('\r', '\n')
-
- if flush == False:
- rsep_pos = str.rfind('\n')
- if rsep_pos == -1:
- # not found full line.
- return
- self.buffered_data = str[rsep_pos+1:]
- str = str[:rsep_pos+1]
-
- self.show_tests_panel()
- selection_was_at_end = (len(self.output_view.sel()) == 1 and self.output_view.sel()[0] == sublime.Region(self.output_view.size()))
- self.output_view.set_read_only(False)
- edit = self.output_view.begin_edit()
- self.output_view.insert(edit, self.output_view.size(), str)
- if selection_was_at_end:
- self.output_view.show(self.output_view.size())
- self.output_view.end_edit(edit)
- self.output_view.set_read_only(True)
-
- if flush:
- self.output_view.run_command("goto_line", {"line": 1})
-
- def update_status(self, msg, progress):
- sublime.status_message(msg + " " + progress)
-
- def proc_terminated(self, proc):
- if proc.returncode == 0:
- msg = self.file_name + ' fixjsstyle done!'
- else:
- msg = ''
- self.append_data(proc, msg, True)
-
- ClosureLinterEventListener.disabled = False
-
-class ClosureLinterEventListener(sublime_plugin.EventListener):
- """jslint event"""
- disabled = False
- def __init__(self):
- self.previous_resion = None
- self.file_view = None
-
- def on_deactivated(self, view):
- if view.name() != RESULT_VIEW_NAME:
- return
- self.previous_resion = None
-
- if self.file_view:
- self.file_view.erase_regions(RESULT_VIEW_NAME)
-
- def on_selection_modified(self, view):
- if ClosureLinterEventListener.disabled:
- return
- if view.name() != RESULT_VIEW_NAME:
- return
- region = view.line(view.sel()[0])
-
- # make sure call once.
- if self.previous_resion == region:
- return
- self.previous_resion = region
-
- # extract line from jslint result.
- m = re.match('^Line (\d+), E:(\d+):', view.substr(region))
- if m == None:
- return
- line = int(m.group(1))
- col = int(m.group(2))
-
- # hightligh view line.
- view.add_regions(RESULT_VIEW_NAME, [region], "comment")
-
- # find the file view.
- file_path = view.settings().get('file_path')
- window = sublime.active_window()
- file_view = None
- for v in window.views():
- if v.file_name() == file_path:
- file_view = v
- break
- if file_view == None:
- return
-
- self.file_view = file_view
- window.focus_view(file_view)
- file_view.run_command("goto_line", {"line": line})
- file_region = file_view.line(file_view.sel()[0])
-
- # highlight file_view line
- file_view.add_regions(RESULT_VIEW_NAME, [file_region], "string")
View
62 listener.py
@@ -0,0 +1,62 @@
+import os
+import re
+import sublime
+import sublime_plugin
+from const import *
+
+class ClosureLinterEventListener(sublime_plugin.EventListener):
+ """jslint event"""
+ disabled = False
+
+ def __init__(self):
+ self.previous_resion = None
+ self.file_view = None
+
+ def on_deactivated(self, view):
+ if view.name() != RESULT_VIEW_NAME:
+ return
+ self.previous_resion = None
+
+ if self.file_view:
+ self.file_view.erase_regions(RESULT_VIEW_NAME)
+
+ def on_selection_modified(self, view):
+ if ClosureLinterEventListener.disabled:
+ return
+ if view.name() != RESULT_VIEW_NAME:
+ return
+ region = view.line(view.sel()[0])
+
+ # make sure call once.
+ if self.previous_resion == region:
+ return
+ self.previous_resion = region
+
+ # extract line from jslint result.
+ m = re.match('^Line (\d+), E:(\d+):', view.substr(region))
+ if m == None:
+ return
+ line = int(m.group(1))
+ col = int(m.group(2))
+
+ # hightligh view line.
+ view.add_regions(RESULT_VIEW_NAME, [region], "comment")
+
+ # find the file view.
+ file_path = view.settings().get('file_path')
+ window = sublime.active_window()
+ file_view = None
+ for v in window.views():
+ if v.file_name() == file_path:
+ file_view = v
+ break
+ if file_view == None:
+ return
+
+ self.file_view = file_view
+ window.focus_view(file_view)
+ file_view.run_command("goto_line", {"line": line})
+ file_region = file_view.line(file_view.sel()[0])
+
+ # highlight file_view line
+ file_view.add_regions(RESULT_VIEW_NAME, [file_region], "string")
Please sign in to comment.
Something went wrong with that request. Please try again.