Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/comment/browser/comment.ts
5241 views
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
6
import { KeyChord, KeyCode, KeyMod } from '../../../../base/common/keyCodes.js';
7
import * as nls from '../../../../nls.js';
8
import { MenuId } from '../../../../platform/actions/common/actions.js';
9
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
10
import { ICodeEditor } from '../../../browser/editorBrowser.js';
11
import { EditorAction, IActionOptions, registerEditorAction, ServicesAccessor } from '../../../browser/editorExtensions.js';
12
import { EditorOption } from '../../../common/config/editorOptions.js';
13
import { Range } from '../../../common/core/range.js';
14
import { ICommand } from '../../../common/editorCommon.js';
15
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
16
import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';
17
import { BlockCommentCommand } from './blockCommentCommand.js';
18
import { LineCommentCommand, Type } from './lineCommentCommand.js';
19
20
abstract class CommentLineAction extends EditorAction {
21
22
private readonly _type: Type;
23
24
constructor(type: Type, opts: IActionOptions) {
25
super(opts);
26
this._type = type;
27
}
28
29
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
30
const languageConfigurationService = accessor.get(ILanguageConfigurationService);
31
32
if (!editor.hasModel()) {
33
return;
34
}
35
36
const model = editor.getModel();
37
const commands: ICommand[] = [];
38
const modelOptions = model.getOptions();
39
const commentsOptions = editor.getOption(EditorOption.comments);
40
41
const selections = editor.getSelections().map((selection, index) => ({ selection, index, ignoreFirstLine: false }));
42
selections.sort((a, b) => Range.compareRangesUsingStarts(a.selection, b.selection));
43
44
// Remove selections that would result in copying the same line
45
let prev = selections[0];
46
for (let i = 1; i < selections.length; i++) {
47
const curr = selections[i];
48
if (prev.selection.endLineNumber === curr.selection.startLineNumber) {
49
// these two selections would copy the same line
50
if (prev.index < curr.index) {
51
// prev wins
52
curr.ignoreFirstLine = true;
53
} else {
54
// curr wins
55
prev.ignoreFirstLine = true;
56
prev = curr;
57
}
58
}
59
}
60
61
62
for (const selection of selections) {
63
commands.push(new LineCommentCommand(
64
languageConfigurationService,
65
selection.selection,
66
modelOptions.indentSize,
67
this._type,
68
commentsOptions.insertSpace,
69
commentsOptions.ignoreEmptyLines,
70
selection.ignoreFirstLine
71
));
72
}
73
74
editor.pushUndoStop();
75
editor.executeCommands(this.id, commands);
76
editor.pushUndoStop();
77
}
78
79
}
80
81
class ToggleCommentLineAction extends CommentLineAction {
82
constructor() {
83
super(Type.Toggle, {
84
id: 'editor.action.commentLine',
85
label: nls.localize2('comment.line', "Toggle Line Comment"),
86
precondition: EditorContextKeys.writable,
87
kbOpts: {
88
kbExpr: EditorContextKeys.editorTextFocus,
89
primary: KeyMod.CtrlCmd | KeyCode.Slash,
90
weight: KeybindingWeight.EditorContrib
91
},
92
menuOpts: {
93
menuId: MenuId.MenubarEditMenu,
94
group: '5_insert',
95
title: nls.localize({ key: 'miToggleLineComment', comment: ['&& denotes a mnemonic'] }, "&&Toggle Line Comment"),
96
order: 1
97
},
98
canTriggerInlineEdits: true,
99
});
100
}
101
}
102
103
class AddLineCommentAction extends CommentLineAction {
104
constructor() {
105
super(Type.ForceAdd, {
106
id: 'editor.action.addCommentLine',
107
label: nls.localize2('comment.line.add', "Add Line Comment"),
108
precondition: EditorContextKeys.writable,
109
kbOpts: {
110
kbExpr: EditorContextKeys.editorTextFocus,
111
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.KeyC),
112
weight: KeybindingWeight.EditorContrib
113
},
114
canTriggerInlineEdits: true,
115
});
116
}
117
}
118
119
class RemoveLineCommentAction extends CommentLineAction {
120
constructor() {
121
super(Type.ForceRemove, {
122
id: 'editor.action.removeCommentLine',
123
label: nls.localize2('comment.line.remove', "Remove Line Comment"),
124
precondition: EditorContextKeys.writable,
125
kbOpts: {
126
kbExpr: EditorContextKeys.editorTextFocus,
127
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.KeyU),
128
weight: KeybindingWeight.EditorContrib
129
},
130
canTriggerInlineEdits: true,
131
});
132
}
133
}
134
135
class BlockCommentAction extends EditorAction {
136
137
constructor() {
138
super({
139
id: 'editor.action.blockComment',
140
label: nls.localize2('comment.block', "Toggle Block Comment"),
141
precondition: EditorContextKeys.writable,
142
kbOpts: {
143
kbExpr: EditorContextKeys.editorTextFocus,
144
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KeyA,
145
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyA },
146
weight: KeybindingWeight.EditorContrib
147
},
148
menuOpts: {
149
menuId: MenuId.MenubarEditMenu,
150
group: '5_insert',
151
title: nls.localize({ key: 'miToggleBlockComment', comment: ['&& denotes a mnemonic'] }, "Toggle &&Block Comment"),
152
order: 2
153
},
154
canTriggerInlineEdits: true,
155
});
156
}
157
158
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
159
const languageConfigurationService = accessor.get(ILanguageConfigurationService);
160
161
if (!editor.hasModel()) {
162
return;
163
}
164
165
const commentsOptions = editor.getOption(EditorOption.comments);
166
const commands: ICommand[] = [];
167
const selections = editor.getSelections();
168
for (const selection of selections) {
169
commands.push(new BlockCommentCommand(selection, commentsOptions.insertSpace, languageConfigurationService));
170
}
171
172
editor.pushUndoStop();
173
editor.executeCommands(this.id, commands);
174
editor.pushUndoStop();
175
}
176
}
177
178
registerEditorAction(ToggleCommentLineAction);
179
registerEditorAction(AddLineCommentAction);
180
registerEditorAction(RemoveLineCommentAction);
181
registerEditorAction(BlockCommentAction);
182
183