Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Fix initial indentation of first line
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lucaswerkmeister committed May 13, 2017
1 parent ec60934 commit fc037e3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ceylon-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit fc037e3

Please sign in to comment.