Path: blob/main/src/vs/editor/common/languages/supports/indentRules.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IndentationRule } from '../languageConfiguration.js';67export const enum IndentConsts {8INCREASE_MASK = 0b00000001,9DECREASE_MASK = 0b00000010,10INDENT_NEXTLINE_MASK = 0b00000100,11UNINDENT_MASK = 0b00001000,12}1314function resetGlobalRegex(reg: RegExp) {15if (reg.global) {16reg.lastIndex = 0;17}1819return true;20}2122export class IndentRulesSupport {2324private readonly _indentationRules: IndentationRule;2526constructor(indentationRules: IndentationRule) {27this._indentationRules = indentationRules;28}2930public shouldIncrease(text: string): boolean {31if (this._indentationRules) {32if (this._indentationRules.increaseIndentPattern && resetGlobalRegex(this._indentationRules.increaseIndentPattern) && this._indentationRules.increaseIndentPattern.test(text)) {33return true;34}35// if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(text)) {36// return true;37// }38}39return false;40}4142public shouldDecrease(text: string): boolean {43if (this._indentationRules && this._indentationRules.decreaseIndentPattern && resetGlobalRegex(this._indentationRules.decreaseIndentPattern) && this._indentationRules.decreaseIndentPattern.test(text)) {44return true;45}46return false;47}4849public shouldIndentNextLine(text: string): boolean {50if (this._indentationRules && this._indentationRules.indentNextLinePattern && resetGlobalRegex(this._indentationRules.indentNextLinePattern) && this._indentationRules.indentNextLinePattern.test(text)) {51return true;52}5354return false;55}5657public shouldIgnore(text: string): boolean {58// the text matches `unIndentedLinePattern`59if (this._indentationRules && this._indentationRules.unIndentedLinePattern && resetGlobalRegex(this._indentationRules.unIndentedLinePattern) && this._indentationRules.unIndentedLinePattern.test(text)) {60return true;61}6263return false;64}6566public getIndentMetadata(text: string): number {67let ret = 0;68if (this.shouldIncrease(text)) {69ret += IndentConsts.INCREASE_MASK;70}71if (this.shouldDecrease(text)) {72ret += IndentConsts.DECREASE_MASK;73}74if (this.shouldIndentNextLine(text)) {75ret += IndentConsts.INDENT_NEXTLINE_MASK;76}77if (this.shouldIgnore(text)) {78ret += IndentConsts.UNINDENT_MASK;79}80return ret;81}82}838485