Thomas Upton
Thomas Upton

Reputation: 1899

Disable folding comments in vim

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?

Upvotes: 0

Views: 734

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172758

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

Upvotes: 3

Related Questions