45329bee51ed62a6b32940c54d99a81e9151d24e525509eab5ada27ffe64307db13a843e6ff3e422ed03facc31923701cb251666359efb3024c6e8eded3dc4 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. export class CodeActionKind {
  6. constructor(value) {
  7. this.value = value;
  8. }
  9. equals(other) {
  10. return this.value === other.value;
  11. }
  12. contains(other) {
  13. return this.equals(other) || this.value === '' || other.value.startsWith(this.value + CodeActionKind.sep);
  14. }
  15. intersects(other) {
  16. return this.contains(other) || other.contains(this);
  17. }
  18. append(part) {
  19. return new CodeActionKind(this.value + CodeActionKind.sep + part);
  20. }
  21. }
  22. CodeActionKind.sep = '.';
  23. CodeActionKind.None = new CodeActionKind('@@none@@'); // Special code action that contains nothing
  24. CodeActionKind.Empty = new CodeActionKind('');
  25. CodeActionKind.QuickFix = new CodeActionKind('quickfix');
  26. CodeActionKind.Refactor = new CodeActionKind('refactor');
  27. CodeActionKind.Source = new CodeActionKind('source');
  28. CodeActionKind.SourceOrganizeImports = CodeActionKind.Source.append('organizeImports');
  29. CodeActionKind.SourceFixAll = CodeActionKind.Source.append('fixAll');
  30. export var CodeActionTriggerSource;
  31. (function (CodeActionTriggerSource) {
  32. CodeActionTriggerSource["Refactor"] = "refactor";
  33. CodeActionTriggerSource["RefactorPreview"] = "refactor preview";
  34. CodeActionTriggerSource["Lightbulb"] = "lightbulb";
  35. CodeActionTriggerSource["Default"] = "other (default)";
  36. CodeActionTriggerSource["SourceAction"] = "source action";
  37. CodeActionTriggerSource["QuickFix"] = "quick fix action";
  38. CodeActionTriggerSource["FixAll"] = "fix all";
  39. CodeActionTriggerSource["OrganizeImports"] = "organize imports";
  40. CodeActionTriggerSource["AutoFix"] = "auto fix";
  41. CodeActionTriggerSource["QuickFixHover"] = "quick fix hover window";
  42. CodeActionTriggerSource["OnSave"] = "save participants";
  43. CodeActionTriggerSource["ProblemsView"] = "problems view";
  44. })(CodeActionTriggerSource || (CodeActionTriggerSource = {}));
  45. export function mayIncludeActionsOfKind(filter, providedKind) {
  46. // A provided kind may be a subset or superset of our filtered kind.
  47. if (filter.include && !filter.include.intersects(providedKind)) {
  48. return false;
  49. }
  50. if (filter.excludes) {
  51. if (filter.excludes.some(exclude => excludesAction(providedKind, exclude, filter.include))) {
  52. return false;
  53. }
  54. }
  55. // Don't return source actions unless they are explicitly requested
  56. if (!filter.includeSourceActions && CodeActionKind.Source.contains(providedKind)) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. export function filtersAction(filter, action) {
  62. const actionKind = action.kind ? new CodeActionKind(action.kind) : undefined;
  63. // Filter out actions by kind
  64. if (filter.include) {
  65. if (!actionKind || !filter.include.contains(actionKind)) {
  66. return false;
  67. }
  68. }
  69. if (filter.excludes) {
  70. if (actionKind && filter.excludes.some(exclude => excludesAction(actionKind, exclude, filter.include))) {
  71. return false;
  72. }
  73. }
  74. // Don't return source actions unless they are explicitly requested
  75. if (!filter.includeSourceActions) {
  76. if (actionKind && CodeActionKind.Source.contains(actionKind)) {
  77. return false;
  78. }
  79. }
  80. if (filter.onlyIncludePreferredActions) {
  81. if (!action.isPreferred) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. function excludesAction(providedKind, exclude, include) {
  88. if (!exclude.contains(providedKind)) {
  89. return false;
  90. }
  91. if (include && exclude.contains(include)) {
  92. // The include is more specific, don't filter out
  93. return false;
  94. }
  95. return true;
  96. }
  97. export class CodeActionCommandArgs {
  98. constructor(kind, apply, preferred) {
  99. this.kind = kind;
  100. this.apply = apply;
  101. this.preferred = preferred;
  102. }
  103. static fromUser(arg, defaults) {
  104. if (!arg || typeof arg !== 'object') {
  105. return new CodeActionCommandArgs(defaults.kind, defaults.apply, false);
  106. }
  107. return new CodeActionCommandArgs(CodeActionCommandArgs.getKindFromUser(arg, defaults.kind), CodeActionCommandArgs.getApplyFromUser(arg, defaults.apply), CodeActionCommandArgs.getPreferredUser(arg));
  108. }
  109. static getApplyFromUser(arg, defaultAutoApply) {
  110. switch (typeof arg.apply === 'string' ? arg.apply.toLowerCase() : '') {
  111. case 'first': return "first" /* CodeActionAutoApply.First */;
  112. case 'never': return "never" /* CodeActionAutoApply.Never */;
  113. case 'ifsingle': return "ifSingle" /* CodeActionAutoApply.IfSingle */;
  114. default: return defaultAutoApply;
  115. }
  116. }
  117. static getKindFromUser(arg, defaultKind) {
  118. return typeof arg.kind === 'string'
  119. ? new CodeActionKind(arg.kind)
  120. : defaultKind;
  121. }
  122. static getPreferredUser(arg) {
  123. return typeof arg.preferred === 'boolean'
  124. ? arg.preferred
  125. : false;
  126. }
  127. }