Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/languages/supports/indentRules.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IndentationRule } from '../languageConfiguration.js';
7
8
export const enum IndentConsts {
9
INCREASE_MASK = 0b00000001,
10
DECREASE_MASK = 0b00000010,
11
INDENT_NEXTLINE_MASK = 0b00000100,
12
UNINDENT_MASK = 0b00001000,
13
}
14
15
function resetGlobalRegex(reg: RegExp) {
16
if (reg.global) {
17
reg.lastIndex = 0;
18
}
19
20
return true;
21
}
22
23
export class IndentRulesSupport {
24
25
private readonly _indentationRules: IndentationRule;
26
27
constructor(indentationRules: IndentationRule) {
28
this._indentationRules = indentationRules;
29
}
30
31
public shouldIncrease(text: string): boolean {
32
if (this._indentationRules) {
33
if (this._indentationRules.increaseIndentPattern && resetGlobalRegex(this._indentationRules.increaseIndentPattern) && this._indentationRules.increaseIndentPattern.test(text)) {
34
return true;
35
}
36
// if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(text)) {
37
// return true;
38
// }
39
}
40
return false;
41
}
42
43
public shouldDecrease(text: string): boolean {
44
if (this._indentationRules && this._indentationRules.decreaseIndentPattern && resetGlobalRegex(this._indentationRules.decreaseIndentPattern) && this._indentationRules.decreaseIndentPattern.test(text)) {
45
return true;
46
}
47
return false;
48
}
49
50
public shouldIndentNextLine(text: string): boolean {
51
if (this._indentationRules && this._indentationRules.indentNextLinePattern && resetGlobalRegex(this._indentationRules.indentNextLinePattern) && this._indentationRules.indentNextLinePattern.test(text)) {
52
return true;
53
}
54
55
return false;
56
}
57
58
public shouldIgnore(text: string): boolean {
59
// the text matches `unIndentedLinePattern`
60
if (this._indentationRules && this._indentationRules.unIndentedLinePattern && resetGlobalRegex(this._indentationRules.unIndentedLinePattern) && this._indentationRules.unIndentedLinePattern.test(text)) {
61
return true;
62
}
63
64
return false;
65
}
66
67
public getIndentMetadata(text: string): number {
68
let ret = 0;
69
if (this.shouldIncrease(text)) {
70
ret += IndentConsts.INCREASE_MASK;
71
}
72
if (this.shouldDecrease(text)) {
73
ret += IndentConsts.DECREASE_MASK;
74
}
75
if (this.shouldIndentNextLine(text)) {
76
ret += IndentConsts.INDENT_NEXTLINE_MASK;
77
}
78
if (this.shouldIgnore(text)) {
79
ret += IndentConsts.UNINDENT_MASK;
80
}
81
return ret;
82
}
83
}
84
85