Flet / SublimeLinter-contrib-standard

MIT License
71 stars 13 forks source link

Add option to format using --format every run #3

Closed Flet closed 9 years ago

Flet commented 9 years ago

Since maxogden/standard-format is progressing, it makes sense to add an option to the linter to turn on the --format flag to format code automatically every run.

feross commented 9 years ago

+1

Flet commented 9 years ago

After a bit of tinkering and researching, I don't think this will be possible in the same plugin.

Locally I'm able to pass the --format flag just fine, however when the file is rewritten on disk, it triggers another run of the linter. This causes an infinite loop of the linter running...

Also this does not seem to fit the scope of SublimeLinter according to this old issue: https://github.com/SublimeLinter/SublimeLinter3/issues/80

A separate formatting plugin may be required...

Flet commented 9 years ago

OK, I think I have something that works! Added a "Build System" to execute "standard --format" on the current file. So, to format a file just hit CTRL+B.

Once 1.1.0 is on package control, can you give it a shot, @feross ?

feross commented 9 years ago

Sure, let me know when it's on package control.

Flet commented 9 years ago

Its out there now :)

feross commented 9 years ago

After upgrading all my packages, I started getting this error:

Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 303, in on_activated
    callback.on_activated(v)
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/sublimelinter.py", line 282, in on_activated
    self.check_syntax(view)
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/sublimelinter.py", line 209, in check_syntax
    Linter.assign(view, reset=True)
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/linter.py", line 744, in assign
    linter = linter_class(view, syntax)
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/node_linter.py", line 55, in __init__
    self.read_manifest(path.getmtime(self.manifest_path))
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/node_linter.py", line 225, in read_manifest
    self.cached_manifest_hash = self.hash_manifest()
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/node_linter.py", line 232, in hash_manifest
    return hashlib.sha1(f.read()).hexdigest()
TypeError: Unicode-objects must be encoded before hashing

Can't tell if it's this module or SublimeLinter that introduced the bug.

feross commented 9 years ago

Hm, okay it's definitely related to this module. When I uninstall SublimeLinter-contrib-standard the error goes away. Upon re-installing, it errors right away:

reloading plugin SublimeLinter-contrib-standard.linter
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 73, in reload_plugin
    m = importlib.import_module(modulename)
  File "./importlib/__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "/Applications/Sublime Text.app/Contents/MacOS/sublime_plugin.py", line 671, in load_module
    exec(compile(source, source_path, 'exec'), mod.__dict__)
  File "linter in /Users/feross/Library/Application Support/Sublime Text 3/Installed Packages/SublimeLinter-contrib-standard.sublime-package", line 16, in <module>
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/linter.py", line 127, in __init__
    persist.register_linter(cls, name, attrs)
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/persist.py", line 462, in register_linter
    linter.Linter.assign(view, linter_name=linter_name)
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/linter.py", line 744, in assign
    linter = linter_class(view, syntax)
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/node_linter.py", line 55, in __init__
    self.read_manifest(path.getmtime(self.manifest_path))
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/node_linter.py", line 225, in read_manifest
    self.cached_manifest_hash = self.hash_manifest()
  File "/Users/feross/Library/Application Support/Sublime Text 3/Packages/SublimeLinter/lint/node_linter.py", line 232, in hash_manifest
    return hashlib.sha1(f.read()).hexdigest()
TypeError: Unicode-objects must be encoded before hashing
Flet commented 9 years ago

Really bad timing :)

SublimeLinter/SublimeLinter3#209

Flet commented 9 years ago

Latest SublimeLinter is now working... still not quite satisfied with this technique.

Flet commented 9 years ago

OK, so we now have an alternative solution:

first ensure standard-format is installed

npm install standard-format -g

Formatting using standard-format

Automatically reformat a file to JavaScript Standard Style by doing one of the following:

If the shortcut is already mapped or you wish to have a different key mapped, open Preferences > Key Bindings (User) and map a key to the standard_format command like so:

{ "keys": ["ctrl+alt+f"], "command": "standard_format", "context": [{"key": "selector", "operator": "equal", "operand": "source.js,source.json"}] },
Flet commented 9 years ago

@feross this new 2.0.0 should be on package control in 30 minutes or so. Could you give it a try when you have time?

feross commented 9 years ago

works great!

On Wed, Mar 4, 2015 at 10:58 AM Dan Flettre notifications@github.com wrote:

@feross https://github.com/feross this new 2.0.0 should be on package control in 30 minutes or so. Could you give it a try when you have time?

— Reply to this email directly or view it on GitHub https://github.com/Flet/SublimeLinter-contrib-standard/issues/3#issuecomment-77221668 .

Flet commented 9 years ago

Closing as we now have a way to format files on demand!

feross commented 9 years ago

Out of curiosity, why didn't you use the standard --format command instead of requiring standard-format?

Flet commented 9 years ago

standard --format does not work with stdin right now, but standard-format does.

It should not be too difficult to fix in standard, but one question is: should standard --format send the formatted text to stdout and any lint errors to stderr? It can't send both to stdout. I was going to ponder this later tonight, but I'll open an issue on standard to keep track.