Skip to content

Commit dbb2a5c

Browse files
committed
[clike mode] Improve indentation of Scala => functions
Issue codemirror#3779
1 parent 29555c9 commit dbb2a5c

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

mode/clike/clike.js

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,42 @@
1111
})(function(CodeMirror) {
1212
"use strict";
1313

14+
function Context(indented, column, type, align, prev) {
15+
this.indented = indented;
16+
this.column = column;
17+
this.type = type;
18+
this.align = align;
19+
this.prev = prev;
20+
}
21+
function isStatement(type) {
22+
return type == "statement" || type == "switchstatement" || type == "namespace";
23+
}
24+
function pushContext(state, col, type) {
25+
var indent = state.indented;
26+
if (state.context && isStatement(state.context.type) && !isStatement(type))
27+
indent = state.context.indented;
28+
return state.context = new Context(indent, col, type, null, state.context);
29+
}
30+
function popContext(state) {
31+
var t = state.context.type;
32+
if (t == ")" || t == "]" || t == "}")
33+
state.indented = state.context.indented;
34+
return state.context = state.context.prev;
35+
}
36+
37+
function typeBefore(stream, state) {
38+
if (state.prevToken == "variable" || state.prevToken == "variable-3") return true;
39+
if (/\S(?:[^- ]>|[*\]])\s*$|\*$/.test(stream.string.slice(0, stream.start))) return true;
40+
}
41+
42+
function isTopScope(context) {
43+
for (;;) {
44+
if (!context || context.type == "top") return true;
45+
if (context.type == "}" && context.prev.type != "namespace") return false;
46+
context = context.prev;
47+
}
48+
}
49+
1450
CodeMirror.defineMode("clike", function(config, parserConfig) {
1551
var indentUnit = config.indentUnit,
1652
statementIndentUnit = parserConfig.statementIndentUnit || indentUnit,
@@ -111,42 +147,6 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
111147
return "comment";
112148
}
113149

114-
function Context(indented, column, type, align, prev) {
115-
this.indented = indented;
116-
this.column = column;
117-
this.type = type;
118-
this.align = align;
119-
this.prev = prev;
120-
}
121-
function isStatement(type) {
122-
return type == "statement" || type == "switchstatement" || type == "namespace";
123-
}
124-
function pushContext(state, col, type) {
125-
var indent = state.indented;
126-
if (state.context && isStatement(state.context.type) && !isStatement(type))
127-
indent = state.context.indented;
128-
return state.context = new Context(indent, col, type, null, state.context);
129-
}
130-
function popContext(state) {
131-
var t = state.context.type;
132-
if (t == ")" || t == "]" || t == "}")
133-
state.indented = state.context.indented;
134-
return state.context = state.context.prev;
135-
}
136-
137-
function typeBefore(stream, state) {
138-
if (state.prevToken == "variable" || state.prevToken == "variable-3") return true;
139-
if (/\S(?:[^- ]>|[*\]])\s*$|\*$/.test(stream.string.slice(0, stream.start))) return true;
140-
}
141-
142-
function isTopScope(context) {
143-
for (;;) {
144-
if (!context || context.type == "top") return true;
145-
if (context.type == "}" && context.prev.type != "namespace") return false;
146-
context = context.prev;
147-
}
148-
}
149-
150150
// Interface
151151

152152
return {
@@ -497,7 +497,7 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
497497
),
498498
types: words(
499499
"AnyVal App Application Array BufferedIterator BigDecimal BigInt Char Console Either " +
500-
"Enumeration Equiv Error Exception Fractional Function IndexedSeq Integral Iterable " +
500+
"Enumeration Equiv Error Exception Fractional Function IndexedSeq Int Integral Iterable " +
501501
"Iterator List Map Numeric Nil NotNull Option Ordered Ordering PartialFunction PartialOrdering " +
502502
"Product Proxy Range Responder Seq Serializable Set Specializable Stream StringBuilder " +
503503
"StringContext Symbol Throwable Traversable TraversableOnce Tuple Unit Vector " +
@@ -527,6 +527,15 @@ CodeMirror.defineMode("clike", function(config, parserConfig) {
527527
"'": function(stream) {
528528
stream.eatWhile(/[\w\$_\xa1-\uffff]/);
529529
return "atom";
530+
},
531+
"=": function(stream, state) {
532+
var cx = state.context
533+
if (cx.type == "}" && cx.align && stream.eat(">")) {
534+
state.context = new Context(cx.indented, cx.column, cx.type, null, cx.prev)
535+
return "operator"
536+
} else {
537+
return false
538+
}
530539
}
531540
},
532541
modeProps: {closeBrackets: {triples: '"'}}

0 commit comments

Comments
 (0)