Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have been trying to set up code folding in Vim for JavaScript but the default isn't working correctly. I have little experience with Vim and have a hard time understanding the help files (as proven by my failed attempts at doing this myself).

The JavaScript code that creates problems more or less looks like this:

var x = {};

x.prototype = (function () {
    // Variable declarations...
    var y;

    // Other content...

    y = (function () {
        // Stuff...
    }());

    // Other content continues... eg:
    return {
        // Other stuff...
    };
}());

When I fold y = (function () {}());, it looks like this:

var x = {};

x.prototype = (function () {
    // Variable declarations...
    var y;

    // Other content...

    y = (function () {----------------------------------...

Notice how the rest of the function disappears! It should actually be looking like this:

var x = {};

x.prototype = (function () {
    // Variable declarations...
    var y;

    // Other content

    y = (function () {----------------------------------...

    // Other content continues... eg:
    return {
        // Other stuff...
    };
}());

Like I said above I tried playing around with the syntax file but it only broke the folding.

For convenience, here is the code folding section of the JavaScript syntax file:

if exists("javaScript_fold")
    syn match   javaScriptFunction  "\<function\>"
    syn region  javaScriptFunctionFold  start="\<function\>.*[^};]$" end="^\z1}.*$" transparent fold keepend

    syn sync match javaScriptSync   grouphere javaScriptFunctionFold "\<function\>"
    syn sync match javaScriptSync   grouphere NONE "^}"

    setlocal foldmethod=syntax
    setlocal foldtext=getline(v:foldstart)
else
    syn keyword javaScriptFunction  function
    syn match   javaScriptBraces       "[{}\[\]]"
    syn match   javaScriptParens       "[()]"
endif

syn sync fromstart
syn sync maxlines=100

I have 3 questions:

  1. Is it possible to fix the code folding through ~/.gvimrc?
  2. How do I fix the code folding?
  3. How do I make the folded code take this form y = (function () { ... }());?
share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

To make things simple a quick fix is to change the line in your javascript.vim file for the fold to:

syn region javaScriptFunctionFold  start="{" end="}" transparent fold

Over the next few weeks I'll try to do an overhaul on the entire file to make sure it's current with the latest changes for ECMA5 and the various HTML5 APIs and updates but this patch should fix things in the interim.

share|improve this answer
    
Thanks Scott. Much appreciated! –  user76864 May 26 '11 at 6:32
    
@ScottShattuck updates on that? Is there good JS folding aready? –  Dokkat Mar 22 '13 at 8:28
add comment

I've found this replacement syntax file to work very well, it folds even complicated prototype objects. It was specifically targeted at JS commonly used at Mozilla and in Dojo projects, which looks very much like what you are doing.

share|improve this answer
    
Please provide with some context for the linked article. –  bytebuster Nov 12 '12 at 8:03
1  
@bytebuster, how is this? –  Philipp Kewisch Dec 8 '12 at 22:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.