Skip to content

Commit

Permalink
julia-mode: Improve paren indent perf
Browse files Browse the repository at this point in the history
Replace julia-paren-indent with a version that uses the higher level
function backward-up-list to find any previous opening parentheses. This
function automatically skips balanced expressions like strings, as well
as comments.

On some test files the time to indent falls by more than 75% with this
change, even after increasing the threshold for paren lookback to 10k.
  • Loading branch information
justbur committed Feb 21, 2016
1 parent 1800fc3 commit 63eed4e
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions contrib/julia-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ beginning of the buffer."
(unless (eq (point) (point-min))
(backward-char)))

(defvar julia-max-paren-lookback 400
(defvar julia-max-paren-lookback 10000
"When indenting, don't look back more than this
many characters to see if there are unclosed parens.
Expand All @@ -477,28 +477,14 @@ containing paren before point, so we can align succeeding code
with it. Returns nil if we're not within nested parens."
(save-excursion
;; Back up to previous line (beginning-of-line was already called)
(backward-char)
(let ((min-pos (max (- (point) julia-max-paren-lookback)
(point-min)))
(open-count 0))
(while (and (> (point) min-pos)
(not (plusp open-count)))

(when (looking-at (rx (any "[" "]" "(" ")")))
(unless (or (julia-in-string) (julia-in-comment))
(cond ((looking-at (rx (any "[" "(")))
(incf open-count))
((looking-at (rx (any "]" ")")))
(decf open-count)))))

(julia--safe-backward-char))

(if (plusp open-count)
(progn (forward-char 2)
(while (looking-at (rx blank))
(forward-char))
(current-column))
nil))))
(let ((parser-state (syntax-ppss)))
(cond ((nth 3 parser-state) nil) ;; strings
((= (nth 0 parser-state) 0) nil) ;; top level
(t
(backward-up-list 1 t t)
(forward-char)
(skip-syntax-forward " ")
(current-column))))))

(defun julia-prev-line-skip-blank-or-comment ()
"Move point to beginning of previous line skipping blank lines
Expand Down Expand Up @@ -558,13 +544,10 @@ only comments."
"Indent current line of julia code."
(interactive)
(let* ((point-offset (- (current-column) (current-indentation))))
(end-of-line)
(indent-line-to
(or
;; If we're inside an open paren, indent to line up arguments.
(save-excursion
(beginning-of-line)
(ignore-errors (julia-paren-indent)))
(julia-paren-indent)
;; indent due to hanging operators or a line ending in =
(julia-indent-hanging)
;; Indent according to how many nested blocks we are in.
Expand Down

0 comments on commit 63eed4e

Please sign in to comment.