Skip to content

Commit

Permalink
julia-mode: Indentation performance improvements
Browse files Browse the repository at this point in the history
These focus on the block indentation algorithm, and basically remove
unnecessary checks.

  1. If we jump out of any comments at the beginning of the block
     indentation routine we don't need to check later if we are in a
     comment because backward-sexp jumps over comment blocks.
  2. We also don't need to check if an end keyword is inside of
     brackets, because any time we are in brackets indentation is
     handled by julia-paren-indent.

For an example of performance improvements, the time to indent lapack.jl
falls from ~9s to ~3s on my machine.

Added an indentation test for the end keyword appearing in brackets.
  • Loading branch information
justbur committed Feb 23, 2016
1 parent 4ce06c2 commit c8da4fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 14 additions & 0 deletions contrib/julia-mode-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,20 @@ bar
end
\"\"\""))

(ert-deftest julia--test-indent-of-end-in-brackets ()
"Ignore end keyword in brackets for the purposes of indenting blocks."
(julia--should-indent
"begin
begin
arr[1: end - 1]
end
end"
"begin
begin
arr[1: end - 1]
end
end"))

(defun julia--run-tests ()
(interactive)
(if (featurep 'ert)
Expand Down
18 changes: 11 additions & 7 deletions contrib/julia-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,13 @@ As a result, it is true inside \"foo\", `foo` and 'f'."
(defun julia-at-keyword (kw-list)
"Return the word at point if it matches any keyword in KW-LIST.
KW-LIST is a list of strings. The word at point is not considered
a keyword if used as a field name, X.word, or quoted, :word."
a keyword if used as a field name, X.word, or quoted, :word.
Assumes that point is not inside a comment."
(and (or (= (point) 1)
(and (not (equal (char-before (point)) ?.))
(not (equal (char-before (point)) ?:))))
(member (current-word t) kw-list)
(not (julia-in-comment))
;; 'end' is not a keyword when used for indexing, e.g. foo[end-2]
(or (not (equal (current-word t) "end"))
(not (julia-in-brackets)))))
Expand All @@ -426,9 +427,7 @@ Do not move back beyond position MIN."
(setq count
(cond ((julia-at-keyword julia-block-start-keywords)
(+ count 1))
;; fixme: breaks on strings
((and (equal (current-word t) "end")
(not (julia-in-comment)) (not (julia-in-brackets)))
((equal (current-word t) "end")
(- count 1))
(t count))))
(if (> count 0)
Expand Down Expand Up @@ -547,13 +546,18 @@ meaning always increase indent on TAB and decrease on S-TAB."
;; note: if this first function returns nil the beginning of the line
;; cannot be in a string
(julia-indent-in-string)
;; If we're inside an open paren, indent to line up arguments.
;; If we're inside an open paren, indent to line up arguments. After this,
;; we cannot be inside parens which includes brackets
(julia-paren-indent)
;; indent due to hanging operators or a line ending in =
;; indent due to hanging operators (lines ending in an operator)
(julia-indent-hanging)
;; Indent according to how many nested blocks we are in.
(save-excursion
(beginning-of-line)
;; jump out of any comments
(let ((state (syntax-ppss)))
(when (nth 4 state)
(goto-char (nth 8 state))))
(forward-to-indentation 0)
(let ((endtok (julia-at-keyword julia-block-end-keywords))
(last-open-block (julia-last-open-block (- (point) julia-max-block-lookback))))
Expand Down

0 comments on commit c8da4fb

Please sign in to comment.