Skip to content

Commit

Permalink
Support closed range operator
Browse files Browse the repository at this point in the history
  • Loading branch information
taku0 committed Jan 15, 2023
1 parent 3e548c2 commit 30510b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
68 changes: 34 additions & 34 deletions kotlin-mode-lexer.el
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ expression as a token with one of the following types:
(kotlin-mode--token-text previous-token)
'("(" "{" "[" "*" "%" "/" "+" "-" "&&" "||" ":" "&"
"=" "+=" "-=" "*=" "/=" "%="
"->" "." ".." "::" "?:" "?." "<=" ">=" "!=" "!==" "==" "==="
"->" "." ".." "..<" "::" "?:" "?." "<=" ">=" "!=" "!==" "==" "==="
"as" "as?" "is" "!is" "in" "!in" "," ";" "{" "[" "("
;; "class" will be handled later.
"package" "import" "interface" "fun" "object"
Expand Down Expand Up @@ -791,7 +791,7 @@ expression as a token with one of the following types:
(member
(kotlin-mode--token-text next-token)
'("*" "%" "/" "&" "&&" "||" ":" "=" "+=" "-=" "*=" "/=" "%="
"->" "." ".." "::" "?:" "?." "?" "<" ">" "<=" ">="
"->" "." ".." "..<" "::" "?:" "?." "?" "<" ">" "<=" ">="
"!=" "!==" "==" "==="
"," ";" ")" "]" "}"
"as" "as?" "get" "set" "by" "where" "else" "catch" "finally"
Expand Down Expand Up @@ -1487,7 +1487,7 @@ It is a type parameter list if:
"is" "!is" "!in" "as" "as?"
"!"
"=" "+=" "-=" "*=" "/=" "%="
".." "::" "?:" "?." "<=" ">=" "==" "==="
".." "..<" "::" "?:" "?." "<=" ">=" "==" "==="
"package" "import" "class" "interface" "fun" "object" "val" "var"
"typealias" "constructor" "by" "companion" "init" "if" "else" "when"
"try" "catch" "finally" "for" "do" "while" "throw"
Expand Down Expand Up @@ -1825,9 +1825,9 @@ This function does not return `implicit-;'."
"!==" "!="
(seq "!is" word-end) (seq "!in" word-end) "as?"
"!"
"=" "+=" "-=" "*=" "/=" "%="
"->"
".." "." "?:" "?." "?" "<" ">" "<=" ">=" "==" "==="
"..<" ".." "." "?:" "?." "?" "<" ">" "<=" ">=" "===" "=="
"=" "+=" "-=" "*=" "/=" "%="
"*" "%" "/" "+" "-" "&")))
(let ((text (match-string-no-properties 0))
(start (match-beginning 0))
Expand Down Expand Up @@ -2151,6 +2151,35 @@ This function does not return `implicit-;'"
:start (point)
:end (1+ (point))))

;; Operator (3 letters)
((member (buffer-substring-no-properties
(max (point-min) (- (point) 3))
(point))
'("===" "!==" "!is" "!in" "..<"))
(backward-char 3)
(make-instance 'kotlin-mode--token
:type 'operator
:text (buffer-substring-no-properties (point) (+ 3 (point)))
:start (point)
:end (+ 3 (point))))

;; Operator (2 letters, other than as, in, or is)
((member (buffer-substring-no-properties
(max (point-min) (- (point) 2))
(point))
'("++" "--"
"&&" "||"
"+=" "-=" "*=" "/=" "%="
".." "?."
"<=" ">=" "!=" "=="
"->"))
(backward-char 2)
(make-instance 'kotlin-mode--token
:type 'operator
:text (buffer-substring-no-properties (point) (+ 2 (point)))
:start (point)
:end (+ 2 (point))))

;; Open angle bracket for type parameters
;;
;; We use a heuristic: spaces are inserted around inequality sign,
Expand Down Expand Up @@ -2185,35 +2214,6 @@ This function does not return `implicit-;'"
:start (point)
:end (1+ (point))))

;; Operator (3 letters)
((member (buffer-substring-no-properties
(max (point-min) (- (point) 3))
(point))
'("===" "!==" "!is" "!in"))
(backward-char 3)
(make-instance 'kotlin-mode--token
:type 'operator
:text (buffer-substring-no-properties (point) (+ 3 (point)))
:start (point)
:end (+ 3 (point))))

;; Operator (2 letters, other than as, in, or is)
((member (buffer-substring-no-properties
(max (point-min) (- (point) 2))
(point))
'("++" "--"
"&&" "||"
"+=" "-=" "*=" "/=" "%="
".." "?."
"<=" ">=" "!=" "=="
"->"))
(backward-char 2)
(make-instance 'kotlin-mode--token
:type 'operator
:text (buffer-substring-no-properties (point) (+ 2 (point)))
:start (point)
:end (+ 2 (point))))

;; ? or as?
((eq (char-before) ??)
(let ((pos-before-comment (point)))
Expand Down
22 changes: 14 additions & 8 deletions test/pathological.kt
Original file line number Diff line number Diff line change
Expand Up @@ -667,25 +667,31 @@ fun foo() {
val x =
a shl // Line breaks are allowed after infix function.
b // KNOWN_BUG
val y = 1 // KNOWN_BUG

var shl = 1 // KNOWN_BUG
val x = shl shl shl
shl < 100 && foo() // this is not a continuation of the previous line.
var shl = 1
val x = shl shl shl
shl < 100 && foo() // this is not a continuation of the previous line.

var shl = 1
val x = shl shl
shl < 100 && foo() // this is a continuation of the previous line. // KNOWN_BUG
var shl = 1
val x = shl shl
shl < 100 && foo() // this is a continuation of the previous line. // KNOWN_BUG
val y = 1 // KNOWN_BUG

// This is not a infix function call; line breaks are // KNOWN_BUG
// This is not a infix function call; line breaks are
// prohibited before infix function.
val x = // KNOWN_BUG
val x =
a
f (b) // So this line should not indented.

val x =
a .. // Line breaks are prohibited before range operator.
b

val x =
a ..< // Line breaks are prohibited before closed range operator.
b

val x =
a + // Line breaks are prohibited before additive operators.
b -
Expand Down

0 comments on commit 30510b2

Please sign in to comment.