Skip to content

Commit

Permalink
Update filling functionality.
Browse files Browse the repository at this point in the history
`fill-paragraph`, `fill-region`, and `auto-fill-mode` parse
comments and multline strings as KDoc markdown and fill paragraphs with
appropriate prefix (combination of `/`, `*`, `>`, and indentation).

Example:

```kotlin
/**
 *
 * *  >>  -  >10.  2)  aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
 *    >>     >         aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
 *    >>     >         aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
 *
 * @param foo  aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
 *     aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
 *     aaa aaa aaa
 * @param bar
 *     aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
 *     aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
 *     aaa
 */
```
  • Loading branch information
taku0 committed Mar 21, 2023
1 parent fddd747 commit 0a89bdf
Show file tree
Hide file tree
Showing 13 changed files with 5,195 additions and 33 deletions.
716 changes: 716 additions & 0 deletions kotlin-mode-fill.el

Large diffs are not rendered by default.

872 changes: 872 additions & 0 deletions kotlin-mode-kdoc-comment-parser.el

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions kotlin-mode-lexer.el
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ It have the type and the start position.")
"Return the start position of the CHUNK."
(and chunk (oref chunk start)))

(defun kotlin-mode--chunk-end (chunk)
"Return the end position of the CHUNK."
(save-excursion
(goto-char (oref chunk start))
(if (kotlin-mode--chunk-comment-p chunk)
(forward-comment 1)
(kotlin-mode--forward-token))
(point)))

(defun kotlin-mode--chunk-comment-p (chunk)
"Return non-nil if the CHUNK is a comment."
(and chunk
Expand Down Expand Up @@ -455,6 +464,17 @@ If PARSER-STATE is given, it is used instead of (syntax-ppss)."
(t
nil))))

(defun kotlin-mode--chunk-after-spaces ()
"Return the chunk at the point or after spaces.
If there is no chunk at the point nor after spaces, return nil."
(or (kotlin-mode--chunk-after)
(save-excursion
(skip-chars-forward " ")
(when (memq (char-after) '(?\" ?/))
(forward-char)
(kotlin-mode--chunk-after)))))

;; Syntax table

(defvar kotlin-mode-syntax-table
Expand Down Expand Up @@ -2584,6 +2604,17 @@ Newlines inside comments are ignored."
(forward-comment (- (point)))
(point)))))

(defun kotlin-mode--same-line-p (point1 point2)
"Return non-nil if POINT1 and POINT2 is on the same line.
Return nil otherwise."
(= (save-excursion
(goto-char point1)
(line-beginning-position))
(save-excursion
(goto-char point2)
(line-beginning-position))))

(provide 'kotlin-mode-lexer)

;;; kotlin-mode-lexer.el ends here
20 changes: 10 additions & 10 deletions kotlin-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

(require 'kotlin-mode-lexer)
(require 'kotlin-mode-indent)
(require 'kotlin-mode-fill)

(defgroup kotlin nil
"A Kotlin major mode."
Expand Down Expand Up @@ -358,18 +359,17 @@ and return non-nil. Return nil otherwise."
;; Multi-line comment
(seq "/" (one-or-more "*"))
;; Middle of multi-line-comment
(seq (one-or-more "*") " "))
(seq (one-or-more "*") (zero-or-one " ")))
(zero-or-more (syntax whitespace)))))
(setq-local adaptive-fill-regexp
(rx (seq (zero-or-more (syntax whitespace))
(or
;; Single-line comment
(seq "/" (one-or-more "/"))
;; Middle of multi-line-comment
(seq (one-or-more "*") " "))
(zero-or-more (syntax whitespace)))))
(setq-local fill-indent-according-to-mode t)
(setq-local adaptive-fill-first-line-regexp ".*")
(setq-local adaptive-fill-function #'kotlin-mode--adaptive-fill)
(setq-local fill-indent-according-to-mode nil)
(setq-local comment-multi-line t)
(setq-local fill-paragraph-function #'kotlin-mode--fill-paragraph)
(setq-local fill-forward-paragraph-function
#'kotlin-mode--fill-forward-paragraph)
(setq-local normal-auto-fill-function #'kotlin-mode--do-auto-fill)
(kotlin-mode--install-fill-region-as-paragraph-advice)

(setq-local indent-line-function 'kotlin-mode--indent-line)

Expand Down
2 changes: 1 addition & 1 deletion scripts/run_linter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ rm -f *-autoloads.el || exit 1
-l elisp-lint.el \
--eval '(setq elisp-lint--debug t)' \
-f elisp-lint-files-batch \
*.el || exit 1
*.el "$@" || exit 1
rm -f *.elc test/*.elc || exit 1
rm -f *-autoloads.el || exit 1
5 changes: 3 additions & 2 deletions scripts/run_linter_in_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Cask does't support Emacs 24.
for version in 28 27 26 25 # 24
do
ARGS=$( [ "$version" -lt 26 ] && echo "--no-checkdoc" )
docker \
run \
--rm \
Expand All @@ -13,8 +14,8 @@ do
--workdir="/src" \
--env=ELDEV_DIR=/src/.eldev \
--env=HOME=/tmp \
silex/emacs:${version} \
bash -c "/src/scripts/run_linter.sh" \
silex/emacs:${version}-ci \
bash -c "/src/scripts/run_linter.sh $ARGS" \
|| exit 1
done

Expand Down
2 changes: 1 addition & 1 deletion scripts/run_test_in_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ do
--workdir="/src" \
--env=ELDEV_DIR=/src/.eldev \
--env=HOME=/tmp \
silex/emacs:${version} \
silex/emacs:${version}-ci \
bash -c "/src/scripts/run_test.sh" \
|| exit 1
done
Expand Down
Loading

0 comments on commit 0a89bdf

Please sign in to comment.