sindresorhus / atom-editorconfig

Helps developers maintain consistent coding styles between different editors
https://atom.io/packages/editorconfig
MIT License
812 stars 80 forks source link

Option to preserve whitespace on cursor on autosave #239

Closed Treeways closed 1 year ago

Treeways commented 4 years ago

This is an extension of issue #207.

I hit enter to type another statement, and editorconfig deletes the indentation, due to autosave.

6fDVk9nSy7

The reason why I made this issue is because VSCode offers a fix specifically for autosave. https://github.com/Microsoft/vscode/issues/24400

Involved .editorconfig-files

[*.{js,jsx,ts,tsx,vue}]
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100

Installed packages

I'm using https://atom.io/packages/autosave-onchange to autosave files. I have Atom's built in autosave disabled. https://atom.io/packages/autosave and of course I'm using the official .editorconfig package https://atom.io/packages/editorconfig

Other packages seem irrelevant in this context. Built-in Atom Packages (93) ├── atom-dark-syntax@0.29.1 ├── atom-dark-ui@0.53.3 ├── atom-light-syntax@0.29.1 ├── atom-light-ui@0.46.3 ├── base16-tomorrow-dark-theme@1.6.0 ├── base16-tomorrow-light-theme@1.6.0 ├── one-dark-ui@1.12.5 ├── one-light-ui@1.12.5 ├── one-dark-syntax@1.8.4 ├── one-light-syntax@1.8.4 ├── solarized-dark-syntax@1.3.0 ├── solarized-light-syntax@1.3.0 ├── about@1.9.1 ├── archive-view@0.65.1 ├── autocomplete-atom-api@0.10.7 ├── autocomplete-css@0.17.5 ├── autocomplete-html@0.8.8 ├── autocomplete-plus@2.42.3 ├── autocomplete-snippets@1.12.1 ├── autoflow@0.29.4 ├── autosave@0.24.6 ├── background-tips@0.28.0 ├── bookmarks@0.46.0 ├── bracket-matcher@0.91.2 ├── command-palette@0.43.5 ├── dalek@0.2.2 ├── deprecation-cop@0.56.9 ├── dev-live-reload@0.48.1 ├── encoding-selector@0.23.9 ├── exception-reporting@0.43.1 ├── find-and-replace@0.219.1 ├── fuzzy-finder@1.14.1 ├── github@0.34.0 ├── git-diff@1.3.9 ├── go-to-line@0.33.0 ├── grammar-selector@0.50.1 ├── image-view@0.64.0 ├── incompatible-packages@0.27.3 ├── keybinding-resolver@0.39.0 ├── line-ending-selector@0.7.7 ├── link@0.31.6 ├── markdown-preview@0.160.2 ├── metrics@1.8.1 ├── notifications@0.71.0 ├── open-on-github@1.3.2 ├── package-generator@1.3.0 ├── settings-view@0.261.3 ├── snippets@1.5.0 ├── spell-check@0.75.0 ├── status-bar@1.8.17 ├── styleguide@0.49.12 ├── symbols-view@0.118.2 ├── tabs@0.110.0 ├── timecop@0.36.2 ├── tree-view@0.228.0 ├── update-package-dependencies@0.13.1 ├── welcome@0.36.9 ├── whitespace@0.37.7 (disabled) ├── wrap-guide@0.41.0 ├── language-c@0.60.18 ├── language-clojure@0.22.8 ├── language-coffee-script@0.50.0 ├── language-csharp@1.1.0 ├── language-css@0.44.2 ├── language-gfm@0.90.7 ├── language-git@0.19.1 ├── language-go@0.47.1 ├── language-html@0.53.0 ├── language-hyperlink@0.17.1 ├── language-java@0.31.4 ├── language-javascript@0.134.0 ├── language-json@1.0.4 ├── language-less@0.34.3 ├── language-make@0.23.0 ├── language-mustache@0.14.5 ├── language-objective-c@0.16.0 ├── language-perl@0.38.1 ├── language-php@0.44.4 ├── language-property-list@0.9.1 ├── language-python@0.53.3 ├── language-ruby@0.72.22 ├── language-ruby-on-rails@0.25.3 ├── language-rust-bundled@0.1.0 ├── language-sass@0.62.1 ├── language-shellscript@0.28.0 ├── language-source@0.9.0 ├── language-sql@0.25.10 ├── language-text@0.7.4 ├── language-todo@0.29.4 ├── language-toml@0.20.0 ├── language-typescript@0.6.0 ├── language-xml@0.35.3 └── language-yaml@0.32.0 Community Packages (35) C:\Users\Nick\.atom\packages ├── atom-icons@0.2.2 (disabled) ├── atom-live-server@2.3.0 ├── atom-live-server-plus@3.0.1 ├── atom-material-syntax-dark@1.0.0 ├── auto-close-html2@0.7.0 ├── autosave-onchange@1.5.1 ├── busy-signal@2.0.1 ├── city-lights-syntax@1.1.8 ├── city-lights-ui@1.5.3 ├── color-tabs@0.1.8 (disabled) ├── editorconfig@2.6.0 ├── emmet@2.4.3 ├── icon-fonts@3.2.2 ├── ide-vue@0.1.9 ├── intentions@1.1.5 ├── language-vue@0.26.0 ├── language-vue-component@0.5.0 (disabled) ├── linter@2.3.1 ├── linter-eslint@8.5.5 ├── linter-ui-default@1.8.1 ├── minimap@4.29.9 ├── monokai-neon-syntax@0.0.1 ├── monokai-one-dark-vivid-syntax@1.6.0 ├── monokai-seti@0.9.0 ├── multiline-tab@0.2.1 ├── no-file-icons@1.0.0 (disabled) ├── one-dark-pro-vivid-syntax@2.3.0 ├── platformio-ide-terminal@2.10.0 ├── rzx-dark-ui@1.1.3 ├── seti-icons@1.5.4 (disabled) ├── seti-syntax@1.2.0 ├── seti-ui@1.11.0 ├── seti-vs-syntax@0.1.1 ├── svg-icons@0.5.4 └── vue-stylefmt@0.5.0
Alhadis commented 4 years ago

