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
3296 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 { ICodeEditor } from '../../../browser/editorBrowser.js';
8
import { EditorAction, IActionOptions, registerEditorAction, ServicesAccessor } from '../../../browser/editorExtensions.js';
9
import { EditorOption } from '../../../common/config/editorOptions.js';
10
import { Range } from '../../../common/core/range.js';
11
import { ICommand } from '../../../common/editorCommon.js';
12
import { EditorContextKeys } from '../../../common/editorContextKeys.js';
13
import { ILanguageConfigurationService } from '../../../common/languages/languageConfigurationRegistry.js';
14
import { BlockCommentCommand } from './blockCommentCommand.js';
15
import { LineCommentCommand, Type } from './lineCommentCommand.js';
16
import * as nls from '../../../../nls.js';
17
import { MenuId } from '../../../../platform/actions/common/actions.js';
18
import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.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
});
99
}
100
}
101
102
class AddLineCommentAction extends CommentLineAction {
103
constructor() {
104
super(Type.ForceAdd, {
105
id: 'editor.action.addCommentLine',
106
label: nls.localize2('comment.line.add', "Add Line Comment"),
107
precondition: EditorContextKeys.writable,
108
kbOpts: {
109
kbExpr: EditorContextKeys.editorTextFocus,
110
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.KeyC),
111
weight: KeybindingWeight.EditorContrib
112
}
113
});
114
}
115
}
116
117
class RemoveLineCommentAction extends CommentLineAction {
118
constructor() {
119
super(Type.ForceRemove, {
120
id: 'editor.action.removeCommentLine',
121
label: nls.localize2('comment.line.remove', "Remove Line Comment"),
122
precondition: EditorContextKeys.writable,
123
kbOpts: {
124
kbExpr: EditorContextKeys.editorTextFocus,
125
primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KeyK, KeyMod.CtrlCmd | KeyCode.KeyU),
126
weight: KeybindingWeight.EditorContrib
127
}
128
});
129
}
130
}
131
132
class BlockCommentAction extends EditorAction {
133
134
constructor() {
135
super({
136
id: 'editor.action.blockComment',
137
label: nls.localize2('comment.block', "Toggle Block Comment"),
138
precondition: EditorContextKeys.writable,
139
kbOpts: {
140
kbExpr: EditorContextKeys.editorTextFocus,
141
primary: KeyMod.Shift | KeyMod.Alt | KeyCode.KeyA,
142
linux: { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KeyA },
143
weight: KeybindingWeight.EditorContrib
144
},
145
menuOpts: {
146
menuId: MenuId.MenubarEditMenu,
147
group: '5_insert',
148
title: nls.localize({ key: 'miToggleBlockComment', comment: ['&& denotes a mnemonic'] }, "Toggle &&Block Comment"),
149
order: 2
150
}
151
});
152
}
153
154
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
155
const languageConfigurationService = accessor.get(ILanguageConfigurationService);
156
157
if (!editor.hasModel()) {
158
return;
159
}
160
161
const commentsOptions = editor.getOption(EditorOption.comments);
162
const commands: ICommand[] = [];
163
const selections = editor.getSelections();
164
for (const selection of selections) {
165
commands.push(new BlockCommentCommand(selection, commentsOptions.insertSpace, languageConfigurationService));
166
}
167
168
editor.pushUndoStop();
169
editor.executeCommands(this.id, commands);
170
editor.pushUndoStop();
171
}
172
}
173
174
registerEditorAction(ToggleCommentLineAction);
175
registerEditorAction(AddLineCommentAction);
176
registerEditorAction(RemoveLineCommentAction);
177
registerEditorAction(BlockCommentAction);
178
179