comment.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 { KeyChord } from '../../../base/common/keyCodes.js';
  6. import { EditorAction, registerEditorAction } from '../../browser/editorExtensions.js';
  7. import { Range } from '../../common/core/range.js';
  8. import { EditorContextKeys } from '../../common/editorContextKeys.js';
  9. import { BlockCommentCommand } from './blockCommentCommand.js';
  10. import { LineCommentCommand } from './lineCommentCommand.js';
  11. import * as nls from '../../../nls.js';
  12. import { MenuId } from '../../../platform/actions/common/actions.js';
  13. class CommentLineAction extends EditorAction {
  14. constructor(type, opts) {
  15. super(opts);
  16. this._type = type;
  17. }
  18. run(accessor, editor) {
  19. if (!editor.hasModel()) {
  20. return;
  21. }
  22. const model = editor.getModel();
  23. const commands = [];
  24. const modelOptions = model.getOptions();
  25. const commentsOptions = editor.getOption(19 /* comments */);
  26. const selections = editor.getSelections().map((selection, index) => ({ selection, index, ignoreFirstLine: false }));
  27. selections.sort((a, b) => Range.compareRangesUsingStarts(a.selection, b.selection));
  28. // Remove selections that would result in copying the same line
  29. let prev = selections[0];
  30. for (let i = 1; i < selections.length; i++) {
  31. const curr = selections[i];
  32. if (prev.selection.endLineNumber === curr.selection.startLineNumber) {
  33. // these two selections would copy the same line
  34. if (prev.index < curr.index) {
  35. // prev wins
  36. curr.ignoreFirstLine = true;
  37. }
  38. else {
  39. // curr wins
  40. prev.ignoreFirstLine = true;
  41. prev = curr;
  42. }
  43. }
  44. }
  45. for (const selection of selections) {
  46. commands.push(new LineCommentCommand(selection.selection, modelOptions.tabSize, this._type, commentsOptions.insertSpace, commentsOptions.ignoreEmptyLines, selection.ignoreFirstLine));
  47. }
  48. editor.pushUndoStop();
  49. editor.executeCommands(this.id, commands);
  50. editor.pushUndoStop();
  51. }
  52. }
  53. class ToggleCommentLineAction extends CommentLineAction {
  54. constructor() {
  55. super(0 /* Toggle */, {
  56. id: 'editor.action.commentLine',
  57. label: nls.localize('comment.line', "Toggle Line Comment"),
  58. alias: 'Toggle Line Comment',
  59. precondition: EditorContextKeys.writable,
  60. kbOpts: {
  61. kbExpr: EditorContextKeys.editorTextFocus,
  62. primary: 2048 /* CtrlCmd */ | 85 /* Slash */,
  63. weight: 100 /* EditorContrib */
  64. },
  65. menuOpts: {
  66. menuId: MenuId.MenubarEditMenu,
  67. group: '5_insert',
  68. title: nls.localize({ key: 'miToggleLineComment', comment: ['&& denotes a mnemonic'] }, "&&Toggle Line Comment"),
  69. order: 1
  70. }
  71. });
  72. }
  73. }
  74. class AddLineCommentAction extends CommentLineAction {
  75. constructor() {
  76. super(1 /* ForceAdd */, {
  77. id: 'editor.action.addCommentLine',
  78. label: nls.localize('comment.line.add', "Add Line Comment"),
  79. alias: 'Add Line Comment',
  80. precondition: EditorContextKeys.writable,
  81. kbOpts: {
  82. kbExpr: EditorContextKeys.editorTextFocus,
  83. primary: KeyChord(2048 /* CtrlCmd */ | 41 /* KeyK */, 2048 /* CtrlCmd */ | 33 /* KeyC */),
  84. weight: 100 /* EditorContrib */
  85. }
  86. });
  87. }
  88. }
  89. class RemoveLineCommentAction extends CommentLineAction {
  90. constructor() {
  91. super(2 /* ForceRemove */, {
  92. id: 'editor.action.removeCommentLine',
  93. label: nls.localize('comment.line.remove', "Remove Line Comment"),
  94. alias: 'Remove Line Comment',
  95. precondition: EditorContextKeys.writable,
  96. kbOpts: {
  97. kbExpr: EditorContextKeys.editorTextFocus,
  98. primary: KeyChord(2048 /* CtrlCmd */ | 41 /* KeyK */, 2048 /* CtrlCmd */ | 51 /* KeyU */),
  99. weight: 100 /* EditorContrib */
  100. }
  101. });
  102. }
  103. }
  104. class BlockCommentAction extends EditorAction {
  105. constructor() {
  106. super({
  107. id: 'editor.action.blockComment',
  108. label: nls.localize('comment.block', "Toggle Block Comment"),
  109. alias: 'Toggle Block Comment',
  110. precondition: EditorContextKeys.writable,
  111. kbOpts: {
  112. kbExpr: EditorContextKeys.editorTextFocus,
  113. primary: 1024 /* Shift */ | 512 /* Alt */ | 31 /* KeyA */,
  114. linux: { primary: 2048 /* CtrlCmd */ | 1024 /* Shift */ | 31 /* KeyA */ },
  115. weight: 100 /* EditorContrib */
  116. },
  117. menuOpts: {
  118. menuId: MenuId.MenubarEditMenu,
  119. group: '5_insert',
  120. title: nls.localize({ key: 'miToggleBlockComment', comment: ['&& denotes a mnemonic'] }, "Toggle &&Block Comment"),
  121. order: 2
  122. }
  123. });
  124. }
  125. run(accessor, editor) {
  126. if (!editor.hasModel()) {
  127. return;
  128. }
  129. const commentsOptions = editor.getOption(19 /* comments */);
  130. const commands = [];
  131. const selections = editor.getSelections();
  132. for (const selection of selections) {
  133. commands.push(new BlockCommentCommand(selection, commentsOptions.insertSpace));
  134. }
  135. editor.pushUndoStop();
  136. editor.executeCommands(this.id, commands);
  137. editor.pushUndoStop();
  138. }
  139. }
  140. registerEditorAction(ToggleCommentLineAction);
  141. registerEditorAction(AddLineCommentAction);
  142. registerEditorAction(RemoveLineCommentAction);
  143. registerEditorAction(BlockCommentAction);