Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsBeautify() is ignoring .editorconfig #46

Closed
nobleach opened this issue Feb 8, 2014 · 52 comments
Closed

JsBeautify() is ignoring .editorconfig #46

nobleach opened this issue Feb 8, 2014 · 52 comments

Comments

@nobleach
Copy link

nobleach commented Feb 8, 2014

I have tried my .editorconfig in both my ~/ dir and ~/.vim/ but it still is ignored. When I invoke :call JsBeautify() it uses 4 spaces instead of 2.

Here is my .editorconfig

root = true

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

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

[**.html]
indent_style = space
indent_size = 2
max_char = 78
brace_style = expand

@maksimr
Copy link
Owner

maksimr commented Feb 8, 2014

It's very strange.

can you put here what print:

echo b:config_Beautifier

and which version vim

@maksimr maksimr added the bug label Feb 8, 2014
@nobleach
Copy link
Author

nobleach commented Feb 8, 2014

It's empty:

{'js': {}, 'html': {}, 'css': {}}

I'm using

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Feb 5 2014 18:08:13)
MacOS X (unix) version
Included patches: 1-161
Compiled by Homebrew

@maksimr
Copy link
Owner

maksimr commented Feb 8, 2014

It seems that not applied any .editorconfig.
Can you run tests on your machine?

cd /tmp/
git clone --recursive git@github.com:maksimr/vim-jsbeautify.git
cd vim-jsbeautify
npm install
grunt urchin

grunt urchin should run test for vim.

@maksimr
Copy link
Owner

maksimr commented Feb 8, 2014

Do you have g:config_Beautifier in your .vimrc?

@nobleach
Copy link
Author

nobleach commented Feb 8, 2014

g:config_Beautifier returns "command not found".

Running grunt urchin is taking a long time. I'll let it run and report the output.

@maksimr
Copy link
Owner

maksimr commented Feb 8, 2014

g:config_Beautifier is vim variable :)

You can setting beautifier through this variable but it but it is not preferable. Maybe you have
predefined g:config_Beautifier variable, and then plugin doesn't apply settings from .editorconfig

This command check existing g:config_Beautifier in .vimrc:

cat ~/.vimrc | grep g:config_Beautifier

@maksimr
Copy link
Owner

maksimr commented Feb 8, 2014

Thanks!

@nobleach
Copy link
Author

nobleach commented Feb 8, 2014

Right. When I put it into my .vimrc, it gives this error while trying to launch Vim:

Pattern not found: config_Beautifier
Press ENTER or type command to continue

So basically, it's not reading .editorconfig when I put it in ~/ or ~/vim/

@maksimr
Copy link
Owner

maksimr commented Feb 9, 2014

Hm ... I can not reproduce bug :(

@nobleach
Copy link
Author

nobleach commented Feb 9, 2014

It's okay, thank you for the advice. I'll try again sometime.

@kittykatattack
Copy link

Hi,

I'm having exactly the same problem :)
.editorconfig is not being read no matter whether its in ~/ or ~/.vim

All I need to do is change the indent to 2.
But I'm not sure how to do this using g:config_Beautifier in the .vimrc.
Could you please give me an example that I could add to .vimrc?

Thank you!
(Other than this, everything is working great!)

@maksimr
Copy link
Owner

maksimr commented Feb 20, 2014

@kittykatattack Hi

g:config_Beautifier should looks like:

{'js': {'path': '~/.vim/bundle/js-beautify/js/lib/beautify.js', 'indent_style': 'space', 'indent_size': '4', 'bin': 'node'}, 
'html': {'brace_style': 'expand', 'preserve_newlines': 'true', 'path': '~/.vim/bundle/js-beautify/js/lib/beautify-html.js', 'indent_inner_html': 'true', 'indent_style': 'space', 'indent_size': '4', 'max_char': '78'}, 
'css': {'path': '~/.vim/bundle/js-beautify/js/lib/beautify-css.js', 'indent_style': 'space', 'indent_size': '4'}}

You use MacOS X?

@kittykatattack
Copy link

Thanks for the reply!
Sorry, I'm a bit confused :)
I just recently started using vim so there might be something obvious that I'm not doing correctly.
Should g:config_Beautifier be set to a value in the .vimrc, like this?

set g:config_Beautifier=someValue

Should I set a path to the JSON file you posted above?
Or, do I assign a value directly, like this:

set g:config_Beautifier_indent_size=2

I apologize if this is a very basic question!

Yes, I'm using MacOS X

@maksimr
Copy link
Owner

maksimr commented Feb 20, 2014

Sorry, I'm a bit confused :)

No problems :)

