[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/scala-mode e30b35a 008/217: Indentation working
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/scala-mode e30b35a 008/217: Indentation working |
Date: |
Sun, 29 Aug 2021 11:30:34 -0400 (EDT) |
branch: elpa/scala-mode
commit e30b35a5dc67bcd35b9bebe5d5bce97ebea88de4
Author: Heikki Vesalainen <heikkivesalainen@yahoo.com>
Commit: Heikki Vesalainen <heikkivesalainen@yahoo.com>
Indentation working
---
Example.scala | 330 ++++++++++++++++++--------
Test.scala | 157 ++++++++++---
scala-mode-fontlock.el | 362 ++++++++++++++++++++++++++++-
scala-mode-indent.el | 614 ++++++++++++++++++++++++++++++++++++++++---------
scala-mode-lib.el | 1 +
scala-mode-map.el | 17 +-
scala-mode-syntax.el | 476 ++++++++++++++++++++++++++++++--------
scala-mode.el | 39 +++-
8 files changed, 1647 insertions(+), 349 deletions(-)
diff --git a/Example.scala b/Example.scala
index c1aa32a..9f893ef 100644
--- a/Example.scala
+++ b/Example.scala
@@ -12,49 +12,83 @@ Indenting happens relative to an indent anchor. Usually the
indent anchor is the
the declaration or expression is inside a parameter list, then the anchor is
inside the list.
*/
-def f(s: String,
- i: Int) =
+/* private */ def f(s: String,
+ i: Int) =
s.take(i) // indented relative to 'def'
-/* */ val x = foo(
- zot, // indented relative to '/* */'
- someThing
- map (x => x.length) // indented relative to 'someThing'
-)
+/* private */ val y = 1; val x = foo(
+ zot, // indented relative to '/* */'
+ someThing
+ map ((s: String, i: Int) =>
+ x.length) // indented relative to 'someThing'
+ )
-val x =
+var foo =
foo( // indented relative to 'val'
zot // indented relative to 'foo'
+ Zot
+ Nil
)
class OmgLol[Option[x] forSome {
- val x: String // indented relative to 'Option'
+ val a, z: String // indented relative to 'Option'
}] {
+
+ val x = y match {
+ /* where do comments go */
+ case x => _
+ }
// body
}
-/*
-1.2. Run-on lines
-
-Any line not beginning with a declaration or expression start reserved word
(i.e. class, trait, object, package, type, val, var, def, for, yield, if, else
if, else, do, while) will be interpreted as a run-on of the previous line, and
will be indented two step from the indent anchor.
-This rule does not apply in the following cases:
-- if the previous lines was empty
-- previous line was an annotation
-- previous statement ended with ';,'
-- the current line starts a body (of declaration or anonymous function)
-- block or simple expression starts with an anonymous function (lambda)
declaration
-*/
+/**
+ * Run-on lines
+ *
+ * ==Foo==
+ *
+ * Any line not beginning with a declaration or expression start reserved
word (i.e. class, trait, object, package, type, val, var, def, for, yield, if,
else if, else, do, while) will be interpreted as a run-on of the previous line,
and will be indented two step from the indent anchor.
+ *
+ * Ordered lists:
+ * 1. foo bar zot zot zot foo baa ölajdsfölkajdsf aödslf aöldskfj asödlf
jasdf asdfjasd kfjas djfa skjdf asdf asdlfkjasd lfajs dfasd flaksdjf sad f
+ * 1. fo bar zot zot zot foo baa ölajdsfölkajdsf aödslf aöldskfj asödlf jasdf
asdfjasd kfjas djfa skjdf asdf asdlfkjasd lfajs dfasd flaksdjf sad f
+ *
+ * This rule does not apply in the following cases:
+ * - if the previous lines was empty
+ * - previous line was an annotation
+ * - previous statement ended with ';,'
+ * - the current line starts a body (of declaration or anonymous function)
+ * - block or simple expression starts with an anonymous function
+ * (lambda) declaration
+ *
+ * @author Heikki Vesalainen
+ * @version 1.2
+ */
+
+val f, g = List("foo", "bar").
+ filter (_ > 0) map ( s => // run-on, anchor is 'val', also start of lambda
+ s.length
+ zot // run-on line, because in newlines-disabled region
+ )
+ sum // still a run-on
-val f = List("foo", "bar")
- map ( s => // run-on, anchor is 'val', also start of lambda
- s.length // not a run-on line, is body
+val f = {
+ List("foo", "bar").
+ filter (_ > 0) map ( s => // run-on, anchor is 'val', also start of lambda
+ s.length
+ zot // run-on line, because in newlines-disabled region
)
- sum // still a run-on
+ sum
+}
-val f =
- List("foo", "bar") // not a run-on line, is body
- map (s => s.length) // run-on line, anchor is 'List'
+val f = List("foo", "bar"). // not a run-on line, is body
+ filter(_ > 0).
+ map ( s =>
+ s.length
+ ) // run-on line, anchor is 'List'
+ filter ( s =>
+ s.length
+)
zot() // previous line was empty
@@ -62,39 +96,55 @@ val f = List("foo", "bar");
zot() // previous statement ended with ';'
val f: Bar = new Zot
- with Bar // run-on line, achor is 'val'
+ with Bar { // run-on line, achor is 'val'
+ def a
+ :String = {
+ asd
+ }
+ }
-class Foo(
+/* private */ class Foo(
foo: String,
bar: Bar
- with Zot // run-on line, anchor is 'bar'
-) extends Bar
- with Zot { // run-on line, anchor is 'class'
+ with
+ Zot // run-on line, anchor is 'bar'
+) extends
+ Bar
+ with
+ Zot { // run-on line, anchor is 'class'
+
// body here
}
+
+
trait Leijona( x: Int with Int,
y: Int )
- extends Kissa // run-one line, acnhor is 'trait'
- with Harja // ditto
+ extends cam.test.Kissa // run-one line, acnhor is 'trait'
+ with com.foo.super[Zot].Koira.this.type#Bar // ditto
-def someThingReallyLong(having: String, aLot: Int, ofParameters: Boolean):
+def someThingReallyLong(having: String, aLot: Int, ofParameters: Boolean)://
foo zot
SomeAbsurdlyLongTypeThatBarelyFitsOnALine[Z, F] // run-on line, anchor is
'def'
with SomeOtherType = { // ditto
// body here
}
List("foo", "bar")
- map { s =>
- s.length // 'map' indented as run-on, 's =>' start lambda
- toString
- }
+map { s/* */
+ : /* */ Any =>
+ s.length // 'map' indented as run-on, 's =>' start lambda
+ toString
+}
+filter (foo =>
+ bar
+)
-List("foo") map (
+List("foo") map (
s => // start lambda
s.length // run-on rule does not apply
)
+
/*
1.3 Parameter lists
@@ -117,22 +167,32 @@ class Foo( /* */
) // at indent anchor column
def f( /* */ i: String, j: String with Bar,
- k: String) // indented acording to previous
+ k: String or
+ Int) // indented acording to previous
-val f = foo(kissa,
- kala)
+val f =
+{
+ foo(kissa,
+ kala
+ filter (odd))
+}
+
+{
+ val g =
+ bar.
+ zot.
+ babarbar(
+ kissa,
+ kala
+ )
+}
-val g = bar(
+val h = "foo"
+zot(
kissa,
kala
)
-val h =
- zot(
- kissa,
- kala
- )
-
type ->[FirstElementOfAPair,
SecondElementOfAPair] =
(FirstElementOfAPair,
@@ -149,16 +209,17 @@ Any block whose first word is 'case', but not 'case
class' is a case block. The
indented with two steps, except for lines beginning with the word 'case'
*/
-val x =
- f match {
- case xs: Seq[String] =>
+val x = f match {
+ case xs: Seq[String]
+ if xs.size > 7 =>
xs map ( x => x.length)
- sum
- case x: String =>
+ sum
+
+ case a: Int if a < 0 =>
println(x);
- x.length // not the ';' on previous line. Without it, this line would be
interpreted as run-on
+ x.length // note the ';' on previous line. Without it, this line would
be interpreted as run-on
}
-
+
/*
1.6 For comprehension
@@ -169,19 +230,35 @@ parentheses indenting rules.
- yield is aligned with for
*/
-val y = for { i <- 1 to 10,
- j <- 2 to 20 }
- yield i +j
+val y = for
+ {
+ i <- 1 to 10
+ j <- 2 to 20
+ }
+ yield {
+ i + j
+ }
val z =
for {
- i <- 1 to 10,
+ i <- 1 to 10
+ j <- 2 to 20
+ val zot = jaada
+ if asdasd
+ } yield
+ i + j
+
+
+val z =
+ for
+ {
+ i <- 1 to 10
j <- 2 to 20
} yield {
i + j
}
-val z = for { i <- 1 to 10,
+val z = for { i <- 1 to 10
j <- 2 to 20 }
yield {
i + j
@@ -203,64 +280,94 @@ Try statements will be indented acording to the following
rules:
val x = if (kissa)
foo
+ map(s => s.toInt)
+ filter(_ > 0)
else if (kala)
bar
else
zot
val y = if (kissa) {
- foo
-} else if (kala) {
- bar
-} else {
- zot
-}
+ foo
+ bar
+ zot
+ } else if (kala) {
+ bar
+ } else {
+ zot
+ }
-val a = try
- foo()
- catch {
- case e => bar()
- }
+val a = try {
+ foo()
+ }
+ catch {
+ case e => bar()
+ }
val b = try {
- foo()
-} catch {
- case e => bar()
-} finally {
- zot()
-}
+ foo()
+ } catch {
+ case e => bar()
+ } finally {
+ zot()
+ }
val c = try
zot()
+ catch {
+ case x =>
+ foo
+ }
finally
log("zotted")
+do {
+ zot
+} while (bar)
+
+while (zot)
+bar
+
+
+
/*
1.8 Block opening curly brackets on new line
-While not couraged, should the opening curly bracket of a block be on a new
line, it is indented acording to the following rules:
+While not couraged, should the opening curly bracket of a block be on
+a new line, it is indented acording to the following rules:
-- If the block is the body of a package object, object, class or trait, then
the opening is aligned acording to the anchor.
-- If the block is the body or value of a val, var, type, then the opening is
aligned one step from the anchor
-- If the block is the body of a def, then the opening is aligned acording to
the anchor for Unit functions without explicitly
+- If the block is the body of a package object, object, class or
+ trait, then the opening is aligned acording to the anchor.
+- If the block is the body or value of a val, var, type, then the
+ opening is aligned one step from the anchor
+- If the block is the body of a def, then the opening is aligned
+ acording to the anchor for Unit functions without explicitly
specified return value and one step from anchor otherwise.
*/
-class Foo {
- def foo
+{
+ class Foo
{
- zot
+
+ def foo
+ {
+ zot
foo
- }
-
- def bar =
- {
+ }
+
+ def bar =
+ {
zot
- }
-
- val zot =
- {
-"hello"
+ }
+
+ def bar =
+ (zot,
+ bar)
+
+ val zot =
+ {
+ "hello"
+ }
}
}
@@ -272,10 +379,35 @@ class Foo {
2.1 Types
*/
-val strings = Seq("""multi
-line"quote
-strings""", "normal strings", 'c')
+val strings = Seq("""
+ | multi line"
+ | quote strings
+ | zot
+ """, "normal strings", 'c')
-val `quoted id` = 1
+val `yield` = 1
val symbol = 'Symbol
+
+var List(foo, bar)
+
+var foo, bar :: bar::Nil, foo @ (foo, bar), List(foo: Int, bar: String) = foo
+
+x match {
+ case zot => zot
+ case (foo, zot) => foo + zot
+ case List(a, b) => a + b
+ case a :: Nil => a + b
+ case Zot | Bar => println("foo")
+}
+
+val x: Zot with Apropos[Zot forSome { val x: String }, Zot -> Bar <% Dif]
+
+type ->[A,B] = (A,B)
+val x: List[Bar] -> List[Zot] => List[Bar -> Zot] = _.toZot
+
+class Mail extends Communication with { def x = 2 } with OldStyle[A <% String]
+
+{
+ case a: String if zot > bar =>
+}
diff --git a/Test.scala b/Test.scala
index afbdaca..7a55162 100644
--- a/Test.scala
+++ b/Test.scala
@@ -1,3 +1,112 @@
+val x = if (foo) {
+ zot
+ } else {
+ bar
+ }
+
+val x =
+ if (foo)
+ zot
+ else
+ bar
+
+val x =
+ if (foo)
+ {
+ zot
+ }
+ else
+ {
+ bar
+ }
+
+
+
+
+
+
+val x = for {
+ a <- 1 to 10
+ b <- 1 to 20
+ if a > b
+ var x = 10
+ }
+ yield
+ ( a+b )
+
+def x
+ (a,
+ b)
+ (c,
+ d) =
+{
+ zot
+}
+
+@foo val x,
+ y = foo
+| bar
+| zot
+
+
+val y /* */ = bazoot {
+ foofoo
+ }
+
+def x = else
+ (x, y)
+
+def x = if (foo)
+ {
+ then
+ }
+ else if (zot)
+ {
+ fi
+ }
+ else
+ {
+ zot
+ }
+
+val y =
+ if (foo)
+ bar
+ else if (bar)
+ zot
+ else
+ zot
+}
+
+
+val x = try {
+ foo
+ } catch {
+ asd
+ } finally {
+ bar
+ }
+
+while (bar) {
+ zot
+}
+
+do
+ zot
+while (bar)
+
+
+var x = if (foo)
+ if (bar) {
+ bar
+ } else {
+ zot
+ }
+ else
+ bar
+
+
+
{
@@ -5,39 +114,33 @@
extends B {
asd
}
-
- someReallyAbsurdyLongLine
- thatMadeMeWantToCutIt.
- yeahMoreOfThatLongStuff map {foo =>
- asdasd
-
-
- )
+
+ someReallyAbsurdyLongLine.
+ thatMadeMeWantToCutIt.
+ yeahMoreOfThatLongStuff map {
+ foo =>
+ asdasd
+ }
/*
* asd asd
*/
+ val a,
+ b,
+ c = "foo"
def myVeryLongMethodName(x: String,
y: String)
- (x: String): Foo[ Alko
- + Zugurov
- + Bagurov] =
- {
- body
- }
-
-
- def myVeryLongMethodName
- (
- x: String,
- y: String
- )(
- z: String,
- b: String
- )
+ (x: String):
+ Foo[Alko + Zugurov + Bagurov] = {
+ asdasdf
+ }
+ def myVeryLongMethodName( x: String, y: String )
+ ( z: String, b: String ) = {
+ asd
+ }
/* fooo bar */
@@ -54,9 +157,9 @@ qasduote_asdasd a+_
val x:[Type <% Foo] => '\u123c'
val y = "foo a\"sdasd"
- " ", " ", "as\"dasd"
- `asdasd`> asdasdasd
- "asdasd
+ " ", " ", "as\"dasd"
+ `asdasd`> asdasdasd
+ "asdasd
"""asdasdasd
asdasda
diff --git a/scala-mode-fontlock.el b/scala-mode-fontlock.el
index 31f64ee..dd112f3 100644
--- a/scala-mode-fontlock.el
+++ b/scala-mode-fontlock.el
@@ -7,22 +7,362 @@
(require 'scala-mode-syntax)
(require 'scala-mode-constants)
-(defun scala-font-lock:mark-symbol (limit)
- (if (re-search-forward scala-syntax:reserved-symbols-re limit t)
- (goto-char (match-end 2)) ;; step back to the match (re matches futher)
- nil))
+(defcustom scala-font-lock:constant-list '("Nil")
+ "A list of strigs that should be fontified in constant
+face. This customization property takes effect only after the
+scala-mode has been reloaded."
+ :type '(repeat string)
+ :group 'scala)
+
+(defun scala-font-lock:create-user-constant-re ()
+ (regexp-opt scala-font-lock:constant-list 'words))
+
+(defun scala-font-lock:mark-reserved-symbols (limit)
+ (when (re-search-forward scala-syntax:reserved-symbols-re limit t)
+ (goto-char (match-end 2)))) ;; step back to the match (re matches futher)
(defun scala-font-lock:mark-underscore (limit)
- (if (re-search-forward scala-syntax:reserved-symbol-underscore-re limit t)
- (goto-char (match-end 2)) ;; step back to the match (re matches futher)
- nil))
+ (when (re-search-forward scala-syntax:reserved-symbol-underscore-re limit t)
+ (goto-char (match-end 2)))) ;; step back to the match (re matches futher)
+
+;(defun scala-font-lock:extend-region-function ()
+
+(defun scala-font-lock:limit-pattern2 (&optional start)
+ (save-excursion
+ (when start (goto-char start))
+ (scala-syntax:skip-forward-ignorable)
+ (ignore-errors
+ (while (and (not (or (eobp)
+ (looking-at scala-syntax:other-keywords-unsafe-re)
+ (scala-syntax:looking-at-reserved-symbol nil)))
+ (looking-at scala-syntax:simplePattern-beginning-re))
+ (message "- now at %d" (point))
+ (if (= (char-after) ?\()
+ (forward-list)
+ (goto-char (match-end 0))
+ (scala-syntax:skip-forward-ignorable)
+ (message "+ now at %d" (point))
+ (cond ((looking-at "(")
+ (forward-list))
+ ((looking-at "@")
+ (goto-char (match-end 0)))
+ ((or (scala-syntax:looking-at-reserved-symbol nil)
+ (looking-at scala-syntax:other-keywords-unsafe-re))
+ (messssage "saw reserved symbol or keyword"))
+ ((looking-at scala-syntax:id-re)
+ (message "saw id-re %d" (match-beginning 0))
+ (goto-char (match-end 0)))
+ (t
+ (message "nothing special here %s" (point)))
+ ))
+ (scala-syntax:skip-forward-ignorable)))
+ (point)))
+
+(defun scala-font-lock:limit-pattern2-list (&optional start)
+ (let ((limit (scala-font-lock:limit-pattern2 start)))
+ (while (= (char-after limit) ?,)
+ (setq limit (scala-font-lock:limit-pattern2 (1+ limit))))
+ limit))
+
+(defun scala-font-lock:mark-pattern1-part (&optional limit pattern-p)
+ "Parses a part of val, var and case pattern (or id). Always
+parses a variable or constant name first and then type, leaving
+the pointer at the next variablename, constnat name, list or
+Pattern3, if any, and setting up match data 1 (variable),
+2 (constant) and 3 (type) acordingly. If there is no variable
+name before the first type, then the match data for the variable
+name is nil. Returns t if something was matched or nil if nothing
+was found.
+
+If pattern-p is defined, then only varid is matched as variable
+and everything else is constant.
+
+Does not continue past limit.
+"
+; (message "will stop at %d" limit)
+ (cond
+ ;; quit if we are past limit
+ ((or (and limit (>= (point) limit))
+ (eobp))
+ (message "limit")
+ nil)
+ ;; Type pattern, just skip the whole thing. It will end at ',' or ')'.
+ ;; Note: forms starting with ':' are handled by a completely separete
+ ;; font-lock matcher.
+ ((scala-syntax:looking-at-reserved-symbol ":")
+ (message ":")
+ (while (not (or (eobp)
+ (scala-syntax:looking-at "[,);]")
+ (scala-syntax:looking-at-reserved-symbol "|")
+ (scala-syntax:looking-at-reserved-symbol
+ scala-syntax:double-arrow-unsafe-re)
+ (scala-syntax:looking-at-empty-line-p)))
+ (scala-syntax:forward-sexp)
+ (scala-syntax:skip-forward-ignorable))
+ (set-match-data nil)
+ t)
+ ;; Binding part cannot start with reserved symbols. If they
+ ;; are seen, we must quit.
+ ((scala-syntax:looking-at-reserved-symbol nil)
+ (message "symbol")
+ nil)
+ ((scala-syntax:looking-at-stableIdOrPath)
+ (message "stableId")
+ (let ((beg (match-beginning 0))
+ (end (match-end 0))
+ (varid (scala-syntax:looking-at-varid-p)))
+ (goto-char end)
+ (let ((new-match-data
+ (cond
+ ((= (char-after end) ?\()
+ ;; matched type
+ (message "it's a type")
+ `(,beg ,end nil nil nil nil ,beg ,end))
+ ((progn (scala-syntax:backward-sexp)
+ (= (char-before) ?.))
+ ;; matched constant
+ `(,beg ,end nil nil ,(point) ,end nil nil))
+ ((or varid (not pattern-p))
+ ;; matched variable name or we can't be sure
+ `(,beg ,end ,beg ,end nil nil nil nil))
+ (t
+ ;; matched constant
+ `(,beg ,end nil nil ,beg ,end nil nil)))))
+ (goto-char end)
+ (scala-syntax:skip-forward-ignorable)
+ (cond
+ ((and (not (or (scala-syntax:looking-at-reserved-symbol nil)
+ (scala-syntax:looking-at-reserved-symbol "|")))
+ (scala-syntax:looking-at-stableIdOrPath))
+ (setq new-match-data
+ (append (butlast new-match-data 2)
+ `(,(match-beginning 0)
+ ,(match-end 0))))
+ (goto-char (match-end 0))
+ (scala-syntax:skip-forward-ignorable))
+ ((= (char-after) ?@)
+ (forward-char)
+ (scala-syntax:skip-forward-ignorable)))
+ (set-match-data new-match-data)))
+ t)
+ ;; Pattern3 can be a literal. Just skip them.
+ ((looking-at scala-syntax:literal-re)
+ (message "literal")
+ (goto-char (match-end 0))
+ (scala-syntax:skip-forward-ignorable)
+ (set-match-data nil)
+ t)
+ ;; Start of a patterns list or alternatives. Skip if alternatives or
+ ;; else leave point at start of first element.
+ ((= (char-after) ?\()
+ (message "(")
+ (let ((alternatives-p
+ (save-excursion
+ (forward-char)
+ (ignore-errors
+ ;; forward-sexp will terminate the loop with error
+ ;; if '|' is not found before end of list ')'
+ (while (not (or (eobp)
+ (= (char-before) ?|)
+ (scala-syntax:looking-at-empty-line-p)))
+ (scala-syntax:forward-sexp))
+ t))))
+ (if alternatives-p
+ (forward-list)
+ (forward-char)))
+ (scala-syntax:skip-forward-ignorable)
+ (set-match-data nil)
+ t)
+ ;; continuation or end of list, just skip and position at the
+ ;; next element
+ ((or (= (char-after) ?,)
+ (= (char-after) ?\)))
+ (message ", or )")
+ (forward-char)
+ (scala-syntax:skip-forward-ignorable)
+ (set-match-data nil)
+ t)
+ ;; none of the above, just stop
+ (t
+ (message "Cannot continue Pattern1 at %d" (point))
+ nil)
+))
+
+(defun scala-font-lock:limit-pattern (&optional start)
+ (save-excursion
+ (goto-char (scala-font-lock:limit-pattern2 start))
+ (message "now at %d" (point))
+ (when (scala-syntax:looking-at-reserved-symbol ":")
+ (while (not (or (eobp)
+ (scala-syntax:looking-at-reserved-symbol "|")
+ (scala-syntax:looking-at-reserved-symbol
+ scala-syntax:double-arrow-unsafe-re)
+ (scala-syntax:looking-at-empty-line-p)))
+ (scala-syntax:forward-sexp)
+ (scala-syntax:skip-forward-ignorable)))
+ (if (or (/= (char-after) ?|)
+ (scala-syntax:looking-at-reserved-symbol
+ scala-syntax:double-arrow-unsafe-re))
+ (point)
+ (forward-char)
+ (scala-font-lock:limit-pattern))))
+
+(defun scala-font-lock:mark-pattern-part (&optional limit)
+ (when (scala-syntax:looking-at-reserved-symbol "|")
+ (message "skipping |")
+ (forward-char)
+ (scala-syntax:skip-forward-ignorable))
+ (scala-font-lock:mark-pattern1-part limit t))
+
+(defun scala-font-lock:limit-type (&optional start)
+ start)
-(defvar scala-font-lock:keywords
+
+(defun scala-font-lock:limit-simpleType (&optional start)
+ (when start (goto-char start))
+ (scala-syntax:skip-forward-ignorable)
+ (setq start (point))
+
+ (if (= (char-after) ?\()
+ (ignore-errors (forward-list))
+ (scala-font-lock:mark-simpleType))
+ (when (and (not (eobp)) (= (char-after) ?#))
+ (scala-font-lock:mark-simpleType))
+ (when (and (not (eobp)) (= (char-after) ?\[))
+ (ignore-errors (forward-list))
+ (scala-syntax:skip-forward-ignorable))
+ (let ((limit (point)))
+ (goto-char start)
+ (message "simpeType limit at %d" limit)
+ limit))
+
+(defun scala-font-lock:mark-simpleType (&optional limit)
+ (message "looking for simpleType at %d" (point))
+ (cond
+ ;; stop at limit
+ ((and limit (>= (point) limit))
+ nil)
+ ;; just dive into lists
+ ((> (skip-chars-forward "[(,)]") 0)
+ (message "skipping list-marks")
+ (scala-syntax:skip-forward-ignorable)
+ (set-match-data nil)
+ t)
+ ;; jump over blocks
+ ((= (char-after) ?\{)
+ (ignore-errors
+ (forward-list)
+ (set-match-data nil)
+ t))
+ ;; ignore arrows and reserved words and symbols
+ ((or (scala-syntax:looking-at-reserved-symbol
+ scala-syntax:double-arrow-unsafe-re)
+ (scala-syntax:looking-at-reserved-symbol
+ "<[:%]\\|>?:")
+ (looking-at "\\<forSome\\>"))
+ (message "skipping reserved")
+ (goto-char (match-end 0))
+ (scala-syntax:skip-forward-ignorable)
+ (set-match-data nil)
+ t)
+ ;; color id after '#'
+ ((= (char-after) ?#)
+ (message "at #")
+ (forward-char)
+ (if (and (not (or (looking-at scala-syntax:keywords-unsafe-re)
+ (scala-syntax:looking-at-reserved-symbol nil)))
+ (looking-at scala-syntax:id-re))
+ (goto-char (match-end 0)) nil))
+ ;; color paths (including stableid)
+ ((scala-syntax:looking-at-stableIdOrPath t)
+ (message "at path")
+ (let ((end (match-end 0)))
+ (goto-char end)
+ (while (scala-syntax:looking-back-token "this\\|type")
+ (goto-char (match-beginning 0))
+ (skip-chars-backward "."))
+ (unless (scala-syntax:looking-back-token scala-syntax:id-re)
+ (set-match-data nil))
+ (goto-char end))
+ (scala-syntax:skip-forward-ignorable)
+ t)
+ (t
+ (message "Cannot continue simpleType at %d" (point))
+ nil)))
+
+(defun s-mark ()
+ (interactive)
+ (message "limit %d" (scala-font-lock:limit-simpleType (point)))
+ (message "mark from %d: %s at %d" (point) (scala-font-lock:mark-simpleType)
+ (point)))
+
+(defun scala-font-lock:keywords ()
+ ;; chars, string, comments are handled acording to syntax and
+ ;; syntax propertize
+
`(;; keywords
- (,scala-syntax:keywords-re 0 font-lock-keyword-face)
+ (,scala-syntax:other-keywords-re 2 font-lock-keyword-face)
+ (,scala-syntax:value-keywords-re 2 font-lock-constant-face)
+ (,scala-syntax:path-keywords-re 2 font-lock-keyword-face)
+
+ ;; User defined constants
+ (,(scala-font-lock:create-user-constant-re) 0 font-lock-constant-face)
+
+ ;; reserved symbols
+ (scala-font-lock:mark-reserved-symbols 2 font-lock-keyword-face)
+
+ ;; 'Symbols
+ (,scala-syntax:symbolLiteral-re 1 font-lock-string-face)
- ;; symbols
- (scala-font-lock:mark-symbol 2 font-lock-keyword-face)
+ ;; underscore
(scala-font-lock:mark-underscore 2 font-lock-keyword-face)
+ ;; object
+ (,(concat "\\<object[ \t]+\\("
+ scala-syntax:id-re
+ "\\)")
+ 1 font-lock-constant-face)
+
+ ;; class, trait
+ (,(concat "\\<\\(class\\|trait\\)[ \t]+\\("
+ scala-syntax:id-re
+ "\\)")
+ 2 font-lock-type-face)
+
+ ;; extends, with, new
+ (,(concat "\\<\\(extends\\|with\\|new\\)[ \t]+\\([("
+ scala-syntax:id-first-char-group "]\\)")
+ (scala-font-lock:mark-simpleType (scala-font-lock:limit-simpleType
+ (goto-char (match-beginning 2)))
+ nil
+ (0 font-lock-type-face nil t)))
+
+ ;; ':'
+ (,scala-syntax:colon-re
+ (scala-font-lock:mark-simpleType (scala-font-lock:limit-simpleType
+ (goto-char (match-end 2)))
+ nil
+ (0 font-lock-type-face nil t)))
+
+ ;; def
+ (,(concat "\\<def[ \t]+\\(" scala-syntax:id-re "\\)") 1
font-lock-function-name-face)
+
+ ;; VarDcl
+ ("\\<va[rl][ \t]+\\([^:]\\)"
+ (scala-font-lock:mark-pattern1-part (scala-font-lock:limit-pattern2-list
+ (goto-char (match-beginning 1)))
+ nil
+ (1 font-lock-variable-name-face nil t)
+ (2 font-lock-constant-face nil t)
+ (3 font-lock-type-face nil t)))
+
+ ;; Case (but not case class|object)
+ ("\\<case[ \t]+\\([^:]\\)"
+ (scala-font-lock:mark-pattern-part (scala-font-lock:limit-pattern
+ (goto-char (match-beginning 1)))
+ nil
+ (1 font-lock-variable-name-face nil t)
+ (2 font-lock-constant-face nil t)
+ (3 font-lock-type-face nil t)))
+
))
diff --git a/scala-mode-indent.el b/scala-mode-indent.el
index 3b18b5b..5f2ce93 100644
--- a/scala-mode-indent.el
+++ b/scala-mode-indent.el
@@ -13,6 +13,81 @@ indentation will be one or two steps depending on context."
:type 'integer
:group 'scala)
+(defconst scala-indent:eager-strategy 0
+ "See 'scala-indent:run-on-strategy'")
+(defconst scala-indent:operator-strategy 1
+ "See 'scala-indent:run-on-strategy'")
+(defconst scala-indent:reluctant-strategy 2
+ "See 'scala-indent:run-on-strategy'")
+(defconst scala-indent:keywords-only-strategy 3
+ "A strategy used internally by indent engine")
+
+(defcustom scala-indent:default-run-on-strategy 0
+ "What strategy to use for detecting run-on lines, i.e. lines
+that continue a statement from the previous line. Possible values
+are:
+
+'reluctant', which marks only lines that begin with -- or
+that follow a line that ends with -- a reserved word that cannot start
+or end a line, such as 'with'.
+
+'operators', which extends the previous strategy by marking also
+lines that begin with -- or that follow a line that ends with --
+an operator character. For example, '+', '-', etc.
+
+'eager', which marks all rows which could be run-ons, i.e. which
+are not ruled out by the language specification.
+"
+ :type `(choice (const :tag "eager" ,scala-indent:eager-strategy)
+ (const :tag "operators" ,scala-indent:operator-strategy)
+ (const :tag "reluctant" ,scala-indent:reluctant-strategy))
+ :group 'scala)
+
+(make-variable-buffer-local 'scala-indent:effective-run-on-strategy)
+
+(defun scala-indent:run-on-strategy ()
+ "Returns the currently effecti run-on strategy"
+ (or scala-indent:effective-run-on-strategy
+ scala-indent:default-run-on-strategy
+ scala-indent:eager-strategy))
+
+(defun scala-indent:toggle-effective-run-on-strategy ()
+ "If effective run-on strategy is not set, it is set as follows:
+- if default is eager or operators, then it is set to reluctant
+- if default is reluctant, then it is set to eager. If it is set,
+it is nilled."
+ (if scala-indent:effective-run-on-strategy
+ (setq scala-indent:effective-run-on-strategy nil)
+ (let ((new-strategy
+ (cond ((= (scala-indent:run-on-strategy)
+ scala-indent:reluctant-strategy)
+ scala-indent:eager-strategy)
+ ((or (= (scala-indent:run-on-strategy)
+ scala-indent:operator-strategy)
+ (= (scala-indent:run-on-strategy)
+ scala-indent:eager-strategy))
+ scala-indent:reluctant-strategy))))
+ (setq scala-indent:effective-run-on-strategy new-strategy))))
+
+(defun scala-indent:reset-effective-run-on-strategy ()
+ (setq scala-indent:effective-run-on-strategy nil))
+
+(defun scala-indent:rotate-run-on-strategy ()
+ (interactive)
+ (let ((new-strategy
+ (cond ((= scala-indent:default-run-on-strategy
+ scala-indent:reluctant-strategy)
+ scala-indent:operator-strategy)
+ ((= scala-indent:default-run-on-strategy
+ scala-indent:operator-strategy)
+ scala-indent:eager-strategy)
+ ((= scala-indent:default-run-on-strategy
+ scala-indent:eager-strategy)
+ scala-indent:reluctant-strategy))))
+ (setq scala-indent:default-run-on-strategy new-strategy)
+ (message "scala-indent:default-run-on-strategy set to %s"
+ scala-indent:default-run-on-strategy)))
+
(defun scala-indent:backward-sexp-to-beginning-of-line ()
"Skip sexps backwards until reaches beginning of line (i.e. the
point is at the first non whitespace or comment character). It
@@ -28,99 +103,337 @@ enclosing list."
;; moved to previous line, set new target
(setq code-beg (scala-lib:point-after
(scala-syntax:beginning-of-code-line))))))
- (if (> (point) code-beg)
- nil
+ (unless (> (point) code-beg)
(point))))
-(defun scala-indent:run-on-p (&optional point)
- (interactive)
+;;;
+;;; Run-on
+;;;
+
+(defconst scala-indent:mustNotTerminate-keywords-re
+ (regexp-opt '("extends" "forSome" "match" "with") 'words)
+ "Some keywords which occure only in the middle of an
+expression")
+
+(defconst scala-indent:mustNotTerminate-line-beginning-re
+ (concat "\\(" scala-indent:mustNotTerminate-keywords-re
+ "\\|:\\(" scala-syntax:after-reserved-symbol-re "\\)\\)")
+ "All keywords and symbols that cannot terminate a expression
+and must be handled by run-on. Reserved-symbols not included.")
+
+(defconst scala-indent:mustTerminate-re
+ (concat "\\([,;\u21D2]\\|=>?" scala-syntax:end-of-code-line-re
+ "\\|\\s(\\|" scala-syntax:empty-line-re "\\)")
+ "Symbols that must terminate an expression or start a
+sub-expression, i.e the following expression cannot be a
+run-on. This includes only parenthesis, '=', '=>', ',' and ';'
+and the empty line")
+
+(defconst scala-indent:mustNotContinue-re
+ (regexp-opt '("abstract" "catch" "case" "class" "def" "do" "else" "final"
+ "finally" "for" "if" "implicit" "import" "lazy" "new" "object"
+ "override" "package" "private" "protected" "return" "sealed"
+ "throw" "trait" "try" "type" "val" "var" "while" "yield")
+ 'words)
+ "Words that we don't want to continue the previous line")
+
+(defconst scala-indent:mustBeContinued-line-end-re
+ (concat "\\(" scala-syntax:other-keywords-unsafe-re
+ "\\|:" scala-syntax:end-of-code-line-re "\\)")
+ "All keywords and symbols that cannot terminate a expression
+and are infact a sign of run-on. Reserved-symbols not included.")
+
+(defun scala-indent:run-on-p (&optional point strategy)
+ "Returns t if the current point is in the middle of an expression"
+ ;; use default strategy if none given
+ (when (not strategy) (setq strategy (scala-indent:run-on-strategy)))
+ (save-excursion
+ (when point (goto-char point))
+ (unless (eobp)
+ ;; Note: ofcourse this 'cond' could be written as one big boolean
+ ;; expression, but I doubt that would be so readable and
+ ;; maintainable
+ (cond
+ ;; NO: this line starts with close parenthesis
+ ((= (char-syntax (char-after)) ?\))
+ nil)
+ ;; NO: the previous line must terminate
+ ((save-excursion
+ (scala-syntax:skip-backward-ignorable)
+ (or (bobp)
+ (scala-syntax:looking-back-empty-line-p)
+ (scala-syntax:looking-back-token scala-indent:mustTerminate-re)))
+ nil)
+ ;; YES: in a region where newlines are disabled
+ ((and (scala-syntax:newlines-disabled-p)
+ (not (= strategy scala-indent:keywords-only-strategy)))
+ t)
+ ;; NO: this line starts with a keyword that starts a new
+ ;; expression (e.g. 'def' or 'class')
+ ((looking-at scala-indent:mustNotContinue-re)
+ nil)
+ ;; NO: this line is the start of value body
+ ((scala-indent:body-p)
+ nil)
+ ;; YES: eager strategy can stop here, everything is a run-on if no
+ ;; counter evidence
+ ((= strategy scala-indent:eager-strategy)
+ t)
+ ;; YES: this line must not terminate because it starts with a
+ ;; middle of expression keyword
+ ((looking-at scala-indent:mustNotTerminate-line-beginning-re)
+ t)
+ ;; YES: end of prev line must not terminate
+ ((scala-syntax:looking-back-token
+ scala-indent:mustBeContinued-line-end-re)
+ t)
+ ;; YES: this line starts with type param
+ ((= (char-after) ?[)
+ t)
+ ;; YES: this line starts with open paren and the expression
+ ;; after all parens is a run-on
+ ((and (= (char-after) ?\()
+ (save-excursion (scala-syntax:forward-parameter-groups)
+ (scala-syntax:skip-forward-ignorable)
+ (or (= (char-after) ?=)
+ (= (char-after) ?{)
+ (scala-indent:run-on-p nil strategy))))
+ t)
+ ;; NO: that's all for keywords-only strategy
+ ((= strategy scala-indent:keywords-only-strategy)
+ nil)
+ ;; YES: this line starts with punctuation
+ ((= (char-after) ?\.)
+ t)
+ ;; YES: prev line ended with punctuation
+ ((scala-syntax:looking-back-token ".*[.]")
+ t)
+ ;; NO: that's all for reluctant-strategy
+ ((= strategy scala-indent:reluctant-strategy)
+ nil)
+ ;; YES: this line starts with opchars
+ ((save-excursion
+ (< 0 (skip-chars-forward scala-syntax:opchar-group)))
+ t)
+ ;; YES: prev line ends with opchars
+ ((save-excursion
+ (scala-syntax:skip-backward-ignorable)
+ (> 0 (skip-chars-backward scala-syntax:opchar-group)))
+ t)
+ ;; NO: else nil (only operator strategy should reach here)
+ (t nil)))))
+
+(defun scala-indent:run-on-line-p (&optional point strategy)
"Returns t if the current point (or point at 'point) is on a
-line that is a run-on from a previous line."
+line that is a run-on from a previous line."
(save-excursion
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
- (if (looking-at scala-syntax:mustNotContinue-re)
- nil
- (scala-syntax:skip-backward-ignorable)
- (not (or (bobp)
- (scala-syntax:looking-back-empty-line-p)
- (scala-syntax:looking-back-token
- scala-syntax:mustTerminate-re))))))
-
-; (defconst scala-syntax:mustNotTerminate-re
-; scala-syntax:reserved-symbols-unsafe-re
-
-(defun scala-indent:goto-run-on-anchor (&optional point)
+ (scala-indent:run-on-p nil strategy)))
+
+(defun scala-indent:goto-run-on-anchor (&optional point strategy)
"Moves back to the point whose column will be used as the
anchor relative to which indenting for current point (or point
'point') is calculated. Returns the new point or nil if the point
is not on a run-on line."
- (if (not (scala-indent:run-on-p point))
- nil
+ (when (scala-indent:run-on-line-p point strategy)
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
- (while (and (scala-indent:run-on-p)
+ (while (and (scala-indent:run-on-line-p nil strategy)
(scala-syntax:skip-backward-ignorable)
(scala-indent:backward-sexp-to-beginning-of-line)))
+ (when (scala-indent:backward-sexp-to-beginning-of-line)
+ (back-to-indentation))
(point)))
-(defun scala-indent:list-p (&optional point)
- "Returns the start of the list, if the current point (or point
-'point') is in a list, or nil. A list must be either enclosed in
-parentheses or start with 'val', 'var' or 'import'."
+(defconst scala-indent:double-indent-re
+ (concat (regexp-opt '("with" "extends" "forSome") 'words)
+ "\\|:\\(" scala-syntax:after-reserved-symbol-re "\\)"))
+
+(defun scala-indent:resolve-run-on-step (start &optional anchor)
+ "Resolves the appropriate indent step for run-on line at position
+'start'"
+ (save-excursion
+ (goto-char anchor)
+ (if (scala-syntax:looking-at-case-p)
+ ;; case run-on lines get double indent, except '|' which get
+ ;; special indents
+ (progn (goto-char start)
+ (- (* 2 scala-indent:step)
+ (skip-chars-forward "|")))
+ (goto-char start)
+ (cond
+ ;; some keywords get double indent
+ ((or (looking-at scala-indent:double-indent-re)
+ (scala-syntax:looking-back-token scala-indent:double-indent-re))
+ (* 2 scala-indent:step))
+ ;; no indent if the previous line is just close parens
+ ;; ((save-excursion
+ ;; (scala-syntax:skip-backward-ignorable)
+ ;; (let ((end (point)))
+ ;; (scala-syntax:beginning-of-code-line)
+ ;; (skip-syntax-forward ")")
+ ;; (= (point) end)))
+ ;; 0)
+ ;; else normal indent
+ (t
+ scala-indent:step)))))
+
+(defconst scala-indent:special-align-re
+ (regexp-opt '("yield" "else" "catch" "finally") 'words))
+
+(defun scala-indent:special-align-p (&optional point)
+ "Returns scala-syntax:beginning-of-code-line for the line on
+which current point (or point 'point') is, if the line starts
+with one of 'yield', 'else', 'catch' and 'finally', otherwise
+nil. Also, the previous line must not be with '}'"
(save-excursion
- ;; first check that the previous line ended with ','
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
- (if (not (scala-syntax:looking-back-token "," 1))
- nil
+ (when (looking-at scala-indent:special-align-re)
(goto-char (match-beginning 0))
- (ignore-errors ; catches when we get at parentheses
- (while (not (or (bobp)
- (looking-at scala-syntax:list-keywords-re)
- (scala-syntax:looking-back-empty-line-p)
- (scala-syntax:looking-back-token ";")))
- (scala-syntax:backward-sexp)))
- (cond ((= (char-syntax (char-before)) ?\()
- (point))
- ((looking-at scala-syntax:list-keywords-re)
- (goto-char (match-end 0)))))))
+ (point))))
+
+
+(defun scala-indent:goto-special-align-anchor (&optional point)
+ "Moves back to the point whose column will be used as the
+anchor relative to which indenting of special words on beginning
+of the line on which point (or point 'point') is, or nul if not
+special word found. Special words include 'yield', 'else',
+'catch' and 'finally'"
+ (let ((special-beg (scala-indent:special-align-p point)))
+ (when special-beg
+ (goto-char special-beg)
+ (if (and (scala-syntax:looking-back-token "}")
+ (save-excursion
+ (goto-char (match-beginning 0))
+ (= (match-beginning 0) (scala-lib:point-after
(scala-syntax:beginning-of-code-line)))))
+ (goto-char (match-beginning 0))
+ (cond ((looking-at "\\<yield\\>")
+ ;; align with 'for'
+ (if (scala-syntax:search-backward-sexp "\\<for\\>")
+ (point)
+ (message "matching 'for' not found")
+ nil))
+ ((looking-at "\\<else\\>")
+ ;; align with 'if' or 'else if'
+ (if (scala-syntax:search-backward-sexp "\\<if\\>")
+ (if (scala-syntax:looking-back-token "\\<else\\>")
+ (goto-char (match-beginning 0))
+ (point))
+ nil))
+ ((looking-at "\\<catch\\>")
+ ;; align with 'try'
+ (if (scala-syntax:search-backward-sexp "\\<try\\>")
+ (point)
+ (message "matching 'try' not found")
+ nil))
+ ((looking-at "\\<finally\\>")
+ ;; align with 'try'
+ (if (scala-syntax:search-backward-sexp "\\<try\\>")
+ (point)
+ (message "matching 'try' not found")
+ nil)))))))
+
+;;;
+;;; Lists and enumerators
+;;;
+
+(defun scala-indent:goto-list-anchor-impl (point)
+ (goto-char point)
+ ;; find the first element of the list
+ (forward-comment (buffer-size))
+ (if (= (line-number-at-pos point)
+ (line-number-at-pos))
+ (goto-char point)
+ (beginning-of-line))
+
+ ;; align list with first non-whitespace character
+ (skip-syntax-forward " ")
+ (point))
(defun scala-indent:goto-list-anchor (&optional point)
"Moves back to the point whose column will be used to indent
list rows at current point (or point 'point'). Returns the new
point or nil if the point is not in a list element > 1."
- (let ((list-beg (scala-indent:list-p (point))))
- (if (not list-beg)
- nil
- (goto-char list-beg)
- ;; find the first element of the list
- (forward-comment (buffer-size))
- (if (= (line-number-at-pos list-beg)
- (line-number-at-pos))
- (goto-char list-beg)
- (beginning-of-line))
-
- ;; align list with first non-whitespace character
- (skip-syntax-forward " "))))
+ (let ((list-beg (scala-syntax:list-p point)))
+ (when list-beg
+ (scala-indent:goto-list-anchor-impl list-beg))))
+
+(defun scala-indent:for-enumerators-p (&optional point)
+ "Returns the point after opening parentheses if the current
+point (or point 'point') is in a block of enumerators. Return nil
+if not in a list of enumerators or at the first enumerator."
+ (unless point (setq point (point)))
+ (save-excursion
+ (goto-char point)
+ (scala-syntax:beginning-of-code-line)
+ (let ((state (syntax-ppss point)))
+ (unless (or (eobp) (= (char-syntax (char-after)) ?\)))
+ (when (and state (nth 1 state))
+ (goto-char (nth 1 state))
+ (when (scala-syntax:looking-back-token scala-syntax:for-re)
+ (forward-char)
+ (forward-comment (buffer-size))
+ (when (< (point) point)
+ (1+ (nth 1 state)))))))))
+
+(defun scala-indent:goto-for-enumerators-anchor (&optional point)
+ "Moves back to the point whose column will be used to indent
+for enumerator at current point (or point 'point'). Returns the new
+point or nil if the point is not in a enumerator element > 1."
+ (let ((enumerators-beg (scala-indent:for-enumerators-p point)))
+ (when enumerators-beg
+ (scala-indent:goto-list-anchor-impl enumerators-beg))))
+
+;;;
+;;; Body
+;;;
+
+(defconst scala-indent:value-keyword-re
+ (regexp-opt '("if" "else" "yield" "for" "try" "finally" "catch") 'words))
(defun scala-indent:body-p (&optional point)
- "Returns the position of equals or double arrow symbol if
-current point (or point 'point) is on a line that follows = or
-=> (or it's unicode equivalent), or nil if not."
+ "Returns the position of '=' or '=>' (TODO: not ATM) symbol if
+current point (or point 'point) is on a line that follows said
+symbol, or nil if not."
(save-excursion
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
- (scala-syntax:looking-back-token scala-syntax:body-start-re 3)))
+ (or (scala-syntax:looking-back-token scala-syntax:body-start-re 3)
+ (progn
+ (when (scala-syntax:looking-back-token ")" 1)
+ (goto-char (match-end 0))
+ (backward-list))
+ (when (scala-syntax:looking-back-token scala-indent:value-keyword-re)
+ (goto-char (match-beginning 0))
+ (if (looking-at "\\<if\\>")
+ (if (scala-syntax:looking-back-token "\\<else\\>")
+ (match-beginning 0)
+ (point))
+ (point)))))))
(defun scala-indent:goto-body-anchor (&optional point)
(let ((declaration-end (scala-indent:body-p point)))
- (if (not declaration-end)
- nil
+ (when declaration-end
(goto-char declaration-end)
- (when (scala-indent:backward-sexp-to-beginning-of-line)
- (scala-indent:goto-run-on-anchor))
- (point))))
+ (if (looking-at scala-indent:value-keyword-re)
+ (point)
+ (when (scala-indent:backward-sexp-to-beginning-of-line)
+ (scala-indent:goto-run-on-anchor nil
+
scala-indent:keywords-only-strategy))
+ (when (scala-indent:backward-sexp-to-beginning-of-line)
+ (back-to-indentation))
+ (point)))))
+
+(defun scala-indent:resolve-body-step (start &optional anchor)
+ (if (= (char-after start) ?\{)
+ 0
+ scala-indent:step))
+
+;;;
+;;; Block
+;;;
(defun scala-indent:goto-block-anchor (&optional point)
"Moves back to the point whose column will be used as the
@@ -128,24 +441,63 @@ anchor for calculating block indent for current point (or
point
'point'). Returns point or (point-min) if not inside a block."
(let ((block-beg (nth 1 (syntax-ppss
(scala-lib:point-after (beginning-of-line))))))
- (if (not block-beg)
- nil
+ (when block-beg
;; check if the opening paren is the first on the line,
;; if so, it is the anchor. If not, then go back to the
;; start of the line
(goto-char block-beg)
- (scala-syntax:backward-parameter-groups)
(if (= (point) (scala-lib:point-after
(scala-syntax:beginning-of-code-line)))
(point)
+ (goto-char (or (scala-syntax:looking-back-token
+ scala-syntax:body-start-re 3)
+ (point)))
+ (scala-syntax:backward-parameter-groups)
(when (scala-indent:backward-sexp-to-beginning-of-line)
- (scala-indent:goto-run-on-anchor))
- (point)))))
+ (scala-indent:goto-run-on-anchor nil
+
scala-indent:keywords-only-strategy))
+ (when (scala-indent:backward-sexp-to-beginning-of-line)
+ (back-to-indentation))
+ (point)))))
+
+(defun scala-indent:resolve-block-step (start anchor)
+ "Resolves the appropriate indent step for block line at position
+'start' relative to the block anchor 'anchor'."
+ (let
+ ;; calculate a lead that is used for all steps. The lead is one
+ ;; indent step if there is a '=' between anchor and start,
+ ;; otherwise 0.
+ ((lead (if (ignore-errors
+ (save-excursion
+ (let ((block-beg (nth 1 (syntax-ppss start))))
+ (goto-char anchor)
+ (scala-syntax:has-char-before ?= block-beg))))
+ scala-indent:step 0)))
+ (cond
+ ;; block close parentheses line up with anchor in normal case
+ ((= (char-syntax (char-after start)) ?\))
+ (+ 0 lead))
+ ;; case-lines indent normally, regardless of where they are
+ ((scala-syntax:looking-at-case-p start)
+ (+ scala-indent:step lead))
+ ;; other than case-line in case-block get double indent
+ ((save-excursion
+ (goto-char (1+ (nth 1 (syntax-ppss start))))
+ (forward-comment (buffer-size))
+ (and (scala-syntax:looking-at-case-p)
+ (> start (match-beginning 0))))
+ (+ (* 2 scala-indent:step) lead))
+ ;; normal block line
+ (t (+ scala-indent:step lead)))))
+
+;;;
+;;; Open parentheses
+;;;
(defun scala-indent:open-parentheses-line-p (&optional point)
"Returns the position of the first character of the line,
if the current point (or point 'point') is on a line that starts
-with a parentheses, or nil if not."
+with an opening parentheses, or nil if not."
(save-excursion
(when point (goto-char point))
(scala-syntax:beginning-of-code-line)
@@ -157,63 +509,97 @@ anchor for calculating opening parenthesis indent for the
current
point (or point 'point'). Returns point or nil, if line does not
start with opening parenthesis."
;; There are four cases we need to consider:
- ;; 1. parameters on separate line (who would be so mad?).
- ;; 2. curry parentheses, i.e. 2..n parentheses groups.
- ;; 3. value body parentheses (follows '=').
+ ;; 1. curry parentheses, i.e. 2..n parentheses groups.
+ ;; 2. value body parentheses (follows '=').
+ ;; 3. parameters, etc on separate line (who would be so mad?)
;; 4. non-value body parentheses (follows class, trait, new, def, etc).
- ;; Of these 1. will be handled by run-on indent rule and 3. should
- ;; be handled by body indent rule.
(let ((parentheses-beg (scala-indent:open-parentheses-line-p point)))
- (if (not parentheses-beg)
- nil
+ (when parentheses-beg
(goto-char parentheses-beg)
- (cond
- ;; case 2
- ((scala-syntax:looking-back-token "[])]" 1)
+ (cond
+ ;; case 1
+ ((and (= (char-after) ?\()
+ (scala-indent:run-on-p)
+ (scala-syntax:looking-back-token ")" 1))
(scala-syntax:backward-parameter-groups)
- (point))
+ (let ((curry-beg (point)))
+ (forward-char)
+ (forward-comment (buffer-size))
+ (if (= (line-number-at-pos curry-beg)
+ (line-number-at-pos))
+ (goto-char curry-beg)
+ nil)))
+ ;; case 2
+ ((scala-syntax:looking-back-token "=" 1)
+ nil) ; let body rule handle it
;; case 4
((and (= (char-after) ?\{)
- (not (scala-syntax:looking-back-token "=" 1)))
- (scala-indent:goto-run-on-anchor)
- (point))))))
+ (scala-indent:goto-run-on-anchor
+ nil scala-indent:keywords-only-strategy)) ; use customized
strategy
+ (point))
+ ;; case 3
+ ;;((scala-indent:run-on-p)
+ ;; (scala-syntax:skip-backward-ignorable)
+ ;; (back-to-indentation)
+ ;; (point))
+ (t
+ nil)
+ ))))
+
+(defun scala-indent:resolve-open-parentheses-step (start anchor)
+ "Resolves the appropriate indent step for an open paren
+anchored at 'anchor'."
+ (cond ((scala-syntax:looking-back-token ")")
+ (message "curry")
+ 0)
+ ((save-excursion
+ (goto-char anchor)
+ ;; find =
+ (scala-syntax:has-char-before ?= start))
+ (message "=")
+ scala-indent:step)
+ (t
+ (message "normal at %d" (current-column))
+ 0)))
+
+;;;
+;;; Indentation engine
+;;;
(defun scala-indent:apply-indent-rules (rule-indents &optional point)
"Evaluates each rule, until one returns non-nil value. Returns
the sum of the value and the respective indent step, or nil if
nothing was applied."
- (if (not rule-indents)
- nil
+ (when rule-indents
(save-excursion
(let* ((pos (scala-syntax:beginning-of-code-line))
(rule-indent (car rule-indents))
(rule-statement (car rule-indent))
(indent-statement (cadr rule-indent))
- (anchor (funcall rule-statement point))
- (indent (if (functionp indent-statement)
- (funcall indent-statement pos anchor)
- (eval indent-statement))))
+ (anchor (funcall rule-statement point)))
(if anchor
- (+ (current-column) indent)
+ (progn
+ (message "indenting acording to %s at %d" rule-statement anchor)
+ (when (/= anchor (point))
+ (error (format "Assertion error: anchor=%d, point=%d" anchor
(point))))
+ (+ (current-column)
+ (save-excursion
+ (if (functionp indent-statement)
+ (funcall indent-statement pos anchor)
+ (eval indent-statement)))))
(scala-indent:apply-indent-rules (cdr rule-indents)))))))
-(defun scala-indent:resolve-block-step (start anchor)
- "Resolves the appropriate indent step for block line at position
-'start' relative to the block anchor 'anchor'."
- (if (= (char-syntax (char-after start)) ?\))
- 0 ;; block close parentheses line up with anchor
- ;; TODO: case blocks
- scala-indent:step))
-
(defun scala-indent:calculate-indent-for-line (&optional point)
"Calculate the appropriate indent for the current point or the
point 'point'. Returns the new column, or nil if the indent
cannot be determined."
(or (scala-indent:apply-indent-rules
- `((scala-indent:goto-open-parentheses-anchor 0)
- (scala-indent:goto-run-on-anchor (* 2 scala-indent:step))
+ `((scala-indent:goto-open-parentheses-anchor
scala-indent:resolve-open-parentheses-step)
+ (scala-indent:goto-for-enumerators-anchor 0)
+ (scala-indent:goto-run-on-anchor scala-indent:resolve-run-on-step)
(scala-indent:goto-list-anchor 0)
- (scala-indent:goto-body-anchor scala-indent:step)
+ (scala-indent:goto-body-anchor scala-indent:resolve-body-step)
+ (scala-indent:goto-special-align-anchor 0)
(scala-indent:goto-block-anchor scala-indent:resolve-block-step)
)
point)
@@ -222,24 +608,32 @@ cannot be determined."
(defun scala-indent:indent-line-to (column)
"Indent the line to column and move cursor to the indent
column, if it was at the left margin."
- (if (<= (current-column) (current-indentation))
- (indent-line-to column)
- (save-excursion (indent-line-to column))))
+ (when column
+ (if (<= (current-column) (current-indentation))
+ (indent-line-to column)
+ (save-excursion (indent-line-to column)))))
-(defun scala-indent:indent-code-line ()
+(defun scala-indent:indent-code-line (&optional strategy)
"Indent a line of code. Expect to be outside of any comments or
strings"
- (let ((indent (scala-indent:calculate-indent-for-line)))
- (if indent
- (scala-indent:indent-line-to indent)
- (message "No indent rule for current line"))))
-
-(defun scala-indent:indent-line ()
+ (if strategy
+ (setq scala-indent:effective-run-on-strategy strategy)
+ (if (eq last-command this-command)
+ (scala-indent:toggle-effective-run-on-strategy)
+ (scala-indent:reset-effective-run-on-strategy)))
+ (message "run-on-strategy is %s" (scala-indent:run-on-strategy))
+ (scala-indent:indent-line-to (scala-indent:calculate-indent-for-line)))
+
+(defun scala-indent:indent-line (&optional strategy)
"Indents the current line."
(interactive)
(let ((state (save-excursion (syntax-ppss (line-beginning-position)))))
(if (not (nth 8 state)) ;; 8 = start pos of comment or string, nil if none
- (scala-indent:indent-code-line)
+ (scala-indent:indent-code-line strategy)
(scala-indent:indent-line-to (current-indentation))
- nil
- )))
+ nil)))
+
+(defun scala-indent:indent-with-reluctant-strategy ()
+ (interactive)
+ (scala-indent:indent-line scala-indent:reluctant-strategy))
+
diff --git a/scala-mode-lib.el b/scala-mode-lib.el
index 2460609..373f493 100644
--- a/scala-mode-lib.el
+++ b/scala-mode-lib.el
@@ -13,3 +13,4 @@
`(save-excursion
,@body
(point)))
+
diff --git a/scala-mode-map.el b/scala-mode-map.el
index fc95277..6c44f45 100644
--- a/scala-mode-map.el
+++ b/scala-mode-map.el
@@ -13,13 +13,26 @@
(defvar scala-mode-map nil
"Local key map used for scala mode")
+(defun scala-mode-map:indent-parentheses ()
+ (when (and (= (char-syntax (char-before)) ?\))
+ (= (save-excursion (back-to-indentation) (point)) (1- (point))))
+ (scala-indent:indent-line)))
+
+(defun scala-mode-map:add-self-insert-hooks ()
+ (add-hook 'post-self-insert-hook
+ #'scala-mode-map:indent-parentheses))
+
(when (not scala-mode-map)
(let ((keymap (make-sparse-keymap)))
(scala-mode-map:define-keys
keymap
(([backspace] 'backward-delete-char-untabify)
- ((kbd "C-M-b") 'scala-syntax:backward-sexp)
- ([(control c)(control r)] 'scala-indent:goto-block-anchor)
+
+ ;; TODO: remove, use forward-sexp-function insetead
+ ([backtab] 'scala-indent:indent-with-reluctant-strategy)
+ ((kbd "C-M-b") 'scala-syntax:backward-sexp)
+
+ ([(control c)(control r)] 'scala-indent:rotate-run-on-strategy)
;; ("\r" 'scala-newline)
([(control c)(control c)] 'comment-region)
;; ("}" 'scala-electric-brace)
diff --git a/scala-mode-syntax.el b/scala-mode-syntax.el
index 645d994..eed7fc3 100644
--- a/scala-mode-syntax.el
+++ b/scala-mode-syntax.el
@@ -25,7 +25,7 @@
(defconst scala-syntax:hexDigit-group "0-9A-Fa-f")
(defconst scala-syntax:UnicodeEscape-re (concat "\\\\u["
scala-syntax:hexDigit-group "]\\{4\\}"))
-(defconst scala-syntax:upper-group "_[:upper:]\\$") ;; missing _ to make ids
work
+(defconst scala-syntax:upper-group "[:upper:]\\$") ;; missing _ to make ids
work
(defconst scala-syntax:upperAndUnderscore-group (concat "_"
scala-syntax:upper-group ))
(defconst scala-syntax:lower-group "[:lower:]")
(defconst scala-syntax:letter-group (concat scala-syntax:lower-group
scala-syntax:upper-group)) ;; TODO: add Lt, Lo, Nl
@@ -34,7 +34,10 @@
scala-syntax:upperAndUnderscore-group
scala-syntax:lower-group
scala-syntax:digit-group))
-(defconst scala-syntax:opchar-group "!#%&*+/:<=>?@\\\\\\^|~-") ;; TODO: Sm, So
+(defconst scala-syntax:opchar-safe-group "!%&*+/?\\\\^|~-") ;; TODO: Sm, So
+(defconst scala-syntax:opchar-unsafe-group "#:<=>@")
+(defconst scala-syntax:opchar-group (concat scala-syntax:opchar-unsafe-group
+ scala-syntax:opchar-safe-group))
;; Scala delimiters (Chapter 1), but no quotes
(defconst scala-syntax:delimiter-group ".,;")
@@ -90,7 +93,8 @@
"\\|" scala-syntax:oneLineStringLiteral-re "\\)" ))
;; Identifiers (Chapter 1.1)
-(defconst scala-syntax:op-re (concat "[" scala-syntax:opchar-group "]+"))
+(defconst scala-syntax:op-re
+ (concat "[" scala-syntax:opchar-group "]+" ))
(defconst scala-syntax:idrest-re
;; Eagerness of regexp causes problems with _. The following is a workaround,
;; but the resulting regexp matches only what SLS demands.
@@ -98,19 +102,23 @@
"\\(" "_+" scala-syntax:op-re "\\|" "_" "\\)?"))
(defconst scala-syntax:varid-re (concat "[" scala-syntax:lower-group "]"
scala-syntax:idrest-re))
(defconst scala-syntax:capitalid-re (concat "["
scala-syntax:upperAndUnderscore-group "]" scala-syntax:idrest-re))
-(defconst scala-syntax:plainid-re (concat "\\(" scala-syntax:capitalid-re
- "\\|" scala-syntax:varid-re
- "\\|" scala-syntax:op-re "\\)"))
+(defconst scala-syntax:plainid-re (concat "\\(" "[" scala-syntax:lower-group
scala-syntax:upperAndUnderscore-group "]" scala-syntax:idrest-re
+ "\\|" scala-syntax:op-re "\\)"))
;; stringlit is referred to, but not defined Scala Language Specification 2.9
-(defconst scala-syntax:stringlit-re (concat scala-syntax:stringElement-re
"*?"))
+;; we define it as consisting of anything but '`' and newline
+(defconst scala-syntax:stringlit-re "[^`\n\r]")
(defconst scala-syntax:quotedid-re (concat "`" scala-syntax:stringlit-re "`"))
(defconst scala-syntax:id-re (concat "\\(" scala-syntax:plainid-re
"\\|" scala-syntax:quotedid-re "\\)"))
+(defconst scala-syntax:id-first-char-group
+ (concat scala-syntax:lower-group
+ scala-syntax:upperAndUnderscore-group
+ scala-syntax:opchar-group))
;; Symbol literals (Chapter 1.3.7)
(defconst scala-syntax:symbolLiteral-re
;; must end with non-' to not conflict with scala-syntax:characterLiteral-re
- (concat "'" scala-syntax:plainid-re "\\([^']\\|$\\)"))
+ (concat "\\('" scala-syntax:plainid-re "\\)\\([^']\\|$\\)"))
;; Literals (Chapter 1.3)
(defconst scala-syntax:literal-re
@@ -123,17 +131,71 @@
"\\|" "null" "\\)"))
;; Paths (Chapter 3.1)
-(defconst scala-syntax:classQualifier-re (concat "\\[" scala-syntax:id-re
"\\]"))
+;; emacs has a problem with these regex, don't use them
+(defconst scala-syntax:classQualifier-re (concat "[[]" scala-syntax:id-re
"[]]"))
(defconst scala-syntax:stableId-re
- (concat "\\(\\(" scala-syntax:id-re
- "\\|" "this"
- "\\|" "super" scala-syntax:classQualifier-re "\\)\\.\\)*"
+ (concat "\\(\\(" "this"
+ "\\|" "super" scala-syntax:classQualifier-re
+ "\\|" scala-syntax:id-re
+ "\\)\\.\\)*"
scala-syntax:id-re))
(defconst scala-syntax:path-re
(concat "\\(" scala-syntax:stableId-re
"\\|" "\\(" scala-syntax:id-re "\\." "\\)?" "this" "\\)"))
+(defun scala-syntax:looking-at-super ()
+ (save-excursion
+ (when (looking-at "\\<super\\>")
+ (let ((beg (match-beginning 0)))
+ (when (and (goto-char (match-end 0))
+ (or (when (= (char-after) ?.)
+ (forward-char)
+ t)
+ (and (when (and (not (eobp)) (= (char-after) ?\[))
+ (forward-char)
+ t)
+ (progn (scala-syntax:skip-forward-ignorable)
+ (looking-at scala-syntax:id-re))
+ (progn (goto-char (match-end 0))
+ (scala-syntax:skip-forward-ignorable)
+ (when (and (not (eobp)) (= (char-after)
?\]))
+ (forward-char)
+ t))
+ (when (and (not (eobp)) (= (char-after) ?.))
+ (forward-char)
+ t)))
+ (looking-at scala-syntax:id-re))
+ (set-match-data `(,beg ,(match-end 0)))
+ t)))))
+
+(defun scala-syntax:looking-at-stableIdOrPath (&optional path-p beg)
+ (unless beg (setq beg (point)))
+ (save-excursion
+ (cond ((looking-at "\\<this\\>")
+ (goto-char (match-end 0))
+ (if (and (not (eobp)) (= (char-after) ?.))
+ (progn (forward-char)
+ (scala-syntax:looking-at-stableIdOrPath path-p beg))
+ path-p))
+ ((or (scala-syntax:looking-at-super)
+ (and (not (or (looking-at scala-syntax:keywords-unsafe-re)
+ (scala-syntax:looking-at-reserved-symbol nil)))
+ (looking-at scala-syntax:id-re)))
+ (goto-char (match-end 0))
+ (if (and (not (eobp)) (= (char-after) ?.))
+ (progn (forward-char)
+ (scala-syntax:looking-at-stableIdOrPath path-p beg))
+ (set-match-data `(,beg ,(match-end 0)))
+ (point))))))
+
+;; Patterns
+(defconst scala-syntax:simplePattern-beginning-re
+ (concat "\\([_(]"
+ "\\|" scala-syntax:literal-re
+ "\\|" scala-syntax:stableId-re
+ "\\)"))
+
;;;
;;; Other regular expressions
;;;
@@ -141,90 +203,120 @@
(defconst scala-syntax:empty-line-re
"^\\s *$")
-(defconst scala-syntax:keywords-re
- (regexp-opt '("abstract" "case" "catch" "class" "def"
- "do" "else" "extends" "false" "final"
- "finally" "for" "forSome" "if" "implicit"
- "import" "lazy" "match" "new" "null"
- "object" "override" "package" "private" "protected"
- "return" "sealed" "super" "this" "throw"
- "trait" "try" "true" "type" "val"
- "var" "while" "with" "yield") 'words))
+(defconst scala-syntax:comment-start-re
+ "/[/*]")
+
+(defconst scala-syntax:end-of-code-line-re
+ (concat "\\([ ]\\|$\\|" scala-syntax:comment-start-re "\\)")
+ "A special regexp that can be concatenated to an other regular
+ expression when used with scala-syntax:looking-back-token. Not
+ meaningfull in other contexts.")
+
+(defconst scala-syntax:path-keywords-unsafe-re
+ (regexp-opt '("super" "this") 'words))
+
+(defconst scala-syntax:path-keywords-re
+ (concat "\\(^\\|[^`]\\)\\(" scala-syntax:path-keywords-unsafe-re "\\)"))
+
+(defconst scala-syntax:value-keywords-unsafe-re
+ (regexp-opt '("false" "null" "true") 'words))
+
+(defconst scala-syntax:value-keywords-re
+ (concat "\\(^\\|[^`]\\)\\(" scala-syntax:value-keywords-unsafe-re "\\)"))
+(defconst scala-syntax:other-keywords-unsafe-re
+ (regexp-opt '("abstract" "case" "catch" "class" "def" "do" "else" "extends"
+ "final" "finally" "for" "forSome" "if" "implicit" "import"
+ "lazy" "match" "new" "object" "override" "package" "private"
+ "protected" "return" "sealed" "throw" "trait" "try" "type"
+ "val" "var" "while" "with" "yield") 'words))
+
+(defconst scala-syntax:other-keywords-re
+ (concat "\\(^\\|[^`]\\)\\(" scala-syntax:other-keywords-unsafe-re "\\)"))
+
+(defconst scala-syntax:keywords-unsafe-re
+ (concat "\\(" scala-syntax:path-keywords-unsafe-re
+ "\\|" scala-syntax:value-keywords-unsafe-re
+ "\\|" scala-syntax:other-keywords-unsafe-re
+ "\\)"))
+
+;; TODO: remove
+;; (defconst scala-syntax:keywords-re
+;; (concat "\\(^\\|[^`]\\)\\(" scala-syntax:value-keywords-unsafe-re
+;; "\\|" scala-syntax:path-keywords-unsafe-re
+;; "\\|" scala-syntax:other-keywords-unsafe-re "\\)"))
-;; false, true, null, super, this are neither
(defconst scala-syntax:after-reserved-symbol-underscore-re
- ;; what can be after reserved symbol _ (if there is something else, it
- ;; will be upper case letter per SLS)
- (concat "$\\|[^" scala-syntax:letterOrDigit-group "]"))
+ (concat "$\\|" scala-syntax:comment-start-re
+ "\\|[^" scala-syntax:letterOrDigit-group "]"))
(defconst scala-syntax:reserved-symbol-underscore-re
;; reserved symbol _
(concat "\\(^\\|[^" scala-syntax:letterOrDigit-group "]\\)"
"\\(_\\)"
- "\\($\\|[^" scala-syntax:letterOrDigit-group "]\\)"))
+ "\\(" scala-syntax:after-reserved-symbol-underscore-re "\\)"))
(defconst scala-syntax:reserved-symbols-unsafe-re
;; reserved symbols. The regexp is unsafe as it does not
;; check the context.
- "\\([:=#@\u21D2\u2190]\\|=>\\|<[:%!?\\-]\\|>:\\)" )
+ "\\([:#@\u21D2\u2190]\\|=>?\\|<[:%!?\\-]\\|>:\\)" )
+
+(defconst scala-syntax:double-arrow-unsafe-re
+ "\\(=>\\|\u21D2\\)")
+
+(defconst scala-syntax:after-reserved-symbol-re
+ (concat "\\($\\|" scala-syntax:comment-start-re
+ "\\|[^" scala-syntax:opchar-group "]\\)"))
(defconst scala-syntax:reserved-symbols-re
;; reserved symbols and XML starts ('<!' and '<?')
(concat "\\(^\\|[^" scala-syntax:opchar-group "]\\)"
scala-syntax:reserved-symbols-unsafe-re
- "\\($\\|[^" scala-syntax:opchar-group "]\\)"))
-
-(defconst scala-syntax:reserved-re
- (concat scala-syntax:keywords-re
- "\\|" scala-syntax:reserved-symbols-re
- "\\|" scala-syntax:reserved-symbol-underscore-re))
-
-(defconst scala-syntax:mustNotTerminate-keywords-re
- (regexp-opt '("catch" "else" "extends" "finally"
- "forSome" "match" "with" "yield") 'words)
- "Keywords whiOAch cannot end a expression and are infact a sign
- of run-on.")
-
-(defconst scala-syntax:mustNotTerminate-re
- (concat "\\(" scala-syntax:mustNotTerminate-keywords-re
- "\\|[.]\\)")
- "All keywords and symbols that cannot terminate a expression
-and are infact a sign of run-on. Reserved-symbols not included.")
-
-(defconst scala-syntax:mustTerminate-re
- (concat "\\([,;]\\|=>?\\([ ]\\|$\\)\\|\\s(\\|" scala-syntax:empty-line-re
"\\)")
- "Symbols that must terminate an expression or start a
-sub-expression, i.e the following expression cannot be a
-run-on. This includes only parenthesis, '=', '=>', ',' and ';'
-and the empty line")
-
-(defconst scala-syntax:mustNotContinue-re
- ;; Although SLS says case continues an expression, we say
- ;; for the sake of indenting, it does not. Hence case is
- ;; never a run-on of the previous line.
- ;; While we cannot handle here since it could be 'do..while'
- (concat "\\("
- (regexp-opt '("abstract" "class" "def" "do" "final"
- "for" "if" "implicit" "import" "lazy"
- "new" "object" "override" "package" "private"
- "protected" "return" "sealed" "throw"
- "trait" "try" "type" "val" "var" "case")
- 'words)
- "\\|[]})]\\)")
- "Keywords that begin an expression and parenthesis that end an
-expression, i.e they cannot be run-on to the previous line even
-if there is no semi in between.")
+ "\\(" scala-syntax:after-reserved-symbol-re "\\)"))
+
+(defconst scala-syntax:colon-re
+ (concat "\\(^\\|[^" scala-syntax:opchar-group "]\\)"
+ "\\(:\\)"
+ "\\(" scala-syntax:after-reserved-symbol-re "\\)"))
+
+
+;; (defconst scala-syntax:colon-looking-at-re
+;; ;; reserved symbol ':'. The regexp is safe for use with
+;; ;; 'looking-at' function, if the point before is not opchar
+;; (concat "\\(:\\)\\(" scala-syntax:after-reserved-symbol-re "\\)"))
+
+;; (defconst scala-syntax:bar-looking-at-re
+;; ;; reserved symbol '|'. The regexp is safe for use with
+;; ;; 'looking-at' function, if the point before is not opchar
+;; (concat "\\(|\\)\\(" scala-syntax:after-reserved-symbol-re "\\)"))
+
+;; (defconst scala-syntax:reserved-symbols-looking-at-re
+;; ;; reserved symbols. The regexp is safe for use with
+;; ;; 'looking-at' function, if the point before is not opchar
+;; (concat scala-syntax:reserved-symbols-unsafe-re
+;; "\\(" scala-syntax:after-reserved-symbol-re "\\)"))
+
+;; TODO: remove
+;; (defconst scala-syntax:reserved-re
+;; (concat scala-syntax:keywords-re
+;; "\\|" scala-syntax:reserved-symbols-re
+;; "\\|" scala-syntax:reserved-symbol-underscore-re))
+
+(defconst scala-syntax:modifiers-re
+ (regexp-opt '("override" "abstract" "final" "sealed" "implicit" "lazy"
+ "private" "protected") 'words))
(defconst scala-syntax:body-start-re
- "\\(=>?\\|\u21D2\\)\\([ ]\\|$\\)"
- "A regexp for detecting if a line ends with '=', '=>' or the
- unicode symbol 'double arrow'")
+ (concat "=" scala-syntax:end-of-code-line-re)
+ "A regexp for detecting if a line ends with '='")
+; "\\(=>?\\|\u21D2\\)\\([ ]\\|$\\)"
+; "A regexp for detecting if a line ends with '=', '=>' or the
+; unicode symbol 'double arrow'")
-(defconst scala-syntax:continue-body-keywords-re
- (regexp-opt '("catch" "else" "finally" "forSome" "match" "yield") 'words)
- "Keywords which continue a statement, but have their own body")
+;;(defconst scala-syntax:continue-body-keywords-re
+;; (regexp-opt '("catch" "else" "finally" "forSome" "match" "yield") 'words)
+;; "Keywords which continue a statement, but have their own body")
(defconst scala-syntax:list-keywords-re
(regexp-opt '("var" "val" "import") 'words)
@@ -236,10 +328,18 @@ if there is no semi in between.")
(defconst scala-syntax:multiLineStringLiteral-end-re
"\"\"+\\(\"\\)")
-(defun scala-syntax:find-reserved-symbols ()
- (interactive)
- (re-search-forward scala-syntax:reserved-symbols-re nil t))
+(defconst scala-syntax:case-re
+ "\\<case\\>")
+
+(defconst scala-syntax:for-re
+ "\\<for\\>")
+
+(defconst scala-syntax:class-or-object-re
+ (regexp-opt '("class" "object") 'words))
+;; (defconst scala-syntax:case-end-unsafe-re
+;; (concat scala-syntax:double-arrow-unsafe-re
+;; "\\(" scala-syntax:after-reserved-symbol-re "\\)"))
;;;;
;;;; Character syntax table and related syntax-propertize functions
@@ -407,6 +507,7 @@ symbol constituents (syntax 3)"
(scala-syntax:put-syntax-table-property 0 '(3 . nil)))
'(3 . nil))))))))) ;; symbol constituent syntax (3) also for
the '_'
+
(defun scala-syntax:propertize (start end)
"See syntax-propertize-function"
(scala-syntax:propertize-characterLiterals start end)
@@ -421,8 +522,11 @@ symbol constituents (syntax 3)"
(defun scala-syntax:beginning-of-code-line ()
(interactive)
"Move to the beginning of code on the line, or to the end of
-the line, if the line is empty. Return the new point.
-Not to be called on a line whose start is inside a comment."
+the line, if the line is empty. Return the new point. Not to be
+called on a line whose start is inside a comment, i.e. a comment
+begins on the previous line and continues past the start of this
+line."
+ ;; TODO: make it work even if the start IS inside a comment
(beginning-of-line)
(let ((eol (line-end-position))
(pos (point)))
@@ -437,21 +541,77 @@ Not to be called on a line whose start is inside a
comment."
(skip-syntax-forward " " eol)
(point))))
+(defun scala-syntax:looking-at-varid-p (&optional point)
+ "Return true if looking-at varid, and it is not the start of a
+stableId"
+ (save-excursion
+ (when point (goto-char point))
+ (scala-syntax:skip-forward-ignorable)
+ (let ((case-fold-search nil))
+ (when (looking-at scala-syntax:varid-re)
+ (save-match-data
+ (if (or (= (char-after (match-end 0)) ?.)
+ (looking-at "\\<\\(this\\|super\\)\\>"))
+ nil
+ t))))))
+
+(defun scala-syntax:looking-at-empty-line-p ()
+ (save-excursion
+ (or (bolp)
+ (skip-syntax-forward " >" (1+ (line-end-position))))
+ (looking-at scala-syntax:empty-line-re)))
+
+(defun scala-syntax:looking-at-reserved-symbol (re &optional point)
+ (interactive)
+ (unless re (setq re scala-syntax:reserved-symbols-unsafe-re))
+ (save-excursion
+ (when point (goto-char point))
+ (scala-syntax:skip-forward-ignorable)
+ (and (looking-at re)
+ (goto-char (match-end 0))
+ (looking-at-p scala-syntax:after-reserved-symbol-re))))
+
+(defun scala-syntax:looking-at-case-p (&optional point)
+ (save-excursion
+ (when point (goto-char point))
+ (scala-syntax:skip-forward-ignorable)
+ (and (looking-at scala-syntax:case-re)
+ (goto-char (match-end 0))
+ (scala-syntax:skip-forward-ignorable)
+ (not (looking-at-p scala-syntax:class-or-object-re)))))
+
(defun scala-syntax:looking-back-empty-line-p ()
"Return t if the previous line is empty"
(save-excursion
(skip-syntax-backward " " (line-beginning-position))
(and (bolp)
(forward-line -1)
- (looking-at scala-syntax:empty-line-re))))
+ (looking-at-p scala-syntax:empty-line-re))))
+
+(defun scala-syntax:skip-forward-ignorable ()
+ "Moves forward over ignorable whitespace and comments. A
+completely empty line is not ignorable and will not be mobed over."
+ (save-match-data
+ (while (and (not (scala-syntax:looking-at-empty-line-p))
+ (forward-comment 1)))
+ (skip-syntax-forward " " (line-end-position))))
(defun scala-syntax:skip-backward-ignorable ()
"Move backwards over ignorable whitespace and comments. A
completely empty line is not ignorable and will not be moved
over. Returns the number of points moved (will be negative)."
- (while (and (not (scala-syntax:looking-back-empty-line-p))
- (forward-comment -1)))
- (skip-syntax-backward " " (line-beginning-position)))
+ (save-match-data
+ (while (and (not (scala-syntax:looking-back-empty-line-p))
+ (forward-comment -1)))
+ (skip-syntax-backward " " (line-beginning-position))))
+
+(defun scala-syntax:looking-at (re)
+ "Return the end position of the matched re, if the current
+position is followed by it, or nil if not. All ignorable comments
+and whitespace are skipped before matching."
+ (save-excursion
+ (scala-syntax:skip-forward-ignorable)
+ (looking-at re)))
(defun scala-syntax:looking-back-token (re &optional limit)
"Return the start position of the token matched by re, if the
@@ -476,8 +636,24 @@ empty line. Expects to be outside of comment."
(defun scala-syntax:backward-parameter-groups ()
"Move back over all parameter groups to the start of the first
one."
- (while (scala-syntax:looking-back-token "[])]" 1)
- (backward-list)))
+ (save-match-data
+ (while (scala-syntax:looking-back-token "[])]" 1)
+ (backward-list))))
+
+(defun scala-syntax:forward-parameter-groups ()
+ "Move back over all parameter groups to the end of the last
+one."
+ (save-match-data
+ (while (scala-syntax:looking-at "[[(]")
+ (forward-list))))
+
+(defun scala-syntax:forward-modifiers ()
+ "Move forward over any modifiers."
+ (save-match-data
+ (while (scala-syntax:looking-at scala-syntax:modifiers-re)
+ (scala-syntax:forward-sexp)
+ (when (scala-syntax:looking-at "[")
+ (forward-sexp)))))
(defun scala-syntax:looking-back-else-if-p ()
;; TODO: rewrite using (scala-syntax:if-skipped
(scala:syntax:skip-backward-else-if))
@@ -489,6 +665,74 @@ one."
(prog1 (scala-syntax:looking-back-token "else")
(goto-char (match-beginning 0))))
(point) nil)))
+
+(defun scala-syntax:newlines-disabled-p (&optional point)
+ "Return true if newlines are disabled at the current point (or
+point 'point') as specified by SLS chapter 1.2"
+ ;; newlines are disabled if
+ ;; - in '()' or '[]'
+ ;; - between 'case' and '=>'
+ ;; - XML mode (not implemented here)
+ (unless point (setq point (point)))
+ (save-excursion
+ (let* ((state (syntax-ppss point))
+ (parenthesisPos (nth 1 state)))
+ (when parenthesisPos ;; if no parenthesis, then this cannot be a case
block either
+ (goto-char parenthesisPos)
+ (or
+ ;; the trivial cases of being inside ( or [
+ (= (char-after) ?\()
+ (= (char-after) ?\[)
+ ;; else we have to see about case
+ (progn
+ (forward-char)
+ (forward-comment (buffer-size))
+ (skip-syntax-forward " >")
+ (when (looking-at scala-syntax:case-re)
+ (let ((limit (match-beginning 0)))
+ (goto-char (or (nth 8 state) point))
+ ;; go to the start of => or 'case'
+ (while (> (point) limit)
+ (scala-syntax:backward-sexp)
+ (when (or (looking-at scala-syntax:case-re)
+ (scala-syntax:looking-at-reserved-symbol
+ scala-syntax:double-arrow-unsafe-re))
+ (setq limit (point))))
+ ;; unless we found '=>', check if we found 'case' (but
+ ;; 'case class' or 'case object')
+ (unless (scala-syntax:looking-at-reserved-symbol
+ scala-syntax:double-arrow-unsafe-re)
+ (scala-syntax:forward-sexp)
+
+ (and (<= (point) point) ;; check that we were inside in the
first place
+ (progn (scala-syntax:skip-forward-ignorable)
+ (not (looking-at
scala-syntax:class-or-object-re)))))))))))))
+
+(defun scala-syntax:forward-sexp ()
+ "Move forward one scala expression. It can be: paramter list (value or type),
+id, reserved symbol, keyword, block, or literal. Delimiters (.,;)
+and comments are skipped silently. Position is placed at the
+beginning of the skipped expression."
+ (interactive)
+ ;; emacs knows how to properly skip: lists, varid, capitalid,
+ ;; strings, symbols, chars, quotedid. What we have to handle here is
+ ;; most of all ids made of op chars
+
+ ;; skip comments, whitespace and scala delimiter chars .,; so we
+ ;; will be at the start of something interesting
+ (forward-comment (buffer-size))
+ (while (< 0 (+ (skip-syntax-forward " ")
+ (skip-chars-forward scala-syntax:delimiter-group))))
+
+ (if (not (= (char-syntax (char-after)) ?\.))
+ ;; emacs can handle everything but opchars
+ (forward-sexp)
+ ;; just because some char has punctuation syntax, doesn't mean the
+ ;; position has it (since the propertize function can change
+ ;; things. So... let's first try to handle it as punctuation and
+ ;; if we got no success, then we let emacs try
+ (when (= (skip-syntax-forward ".") 0)
+ (forward-sexp))))
(defun scala-syntax:backward-sexp ()
"Move backward one scala expression. It can be: parameter
@@ -497,23 +741,59 @@ one."
silently. Position is placed at the beginning of the skipped
expression."
(interactive)
- ;; emacs knows how to properly skip: lists, varid, capitalid,
- ;; strings, symbols, chars, quotedid. What we have to handle here is
- ;; most of all ids made of op chars
-
- ;; skip comments, whitespace and scala delimiter chars .,; so we
- ;; will be at the start of something interesting
+ ;; for implementation comments, see scala-syntax:forward-sexp
(forward-comment (- (buffer-size)))
(while (> 0 (+ (skip-syntax-backward " ")
(skip-chars-backward scala-syntax:delimiter-group))))
- (if (not (= (char-syntax (char-before)) ?\.))
- ;; emacs can handle everything but opchars
+ (if (not (or (bobp) (= (char-syntax (char-before)) ?\.)))
(backward-sexp)
- ;; just because some char has punctuation syntax, doesn't mean the
- ;; position has it (since the propertize function can change
- ;; things. So... let's first try to handle it as punctuation and
- ;; if we go no success, then we let emacs try
(when (= (skip-syntax-backward ".") 0)
(backward-sexp))))
+(defun scala-syntax:has-char-before (char end)
+ (save-excursion
+ (while (and (< (point) end)
+ (or (bobp)
+ (/= (char-before) char)))
+ (scala-syntax:forward-sexp))
+ (when (= (char-before) char)
+ (scala-syntax:skip-forward-ignorable)
+ (> end (point)))))
+
+(defun scala-syntax:search-backward-sexp (re)
+ "Searches backward sexps until it reaches re, empty line or ;.
+If re is found, point is set to beginning of re and the position
+is returned, otherwise nil is returned"
+ (let ((found (save-excursion
+ (while (not (or (bobp)
+ (scala-syntax:looking-back-empty-line-p)
+ (scala-syntax:looking-back-token "[;,]")
+ (looking-at re)))
+ (scala-syntax:backward-sexp))
+ (if (looking-at re)
+ (point)
+ nil))))
+ (when found (goto-char found))))
+
+(defun scala-syntax:list-p (&optional point)
+ "Returns the start of the list, if the current point (or point
+'point') is on the first line of a list element > 1, or nil if
+not. A list must be either enclosed in parentheses or start with
+'val', 'var' or 'import'."
+ (save-excursion
+ ;; first check that the previous line ended with ','
+ (when point (goto-char point))
+ (scala-syntax:beginning-of-code-line)
+ (when (scala-syntax:looking-back-token "," 1)
+ (goto-char (match-beginning 0))
+ (ignore-errors ; catches when we get at parentheses
+ (while (not (or (bobp)
+ (looking-at scala-syntax:list-keywords-re)
+ (scala-syntax:looking-back-empty-line-p)
+ (scala-syntax:looking-back-token ";")))
+ (scala-syntax:backward-sexp)))
+ (cond ((= (char-syntax (char-before)) ?\()
+ (point))
+ ((looking-at scala-syntax:list-keywords-re)
+ (goto-char (match-end 0)))))))
diff --git a/scala-mode.el b/scala-mode.el
index 2dc9b18..08cf410 100644
--- a/scala-mode.el
+++ b/scala-mode.el
@@ -11,6 +11,7 @@
(require 'scala-mode-fontlock)
(require 'scala-mode-indent)
(require 'scala-mode-map)
+(require 'cc-cmds)
;; Tested only for emacs 23
(unless (<= 23 emacs-major-version)
@@ -25,6 +26,30 @@
(defmacro scala-mode:make-local-variables (&rest quoted-names)
(cons 'progn (mapcar #'(lambda (quoted-name) `(make-local-variable
,quoted-name)) quoted-names)))
+(defconst scala-mode:comment-line-start
+ (concat "[ \t]*" ; whitespace
+ "\\(//+\\|\\**\\)" ; comment start
+ "[ \t]*")) ; whitespace
+
+(defconst scala-mode:paragraph-start
+ (concat scala-mode:comment-line-start
+ "\\($" ; empty line
+ "\\|=[^=\n]+=[ ]*$" ; heading 1
+ "\\|==[^=\n]+==[ ]*$" ; heading 2
+ "\\|===[^=\n]+===[ ]*$" ; heading 3
+ "\\|====+[^=\n]+====+[ ]*$" ; heading 4-n
+ "\\|-" ; unordered liststs
+ "\\|[1IiAa]\\." ; ordered lists
+; "\\|{{{" ; code block start
+; "\\|}}}" ; code block end
+ "\\|@[a-zA-Z]+\\>" ; annotations
+ "\\)"
+ ))
+
+(defconst scala-mode:paragraph-separate
+ (concat scala-mode:comment-line-start "$"))
+
+
;; (defun scala-mode ()
;; "Major mode for editing scala code.
@@ -57,6 +82,9 @@ When started, runs `scala-mode-hook'.
(scala-mode:make-local-variables
'syntax-propertize-function
'font-lock-defaults
+ 'paragraph-start
+ 'paragraph-separate
+ 'fill-paragraph-function
'comment-start
'comment-end
'comment-start-skip
@@ -72,25 +100,32 @@ When started, runs `scala-mode-hook'.
parse-sexp-lookup-properties t
;; TODO: font-lock
- font-lock-defaults '(scala-font-lock:keywords
+ font-lock-defaults '((scala-font-lock:keywords)
nil)
;; TODO: paragraph-start, paragraphs-separate,
paragraph-ignore-fill-prefix
;; TODO: beginning-of-defun-function, end-of-defun-function
;; comments
+ paragraph-start scala-mode:paragraph-start
+ paragraph-separate scala-mode:paragraph-separate
+ fill-paragraph-function 'c-fill-paragraph
comment-start "// "
comment-end ""
- comment-start-skip "\\(//+\\|/\\*+\\)\\s *"
+ comment-start-skip "\\(//+\\|/\\*+\\)[ \t]*"
comment-column 0
comment-multi-line t
;; TODO: comment-indent-function
+ ;; TODO: forward-sexp-function
+
indent-line-function 'scala-indent:indent-line
indent-tabs-mode nil
)
(use-local-map scala-mode-map)
(turn-on-font-lock)
+ ;; add indent functionality to some characters
+ (scala-mode-map:add-self-insert-hooks)
)
;; Attach .scala files to the scala-mode
- [nongnu] elpa/scala-mode 56cba29 194/217: Fix bad syntax when `*/` is prefixed by an opcode. (#143), (continued)
- [nongnu] elpa/scala-mode 56cba29 194/217: Fix bad syntax when `*/` is prefixed by an opcode. (#143), ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode a1ff6f3 202/217: feat: add Scala 3 keywords, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 46bb948 198/217: Fix #155, remove ob-scala.el, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode a2a1b1d 208/217: compile mode with sbt thin client, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 54fa36f 200/217: Merge pull request #159 from kpbochenek/fix-files-association-alist, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 5058a85 204/217: doc: clarify Scala versions supported, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 0c5ed9e 022/217: deleted example files, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 61a511f 021/217: Update README.md, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode fe5c2c4 031/217: Fixes #11: was still using the slow regexp, not faster, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 1a3d958 029/217: forward/backward-sexp, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode e30b35a 008/217: Indentation working,
ELPA Syncer <=
- [nongnu] elpa/scala-mode 59cdd94 074/217: Add `scala-indent:use-javadoc-style` defcustom., ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 347faeb 075/217: Add Evan Meagher to contributors list., ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 081a92b 081/217: scala-indent:remove-indent-from-previous-empty-line, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 0fac9ae 014/217: removed debug messages, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 9476d4c 018/217: Fixes #3: alignment of else inside parens, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 3f724ed 030/217: Add the scala-indent:pad-equals setting., ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 6257e3b 033/217: Renamed bad-equals to indent-value-expression. Added info to README.md, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 4e3a24f 043/217: Updated Examples.md to reflect issues already solved, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 338ff73 060/217: Changed the indent defaults, closes #19, ELPA Syncer, 2021/08/29
- [nongnu] elpa/scala-mode 6c257f6 057/217: Enhanced support for comments and multi-line strings, including, ELPA Syncer, 2021/08/29