d2594893c5c807dc12be760adc35a830a4a6a2376ce87b83596686fb34d90eacc454bc13be87ad254bfd6deaa3e46199f834efc7b4d689b208b6fb66b8c3d7 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. function resetGlobalRegex(reg) {
  6. if (reg.global) {
  7. reg.lastIndex = 0;
  8. }
  9. return true;
  10. }
  11. export class IndentRulesSupport {
  12. constructor(indentationRules) {
  13. this._indentationRules = indentationRules;
  14. }
  15. shouldIncrease(text) {
  16. if (this._indentationRules) {
  17. if (this._indentationRules.increaseIndentPattern && resetGlobalRegex(this._indentationRules.increaseIndentPattern) && this._indentationRules.increaseIndentPattern.test(text)) {
  18. return true;
  19. }
  20. // if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(text)) {
  21. // return true;
  22. // }
  23. }
  24. return false;
  25. }
  26. shouldDecrease(text) {
  27. if (this._indentationRules && this._indentationRules.decreaseIndentPattern && resetGlobalRegex(this._indentationRules.decreaseIndentPattern) && this._indentationRules.decreaseIndentPattern.test(text)) {
  28. return true;
  29. }
  30. return false;
  31. }
  32. shouldIndentNextLine(text) {
  33. if (this._indentationRules && this._indentationRules.indentNextLinePattern && resetGlobalRegex(this._indentationRules.indentNextLinePattern) && this._indentationRules.indentNextLinePattern.test(text)) {
  34. return true;
  35. }
  36. return false;
  37. }
  38. shouldIgnore(text) {
  39. // the text matches `unIndentedLinePattern`
  40. if (this._indentationRules && this._indentationRules.unIndentedLinePattern && resetGlobalRegex(this._indentationRules.unIndentedLinePattern) && this._indentationRules.unIndentedLinePattern.test(text)) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. getIndentMetadata(text) {
  46. let ret = 0;
  47. if (this.shouldIncrease(text)) {
  48. ret += 1 /* IndentConsts.INCREASE_MASK */;
  49. }
  50. if (this.shouldDecrease(text)) {
  51. ret += 2 /* IndentConsts.DECREASE_MASK */;
  52. }
  53. if (this.shouldIndentNextLine(text)) {
  54. ret += 4 /* IndentConsts.INDENT_NEXTLINE_MASK */;
  55. }
  56. if (this.shouldIgnore(text)) {
  57. ret += 8 /* IndentConsts.UNINDENT_MASK */;
  58. }
  59. return ret;
  60. }
  61. }