emacsorphanage / dart-mode

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

Mechanism to disable re-indenting a line that's already indented #114

Closed Hixie closed 2 years ago

Hixie commented 3 years ago

(I've read https://github.com/bradyt/dart-mode/wiki/Indentation-and-Syntax-Highlighting)

I like how when I hit enter, the cursor appears on the next line at a somewhat-reasonable place. However, I don't like that when I hit enter (or type a bracket or some other punctuation) the line the cursor was on gets reindented.

For example, suppose I type this:

void main() {
  fatal int exitCode = await process.exitCode
    ..timeout(

When I press the ) character, it undoes my manually-performed indentation:

void main() {
  fatal int exitCode = await process.exitCode
  ..timeout()

So I fix it by going back to the start of the line:

void main() {
  fatal int exitCode = await process.exitCode
    ..timeout()

...then I fill in the first argument:

void main() {
  fatal int exitCode = await process.exitCode
    ..timeout(const Duration()

...and as soon as I press the ) key:

void main() {
  fatal int exitCode = await process.exitCode
  ..timeout(const Duration())

...so I go and fix it again:

void main() {
  fatal int exitCode = await process.exitCode
    ..timeout(const Duration())

...and enter the second argument, , () { }... and it "fixes" my indentation again at the ) and the }.

Is there any way to set things up so that existing indentation is never changed?

bradyt commented 3 years ago

Okay, I think I may have found a solution that fits your description.1

Apparently both closing parens and pressing return for a new line, causes "reindentation" of the "already indented" line. And this might be fixed with setting electric-indent-inhibit to t in dart-mode.

I will try this in my local config, to see if it improves the situations.

(with-eval-after-load "dart-mode"
  (add-to-list 'dart-mode-hook (lambda () (setq electric-indent-inhibit t)))

If we find it works with an overall better user experience, I presume we can add this as a local default in the dart-mode.el source code.

Thank you for formulating, raising and expounding on this particular question, I suspect this partial issue and fix will make editing Dart code in Emacs a bit more pleasant.

Note that dart-mode's indentation function still does not get the "deindent" right after ending a continued statement with a semicolon. I noticed the indentation function of go-mode.el has seen some changes, I hope the next time I attempt to mimic their approach, I will have an easier time, and we can see better emacs indentation in dart-mode. Mostly I've been just tolerating this general issue.

1: indentation - Change the behaviour of RET (with electric indent) to only indent the new line, not the previous one - Emacs Stack Exchange.html

Hixie commented 3 years ago

Oh that's perfect, that's exactly what I want I think. Thanks so much!

bradyt commented 2 years ago

I've added the setting to main branch at https://github.com/bradyt/dart-mode/commit/ae032b9b30ebadfe1b8a48a4cf278417e506d100. Closing optimistically. Feedback welcome.