Skip to content

Commit

Permalink
Indent GFM code blocks to the current indentation level
Browse files Browse the repository at this point in the history
Fixes #215
  • Loading branch information
phst authored and jrblevin committed Aug 3, 2017
1 parent 3221449 commit 40dbc48
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@
- Prevent clobbering match data in
`markdown-font-lock-extend-region-function`. Thanks to
Philipp Stephani for a patch. ([GH-221][])
- Fix incorrect indentation of inserted GFM code blocks in lists.
([GH-215][])

[gh-81]: https://github.com/jrblevin/markdown-mode/issues/81
[gh-123]: https://github.com/jrblevin/markdown-mode/issues/123
Expand All @@ -260,6 +262,7 @@
[gh-201]: https://github.com/jrblevin/markdown-mode/issues/201
[gh-209]: https://github.com/jrblevin/markdown-mode/issues/209
[gh-213]: https://github.com/jrblevin/markdown-mode/issues/213
[gh-215]: https://github.com/jrblevin/markdown-mode/issues/215
[gh-220]: https://github.com/jrblevin/markdown-mode/pull/220
[gh-221]: https://github.com/jrblevin/markdown-mode/pull/221

Expand Down
20 changes: 15 additions & 5 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -4903,12 +4903,14 @@ automatically in order to have the correct markup."
(setq lang (concat (make-string markdown-spaces-after-code-fence ?\s)
lang)))
(if (markdown-use-region-p)
(let ((b (region-beginning)) (e (region-end)))
(let* ((b (region-beginning)) (e (region-end))
(indent (progn (goto-char b) (current-indentation))))
(goto-char e)
;; if we're on a blank line, don't newline, otherwise the ```
;; should go on its own line
(unless (looking-back "\n" nil)
(newline))
(indent-to indent)
(insert "```")
(markdown-ensure-blank-line-after)
(goto-char b)
Expand All @@ -4918,11 +4920,19 @@ automatically in order to have the correct markup."
(newline)
(forward-line -1))
(markdown-ensure-blank-line-before)
(indent-to indent)
(insert "```" lang))
(markdown-ensure-blank-line-before)
(insert "```" lang "\n\n```")
(markdown-ensure-blank-line-after)
(forward-line -1)))
(let ((indent (current-indentation)))
(delete-horizontal-space :backward-only)
(markdown-ensure-blank-line-before)
(indent-to indent)
(insert "```" lang "\n")
(indent-to indent)
(insert ?\n)
(indent-to indent)
(insert "```")
(markdown-ensure-blank-line-after))
(end-of-line 0)))

(defun markdown-code-block-lang (&optional pos-prop)
"Return the language name for a GFM or tilde fenced code block.
Expand Down
22 changes: 21 additions & 1 deletion tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -4973,7 +4973,27 @@ This includes preserving whitespace after the pipe."
(should (markdown-use-region-p))
(markdown-insert-gfm-code-block "elisp")
(should (string-equal (buffer-string)
"line 1\n\n``` elisp\nline 2\n```\n\nline 3\n"))))
"line 1\n\n``` elisp\nline 2\n```\n\nline 3\n")))
;; Test indented list item
(markdown-test-string-gfm "1. foo\n "
(goto-char (point-max))
(markdown-insert-gfm-code-block "elisp")
(should (equal (buffer-substring-no-properties (point-min) (point-max))
"1. foo\n\n ``` elisp\n \n ```"))
(should (equal (buffer-substring-no-properties (point) (point-max))
"\n ```")))
;; Test indented list item with active region
(markdown-test-string-gfm "1. foo\n bar\n"
(let ((transient-mark-mode t))
(forward-line)
(push-mark nil :nomsg :activate)
(end-of-line)
(should (markdown-use-region-p))
(markdown-insert-gfm-code-block "elisp"))
(should (equal (buffer-substring-no-properties (point-min) (point-max))
"1. foo\n\n ``` elisp\n bar\n ```\n\n"))
(should (equal (buffer-substring-no-properties (point) (point-max))
"\n bar\n ```\n\n"))))

(ert-deftest test-markdown-gfm/gfm-parse-buffer-for-languages ()
"Parse buffer for existing languages for `markdown-gfm-used-languages' test."
Expand Down

0 comments on commit 40dbc48

Please sign in to comment.