6f3ff5e639bc10a09700b2bb9bbbf18c1045635f24ee61e97f12d545d7d2a0329e46396c37a22562a8a31f70876cb2bc39174d9e3c03dd042ae0023ce176c2 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. import { Emitter } from '../../../../base/common/event.js';
  6. import { Disposable } from '../../../../base/common/lifecycle.js';
  7. import { Range } from '../../core/range.js';
  8. import { editorBracketHighlightingForeground1, editorBracketHighlightingForeground2, editorBracketHighlightingForeground3, editorBracketHighlightingForeground4, editorBracketHighlightingForeground5, editorBracketHighlightingForeground6, editorBracketHighlightingUnexpectedBracketForeground } from '../../core/editorColorRegistry.js';
  9. import { registerThemingParticipant } from '../../../../platform/theme/common/themeService.js';
  10. export class ColorizedBracketPairsDecorationProvider extends Disposable {
  11. constructor(textModel) {
  12. super();
  13. this.textModel = textModel;
  14. this.colorProvider = new ColorProvider();
  15. this.onDidChangeEmitter = new Emitter();
  16. this.onDidChange = this.onDidChangeEmitter.event;
  17. this.colorizationOptions = textModel.getOptions().bracketPairColorizationOptions;
  18. this._register(textModel.bracketPairs.onDidChange(e => {
  19. this.onDidChangeEmitter.fire();
  20. }));
  21. }
  22. //#region TextModel events
  23. handleDidChangeOptions(e) {
  24. this.colorizationOptions = this.textModel.getOptions().bracketPairColorizationOptions;
  25. }
  26. //#endregion
  27. getDecorationsInRange(range, ownerId, filterOutValidation) {
  28. if (ownerId === undefined) {
  29. return [];
  30. }
  31. if (!this.colorizationOptions.enabled) {
  32. return [];
  33. }
  34. const result = new Array();
  35. const bracketsInRange = this.textModel.bracketPairs.getBracketsInRange(range);
  36. for (const bracket of bracketsInRange) {
  37. result.push({
  38. id: `bracket${bracket.range.toString()}-${bracket.nestingLevel}`,
  39. options: {
  40. description: 'BracketPairColorization',
  41. inlineClassName: this.colorProvider.getInlineClassName(bracket, this.colorizationOptions.independentColorPoolPerBracketType),
  42. },
  43. ownerId: 0,
  44. range: bracket.range,
  45. });
  46. }
  47. return result;
  48. }
  49. getAllDecorations(ownerId, filterOutValidation) {
  50. if (ownerId === undefined) {
  51. return [];
  52. }
  53. if (!this.colorizationOptions.enabled) {
  54. return [];
  55. }
  56. return this.getDecorationsInRange(new Range(1, 1, this.textModel.getLineCount(), 1), ownerId, filterOutValidation);
  57. }
  58. }
  59. class ColorProvider {
  60. constructor() {
  61. this.unexpectedClosingBracketClassName = 'unexpected-closing-bracket';
  62. }
  63. getInlineClassName(bracket, independentColorPoolPerBracketType) {
  64. if (bracket.isInvalid) {
  65. return this.unexpectedClosingBracketClassName;
  66. }
  67. return this.getInlineClassNameOfLevel(independentColorPoolPerBracketType ? bracket.nestingLevelOfEqualBracketType : bracket.nestingLevel);
  68. }
  69. getInlineClassNameOfLevel(level) {
  70. // To support a dynamic amount of colors up to 6 colors,
  71. // we use a number that is a lcm of all numbers from 1 to 6.
  72. return `bracket-highlighting-${level % 30}`;
  73. }
  74. }
  75. registerThemingParticipant((theme, collector) => {
  76. const colors = [
  77. editorBracketHighlightingForeground1,
  78. editorBracketHighlightingForeground2,
  79. editorBracketHighlightingForeground3,
  80. editorBracketHighlightingForeground4,
  81. editorBracketHighlightingForeground5,
  82. editorBracketHighlightingForeground6
  83. ];
  84. const colorProvider = new ColorProvider();
  85. collector.addRule(`.monaco-editor .${colorProvider.unexpectedClosingBracketClassName} { color: ${theme.getColor(editorBracketHighlightingUnexpectedBracketForeground)}; }`);
  86. const colorValues = colors
  87. .map(c => theme.getColor(c))
  88. .filter((c) => !!c)
  89. .filter(c => !c.isTransparent());
  90. for (let level = 0; level < 30; level++) {
  91. const color = colorValues[level % colorValues.length];
  92. collector.addRule(`.monaco-editor .${colorProvider.getInlineClassNameOfLevel(level)} { color: ${color}; }`);
  93. }
  94. });