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