emacsorphanage / dart-mode

An Emacs mode for the Dart language
GNU General Public License v3.0
15 stars 2 forks source link

Indentation after function opening bracket takes 4 spaces when passed as argument #115

Open XO39 opened 3 years ago

XO39 commented 3 years ago

Such as the body argument of the test function, when I hit Enter, it gets indented by 4 spaces instead of 2, here is an example

test('some test', () {
    var variable = 'value';
  ...
});

It should be like this:

test('some test', () {
  var variable = 'value';
  ...
});

If I just created the function, it's OK as it's less obvious, but if go back to already created functions and tried to add something in the first line of the function body, it's really annoying, this is how it looks like:

test('some test', () {
    var variable3 = 'value';
  var variable1 = 'value';
  var variable1 = 'value';
  ...
});

This is the case for all function-bodies passed as arguments:

void func(void Function() funAsArg) {
  funcAsArg();
}

func(() {
    var stuff = 'stuff'; // here it's indented with 4 spaces
}

Is there any setting for this or something I can do about it? I'm writing a lot of unit test code and I'm getting a little bit frustrated because of this.

bradyt commented 3 years ago

The current implementation determines indentation by calculating the change in depth of parentheses, brackets, etc, from the previous line. This was fairly easy to implement, and handles many lines of Dart code correctly, or not too far off anyways.

Presumably, improving it would be difficult. If anyone is comfortable with writing parsers, my best guess is that emulating the approach taken in go-mode.el's indentation function would be the best way.

I found a description that seems to match yours, at why-isnt-this-function-or-collection-indented-enough. Maybe if we do end up taking the approach like that in go-mode.el, we could recognize where statements start and end, and whether we are in function parameters or arguments, etc, and that might get us close to detecting the scenario you describe, and indenting it correctly in the majority of cases.

Personally, for now, I simply tolerate all the discrepancies in the indentation function versus official formatter, and reformat code once my editing is valid Dart code again. Some users set Emacs to reformat automatically after each save.

XO39 commented 3 years ago

Thanks for your explanation, I thought I've somehow missed some settings somewhere.

I like the way go-mode.el works, I hope dart-mode.el (and other modes as well) can work like that.