Try put it in your .vimrc

let g:config_Beautifier = {}
let g:config_Beautifier['js'] = {}
let g:config_Beautifier['js'].indent_size = '4'

@kittykatattack
Copy link

Thank you, that works perfectly :)

@maksimr
Copy link
Owner

maksimr commented Feb 20, 2014

@kittykatattack Awesome!

But unfortunately this does not solve the bug...

@wayneseymour
Copy link

I'm having the same issue as @nobleach
I've been struggling with it all day.

I'm gonna try to hard code stuff in .vimrc. I too just want 2 spaces. :)

I've tried on two macs: air and pro, both running mavericks and vi/vim/macvim.
I mostly care about vim in iTerm2.

I'm not new to vi, but I'm not at the level of writing plugins for it though! lol

@wayneseymour
Copy link

this did work, but its a workaround:

let g:config_Beautifier = {}
let g:config_Beautifier['js'] = {}
let g:config_Beautifier['js'].indent_size = '2'

@maksimr
Copy link
Owner

maksimr commented May 11, 2014

@wayneseymour Thanks! I will try fix is as soon as possible!

@maksimr
Copy link
Owner

maksimr commented May 11, 2014

You also may use g:editorconfig_Beautifier to determine the path where the editorconfig file is located

@wayneseymour
Copy link

Thanks @maksimr I'll give that a shot!

@maksimr
Copy link
Owner

maksimr commented May 11, 2014

Hey guys can you check last commit. Is it fix problem?

@nobleach
Copy link
Author

Nope, it's still being ignored for me. I have it in my $HOME. I can try it in a project path as well and see if it gets parsed.

@nobleach
Copy link
Author

No, it doesn't get parsed in the project path either.

@nobleach
Copy link
Author

Attempting to manually set the variables from Vim's command mode fails:

let s:plugin_Root_directory = fnamemodify(expand(""), ":h")

returns: E461 Illegal variable name: s:plugin_Root_directory

@maksimr
Copy link
Owner

maksimr commented May 12, 2014

@nobleach thanks!

If you want manually set path to editorconfig file you should define g:editorconfig_Beautifier inside your vimrc file.

@maksimr maksimr reopened this May 12, 2014
@maksimr
Copy link
Owner

maksimr commented May 12, 2014

Unfortunately I don't have MacOS under my hand.

If somebody can try fix it, I think problem in this line, which expand not correct on MacOS

@nobleach
Copy link
Author

If time permits, I will try to look at it. I think the problem might be before that line as the variable isn't setting correctly, and it's not available in the line you highlighted.

@wayneseymour
Copy link

I wish it was in JavaScript, I could help quicker!

--

W.K. Seymour III

Software Engineer - JavaScript, Intel's High Performance Data Division

