blob: 6d36ed3bd000a31e4b04c85f707d496f58e96e5f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import { TreeIndentContext } from '@codemirror/language';
/**
* Finds the `from` of first non-skipped token, if any,
* after the opening keyword in the first line of the declaration.
*
* Based on
* https://github.com/codemirror/language/blob/cd7f7e66fa51ddbce96cf9396b1b6127d0ca4c94/src/indent.ts#L246
*
* @param context the indentation context
* @returns the alignment or `null` if there is no token after the opening keyword
*/
function findAlignmentAfterOpening(context: TreeIndentContext): number | null {
const {
node: tree,
simulatedBreak,
} = context;
const openingToken = tree.childAfter(tree.from);
if (openingToken === null) {
return null;
}
const openingLine = context.state.doc.lineAt(openingToken.from);
const lineEnd = simulatedBreak == null || simulatedBreak <= openingLine.from
? openingLine.to
: Math.min(openingLine.to, simulatedBreak);
const { cursor } = openingToken;
while (cursor.next() && cursor.from < lineEnd) {
if (!cursor.type.isSkipped) {
return cursor.from;
}
}
return null;
}
/**
* Indents text after declarations by a single unit if it begins on a new line,
* otherwise it aligns with the text after the declaration.
*
* Based on
* https://github.com/codemirror/language/blob/cd7f7e66fa51ddbce96cf9396b1b6127d0ca4c94/src/indent.ts#L275
*
* @example
* Result with no hanging indent (indent unit = 2 spaces, units = 1):
* ```
* scope
* Family = 1,
* Person += 5..10.
* ```
*
* @example
* Result with hanging indent:
* ```
* scope Family = 1,
* Person += 5..10.
* ```
*
* @param context the indentation context
* @param units the number of units to indent
* @returns the desired indentation level
*/
function indentDeclarationStrategy(context: TreeIndentContext, units: number): number {
const alignment = findAlignmentAfterOpening(context);
if (alignment !== null) {
return context.column(alignment);
}
return context.baseIndent + units * context.unit;
}
export function indentBlockComment(): number {
// Do not indent.
return -1;
}
export function indentDeclaration(context: TreeIndentContext): number {
return indentDeclarationStrategy(context, 1);
}
export function indentPredicateOrRule(context: TreeIndentContext): number {
const clauseIndent = indentDeclarationStrategy(context, 1);
if (/^\s+[;.]/.exec(context.textAfter) !== null) {
return clauseIndent - 2;
}
if (/^\s+(~>)/.exec(context.textAfter) !== null) {
return clauseIndent - 3;
}
return clauseIndent;
}
|