[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/julia-mode 011435c 237/352: julia-mode: Fix indent for han
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/julia-mode 011435c 237/352: julia-mode: Fix indent for hanging ops and module |
Date: |
Sun, 29 Aug 2021 11:22:52 -0400 (EDT) |
branch: elpa/julia-mode
commit 011435c3079dc23b3f7a53ebaf7c89e89a67ac6b
Author: justbur <justin@burkett.cc>
Commit: Yichao Yu <yyc1992@gmail.com>
julia-mode: Fix indent for hanging ops and module
Introduce julia-indent-hanging to calculate indentation following any
hanging operators as defined in julia-hanging-operator-regexp on the
previous line. If a line follows a hanging operator increase the
indentation by one increment, unless it is preceded by two hanging
operators. julia-hanging-operator-regexp is taken from julia-parser.scm.
Added two tests
Fixes #15118
Fix indentation after module kw
Register "module" as a keyword that starts a block, and add to new var
julia-block-start-keywords-no-indent for block keywords that should not
increase the indentation level.
Fixes #11559
---
julia-mode-tests.el | 44 +++++++++++++++++
julia-mode.el | 138 +++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 169 insertions(+), 13 deletions(-)
diff --git a/julia-mode-tests.el b/julia-mode-tests.el
index 1ad866f..de68089 100644
--- a/julia-mode-tests.el
+++ b/julia-mode-tests.el
@@ -109,6 +109,35 @@ if foo
end
end"))
+(ert-deftest julia--test-indent-module-keyword ()
+ "Module should not increase indentation at any level."
+ (julia--should-indent
+ "
+module
+begin
+ a = 1
+end
+end"
+ "
+module
+begin
+ a = 1
+end
+end")
+ (julia--should-indent
+ "
+begin
+module
+foo
+end
+end"
+ "
+begin
+ module
+ foo
+ end
+end"))
+
(ert-deftest julia--test-indent-function ()
"We should indent function bodies."
(julia--should-indent
@@ -164,6 +193,21 @@ bar"
foo() =
bar"))
+(ert-deftest julia--test-indent-operator ()
+ "We should increase indent after the first trailing operator
+but not again after that."
+ (julia--should-indent
+ "
+foo() |>
+bar |>
+baz
+qux"
+ "
+foo() |>
+ bar |>
+ baz
+qux"))
+
(ert-deftest julia--test-indent-ignores-blank-lines ()
"Blank lines should not affect indentation of non-blank lines."
(julia--should-indent
diff --git a/julia-mode.el b/julia-mode.el
index 58aafa3..fb17d4a 100644
--- a/julia-mode.el
+++ b/julia-mode.el
@@ -131,6 +131,71 @@ This function provides equivalent functionality, but makes
no efforts to optimis
"\\\\"))
(group "'"))))
+(defconst julia-hanging-operator-regexp
+ ;; taken from julia-parser.scm
+ (concat "^[^#\n]+ "
+ (regexp-opt
+ '( ;; conditional
+ "?"
+ ;; assignment
+ "=" ":=" "+=" "-=" "*=" "/=" "//=" ".//=" ".*=" "./=" "\\=" ".\\="
+ "^=" ".^=" "÷=" ".÷=" "%=" ".%=" "|=" "&=" "$=" "=>" "<<=" ">>="
+ ">>>=" "~" ".+=" ".-="
+ ;; arrow
+ "--" "-->" "←" "→" "↔" "↚" "↛" "↠" "↣" "↦" "↮" "⇎" "⇏" "⇒" "⇔" "⇴"
+ "⇶" "⇷" "⇸" "⇹" "⇺" "⇻" "⇼" "⇽" "⇾" "⇿" "⟵" "⟶" "⟷" "⟷" "⟹"
+ "⟺" "⟻" "⟼" "⟽" "⟾" "⟿" "⤀" "⤁" "⤂" "⤃" "⤄" "⤅" "⤆" "⤇" "⤌"
+ "⤍" "⤎" "⤏" "⤐" "⤑" "⤔" "⤕" "⤖" "⤗" "⤘" "⤝" "⤞" "⤟" "⤠" "⥄" "⥅"
+ "⥆" "⥇" "⥈" "⥊" "⥋" "⥎" "⥐" "⥒" "⥓" "⥖" "⥗" "⥚" "⥛" "⥞" "⥟" "⥢"
+ "⥤" "⥦" "⥧" "⥨" "⥩" "⥪" "⥫" "⥬" "⥭" "⥰" "⧴" "⬱" "⬰" "⬲" "⬳" "⬴"
+ "⬵" "⬶" "⬷" "⬸" "⬹" "⬺" "⬻" "⬼" "⬽" "⬾" "⬿" "⭀" "⭁" "⭂" "⭃" "⭄"
+ "⭇" "⭈" "⭉" "⭊" "⭋" "⭌" "←" "→"
+ ;; or and and
+ "&&" "||"
+ ;; comparison
+ ">" "<" ">=" "≥" "<=" "≤" "==" "===" "≡" "!=" "≠" "!==" "≢" ".>"
+ ".<" ".>=" ".≥" ".<=" ".≤" ".==" ".!=" ".≠" ".=" ".!" "<:" ">:"
"∈"
+ "∉" "∋" "∌" "⊆" "⊈" "⊂" "⊄" "⊊" "∝" "∊" "∍" "∥" "∦" "∷" "∺" "∻"
"∽"
+ "∾" "≁" "≃" "≄" "≅" "≆" "≇" "≈" "≉" "≊" "≋" "≌" "≍" "≎" "≐" "≑"
"≒"
+ "≓" "≔" "≕" "≖" "≗" "≘" "≙" "≚" "≛" "≜" "≝" "≞" "≟" "≣" "≦" "≧"
"≨"
+ "≩" "≪" "≫" "≬" "≭" "≮" "≯" "≰" "≱" "≲" "≳" "≴" "≵" "≶" "≷" "≸"
"≹"
+ "≺" "≻" "≼" "≽" "≾" "≿" "⊀" "⊁" "⊃" "⊅" "⊇" "⊉" "⊋" "⊏" "⊐" "⊑"
"⊒"
+ "⊜" "⊩" "⊬" "⊮" "⊰" "⊱" "⊲" "⊳" "⊴" "⊵" "⊶" "⊷" "⋍" "⋐" "⋑" "⋕"
"⋖"
+ "⋗" "⋘" "⋙" "⋚" "⋛" "⋜" "⋝" "⋞" "⋟" "⋠" "⋡" "⋢" "⋣" "⋤" "⋥" "⋦"
"⋧"
+ "⋨" "⋩" "⋪" "⋫" "⋬" "⋭" "⋲" "⋳" "⋴" "⋵" "⋶" "⋷" "⋸" "⋹" "⋺" "⋻"
"⋼"
+ "⋽" "⋾" "⋿" "⟈" "⟉" "⟒" "⦷" "⧀" "⧁" "⧡" "⧣" "⧤" "⧥" "⩦" "⩧" "⩪"
"⩫"
+ "⩬" "⩭" "⩮" "⩯" "⩰" "⩱" "⩲" "⩳" "⩴" "⩵" "⩶" "⩷" "⩸" "⩹" "⩺" "⩻"
"⩼"
+ "⩽" "⩾" "⩿" "⪀" "⪁" "⪂" "⪃" "⪄" "⪅" "⪆" "⪇" "⪈" "⪉" "⪊" "⪋" "⪌"
"⪍"
+ "⪎" "⪏" "⪐" "⪑" "⪒" "⪓" "⪔" "⪕" "⪖" "⪗" "⪘" "⪙" "⪚" "⪛" "⪜" "⪝"
"⪞"
+ "⪟" "⪠" "⪡" "⪢" "⪣" "⪤" "⪥" "⪦" "⪧" "⪨" "⪩" "⪪" "⪫" "⪬" "⪭" "⪮"
"⪯"
+ "⪰" "⪱" "⪲" "⪳" "⪴" "⪵" "⪶" "⪷" "⪸" "⪹" "⪺" "⪻" "⪼" "⪽" "⪾" "⪿"
"⫀"
+ "⫁" "⫂" "⫃" "⫄" "⫅" "⫆" "⫇" "⫈" "⫉" "⫊" "⫋" "⫌" "⫍" "⫎" "⫏" "⫐"
"⫑"
+ "⫒" "⫓" "⫔" "⫕" "⫖" "⫗" "⫘" "⫙" "⫷" "⫸" "⫹" "⫺" "⊢" "⊣"
+ ;; pipe, colon
+ "|>" "<|" ":" ".."
+ ;; plus
+ "+" "-" "⊕" "⊖" "⊞" "⊟" ".+" ".-" "++" "|" "∪" "∨" "$" "⊔" "±" "∓"
+ "∔" "∸" "≂" "≏" "⊎" "⊻" "⊽" "⋎" "⋓" "⧺" "⧻" "⨈" "⨢" "⨣" "⨤" "⨥"
"⨦"
+ "⨧" "⨨" "⨩" "⨪" "⨫" "⨬" "⨭" "⨮" "⨹" "⨺" "⩁" "⩂" "⩅" "⩊" "⩌" "⩏"
"⩐"
+ "⩒" "⩔" "⩖" "⩗" "⩛" "⩝" "⩡" "⩢" "⩣"
+ ;; bitshift
+ "<<" ">>" ">>>" ".<<" ".>>" ".>>>"
+ ;; times
+ "*" "/" "./" "÷" ".÷" "%" "⋅" "∘" "×" ".%" ".*" "\\"
+ ".\\" "&" "∩" "∧" "⊗" "⊘" "⊙" "⊚" "⊛" "⊠" "⊡" "⊓" "∗" "∙" "∤" "⅋"
+ "≀" "⊼" "⋄" "⋆" "⋇" "⋉" "⋊" "⋋" "⋌" "⋏" "⋒" "⟑" "⦸" "⦼" "⦾" "⦿"
"⧶"
+ "⧷" "⨇" "⨰" "⨱" "⨲" "⨳" "⨴" "⨵" "⨶" "⨷" "⨸" "⨻" "⨼" "⨽" "⩀" "⩃"
"⩄"
+ "⩋" "⩍" "⩎" "⩑" "⩓" "⩕" "⩘" "⩚" "⩜" "⩞" "⩟" "⩠" "⫛" "⊍" "▷" "⨝"
"⟕"
+ "⟖" "⟗"
+ ;; rational
+ "//" ".//"
+ ;; power
+ "^" ".^" "↑" "↓" "⇵" "⟰" "⟱" "⤈" "⤉" "⤊" "⤋" "⤒" "⤓" "⥉" "⥌" "⥍"
+ "⥏" "⥑" "⥔" "⥕" "⥘" "⥙" "⥜" "⥝" "⥠" "⥡" "⥣" "⥥" "⥮" "⥯" "↑" "↓"
+ ;; decl, dot
+ "::" "."))
+ (regexp-opt '(" #" " \n" "#" "\n"))))
+
(defconst julia-triple-quoted-string-regex
;; We deliberately put a group on the first and last delimiter, so
;; we can mark these as string delimiters for font-lock.
@@ -258,7 +323,11 @@ This function provides equivalent functionality, but makes
no efforts to optimis
(defconst julia-block-start-keywords
(list "if" "while" "for" "begin" "try" "function" "type" "let" "macro"
- "quote" "do" "immutable"))
+ "quote" "do" "immutable" "module"))
+
+;; For keywords that begin a block without additional indentation
+(defconst julia-block-start-keywords-no-indent
+ (list "module"))
(defconst julia-block-end-keywords
(list "end" "else" "elseif" "catch" "finally"))
@@ -426,6 +495,56 @@ with it. Returns nil if we're not within nested parens."
(current-column))
nil))))
+(defun julia-prev-line-skip-blank-or-comment ()
+ "Move point to beginning of previous line skipping blank lines
+and lines including only comments. Returns number of lines moved.
+A return of -1 signals that we moved to the first line of
+the (possibly narrowed) buffer, so there is nowhere else to go."
+ (catch 'result
+ (let ((moved 0) this-move)
+ (while t
+ (setq this-move (forward-line -1))
+ (cond
+ ;; moved into comment or blank
+ ((and (= 0 this-move)
+ (or (looking-at-p "^\\s-*\\(?:#.*\\)*$")
+ (julia-in-comment)))
+ (incf moved))
+ ;; success
+ ((= 0 this-move)
+ (throw 'result (1+ moved)))
+ ;; on first line and in comment
+ ((and (bobp)
+ (or (looking-at-p "^\\s-*\\(?:#.*\\)*$")
+ (julia-in-comment)))
+ (throw 'result -1))
+ ((bobp)
+ (throw 'result moved))
+ (t
+ (throw 'result 0)))))))
+
+(defun julia-indent-hanging ()
+ "Calculate indentation for lines that follow \"hanging\"
+operators (operators that end the previous line) as defined in
+`julia-hanging-operator-regexp'. An assignment operator ending
+the previous line increases the indent as do the other operators
+unless another operator is found two lines up. Previous line
+means previous line after skipping blank lines and lines with
+only comments."
+ (let (prev-indent)
+ (save-excursion
+ (when (> (julia-prev-line-skip-blank-or-comment) 0)
+ (setq prev-indent (current-indentation))
+ (when (looking-at-p julia-hanging-operator-regexp)
+ (if (and (> (julia-prev-line-skip-blank-or-comment) 0)
+ (looking-at-p julia-hanging-operator-regexp))
+ ;; two preceding hanging operators => indent same as line
+ ;; above
+ prev-indent
+ ;; one preceding hanging operator => increase indent from line
+ ;; above
+ (+ julia-indent-offset prev-indent)))))))
+
(defun julia-indent-line ()
"Indent current line of julia code."
(interactive)
@@ -437,17 +556,8 @@ with it. Returns nil if we're not within nested parens."
(save-excursion
(beginning-of-line)
(ignore-errors (julia-paren-indent)))
- ;; If the previous line ends in =, increase the indent.
- (ignore-errors ; if previous line is (point-min)
- (save-excursion
- (if (and (not (equal (point-min) (line-beginning-position)))
- (progn
- (forward-line -1)
- (end-of-line) (backward-char 1)
- (and (equal (char-after (point)) ?=)
- (not (julia-in-comment)))))
- (+ julia-indent-offset (current-indentation))
- nil)))
+ ;; indent due to hanging operators or a line ending in =
+ (julia-indent-hanging)
;; Indent according to how many nested blocks we are in.
(save-excursion
(beginning-of-line)
@@ -455,7 +565,9 @@ with it. Returns nil if we're not within nested parens."
(let ((endtok (julia-at-keyword julia-block-end-keywords))
(last-open-block (julia-last-open-block (- (point)
julia-max-block-lookback))))
(max 0 (+ (or last-open-block 0)
- (if endtok (- julia-indent-offset) 0)))))))
+ (if (or endtok
+ (julia-at-keyword
julia-block-start-keywords-no-indent))
+ (- julia-indent-offset) 0)))))))
;; Point is now at the beginning of indentation, restore it
;; to its original position (relative to indentation).
(when (>= point-offset 0)
- [nongnu] elpa/julia-mode ac55340 175/352: Factoring out a types regex., (continued)
- [nongnu] elpa/julia-mode ac55340 175/352: Factoring out a types regex., ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode d96a08c 180/352: Refactoring julia-char-regex as an rx form., ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 76d5d75 183/352: Optimising julia-at-keyword., ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 9cdad57 187/352: Make julia-char-regex stricter., ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode ff5fc36 202/352: julia-mode.el: allow space or no space after :: and <:, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 05f002c 203/352: When indenting code, limit how far back we search., ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 70bbe82 204/352: Adding unit tests for indentation., ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode ff8adf5 221/352: Add tests for #11684. [ci skip], ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 8ab2a60 222/352: julia-paren-indent skips blanks after paren, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode c8dfbe2 227/352: Declare indentation as customizable, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 011435c 237/352: julia-mode: Fix indent for hanging ops and module,
ELPA Syncer <=
- [nongnu] elpa/julia-mode c2f0ed8 235/352: julia-mode: Fix prompt regexp, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode a95b06b 252/352: Merge pull request #20 from tpapp/indent-export, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 847908e 249/352: Add indent for import and export., ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 7b1d3fb 253/352: Merge pull request #21 from kshramt/python-paren-indent, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode f591c6d 275/352: Merge pull request #41 from rfourquet/rf/where-return, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 8449c95 295/352: Merge pull request #69 from emmt/master, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 47d2fb0 282/352: Merge pull request #51 from non-Jedi/arg-list, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode e27e6d2 290/352: fix compilation error, silence byte-compiler warnings in emacs>23, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 7102542 304/352: fix tests, ELPA Syncer, 2021/08/29
- [nongnu] elpa/julia-mode 646f758 156/352: Don't skip over end keywords., ELPA Syncer, 2021/08/29