-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
mode-indent.ts
31 lines (29 loc) · 1.05 KB
/
mode-indent.ts
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
/**
* Copyright (c) 2021 GraphQL Contributors
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import CodeMirror from 'codemirror';
import { State } from 'graphql-language-service';
// Seems the electricInput type in @types/codemirror is wrong (i.e it is written all lowercase)
export default function indent(
this: CodeMirror.Mode<any> & {
electricInput?: RegExp;
config?: CodeMirror.EditorConfiguration;
},
state: State,
textAfter: string,
) {
const { levels, indentLevel } = state;
// If there is no stack of levels, use the current level.
// Otherwise, use the top level, preemptively dedenting for close braces.
const level =
!levels || levels.length === 0
? indentLevel
: levels[levels.length - 1] -
(this.electricInput?.test(textAfter) ? 1 : 0);
return (level || 0) * (this.config?.indentUnit || 0);
}