fxbois / web-mode

web template editing mode for emacs
https://web-mode.org
GNU General Public License v3.0
1.63k stars 262 forks source link

Is it possible to define a fixed starting position for first indentation? #1250

Closed iambumblehead closed 2 years ago

iambumblehead commented 2 years ago

(A related issue is opened at mmm-mode project here https://github.com/purcell/mmm-mode/issues/124)

I want to use web-mode from mmm-mode to highlight vue3 components. It looks and works near-perfect. If web-mode could be configured to "start" at the 6-space position it would be perfect. Does anyone know how I might do this?

here are the settings used so far

(defun my-web-mode-hook ()
  (interactive)
  "Hooks for Web mode."
  (setq web-mode-markup-indent-offset 2)
  (setq web-mode-block-padding 2))

I've tried browsing web-mode and experimenting with other settings here but I'm not able to get the result I want.

iambumblehead commented 2 years ago

the mmm-mode setting is improved so open and close parens aren't needed, but this is still not great. Any advice would be appreciated.

Here is what the indentation would ideally look like,

Screen Shot 2022-07-25 at 9 49 59 PM

Here is what the indentation actually looks like,

Screen Shot 2022-07-25 at 9 56 20 PM

if indentation could be disabled for the first markup line <btnIc>, that might be a possible solution. When <btnIc> is manually indented to the correct position, auto-indenting elements below it gives the correct result.

In case it is useful, this is the mmm-mode settting

(mmm-add-classes
    '((js-vue
          :submode web-mode
          :face mmm-code-submode-face
          :front "\: `"
          :back-offset (end-of-line 1)
          :back ">`")))
iambumblehead commented 2 years ago

If the repo owner would recommend an approach, I'd be interested to try and resolve this and to make a PR.

Generally, I believe web-mode-markup-indent-offset is not correct here. The purpose and difference between web-mode-markup-indent-offset and web-mode-markup-indent-padding is not clear or intuitive. Also, changing some of the other settings, such as those with 'code' in the name, seems to affect markup and it would be nice if the settings were more compartmentalized.

iambumblehead commented 2 years ago

the diff below resolves my issue with a hardcoded value 4 added to the first indent position,

diff --git a/web-mode.el b/web-mode.el
index 03be12e..1f6e470 100644
--- a/web-mode.el
+++ b/web-mode.el
@@ -9363,7 +9363,7 @@ Also return non-nil if it is the command `self-insert-command' is remapped to."
      ((not (setq beg (web-mode-markup-indentation-origin pos jsx-depth)))
       (setq offset 0))
      ((null (setq ret (web-mode-element-is-opened beg pos)))
-      (setq offset (web-mode-indentation-at-pos beg)))
+      (setq offset (+ (web-mode-indentation-at-pos beg) (if (= beg 1) 4 0))))
      ((eq ret t)
       (setq offset (+ (web-mode-indentation-at-pos beg)
                       web-mode-markup-indent-offset)))