hcl-emacs / hcl-mode

Emacs major mode of HCL(Hashicorp Configuration Language)
GNU General Public License v3.0
27 stars 9 forks source link

hcl-indent-line modifies the buffer's tick count even when no changes are made #7

Closed ashlineldridge closed 5 months ago

ashlineldridge commented 2 years ago

The function hcl-indent-line is called when TAB is pressed via Emacs' indent-for-tab-command. Even when no indentation changes are required, hcl-indent-line modifies the buffer by calling delete-region (here) and then re-adding the indentation. This has the side-effect of modifying the buffer's tick count which indent-for-tab-command inspects (here) to determine whether or not to call completion-at-point. Because indent-for-tab-command thinks that the buffer has changed every time TAB is pressed it never calls completion-at-point which has the effect of breaking the completion system.

ashlineldridge commented 2 years ago

FYI, I have made the following changes to my config to modify the HCL indentation behavior to only modify the buffer if it needs to. This seems to have addressed the issue. Happy to raise a PR if you like.

(use-package terraform-mode
  :hook
  (terraform-mode . (lambda () (setq-local indent-line-function 'my/hcl-indent-line))))

(defun my/hcl--maybe-indent-line (column)
  "Indent current line to COLUMN if required."
  (let ((curcol (- (point) (line-beginning-position))))
    (unless (= curcol column)
      (delete-region (line-beginning-position) (point))
      (indent-to column))))

(defun my/hcl-indent-line ()
  "Indent current line as HCL configuration."
  (interactive)
  (let* ((curpoint (point))
         (pos (- (point-max) curpoint)))
    (back-to-indentation)
    (if (hcl--in-string-or-comment-p)
        (goto-char curpoint)
      (let ((block-indentation (hcl--block-indentation)))
        (if block-indentation
            (if (looking-at "[]}]")
                (my/hcl--maybe-indent-line block-indentation)
              (my/hcl--maybe-indent-line (+ block-indentation hcl-indent-level)))
          (my/hcl--maybe-indent-line (hcl--previous-indentation)))
        (when (> (- (point-max) pos) (point))
          (goto-char (- (point-max) pos)))))))
Hi-Angel commented 8 months ago

Fixed by https://github.com/hcl-emacs/hcl-mode/pull/10

Hi-Angel commented 5 months ago

@ashlineldridge I presume this can be closed. Should've been fixed by https://github.com/hcl-emacs/hcl-mode/commit/3afcc1ac02ba5d84b21b1806a841d89698eb2b35

ashlineldridge commented 5 months ago

@Hi-Angel Yes, I assume it can be based on the described change. Thanks for fixing 👍🏽