maksimr / vim-jsbeautify

vim plugin which formated javascript files by js-beautify
758 stars 68 forks source link

collapse-preserve-inline #124

Closed kswope closed 8 years ago

kswope commented 8 years ago

will vim-jsbeautify be supporting this option?

brace_style = collapse_preserve_inline

This was a very popular request over at js-beautify.

I've tried to run vim-jsbeautify with

path=/usr/local/bin/js-beautify

but all I get is ugly errors

maksimr commented 8 years ago

@kswope could you post errors

kswope commented 8 years ago
/Users/kevin/.home/.vim/bundle/vim-jsbeautify/plugin/beautify.min.js:1
(function (exports, require, module, __filename, __dirname) { (function(contentPath,options,path){"use strict";var global=function(){return this||eval.call(null,"this")}(),load=global.load,read=global.read,hop=Object.prototype.hasOwnProperty,print=null,hasCache=null,objectPrototypeToString=Object.prototype.toString,isFunction=function(it){return objectPrototypeToString.call(it)==="[object Function]"},isUndefined=function(it){return typeof it==="undefined"},has=function(name){hasCache[name]=isFunction(hasCache[name])?hasCache[name](global):hasCache[name];return hasCache[name]};hasCache=has.cache={};has.add=function(name,test,now,force){if(isUndefined(hasCache[name])||force){hasCache[name]=test}return now&&has(name)};has.add("host-node",global.process&&/node(\.exe||js)?$/.test(global.process.execPath));has.add("host-v8",isFunction(global.load)&&isFunction(global.read));has.add("host-iojs",global.process&&/iojs(\.exe)?$/.test(global.process

TypeError: global.beautify is not a function
    at /Users/kevin/.home/.vim/bundle/vim-jsbeautify/plugin/beautify.min.js:1:1954
    at Object.<anonymous> (/Users/kevin/.home/.vim/bundle/vim-jsbeautify/plugin/beautify.min.js:1:1982)
    at Object.<anonymous> (/Users/kevin/.home/.vim/bundle/vim-jsbeautify/plugin/beautify.min.js:1:2011)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
; .editorconfig

root = true

[**.js]
path=/usr/local/bin/js-beautify
indent_size = 2
indent_style = space
preserve_newlines = true
space_in_paren = true
keep_array_indentation = true
brace_style = collapse_preserve_inline

[**.html]
indent_style = space
indent_size = 2

[**.css]
indent_style = space
indent_size = 2
kevin@walnut frontend $ node --version
v6.4.0
kevin@walnut frontend $ which js-beautify
/usr/local/bin/js-beautify

kevin@walnut frontend $ js-beautify --version
1.6.4
maksimr commented 8 years ago

@kswope thanks!

Try use lib/beautify.js instead bin/js-beautify

.editorconfig

path=/<node_modules>/js-beautify/lib/beautify.js
kswope commented 8 years ago

This works

path=/usr/local/lib/node_modules/js-beautify/js/lib/beautify.js

But unfortunately the reason I did this was for this config, which is being ignored

brace_style = collapse_preserve_inline

Just to make sure it could work I ran this with success

js-beautify --brace-style collapse-preserve-inline file.js
maksimr commented 8 years ago

@kswope you should write collapse-preserve-inline instead _collapse_preserverinline

.editorconfig

brace_style=collapse-preserve-inline

Thanks!

kswope commented 8 years ago

I probably would have tried that, after trying everything else first :)

Thanks.

bubuzzz commented 7 years ago

I have some problem with the collapse-preserve-inline. From the http://jsbeautifier.org/, when I type

import {a,b,c} from 'test' it will be changed to

import { a, b, c } from 'test'; However, when using your settings, it will be changed to

import { 
  a, b, c 
} 
from 'test'; 

My setting is exactly like yours

[**.js]
indent_size = 2
indent_style = space
preserve_newlines = true
space_in_paren = true
keep_array_indentation = true
brace_style = collapse-preserve-inline

Do you have any solution for this ? I stuck with this for a while now

maksimr commented 7 years ago

@bubuzzz I have checked all work as expected. What version of js-beautify you use?

foo

Thanks

bubuzzz commented 7 years ago

@maksimr thank a lot for you quick reply. This is all of my settings

screen shot 2016-12-09 at 11 25 15 am
maksimr commented 7 years ago

@bubuzzz could you post result of :echo g:config_Beautifier and :echo b:config_Beautifier

bubuzzz commented 7 years ago

@maksimr This is my configuration

:echo g:config_Beautifier
{'jsx': {'e4x': 'true', 'indent_style': 'space', 'indent_size': '2'}, 'js': {'keep_array_indentation': 'true', 'space_in_paren': 'true', 'preserve_newlines': 'true', 'brace_style':
'collapse-preserve-inline', 'indent_style': 'space', 'indent_size': '2'}, 'json': {'indent_style': 'space', 'indent_size': '2'}, 'html': {'indent_style': 'space', 'indent_size': '4'
, 'max_char': '78'}, 'css': {'indent_style': 'space', 'indent_size': '2'}}
:echo b:config_Beautifier
{'jsx': {'indent_char': ' ', 'e4x': 'true', 'indent_style': 'space', 'indent_size': '2'}, 'js': {'keep_array_indentation': 'true', 'preserve_newlines': 'true', 'brace_style': 'colla
pse-preserve-inline', 'indent_char': ' ', 'indent_style': 'space', 'indent_size': '2', 'space_in_paren': 'true'}, 'json': {'indent_char': ' ', 'indent_style': 'space', 'indent_size'
: '2'}, 'html': {'indent_char': ' ', 'indent_style': 'space', 'indent_size': '4', 'max_char': '78'}, 'css': {'indent_char': ' ', 'indent_style': 'space', 'indent_size': '2'}}
maksimr commented 7 years ago

@bubuzzz thanks, first of all if you want use custom js-beautify you should specify path

[**.js]

path=~/.vim/bundle/js-beautify/js/lib/beautify.js
indent_style = space
indent_size = 2

Now plugin use own version js-beautify. Could you print result of

cat .vim/bundle/vim-jsbeautify/plugin/lib/package.json | grep version

bubuzzz commented 7 years ago

hmm, i don't have the the js-beautify under the .vim folder

screen shot 2016-12-09 at 11 47 32 am

The cat command shows

bubuzzzs-MBP:~ bubuzzz$ cat .vim/bundle/vim-jsbeautify/plugin/lib/package.json | grep version
  "version": "1.5.1",
maksimr commented 7 years ago

@bubuzzz Thanks, I think problem that plugin use old version of js-beautify. I could suggest two ways:

  1. You could update vim-jsbeautify plugin, now it use js-beautify@1.6.3

  2. or you could set path to your global js-beautify, you can use command which - which js-beautify to get full path

bubuzzz commented 7 years ago

@I tested with the global js-beautify

screen shot 2016-12-09 at 11 53 47 am

Look like the global one indeed make it as that way. I will try to update your plugin to see if it helps

maksimr commented 7 years ago

@bubuzzz you should pass brace-style to js-beautify

js-beautify --brace-style collapse-preserve-inline a.js
bubuzzz commented 7 years ago

@maksimr wow, then i think the .vim/.editorconfig does not affect the beautifier. It is weird. I will update the version of vim-beautify

maksimr commented 7 years ago

@bubuzzz after update vim-jsbeuatify check version of bundled js-beautify cat .vim/bundle/vim-jsbeautify/plugin/lib/package.json | grep version it should be 1.6.3

bubuzzz commented 7 years ago

@maksimr Horray, it worked now. The main problem is because of the version of jsbeautify. In my current vim-jsbeautify, i checked the plugin/lib/package and it still use 1.5.x; however, after I removed the whole vim-jsbeautify and install it again, i check that file and see that the package.json has been updated to 1.6.3. Now i trigger the command and it format code code correctly. Thank a lot for your support

maksimr commented 7 years ago

@bubuzzz no problem!

Example how use js-beautify from npm:

[**.js]
path=/Users/maksimrv/.nvm/versions/node/v5.12.0/lib/node_modules/js-beautify/js/lib/beautify.js
indent_size = 2
indent_style = space
preserve_newlines = true
space_in_paren = true
keep_array_indentation = true
brace_style = collapse-preserve-inline