👋 Hey there, sorry for the wait. I've just installed the autosave-onchange package and noticed the problem immediately. Atom's autosave package does the same thing as well, although it's much less noticeable since it only autosaves an editor when it loses focus. Unfortunately, neither of these packages provide a way to distinguish between an automated save and a manual (user-triggered) save.

The quickest solution (and why it won't work)

I could monkey-patch the package methods responsible for autosaving, so they toggle a flag each time they run which tells editorconfig not to auto-format on the next save event... buuuuut that'll run the risk of the user manually-saving a file during an autosave, and the file not getting auto-formatted. An example might help explain better:

  1. Before the patched method is triggered, a flag is set telling editorconfig to ignore the next onDidSave event fired on this particular editor
  2. The onDidSave event fires; but because of the flag, editorconfig knows not to auto-format the file in response
  3. The flag is cleared, meaning the next onDidSave event will auto-format the editor, as intended

Now, imagine what would happen if between points 1 & 2, the user manually saves the editor and notices it doesn't auto-format. Since autosave-onchange runs continuously as the user's typing, this is going to cause problems.

The less obvious solution (that involves changing the package's behaviour…)

A better approach would be to listen for an onWillDispatch event called with one of the following event-names:

The auto-format flag is then cleared on the next onDidSave event, ensuring it only happens once, and only after a user has explicitly dispatched a save-related command. Unfortunately, this still isn't foolproof, because a pending command can always be intercepted or cancelled by another listener. There're likely also packages that have their own save-like events, meaning we can't accommodate for each one...


I'm sorry for rambling so much, but I didn't know how else to explain why this isn't easy to fix...

Treeways commented 4 years ago

Likewise, apologies for my late response.

Thank you for explaining in-depth. Is there anything I could do to implement this? I could possibly make a fork/pull request of the package to make it editorconfig compatible.

Alhadis commented 4 years ago

I could possibly make a fork/pull request of the package to make it editorconfig compatible.

If you could, that'd be great. 👍

sindresorhus commented 1 year ago

Closing as Atom is abandoned.