Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOWTO] Not add trailing whitespaces on newlines in js2-mode? #1679

Closed
sooqua opened this issue Aug 18, 2019 · 9 comments
Closed

[HOWTO] Not add trailing whitespaces on newlines in js2-mode? #1679

sooqua opened this issue Aug 18, 2019 · 9 comments
Labels
is:support A request for user support: questions, how-to's, and discussions re:elisp Personal configuration issues, conflicts or questions re:keybinds Changes to or discussion about Doom's keybinds status:resolved Issue was addressed internally

Comments

@sooqua
Copy link

sooqua commented Aug 18, 2019

What I want to achieve
elisp mode (good):
Peek 2019-08-18 05-47
js2-mode (bad):
Peek 2019-08-18 05-33

How can I achieve the same behavior in js2-mode as in elisp? I want enter key to add newlines at the same indentation level but don't keep the trailing whitespace on the previous.

System information

- OS: gnu/linux (x86_64-pc-linux-gnu)
- Shell: /usr/bin/zsh
- Emacs: 26.2 (Apr 12, 2019)
- Doom: 2.0.9 (HEAD -> develop, _upgrade/develop f754d4ff 2019-07-23 18:23:17 +0200)
- Graphic display: t (daemon: nil)
- System features: XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND GPM DBUS GSETTINGS GLIB NOTIFY ACL GNUTLS LIBXML2 FREETYPE M17N_FLT LIBOTF XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM MODULES THREADS LIBSYSTEMD LCMS2
- Details:
  ```elisp
  env bootstrapper: envvar-file
  elc count: 0
  uname -a:  Linux 5.2.8-arch1-1-ARCH #1 SMP PREEMPT Fri Aug 9 21:36:07 UTC 2019 x86_64
  modules:   (:completion company ivy :ui doom hl-todo modeline nav-flash ophints (popup +all +defaults) treemacs vc-gutter vi-tilde-fringe window-select workspaces :editor evil file-templates fold multiple-cursors rotate-text snippets :emacs dired electric vc :tools eval flycheck (lookup +docsets) magit :lang cc data emacs-lisp javascript lua markdown (org +dragndrop +ipython +pandoc +present) sh web :config default)
  packages:  n/a
  exec-path: (/usr/local/bin /usr/bin /bin /usr/local/sbin /usr/bin/site_perl /usr/bin/vendor_perl /usr/bin/core_perl /usr/lib/emacs/26.2/x86_64-pc-linux-gnu/)
  ```
@sooqua
Copy link
Author

sooqua commented Aug 18, 2019

And why is this not working?

(defun +javascript/newline-and-indent ()
  "TODO"
  (interactive)
  (doom/backward-kill-to-bol-and-indent)
  (newline-and-indent)
  )
(map! :map js2-mode-map [remap newline] #'+javascript/newline-and-indent)

C-h k <enter> still shows (newline-and-indent). Why is map! not working here? This is taken from here.

@sooqua
Copy link
Author

sooqua commented Aug 18, 2019

This is the only thing that works:

(defun +javascript/newline-and-indent ()
  "TODO"
  (doom/backward-kill-to-bol-and-indent))
(advice-add #'newline-and-indent :before #'+javascript/newline-and-indent)

But it overrides newline-and-indent for every mode. Is there a better way?

EDIT: Oh and it kills the line if the cursor is not at the first character =\
And there is no python-indent-context alternative for js so I don't know how to differentiate between empty and non-empty lines.

@hlissner
Copy link
Member

hlissner commented Aug 18, 2019

Try this instead:

(map! :map js2-mode-map [remap newline-and-indent] #'+javascript/newline-and-indent)

@hlissner
Copy link
Member

hlissner commented Aug 18, 2019

That said, Doom enables ws-butler-global-mode by default. It will clean up trailing whitespace (on lines you've touched) when you save, so is manually cleaning it up necessary?

@hlissner hlissner added is:support A request for user support: questions, how-to's, and discussions re:elisp Personal configuration issues, conflicts or questions re:keybinds Changes to or discussion about Doom's keybinds labels Aug 18, 2019
@sooqua
Copy link
Author

sooqua commented Aug 18, 2019

Well it leaves that trailing red block behind, as if it's telling "something is wrong", until I save, or manually go there and 0D it. This is a bit distracting. I changed the value of indent-line-function in js2-mode from rjsx-indent-line to indent-relative as in elisp-mode, but it just inserts 2 spaces where it needs to be 4 and then continues on column 0 (doesn't keep indentation).
What can I write in +javascript/newline-and-indent? Sorry I'm new to elisp. I just need a way to check if a line is empty and to reset the indentation, but I can probably use evil-shift-left for that...

@sooqua
Copy link
Author

sooqua commented Aug 18, 2019

This worked for me:
(advice-add 'newline-and-indent :override #'reindent-then-newline-and-indent)

@sooqua sooqua closed this as completed Aug 18, 2019
@hlissner
Copy link
Member

hlissner commented Aug 18, 2019

You can detect if the current line is empty with:

(save-excursion
  (beginning-of-line)
  (looking-at-p "[ \t]*$")) ; returns non-nil if the line is just whitespace

Or use the (sp-point-in-blank-line) function (essentially does the above).

@hlissner hlissner added the status:resolved Issue was addressed internally label Aug 18, 2019
@sooqua
Copy link
Author

sooqua commented Aug 18, 2019

Wait! It's this override that's causing the issue! It doesn't call (delete-horizontal-space t) before (newline nil t) like original newline-and-indent does.

@hlissner
Copy link
Member

I could've sworn there was a good reason for that, but the advise was written so many years ago I don't remember anymore. I've added the delete-horizontal-space call back to +default*newline-indent-and-continue-comments, which I hope resolves your issue. If that isn't the case, let me know and I will reopen this issue. Thanks for bringing it to my attention!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
is:support A request for user support: questions, how-to's, and discussions re:elisp Personal configuration issues, conflicts or questions re:keybinds Changes to or discussion about Doom's keybinds status:resolved Issue was addressed internally
Projects
None yet
Development

No branches or pull requests

2 participants