diff --git a/src/services/formatting/rulesMap.ts b/src/services/formatting/rulesMap.ts index 5b4eacd2c2a..d1f6e4724f7 100644 --- a/src/services/formatting/rulesMap.ts +++ b/src/services/formatting/rulesMap.ts @@ -6,38 +6,20 @@ namespace ts.formatting { public map: RulesBucket[]; public mapRowLength: number; - constructor() { - this.map = []; - this.mapRowLength = 0; - } - - static create(rules: Rule[]): RulesMap { - const result = new RulesMap(); - result.Initialize(rules); - return result; - } - - public Initialize(rules: Rule[]) { + constructor(rules: ReadonlyArray) { this.mapRowLength = SyntaxKind.LastToken + 1; - this.map = new Array(this.mapRowLength * this.mapRowLength); // new Array(this.mapRowLength * this.mapRowLength); + this.map = new Array(this.mapRowLength * this.mapRowLength); // This array is used only during construction of the rulesbucket in the map - const rulesBucketConstructionStateList: RulesBucketConstructionState[] = new Array(this.map.length); // new Array(this.map.length); - - this.FillRules(rules, rulesBucketConstructionStateList); - return this.map; - } - - public FillRules(rules: Rule[], rulesBucketConstructionStateList: RulesBucketConstructionState[]): void { - rules.forEach((rule) => { + const rulesBucketConstructionStateList: RulesBucketConstructionState[] = new Array(this.map.length); + for (const rule of rules) { this.FillRule(rule, rulesBucketConstructionStateList); - }); + } } private GetRuleBucketIndex(row: number, column: number): number { Debug.assert(row <= SyntaxKind.LastKeyword && column <= SyntaxKind.LastKeyword, "Must compute formatting context from tokens"); - const rulesBucketIndex = (row * this.mapRowLength) + column; - return rulesBucketIndex; + return (row * this.mapRowLength) + column; } private FillRule(rule: Rule, rulesBucketConstructionStateList: RulesBucketConstructionState[]): void { @@ -57,7 +39,7 @@ namespace ts.formatting { }); } - public GetRule(context: FormattingContext): Rule { + public GetRule(context: FormattingContext): Rule | undefined { const bucketIndex = this.GetRuleBucketIndex(context.currentTokenSpan.kind, context.nextTokenSpan.kind); const bucket = this.map[bucketIndex]; if (bucket) { @@ -74,7 +56,7 @@ namespace ts.formatting { const MaskBitSize = 5; const Mask = 0x1f; - export enum RulesPosition { + enum RulesPosition { IgnoreRulesSpecific = 0, IgnoreRulesAny = MaskBitSize * 1, ContextRulesSpecific = MaskBitSize * 2, diff --git a/src/services/formatting/rulesProvider.ts b/src/services/formatting/rulesProvider.ts index 1dd7acbdc64..fcf08541890 100644 --- a/src/services/formatting/rulesProvider.ts +++ b/src/services/formatting/rulesProvider.ts @@ -10,7 +10,7 @@ namespace ts.formatting { constructor() { this.globalRules = new Rules(); const activeRules = this.globalRules.HighPriorityCommonRules.concat(this.globalRules.UserConfigurableRules).concat(this.globalRules.LowPriorityCommonRules); - this.rulesMap = RulesMap.create(activeRules); + this.rulesMap = new RulesMap(activeRules); } public getRulesMap() {