jdavisclark / jsformat Goto Github PK
View Code? Open in Web Editor NEWJavascript formatting for Sublime Text 2 & 3
Javascript formatting for Sublime Text 2 & 3
Could that formatting choice be disabled? I find it really ruins the readability of a simple if-else, e.g.
$(document).on("keyup", "#LocationsForm input", function () {
if (locationsHaveChanged())
$('#UpdateLocationsButton').prop('disabled', false);
else
$('#UpdateLocationsButton').prop('disabled', true);
});
VS
$(document).on("keyup", "#LocationsForm input", function() {
if (locationsHaveChanged()) $('#UpdateLocationsButton').prop('disabled', false);
else $('#UpdateLocationsButton').prop('disabled', true);
});
Or am I supposed to be curly bracing for reasons I haven't gotten into?
Would be nice to add this option: space_before_conditional
.
The new version of the jsbeautifier library can detected multiple line method chaining and keeps them in separate lines.
Something like this:
textFrame
.attr("width", 200)
.attr("height", 200)
.attr("x", 200)
.attr("y", 200);
Can you update to the new version of jsbeautifier?
Best Nils
README.md states
Package Control
Add https://github.com/jdc0589/JsFormat.git as a Package Control repository. JsFormat will show up in the package install list.
That will return an error in SublimeText2 and will return a 404 in manually clicking on it. I was able to install using https://github.com/jdc0589/JsFormat (removed .git)
Should read:
Package Control
Add https://github.com/jdc0589/JsFormat as a Package Control repository. JsFormat will show up in the package install list.
If I have jsFormat setting "format_on_save" set to true and sublime core setting "ensure_newline_at_eof_on_save" set to true, any EOF newlines end up getting trimmed prior to save.
Hi,
Are there any Package Settings for jsFormat? I can see there are some configurations near the start of the .py files, but are these exposed to the regular package settings files in Sublime Text > Preferences > Package Settings > ...?
Thanks a lot,
Jamie
Is there a way that JSFormat can run when CMD+S is pressed?
Array indentation doesn't seem to work correctly:
Tried to format the same piece of code on http://jsbeautifier.org and it formatted as expected.
The keyboard shortcut ctrl+alt+f
is actually Alt Gr+f
which makes it impossible to type [
character on Windows.
I don't know much about the sublime plugin system (or python) so I might be all wrong. However:
I want to indent my code with 2 spaces.
Looking at the provided default settings, I could find any such related setup. However, http://jsbeautifier.org/ is showing this as an option.
Looking at the jsbeautifier.py code, I could see the following:
self.indent_size = %d
and so, I added my user config as:
{
"indent_size": 2
}
This didn't work, I suspect it is because in the plugin code:
opts.indent_char = " " if settings.get("translate_tabs_to_spaces") else "\t"
opts.indent_size = int(settings.get("tab_size")) if opts.indent_char == " " else 1
Should actually be:
opts.indent_char = " " if settings.get("translate_tabs_to_spaces") else "\t"
opts.indent_size = int(settings.get("indent_size")) if opts.indent_char == " " else 1
Note tab_size ==> indent_size.
Does this makes any sense?
I'm not PRing it because i'm far from being sure on this.
It would be awesome if we could get an usersetting that allows the line width to be set to an arbitrary number like 80, 100 or else.
I noticed that the jslint_happy flag wasn't being respected, and when I copied the settings I had configured for the Default user...
{
// exposed jsbeautifier options
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 4,
"jslint_happy": true,
"brace_style": "collapse", //collapse, expand, end-expand
"keep_array_indentation": false,
"keep_function_indentation": false,
"eval_code": false,
"unescape_strings": false,
"break_chained_methods": false,
// jsformat options
"format_on_save": false
}
...and pasted them into the User settings, the flag was honored. Also, I had to dig into the code to find the brace_style options since they weren't listed in the README.
Not sure whether this is Sublime Text 3 specific or not, and I can't verify because I tried throwing the package in Sublime Text 2 and got the following:
Traceback (most recent call last):
File ".\sublime_plugin.py", line 62, in reload_plugin
File ".\js_formatter.py", line 13, in <module>
import jsbeautifier, jsbeautifier.unpackers
ImportError: No module named unpackers
My issue for Sublime Text 3 should be reproduceable, formatting the whole document (with all or no text selected) works fine, however formating a portion (select a line or two, and hit ctrl+alt+f) throws what I pasted below.
Traceback (most recent call last):
File "D:\Downloads\Sublime Text 3\sublime_plugin.py", line 531, in run_
return self.run(edit)
File "D:\Downloads\Sublime Text 3\Data\Packages\Sublime-JS-Beautifier\js_formatter.py", line 83, in run
_, err = merge_utils.merge_code(self.view, edit, orig, res)
File "D:\Downloads\Sublime Text 3\Data\Packages\Sublime-JS-Beautifier\libs\merge_utils.py", line 84, in merge_code
return (dirty, err)
UnboundLocalError: local variable 'err' referenced before assignment
Hi,
Just discovered your extension, pretty neat!
I'm a big fan of "Braces on own line" indentation style (aka Allman style, cf wikipedia).
First time an indentation plugin catch my attention outside Visual Studio. But with Sublime: it's light & neat!
Unfortunately, the "Space before conditional" option is missing from the sublime plugin :-(
I tried to look for undocumented option in the code and compared with jsbeautifier.org but this option only exists on the website.
Would be super cool to add this into the sublime plugin.
Thx in advance.
PS:
The documentation does not specify the possible values for brace_style (collapse,expand,end-expand), but I managed to find that in the sources so that's ok.
currently when formatting entire files JsFormat will always end the file with a newline, might want to add an option to control this.
Hi, when running a perfectly valid code with default pref I get multiple new line before my function declaration. Is it normal ?
// Blablabla
function validateRules(rules, conditions, actions) {
// code
};
gets indented to this:
// Blablabla
function validateRules(rules, conditions, actions) {
// code
};
After format, there's no newline at end of file. Most code style check tool, editor, scm diff tool will give a warning like "\ No newline at end of file"
It would be better to preserve the newline at the end of time by default.
Format long file with Textmate 2 beautifier in bundled Javascript package.
Format long file with Sublime Text JSFormat package
Hello will this support sublime text 3?
currently the caret it is left at the end of the file after formatting. Would be nice to put it back wherever it was before the formatting took place.
Suggestions on the best way to do this? Everything im thinking of is Regex based and seems like it would be pretty unreliable.
i've upgraded repositories in package control, but is not downloading the last version:
installed version after upgrade:
package: JsFormat
installed_version: 2012.07.11.14.58.52
how to update jsformat in sublime text 2 in order to get the last version of this repo?, using sublime text 2
When running JsFormat on a selected area of text, the contextual indentation is lost.
Reported by rdougan: http://www.sublimetext.com/forum/viewtopic.php?f=5&t=2430&start=10
for example i have such code:
switch (item) {
case 1:
alert('text-1');
break;
case 2:
alert('text-2');
break;
default:
alert('text-3');
}
after js_format
i see such code:
switch (item) {
case 1:
alert('text-1');
break;
case 2:
alert('text-2');
break;
default:
alert('text-3');
}
may be i am doing something wrong, but i can't find appropriate option in sublime-settings to fix this
as above
Recently, I started getting this error below on each startup. However, there is an evalbased.py in JsFormat/libs/jsbeautifier/unpackers/.
Reloading plugin /home/thomas/cloud/Dropbox/shared/sublime-text-2/Packages/JsFormat/js_formatter.py
Warning: finish not called
Traceback (most recent call last):
File "./sublime_plugin.py", line 62, in reload_plugin
File "./js_formatter.py", line 31, in <module>
import jsbeautifier, jsbeautifier.unpackers
File "/home/thomas/cloud/Dropbox/shared/sublime-text-2/Packages/JsFormat/libs/jsbeautifier/unpackers/__init__.py", line 10, in <module>
from jsbeautifier.unpackers import evalbased
ImportError: cannot import name evalbased
This functionality already exists in Sublime core. get it out of JsFormat.
Add a JsFormat.sublime-settings
file with the default settings.
It's a lot quicker to check that in Sublime, than to find the readme.
After formatting could the cursor and buffer return to the position you were at when you triggered the plugin?
Hello.
I've noticed that with JsFormat, my anonymous functions are getting formatted like my named functions, as such:
var fn = function NamedFn() {
// Code
};
var fn2 = function() {
// More Code
};
On jsbeautifier.org, and the way I've learned/read/seen, anonymous functions have a space between the function keyword and the starting parens.
var fn = function () {
// Code
};
hi, I don't know why jsformat can't work. I am using sublime text 2.0.1.
The trace message likes below:
Traceback (most recent call last):
File ".\sublime_plugin.py", line 362, in run_
File ".\js_formatter.py", line 58, in run
File ".\jsbeautifier\__init__.py", line 99, in beautify
File ".\jsbeautifier\__init__.py", line 218, in beautify
File ".\jsbeautifier\__init__.py", line 255, in unpack
ImportError: No module named unpackers
I have checked that jsbeautifier is in the python's sys path.
Can you help me with this?
thanks
I've installed the plugin via Package Control,
and it's there when I list packages,
but when I press ctrl+alt+f in a .js file (jquery) nothing happens
Hello,
when I try to format this code
var log = function(info) {
var text = document.createTextNode("[Line %1, Column %2] %3".replace(/%[0-9]+/g, function(token) {
return info[parseInt(token.substring(1), 10) - 1] || token;
}));
logsPanel.appendChild(text);
};
the lines inside the function's body get 2 tabs instead of just one
var log = function(info) {
var text = document.createTextNode("[Line %1, Column %2] %3".replace(/%[0-9]+/g, function(token) {
return info[parseInt(token.substring(1), 10) - 1] || token;
}));
logsPanel.appendChild(text);
};
Probably something wrong with the inner function
How can I change the default keybinding to initiate a format?
Is there a way to configure it to insert a space between
if(foo)
vs if (foo)
the latter being preferred -- this makes jslint/hint go berzerk and I can't see through all the noize -- plus it isn't a bad rule, since the former could be interpreted as a function invocation
When in HTML files, unless you highlight/select the contents of your script tag first, JsFormat will screw up your html formatting.
Is there a desire for better formatting support in html files?
btw, jsbeautifier can beautify HTML,how about this plugin?
When I set the property "keep_array_indentation": true, having a code as follows:
define (['jquery'
'underscore'
'backbone'
'vm'], function ($, _, Backbone, Vm) ...
Every time I format code, it adds a space in my indentation.
Formatting the first time:
define (['jquery'
'underscore'
'backbone'
'vm'], function ($, _, Backbone, Vm) ...
Formatting the second time:
define (['jquery'
'underscore'
'backbone'
'vm'], function ($, _, Backbone, Vm) ...
And so on.
Allow code to be formatted like this:
var a = 1
, b = "somethign else"
, isAwesome = true;
JSBeautifier supports it (or can be tweaked easily): http://stackoverflow.com/questions/10872184/comma-first-js-formatter
https://gist.github.com/nemtsov/2864266
If I have the option enabled JsFormat also seems to reformat my markdown and other files...How can I avoid this?
Hi,
I've installed JSFormat on Sublime Text 2 but it crashes on startup with this error on the console:
Reloading plugin C:\Documents and Settings\%username%\Application Data\Sublime Text 2\Packages\JsFormat\js_formatter.py
Traceback (most recent call last):
File ".\sublime_plugin.py", line 62, in reload_plugin
File ".\js_formatter.py", line 27, in <module>
add_lib_path(libs_path)
File ".\js_formatter.py", line 21, in add_lib_path
lib_path = _try_get_short_path(lib_path)
File ".\js_formatter.py", line 17, in _try_get_short_path
path = unicode(path)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 29: ordinal not in range(128)
Reloading plugin C:\Documents and Settings\%username%\Application Data\Sublime Text 2\Packages\Package Control\Package Control.py
.\Package Control.py:42: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
I got this error even with all other packages disabled.
With respect to commit 576e645
Seems to be happening as JSFormat works after sublime save. So although sublime adds a newline, plugin removes it.
Suggested Fix
Restore deleted lines 82, 83 in js_formatter.py, but read sublime setting "ensure_newline_at_eof_on_save" to add newline. No need to read it from plugin settings.
It would be super cool if each option was a bit more documented in the README. It's nice to have default values, but some names are not explicit for everyone (me included). Could you add something like :
- "option1_name": "default_value"
`option1_name` does this and that...
- "option2_name": "default_value"
`option2_name` does foo and bar...
Thanks in advance.
What do you think about an implementation for the context menu (more specifically, the right-click menu)?
It seems to be an issue with working directory so that jsbeautifier module cannot be found. Adding the working folder into python's sys path should work.
Would it be possible to add an option like
"method_chaining_indentation": "number"
where
Examples:
Current:
someObject.doFoo(x).doBar(y).doBaz(z);
Indented (better readability):
someObject.doFoo(x)
.doBar(y)
.doBaz(z);
Thanks,
Chris
Hello,
Is there any way to configure spaces around operators?
e.g. var a = 1;
-> var a=1;
Thanks.
Is it possible to incorporate html support.
Sublime Text 2 (OSX ML), from the console after a fresh install of JsFormat through Package Control:
Reloading plugin /Users/niko/Library/Application Support/Sublime Text 2/Packages/JsFormat/js_formatter.py
Traceback (most recent call last):
File "./sublime_plugin.py", line 62, in reload_plugin
File "./js_formatter.py", line 11, in <module>
import jsbeautifier, jsbeautifier.unpackers
ImportError: No module named unpackers
I want to write code like:
var a = 1,
b = 2,
c = function(){};
But after formatting I always get:
var a = 1,
b = 2,
c = function() {};
This is true for default preferences and default keymaps. The issue occurs when the JsFormat folder has been renamed. I don't know how common this is in ST plugins, or how possible it is to get the folder name in python / ST2 API, but implementing a simple lookup for the folder name shouldn't be impossible? (I may be wrong)
Unable to open /D/Downloads/Sublime Text 3/Data/Packages/JsFormat/JsFormat.sublime-settings
As a reference, my actual location is /D/Downloads/Sublime Text 3/Data/Packages/JsFormat-master/JsFormat.sublime-settings
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.