Can I use Allman style on codemirror?

I am currently working with CodeMirror 6 and I need to configure the editor to use the Allman style indentation (also known as BSD style). In the Allman style, the opening brace { should always be placed on a new line, and I need to ensure that the code is indented correctly.

I have tried several approaches, but I am having trouble achieving the desired indentation behavior, particularly when working with the opening braces. I want to configure it so that when I type an opening brace, it moves to the next line and the indentation follows 4 spaces per indent level (the standard for Allman/BSD style).

I would like to know:

  1. How can I configure CodeMirror 6 to use Allman style indentation for languages like JavaScript?
  2. How can I ensure that the opening brace { is placed on a new line automatically when typing, and the indentation works as expected?
  3. Is there a specific setting or extension in CodeMirror 6 that can help with this, or would I need to write a custom indent function?

Here’s a simplified version of what I am trying to achieve:

function example()
{
    if (true)
    {
        console.log("Allman style indentation");
    }
}

To set the indentation unit to 4 spaces, use the indentUnit fact.

Making the opening brace automatically create a new line is something you’ll have to script yourself.

Most modes (including JavaScript) should be able to indent Allman-style code correctly. (Though, to be honest, using it in 2025, when the entire community is doing something else, doesn’t seem like a terribly productive thing.)

To elaborate on the issue, I’m trying to follow the Allman style when writing code. However, when I type a for loop and press Enter, the next line is automatically indented. This means that every time I open a brace in a for loop (or similar structure), I have to press backspace before typing the opening brace.

#include <iostream>
using namespace std;
int main()
{
  // it goes this way
  for(int i = 0; i < 10; i++)
    {
      adadwd
    }

  // i want this
  for(int i = 0; i < 10; i++)
  {
    adsadsdad 
  }
  return 0;
}

I see what you mean. There was code trying to handle this, but for for, while, and do it was broken. This patch should help.

1 Like