Change JS grammar indentation


I want to modify the JS grammar so my current indentation is respected when I auto indent or move lines around(move line down/up). The default grammar is almost ok for me but I want to auto indent var, let and const like this:

  x = 1,
  y = 2;

Any ideas on where to look to make this happen? Thanks.


Is there a regular expression that can determine when the next line should be indented and when the indentation should stop? The part of Atom that indents things has no ability to understand language syntax, so unless you can teach it when to add and remove indent levels based on the text you’re typing, it’s not smart enough to tell that this line is part of the const declaration n lines above it.


I examined the source code for a bit and it led me to:

(?x) \{ [^}"']* $ | \[ [^\]"']* $ | \( [^)"']* $

using this code on the console:

    editor = atom.workspace.getActiveTextEditor(),
    scopeDescriptor = editor.getRootScopeDescriptor(),
    buffer = editor.getBuffer(),
    languageMode = buffer.getLanguageMode(),
    grammar = languageMode.getGrammar();

I looked for parts of the regexp on the source (as they might be escaped), I only found “(?x)” inside “packages\language-rust-bundled\settings\rust.cson” but it is a different regexp.

Changing the string on “languageMode.increaseIndentRegexForScopeDescriptor(scopeDescriptor).source” didn’t affect the indentation behaviour.

I’ll post again if I find anything else when I have time.


Changing the behavior of auto-indent

I have to edit for “.js.source” and use the js regexp.

I don’t think there is a regexp that will get evaluated on several lines to be able to change the indent, so basically what DamnedScholar said.


Yeah, that’s the right technique for changing indent rules, and having more than two lines makes it impossible to do anything very smart with the regular expression. The Tree-sitter parser technology is context-aware, but to the best of my knowledge Atom’s auto-indent feature is still the same as it was a year ago.

It might eventually be possible to build a package that uses the new languageMode API to do smarter and more programmable autoindentation. It might be possible now, but that’s not super well-documented (the official API docs still use the old first-mate library and its GrammarRegistry.