gmlarumbe / vhdl-ts-mode

Emacs VHDL Tree-sitter Major-mode
GNU General Public License v3.0
7 stars 5 forks source link

vhdl-ts-beautify-block-at-point doesn't indent arguments of procedures, functions, ... #8

Open Rutherther opened 3 weeks ago

Rutherther commented 3 weeks ago

vhdl-mode can beautify procedure arguments so that all have : at the same position when vhdl-beautify-buffer is called. vhdl-ts-mode doesn't do this.

Based on this https://github.com/gmlarumbe/vhdl-ts-mode/blob/master/vhdl-ts-mode.el#L963 only generic and port assignments are beautified.

The same goes for <= assignments to signals, := assignments to variables.

Rutherther commented 3 weeks ago

I am trying to research how exactly vhdl-mode does this, and I found a list of alignment keywords.

(defconst vhdl-align-alist
  '(
    ;; after some keywords
    (vhdl-mode "^\\s-*\\(across\\|constant\\|quantity\\|signal\\|subtype\\|terminal\\|through\\|type\\|variable\\)[ \t]"
           "^\\s-*\\(across\\|constant\\|quantity\\|signal\\|subtype\\|terminal\\|through\\|type\\|variable\\)\\([ \t]+\\)" 2)
    ;; before ':'
    (vhdl-mode ":[^=]" "\\([ \t]*\\):[^=]")
    ;; after direction specifications
    (vhdl-mode ":[ \t]*\\(in\\|out\\|inout\\|buffer\\|\\)\\>"
           ":[ \t]*\\(in\\|out\\|inout\\|buffer\\|\\)\\([ \t]+\\)" 2)
    ;; before "==", ":=", "=>", and "<="
    (vhdl-mode "[<:=]=" "\\([ \t]*\\)\\??[<:=]=" 1) ; since "<= ... =>" can occur
    (vhdl-mode "=>" "\\([ \t]*\\)=>" 1)
    (vhdl-mode "[<:=]=" "\\([ \t]*\\)\\??[<:=]=" 1) ; since "=> ... <=" can occur
    ;; before some keywords
    (vhdl-mode "[ \t]after\\>" "[^ \t]\\([ \t]+\\)after\\>" 1)
    (vhdl-mode "[ \t]when\\>" "[^ \t]\\([ \t]+\\)when\\>" 1)
    (vhdl-mode "[ \t]else\\>" "[^ \t]\\([ \t]+\\)else\\>" 1)
    (vhdl-mode "[ \t]across\\>" "[^ \t]\\([ \t]+\\)across\\>" 1)
    (vhdl-mode "[ \t]through\\>" "[^ \t]\\([ \t]+\\)through\\>" 1)
    ;; before "=>" since "when/else ... =>" can occur
    (vhdl-mode "=>" "\\([ \t]*\\)=>" 1)
    )
  "The format of this alist is (MODES [or MODE] REGEXP ALIGN-PATTERN SUBEXP).
It is searched in order.  If REGEXP is found anywhere in the first
line of a region to be aligned, ALIGN-PATTERN will be used for that
region.  ALIGN-PATTERN must include the whitespace to be expanded or
contracted.  It may also provide regexps for the text surrounding the
whitespace.  SUBEXP specifies which sub-expression of
ALIGN-PATTERN matches the white space to be expanded/contracted.")

The reason the vhdl align functions currently do not work for vhdl-ts-mode is coming from the fact that this constant has only vhdl-mode mode defined. The functions for alignment fallback to this list when you do not give them your own list.

I am thinking about overriding (advice-add) those functions to fall back to another list vhdl-ts-align-alist for vhdl-ts-mode that would be a constant for vhdl-ts-mode. This would make vhdl aligns work as well as the vhdl-ts alternatives.

Then it's a question whether to port the aligning behavior to vhdl-ts-mode. I think it could be made better by the vhdl-ts-mode, since right now the alignment does some stuff that doesn't seem very sensible to me, like aligning two when keywords when they are for different statements, not the same one.