Change JS grammar indentation


#1

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:

const
  x = 1,
  y = 2;

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


#2

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.


#3

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

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

using this code on the console:

{
  var
    editor = atom.workspace.getActiveTextEditor(),
    scopeDescriptor = editor.getRootScopeDescriptor(),
    buffer = editor.getBuffer(),
    languageMode = buffer.getLanguageMode(),
    grammar = languageMode.getGrammar();
  console.log(grammar.scopeName);
  console.log(languageMode.increaseIndentRegexForScopeDescriptor(scopeDescriptor).source);
}

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.


#4

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.


#5

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.