console.log((function() {return function(lang) {return lang + ' & AngularJS
rock'}})()('Node.js'))

shameless plug: http://blog.wayneseymour.com/

On Mon, May 12, 2014 at 9:27 AM, nobleach notifications@github.com wrote:

If time permits, I will try to look at it. I think the problem might be
before that line as the variable isn't setting correctly, and it's
available in the line you highlighted.

Reply to this email directly or view it on GitHubhttps://github.com//issues/46#issuecomment-42831198
.

@pgilad
Copy link

pgilad commented Jun 7, 2014

I have osx in my work, and can debug this. But I don't think the problem is in that line.

let s:plugin_Root_directory = fnamemodify(expand("<sfile>"), ":h")
let s:paths_Editorconfig = map(['$HOME/.editorconfig', '$HOME/.vim/.editorconfig', s:plugin_Root_directory.'/.editorconfig'], 'expand(v:val)')

That means you are looks in:

  1. $HOME/.editorconfig
  2. $HOME/.vim/.editorconfig
  3. .editorconfig of this plugin

IMO - the default and first place to look in should be the .git parent directory if found (so it can be assigned per project).
Then look in the CWD/.editorconfig and then $HOME whatever...

@eds101
Copy link

eds101 commented Nov 26, 2014

I was having this kind of problem too. I put some print statements in the plugin .vim file to figure out what was going on, and I fixed it for myself. For me, the cause was that I had leading spaces in my .editorconfig file. Getting rid of those solved everything.

@maksimr

My env: OS X Yosemite, Terminal.app

@XavM
Copy link

XavM commented May 22, 2015

I can confirm that after removing leading spaces in the .editorconfig file, JsBeautify is not ignoring .editorconfig file any more

Credits to @eds101

OSX 10.10.3 Yosemite with terminal.app

@maksimr
Copy link
Owner

maksimr commented May 22, 2015

@eds101 , @XavM thanks guys! It's great that you found source of problem.
Can you provide small example of editorconfig with leading space I will try reproduce it on my linux

Thanks!

@trusktr
Copy link

trusktr commented Jun 14, 2015

IMO - the default and first place to look in should be the .git parent directory if found (so it can be assigned per project).
Then look in the CWD/.editorconfig and then $HOME whatever...

That's a very good idea @pgilad! <-- @maksimr

@TylerYang
Copy link

Hi, I found the same issue in my machine. (OSX Vim 7.4) And after I try to run the unit test with grunt, I got the errors below.

Testing beautify_all_test.js.......FFFFFFF
v8 - beautify JS
Message: should be formatted string.
Actual:
{ [Error: Command failed: /bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.js" {} "plugin/lib/js/lib/beautify.js"
/bin/sh: v8: command not found
]
killed: false,
code: 127,
signal: null,
cmd: '/bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.js" {} "plugin/lib/js/lib/beautify.js"' }
Operator:

Expected:
'(["foo", "bar"]).each(function(i) {\n return i;\n});'
at /Users/tyler/.vim/bundle/vim-jsbeautify/test/javascript/beautify_all_test.js:33:26
at ChildProcess.exithandler (child_process.js:210:5)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

v8 - beautify JS with options
Message: should be formatted string with tab.
Actual:
{ [Error: Command failed: /bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.js" "{"indent_size": 2, "indent_char": "\t"}" "plugin/lib/js/lib/beautify.js"
/bin/sh: v8: command not found
]
killed: false,
code: 127,
signal: null,
cmd: '/bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.js" "{"indent_size": 2, "indent_char": "\t"}" "plugin/lib/js/lib/beautify.js"' }
Operator:

Expected:
'(["foo", "bar"]).each(function(i) {\n\t\treturn i;\n});'
at /Users/tyler/.vim/bundle/vim-jsbeautify/test/javascript/beautify_all_test.js:51:26
at ChildProcess.exithandler (child_process.js:210:5)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

v8 - beautify HTML
Message: should be formatted string.
Actual:
{ [Error: Command failed: /bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.html" {} "plugin/lib/js/lib/beautify-html.js"
/bin/sh: v8: command not found
]
killed: false,
code: 127,
signal: null,
cmd: '/bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.html" {} "plugin/lib/js/lib/beautify-html.js"' }
Operator:

Expected:
'

foo\n
\n
'
at /Users/tyler/.vim/bundle/vim-jsbeautify/test/javascript/beautify_all_test.js:67:26
at ChildProcess.exithandler (child_process.js:210:5)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

v8 - beautify HTML with options
Message: should be formatted string with tab.
Actual:
{ [Error: Command failed: /bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.html" "{"indent_size": 2, "indent_char": "\t"}" "plugin/lib/js/lib/beautify-html.js"
/bin/sh: v8: command not found
]
killed: false,
code: 127,
signal: null,
cmd: '/bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.html" "{"indent_size": 2, "indent_char": "\t"}" "plugin/lib/js/lib/beautify-html.js"' }
Operator:

Expected:
'

foo\n\t\t
\n
'
at /Users/tyler/.vim/bundle/vim-jsbeautify/test/javascript/beautify_all_test.js:85:26
at ChildProcess.exithandler (child_process.js:210:5)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

v8 - HtmlBeautify() adding extra newline (issue 36)
Message: should save additional newline.
Actual:
{ [Error: Command failed: /bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/issue_36.html" {} "plugin/lib/js/lib/beautify-html.js"
/bin/sh: v8: command not found
]
killed: false,
code: 127,
signal: null,
cmd: '/bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/issue_36.html" {} "plugin/lib/js/lib/beautify-html.js"' }
Operator:

Expected:
'

foo\n
\n
\n\n'
at /Users/tyler/.vim/bundle/vim-jsbeautify/test/javascript/beautify_all_test.js:101:26
at ChildProcess.exithandler (child_process.js:210:5)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

v8 - beautify CSS
Message: should be formatted string.
Actual:
{ [Error: Command failed: /bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.css" {} "plugin/lib/js/lib/beautify-css.js"
/bin/sh: v8: command not found
]
killed: false,
code: 127,
signal: null,
cmd: '/bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.css" {} "plugin/lib/js/lib/beautify-css.js"' }
Operator:

Expected:
'.foo {\n padding: 0;\n}'
at /Users/tyler/.vim/bundle/vim-jsbeautify/test/javascript/beautify_all_test.js:117:26
at ChildProcess.exithandler (child_process.js:210:5)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

v8 - beautify CSS with options
Message: should be formatted string with tab.
Actual:
{ [Error: Command failed: /bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.css" "{"indent_size": 2, "indent_char": "\t"}" "plugin/lib/js/lib/beautify-css.js"
/bin/sh: v8: command not found
]
killed: false,
code: 127,
signal: null,
cmd: '/bin/sh -c v8 "plugin/beautify.js" --js_arguments "test/javascript/templates/test.css" "{"indent_size": 2, "indent_char": "\t"}" "plugin/lib/js/lib/beautify-css.js"' }
Operator:

Expected:
'.foo {\n\t\tpadding: 0;\n}'
at /Users/tyler/.vim/bundle/vim-jsbeautify/test/javascript/beautify_all_test.js:135:26
at ChildProcess.exithandler (child_process.js:210:5)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at maybeClose (internal/child_process.js:818:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)

Warning: 7/14 assertions failed (916ms) Use --force to continue.

Can somebody give me a hand? Thx.

@TylerYang
Copy link

After I comment out the v8 mixin in beautify_all_test.js, it passed javascript tests but stuck in the vim uni tests.(Wait for the Urchin task to finish more than 5 mins and nothing happened)

And there is no such command v8 in my terminal env. Can you tell me what is it used for ? Maybe I can try to debug in the js and vimscript.

@maksimr
Copy link
Owner

maksimr commented Dec 21, 2015

v8 is google js engine. In current version we support nodejs and v8 but maybe we should drop v8 and leave only nodejs.
vim tests use urchin how test engine and very fragile and I dont know work they or not on OSX

Thanks

@sujith3g
Copy link

thanks @eds101, same issue solved by removing leading white-spaces from ~/.vim/.editorconfig.

@alex-shamshurin
Copy link

One year latter, the issue is still exists on OS X at least:

  1. setting from all .editorconfig are ignored
  2. for jsx files JSX settings are not applied (filletype is set)

@maksimr
Copy link
Owner

maksimr commented Feb 16, 2017

@alex-shamshurin could you show the content of your.editorconfig?
I use macOSnow and all work fine.

Thanks

@alex-shamshurin
Copy link

;.editorconfig

root = true

[**.js?]
; path to optional external js beautifier, default is vim-jsbeautify/plugin/lib
;path=~/.vim/bundle/vim-jsbeautify/js/lib/beautify.js
; Javascript interpreter to be invoked by default 'node'
bin=node
indent_style = space
indent_size = 2

[*]

# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

And what to do with incorrect function applied for jsx? Why jsxBeautify and jsBeautify functions yields same result?

Here is my vimrc

autocmd FileType javascript noremap <buffer>  <c-f> :call JsBeautify()<cr>
" for json
autocmd FileType json noremap <buffer> <c-f> :call JsonBeautify()<cr>
" for jsx
autocmd FileType jsx noremap <buffer> <c-f> :call JsxBeautify()<cr>
" for html
autocmd FileType html noremap <buffer> <c-f> :call HtmlBeautify()<cr>
" for css or scss
autocmd FileType css noremap <buffer> <c-f> :call CSSBeautify()<cr>"

autocmd FileType javascript vnoremap <buffer>  <c-f> :call RangeJsBeautify()<cr>
autocmd FileType json vnoremap <buffer> <c-f> :call RangeJsonBeautify()<cr>
autocmd FileType jsx vnoremap <buffer> <c-f> :call RangeJsxBeautify()<cr>
autocmd FileType html vnoremap <buffer> <c-f> :call RangeHtmlBeautify()<cr>
autocmd FileType css vnoremap <buffer> <c-f> :call RangeCSSBeautify()<cr>

@maksimr
Copy link
Owner

maksimr commented Feb 16, 2017

@alex-shamshurin thanks,

If we talk about global .editorconfig (not per project) then:

  1. We try to find a config in these folders
1. '$HOME/.editorconfig', 
2. '$HOME/.vim/.editorconfig',
3. <BEAUTIFY_PLUGIN_FOLDER>.'/.editorconfig'
  1. When we found config we parse it (Be carefully we use custom logic for parsing global
    editor config!!!
    We don't use editorconfig plugin here)

Most important steps in parsing

2.a If we does not found section(js,jsx,html) in config we set it as empty object - https://github.com/maksimr/vim-jsbeautify/blob/master/plugin/beautifier.vim#L109-L113

2.b Section should look like [**.js] or [**.jsx] - https://github.com/maksimr/vim-jsbeautify/blob/master/plugin/beautifier.vim#L105

2.c Special comment should start with ;vim: - https://github.com/maksimr/vim-jsbeautify/blob/master/plugin/beautifier.vim#L127

2.d We don't parse [*]

So in your editorconfig you have:

[**.js?] - violation 2.b
;path=~/.vim/bundle/vim-jsbeautify/js/lib/beautify.js -violation 2.c
no set for jsx - rule 2.a, we use empty object

As example of global editorconfig you could look at this config and that

Thanks

@alex-shamshurin
Copy link

Of course I need a local congig and it must take a precedence over global one. I found .editorconfig in plugin's folder. I put this config https://github.com/maksimr/dotfiles/blob/master/export/_editorconfig.symlink into project's folder. And the plugin do not see it.

@maksimr
Copy link
Owner

maksimr commented Feb 16, 2017

@alex-shamshurin do you have installed https://github.com/editorconfig/editorconfig-vim?
Because we use a hook from this plugin inside a project folder.

@alex-shamshurin
Copy link

No. nobody told me (((
Optional: editorconfig-vim I thought it is not needed!

@alex-shamshurin
Copy link

I've just insalled it and what see I got - the same 4 spaces http://joxi.ru/p27808YIoZeXjA

@alex-shamshurin
Copy link

And in given example configconfigs ;path is wrong,

@maksimr
Copy link
Owner

maksimr commented Feb 16, 2017

@alex-shamshurin try use [**.js] in your config, is it fix you problem?
What is wrong with this path?

Thanks

@alex-shamshurin
Copy link

;vim:path=~/.vim/bundle/js-beautify/js/lib/beautify.js - wrong plugin name and location

@alex-shamshurin
Copy link

This is my config

root = true

[**.js]
;This section describe common
;javascript settings

;vim:bin=node
vim:max_char=130
indent_style = space
indent_size = 2

[**.jsx]
;vim:bin=node
vim:max_char=130
e4x = true
indent_style = space
indent_size = 2

[**.css]
vim:max_char=130
indent_style = space
indent_size = 2

[**.html]
indent_style = space
indent_size = 2
indent_inner_html=true
preserve_newlines=true
wrap-line-length = 0

[*]
indent_style = space
end_of_line = lf
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false

vimrc is above. It does not use proper function for jsx formatting

@maksimr
Copy link
Owner

maksimr commented Oct 23, 2018

@alex-shamshurin I have added test for your use case and it works fine.
Please check settings in tests with yours.

Thanks

@alex-shamshurin
Copy link

Thanks, but I think everyone should use Prettier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests