From fc037e3d3dd132ba9b1a099abd40507d3ee56cc8 Mon Sep 17 00:00:00 2001 From: Lucas Werkmeister Date: Sat, 13 May 2017 20:45:38 +0200 Subject: [PATCH] Fix initial indentation of first line The initial indentation of the first line can be partially within the region and partially outside of it. For example, suppose the first line is indented by four spaces, and the region begins with the second space (region-beginning is between the first and second space). The indentation that must be added to all lines after the first one is four spaces, but for the first line, one space was outside the region and was therefore not removed by ceylon.formatter, so only three spaces need to be added to that line. Part of #8. --- ceylon-mode.el | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ceylon-mode.el b/ceylon-mode.el index 6a5fbb4..be59939 100644 --- a/ceylon-mode.el +++ b/ceylon-mode.el @@ -156,6 +156,8 @@ complete declarations." ;; remember initial indentation of the code (`ceylon format --pipe` always uses initial indentation 0) (goto-char region-beginning) (setq initial-indentation (current-indentation)) + ;; remember column of the first line (its initial indentation might be partially within and partially outside of region) + (setq first-line-column (current-column)) ;; pipe region through ceylon.formatter (shell-command-on-region region-beginning region-end "ceylon format --pipe" t t (get-buffer-create "*ceylon-format-errors*") t) ;; remember updated region @@ -166,8 +168,11 @@ complete declarations." (if (> initial-indentation 0) (dotimes (n lines) (beginning-of-line) - (indent-to-column initial-indentation) - (setq region-end (+ region-end initial-indentation)) + (setq adjustment (if (eq n 0) + (- initial-indentation first-line-column) ; part of first line's indentation is outside region and wasn't removed + initial-indentation)) + (indent-to-column adjustment) + (setq region-end (+ region-end adjustment)) (forward-line 1))) ;; ceylon.formatter always adds trailing newline, remove if not present before (when (not newline-at-end)