0

Is there a way to disable folding for comments with foldmethod=syntax for Javascript files in vim?

I'm using the following folding code in my vimrc:

if has("folding")
    set foldenable
    set foldopen=hor,search,tag,undo
    set fillchars=diff:\ ,fold:\ ,vert:\

    function! JavaScriptFold()
            setl foldmethod=syntax
            setl foldlevelstart=1
            syn region foldBraces start=/{/ end=/}/ transparent fold keepend extend
    endfunction
endif    

Calling JavaScriptFold folds the following code:

/**
 * Hello, this is a comment.
 */
function hello() {
    console.log('hello');
}

into this:

+--  3 lines: *
+--  3 lines: function hello() {

I want it to get folded into this:

/**
 * Hello, this is a comment.
 */
+--  3 lines: function hello() {

I found out about c_no_comment_fold via this Stack Overflow question on C comment folding, but I can't find an equivalent for Javascript. Is there a way to do this?

Community
  • 1
  • 1
Thomas Upton
  • 1,853
  • 12
  • 22
  • Which JavaScript syntax file do you use (the default? For which Vim version?) – Ingo Karkat Nov 22 '13 at 22:20
  • I just want to ask same question as Ingos, if you set `fdm=syntax`, the folding rule was defined in syntax file. check the syntax file. – Kent Nov 22 '13 at 22:23
  • I'm using the [Javascript syntax file from this plugin](https://github.com/jelera/vim-javascript-syntax), and I'm using vim 7.3. – Thomas Upton Nov 22 '13 at 22:24
  • I didn't test, but you could give this a try: `let javascript_ignore_javaScriptdoc=1` (in your vimrc). also you don't need that function in your vimrc. – Kent Nov 22 '13 at 22:28
  • > also you don't need that function in your vimrc. Without copying it into my vimrc, I can't call it. I'm not sure what the problem is there, but I would love to solve that, too. – Thomas Upton Nov 23 '13 at 01:00

1 Answers1

3

The available JavaScript syntax plugins are in a bad state. The one you're using is a bit weird (defining a function to enable (some) folding, with comment folding enabled by default), and has unused config variables (javaScript_fold, which does nothing).

To disable folding for comments, either directly edit the script and remove the fold keyword from the syntax region javaScriptDocComment ... line, or add the following redefinition to ~/.vim/after/syntax/javascript.vim:

syntax clear javaScriptDocComment
syntax region javaScriptDocComment        matchgroup=javaScriptComment start="/\*\*\s*$"  end="\*/" contains=javaScriptDocTags,javaScriptCommentTodo,@javaScriptHtml,@Spell
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Editing the `javaScriptDocComment` in the syntax plugin works, but adding the redefinition in `vimrc` does not work. As an aside, the `javaScript_fold` variable seems to be related to some kind of default fold. It's also referenced in [this SO question about folding](http://stackoverflow.com/questions/4789605/how-do-i-enable-automatic-folds-in-vim). – Thomas Upton Nov 23 '13 at 00:59
  • 1
    The `.vimrc` is sourced way too early, you need to use the `.vim/after/syntax` directory! – Ingo Karkat Nov 23 '13 at 19:41
  • 1
    Oh, I missed that you mentioned `after/syntax`. Thanks! – Thomas Upton Nov 24 '13 at 18:45