Skip to content

Commit 024ddb7

Browse files
committed
Keep point at relative text position when indenting
1 parent 9f4fef6 commit 024ddb7

File tree

1 file changed

+106
-101
lines changed

1 file changed

+106
-101
lines changed

Diff for: puppet-mode.el

+106-101
Original file line numberDiff line numberDiff line change
@@ -678,107 +678,112 @@ of the initial include plus puppet-include-indent."
678678
(defun puppet-indent-line ()
679679
"Indent current line as puppet code."
680680
(interactive)
681-
(beginning-of-line)
682-
(if (bobp)
683-
(indent-line-to 0) ; First line is always non-indented
684-
(let ((not-indented t)
685-
(arglist-start (puppet-in-argument-list))
686-
(array-start (puppet-in-array))
687-
(include-start (puppet-in-include))
688-
(block-indent (puppet-block-indent))
689-
cur-indent)
690-
(cond
691-
(array-start (setq cur-indent (puppet-indent-array array-start)))
692-
(arglist-start (setq cur-indent (puppet-indent-arglist arglist-start)))
693-
(include-start
694-
(setq cur-indent include-start))
695-
696-
((and (looking-at "^\\s-*}.*$") block-indent)
697-
;; This line contains a closing brace and we're at the inner
698-
;; block, so we should indent it matching the indentation of
699-
;; the opening brace of the block.
700-
(setq cur-indent block-indent))
701-
(t
702-
;; Otherwise, we did not start on a block-ending-only line.
703-
(save-excursion
704-
;; Iterate backwards until we find an indentation hint
705-
(while not-indented
706-
(forward-line -1)
707-
(cond
708-
;; Comment lines are ignored unless we're at the start of the
709-
;; buffer.
710-
((or (eq (puppet-syntax-context) 'comment)
711-
(save-excursion (end-of-line)
712-
(eq (puppet-syntax-context) 'comment)))
713-
(if (bobp)
714-
(setq not-indented nil)))
715-
716-
;; Closing paren. Use indentation based on start of
717-
;; argument list
718-
((or (looking-at "^\\s-*\)\\s-*$")
719-
(looking-at "^[^\n\(]*[\)],?\\s-*$"))
720-
(goto-char (puppet-in-argument-list))
721-
(setq cur-indent (current-indentation))
722-
(setq not-indented nil))
723-
724-
;; Closing bracket. Use indentation based on start of
725-
;; array.
726-
((looking-at "^[^[\n]*],?\\s-*$")
727-
(goto-char (puppet-in-array))
728-
(setq cur-indent (current-indentation))
729-
(setq not-indented nil))
730-
731-
;; Brace, paren or bracket (possibly followed by a comma)
732-
;; on a line by itself will already be indented to the
733-
;; right level, so we can cheat and stop there.
734-
((looking-at "^\\s-*[])}]\\(,\\|\\s-*[-~]>\\)?\\s-*\s?$")
735-
(setq cur-indent (current-indentation))
736-
(setq not-indented nil))
737-
738-
;; Brace (possibly followed by a comma) or paren not on a line by
739-
;; itself will be indented one level too much, but don't catch
740-
;; cases where the block is started and closed on the same line.
741-
((looking-at "^[^\n\({]*[\)}],?\\s-*$")
742-
(setq cur-indent (- (current-indentation) puppet-indent-level))
743-
(setq not-indented nil))
744-
745-
;; Indent by one level more than the start of our block. We lose
746-
;; if there is more than one block opened and closed on the same
747-
;; line but it's still unbalanced; hopefully people don't do that.
748-
((looking-at "^.*{[^\n}]*$")
749-
(setq cur-indent (+ (current-indentation) puppet-indent-level))
750-
(setq not-indented nil))
751-
752-
;; Indent by one level if the line ends with an open paren.
753-
((looking-at "^.*\(\\s-*$")
754-
(setq cur-indent (+ (current-indentation) puppet-indent-level))
755-
(setq not-indented nil))
756-
757-
;; Semicolon ends a block for a resource when multiple resources
758-
;; are defined in the same block, but try not to get the case of
759-
;; a complete resource on a single line wrong.
760-
((looking-at "^\\([^'\":\n]\\|\"[^\n\"]*\"\\|'[^\n']*'\\)*;\\s-*$")
761-
(setq cur-indent (- (current-indentation) puppet-indent-level))
762-
(setq not-indented nil))
763-
764-
;; Indent an extra level after : since it introduces a resource.
765-
((looking-at "^.*:\\s-*$")
766-
(setq cur-indent (+ (current-indentation) puppet-indent-level))
767-
(setq not-indented nil))
768-
769-
;; Start of buffer.
770-
((bobp)
771-
(setq not-indented nil)))))
772-
773-
;; If this line contains only a closing paren, we should lose one
774-
;; level of indentation.
775-
(if (looking-at "^\\s-*\)\\s-*$")
776-
(setq cur-indent (- cur-indent puppet-indent-level)))))
777-
778-
;; We've figured out the indentation, so do it.
779-
(if (and cur-indent (> cur-indent 0))
780-
(indent-line-to cur-indent)
781-
(indent-line-to 0)))))
681+
(let ((pos (- (point-max) (point))))
682+
(beginning-of-line)
683+
(if (bobp)
684+
(indent-line-to 0) ; First line is always non-indented
685+
(let ((not-indented t)
686+
(arglist-start (puppet-in-argument-list))
687+
(array-start (puppet-in-array))
688+
(include-start (puppet-in-include))
689+
(block-indent (puppet-block-indent))
690+
cur-indent)
691+
(cond
692+
(array-start (setq cur-indent (puppet-indent-array array-start)))
693+
(arglist-start (setq cur-indent (puppet-indent-arglist arglist-start)))
694+
(include-start
695+
(setq cur-indent include-start))
696+
697+
((and (looking-at "^\\s-*}.*$") block-indent)
698+
;; This line contains a closing brace and we're at the inner
699+
;; block, so we should indent it matching the indentation of
700+
;; the opening brace of the block.
701+
(setq cur-indent block-indent))
702+
(t
703+
;; Otherwise, we did not start on a block-ending-only line.
704+
(save-excursion
705+
;; Iterate backwards until we find an indentation hint
706+
(while not-indented
707+
(forward-line -1)
708+
(cond
709+
;; Comment lines are ignored unless we're at the start of the
710+
;; buffer.
711+
((or (eq (puppet-syntax-context) 'comment)
712+
(save-excursion (end-of-line)
713+
(eq (puppet-syntax-context) 'comment)))
714+
(if (bobp)
715+
(setq not-indented nil)))
716+
717+
;; Closing paren. Use indentation based on start of
718+
;; argument list
719+
((or (looking-at "^\\s-*\)\\s-*$")
720+
(looking-at "^[^\n\(]*[\)],?\\s-*$"))
721+
(goto-char (puppet-in-argument-list))
722+
(setq cur-indent (current-indentation))
723+
(setq not-indented nil))
724+
725+
;; Closing bracket. Use indentation based on start of
726+
;; array.
727+
((looking-at "^[^[\n]*],?\\s-*$")
728+
(goto-char (puppet-in-array))
729+
(setq cur-indent (current-indentation))
730+
(setq not-indented nil))
731+
732+
;; Brace, paren or bracket (possibly followed by a comma)
733+
;; on a line by itself will already be indented to the
734+
;; right level, so we can cheat and stop there.
735+
((looking-at "^\\s-*[])}]\\(,\\|\\s-*[-~]>\\)?\\s-*\s?$")
736+
(setq cur-indent (current-indentation))
737+
(setq not-indented nil))
738+
739+
;; Brace (possibly followed by a comma) or paren not on a line by
740+
;; itself will be indented one level too much, but don't catch
741+
;; cases where the block is started and closed on the same line.
742+
((looking-at "^[^\n\({]*[\)}],?\\s-*$")
743+
(setq cur-indent (- (current-indentation) puppet-indent-level))
744+
(setq not-indented nil))
745+
746+
;; Indent by one level more than the start of our block. We lose
747+
;; if there is more than one block opened and closed on the same
748+
;; line but it's still unbalanced; hopefully people don't do that.
749+
((looking-at "^.*{[^\n}]*$")
750+
(setq cur-indent (+ (current-indentation) puppet-indent-level))
751+
(setq not-indented nil))
752+
753+
;; Indent by one level if the line ends with an open paren.
754+
((looking-at "^.*\(\\s-*$")
755+
(setq cur-indent (+ (current-indentation) puppet-indent-level))
756+
(setq not-indented nil))
757+
758+
;; Semicolon ends a block for a resource when multiple resources
759+
;; are defined in the same block, but try not to get the case of
760+
;; a complete resource on a single line wrong.
761+
((looking-at "^\\([^'\":\n]\\|\"[^\n\"]*\"\\|'[^\n']*'\\)*;\\s-*$")
762+
(setq cur-indent (- (current-indentation) puppet-indent-level))
763+
(setq not-indented nil))
764+
765+
;; Indent an extra level after : since it introduces a resource.
766+
((looking-at "^.*:\\s-*$")
767+
(setq cur-indent (+ (current-indentation) puppet-indent-level))
768+
(setq not-indented nil))
769+
770+
;; Start of buffer.
771+
((bobp)
772+
(setq not-indented nil)))))
773+
774+
;; If this line contains only a closing paren, we should lose one
775+
;; level of indentation.
776+
(if (looking-at "^\\s-*\)\\s-*$")
777+
(setq cur-indent (- cur-indent puppet-indent-level)))))
778+
779+
;; We've figured out the indentation, so do it.
780+
(if (and cur-indent (> cur-indent 0))
781+
(indent-line-to cur-indent)
782+
(indent-line-to 0))))
783+
;; If initial point was within line's indentation,
784+
;; position after the indentation. Else stay at same point in text.
785+
(if (> (- (point-max) pos) (point))
786+
(goto-char (- (point-max) pos)))))
782787

783788

784789
;;; Font locking

0 commit comments

Comments
 (0)