8

I am trying to build my own indent file for JavaScript. The default indent file for JavaScript turns cindent on, with minimal modifications, and I am using that as a base.

On Vim 7.3 for OSX El Capitan, typing an array produces the following by default:

var test = [
1,
    2,
    3
    ];

And note, while not legal JavaScript, I noticed not using a comma produces a slightly different behavior:

var test = [
1
2
3
];

On 7.4, typing the above two examples produces what I expect:

var test = [
    1,
    2,
    3
];

var test = [
    1
    2
    3
];

However, if you include some characters immediately following the open square bracket it goes back out of whack:

var test = [1
2,
    3,
    4
];

What controls the indentation of the lines between square brackets as well as the placement of the closing square bracket? When viewing the Vim documentation, I don't see a cinoptions value that captures this.

2
  • 1
    Have you tried this one github.com/pangloss/vim-javascript ?
    – Ôrel
    Mar 26, 2016 at 23:06
  • 1
    I have but that plugin appears to indent everything from scratch using custom VimScript functions instead of built in rules. Mar 27, 2016 at 21:16

1 Answer 1

1

Judging from your comment above, you want to use only cinoptions to control the indent for square brackets. It simply wasn't possible before 7.4.355 from what I see in the source code. Click blame up top to see the tagged version, but it's going take a while to load.

Since it looks like cinoptions was originally for C, C++, and Java, it only dealt with curly braces and parenthesis because square brackets are used for defining array sizes or accessing indexes in those languages-not initializing them.

What controls the indentation of the lines between square brackets as well as the placement of the closing square bracket?

After 7.4.355, it's the J option that controls the indentation, but nothing specifically for square brackets. From what I glossed over in the source, the J option only signifies that JavaScript syntax should be considered (curbuf->b_ind_js). It looks for a previous line that's terminated by a comma or opening bracket to line up with. In your fourth example, the line ends with a 1, so it doesn't increase the indentation.

If the first line in your fourth example ended with a [ or , (or both [,) the subsequent lines would increase the indent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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