Skip to content

Commit a3d4675

Browse files
authored
editor.insertSpaceAfterComment setting (microsoft#41747)
editor.insertSpaceAfterComment setting
2 parents c5171c9 + 668c277 commit a3d4675

8 files changed

Lines changed: 404 additions & 239 deletions

File tree

src/vs/editor/common/config/editorOptions.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ export interface IEditorOptions {
295295
* Enable inline color decorators and color picker rendering.
296296
*/
297297
colorDecorators?: boolean;
298+
/**
299+
* Control the behaviour of comments in the editor.
300+
*/
301+
comments?: IEditorCommentsOptions;
298302
/**
299303
* Enable custom contextmenu.
300304
* Defaults to true.
@@ -992,6 +996,52 @@ class EditorAccessibilitySupport extends BaseEditorOption<EditorOption.accessibi
992996

993997
//#endregion
994998

999+
//#region comments
1000+
1001+
/**
1002+
* Configuration options for editor comments
1003+
*/
1004+
export interface IEditorCommentsOptions {
1005+
/**
1006+
* Insert a space after the line comment token and inside the block comments tokens.
1007+
* Defaults to true.
1008+
*/
1009+
insertSpace?: boolean;
1010+
}
1011+
1012+
export type EditorCommentsOptions = Readonly<Required<IEditorCommentsOptions>>;
1013+
1014+
class EditorComments extends BaseEditorOption<EditorOption.comments, EditorCommentsOptions> {
1015+
1016+
constructor() {
1017+
const defaults: EditorCommentsOptions = {
1018+
insertSpace: true,
1019+
};
1020+
super(
1021+
EditorOption.comments, 'comments', defaults,
1022+
{
1023+
'editor.comments.insertSpace': {
1024+
type: 'boolean',
1025+
default: defaults.insertSpace,
1026+
description: nls.localize('comments.insertSpace', "Controls whether a space character is inserted when commenting.")
1027+
},
1028+
}
1029+
);
1030+
}
1031+
1032+
public validate(_input: any): EditorCommentsOptions {
1033+
if (typeof _input !== 'object') {
1034+
return this.defaultValue;
1035+
}
1036+
const input = _input as IEditorCommentsOptions;
1037+
return {
1038+
insertSpace: EditorBooleanOption.boolean(input.insertSpace, this.defaultValue.insertSpace),
1039+
};
1040+
}
1041+
}
1042+
1043+
//#endregion
1044+
9951045
//#region cursorBlinking
9961046

9971047
/**
@@ -3058,6 +3108,7 @@ export const enum EditorOption {
30583108
autoSurround,
30593109
codeLens,
30603110
colorDecorators,
3111+
comments,
30613112
contextmenu,
30623113
copyWithSyntaxHighlighting,
30633114
cursorBlinking,
@@ -3161,6 +3212,7 @@ export const enum EditorOption {
31613212
* WORKAROUND: TS emits "any" for complex editor options values (anything except string, bool, enum, etc. ends up being "any")
31623213
* @monacodtsreplace
31633214
* /accessibilitySupport, any/accessibilitySupport, AccessibilitySupport/
3215+
* /comments, any/comments, EditorCommentsOptions/
31643216
* /find, any/find, EditorFindOptions/
31653217
* /fontInfo, any/fontInfo, FontInfo/
31663218
* /gotoLocation, any/gotoLocation, GoToLocationOptions/
@@ -3277,6 +3329,7 @@ export const EditorOptions = {
32773329
EditorOption.colorDecorators, 'colorDecorators', true,
32783330
{ description: nls.localize('colorDecorators', "Controls whether the editor should render the inline color decorators and color picker.") }
32793331
)),
3332+
comments: register(new EditorComments()),
32803333
contextmenu: register(new EditorBooleanOption(
32813334
EditorOption.contextmenu, 'contextmenu', true,
32823335
)),

src/vs/editor/common/standalone/standaloneEnums.ts

Lines changed: 96 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -178,101 +178,102 @@ export enum EditorOption {
178178
autoSurround = 10,
179179
codeLens = 11,
180180
colorDecorators = 12,
181-
contextmenu = 13,
182-
copyWithSyntaxHighlighting = 14,
183-
cursorBlinking = 15,
184-
cursorSmoothCaretAnimation = 16,
185-
cursorStyle = 17,
186-
cursorSurroundingLines = 18,
187-
cursorSurroundingLinesStyle = 19,
188-
cursorWidth = 20,
189-
disableLayerHinting = 21,
190-
disableMonospaceOptimizations = 22,
191-
dragAndDrop = 23,
192-
emptySelectionClipboard = 24,
193-
extraEditorClassName = 25,
194-
fastScrollSensitivity = 26,
195-
find = 27,
196-
fixedOverflowWidgets = 28,
197-
folding = 29,
198-
foldingStrategy = 30,
199-
foldingHighlight = 31,
200-
fontFamily = 32,
201-
fontInfo = 33,
202-
fontLigatures = 34,
203-
fontSize = 35,
204-
fontWeight = 36,
205-
formatOnPaste = 37,
206-
formatOnType = 38,
207-
glyphMargin = 39,
208-
gotoLocation = 40,
209-
hideCursorInOverviewRuler = 41,
210-
highlightActiveIndentGuide = 42,
211-
hover = 43,
212-
inDiffEditor = 44,
213-
letterSpacing = 45,
214-
lightbulb = 46,
215-
lineDecorationsWidth = 47,
216-
lineHeight = 48,
217-
lineNumbers = 49,
218-
lineNumbersMinChars = 50,
219-
links = 51,
220-
matchBrackets = 52,
221-
minimap = 53,
222-
mouseStyle = 54,
223-
mouseWheelScrollSensitivity = 55,
224-
mouseWheelZoom = 56,
225-
multiCursorMergeOverlapping = 57,
226-
multiCursorModifier = 58,
227-
multiCursorPaste = 59,
228-
occurrencesHighlight = 60,
229-
overviewRulerBorder = 61,
230-
overviewRulerLanes = 62,
231-
parameterHints = 63,
232-
peekWidgetFocusInlineEditor = 64,
233-
quickSuggestions = 65,
234-
quickSuggestionsDelay = 66,
235-
readOnly = 67,
236-
renderControlCharacters = 68,
237-
renderIndentGuides = 69,
238-
renderFinalNewline = 70,
239-
renderLineHighlight = 71,
240-
renderWhitespace = 72,
241-
revealHorizontalRightPadding = 73,
242-
roundedSelection = 74,
243-
rulers = 75,
244-
scrollbar = 76,
245-
scrollBeyondLastColumn = 77,
246-
scrollBeyondLastLine = 78,
247-
selectionClipboard = 79,
248-
selectionHighlight = 80,
249-
selectOnLineNumbers = 81,
250-
semanticHighlighting = 82,
251-
showFoldingControls = 83,
252-
showUnused = 84,
253-
snippetSuggestions = 85,
254-
smoothScrolling = 86,
255-
stopRenderingLineAfter = 87,
256-
suggest = 88,
257-
suggestFontSize = 89,
258-
suggestLineHeight = 90,
259-
suggestOnTriggerCharacters = 91,
260-
suggestSelection = 92,
261-
tabCompletion = 93,
262-
useTabStops = 94,
263-
wordSeparators = 95,
264-
wordWrap = 96,
265-
wordWrapBreakAfterCharacters = 97,
266-
wordWrapBreakBeforeCharacters = 98,
267-
wordWrapColumn = 99,
268-
wordWrapMinified = 100,
269-
wrappingIndent = 101,
270-
wrappingAlgorithm = 102,
271-
editorClassName = 103,
272-
pixelRatio = 104,
273-
tabFocusMode = 105,
274-
layoutInfo = 106,
275-
wrappingInfo = 107
181+
comments = 13,
182+
contextmenu = 14,
183+
copyWithSyntaxHighlighting = 15,
184+
cursorBlinking = 16,
185+
cursorSmoothCaretAnimation = 17,
186+
cursorStyle = 18,
187+
cursorSurroundingLines = 19,
188+
cursorSurroundingLinesStyle = 20,
189+
cursorWidth = 21,
190+
disableLayerHinting = 22,
191+
disableMonospaceOptimizations = 23,
192+
dragAndDrop = 24,
193+
emptySelectionClipboard = 25,
194+
extraEditorClassName = 26,
195+
fastScrollSensitivity = 27,
196+
find = 28,
197+
fixedOverflowWidgets = 29,
198+
folding = 30,
199+
foldingStrategy = 31,
200+
foldingHighlight = 32,
201+
fontFamily = 33,
202+
fontInfo = 34,
203+
fontLigatures = 35,
204+
fontSize = 36,
205+
fontWeight = 37,
206+
formatOnPaste = 38,
207+
formatOnType = 39,
208+
glyphMargin = 40,
209+
gotoLocation = 41,
210+
hideCursorInOverviewRuler = 42,
211+
highlightActiveIndentGuide = 43,
212+
hover = 44,
213+
inDiffEditor = 45,
214+
letterSpacing = 46,
215+
lightbulb = 47,
216+
lineDecorationsWidth = 48,
217+
lineHeight = 49,
218+
lineNumbers = 50,
219+
lineNumbersMinChars = 51,
220+
links = 52,
221+
matchBrackets = 53,
222+
minimap = 54,
223+
mouseStyle = 55,
224+
mouseWheelScrollSensitivity = 56,
225+
mouseWheelZoom = 57,
226+
multiCursorMergeOverlapping = 58,
227+
multiCursorModifier = 59,
228+
multiCursorPaste = 60,
229+
occurrencesHighlight = 61,
230+
overviewRulerBorder = 62,
231+
overviewRulerLanes = 63,
232+
parameterHints = 64,
233+
peekWidgetFocusInlineEditor = 65,
234+
quickSuggestions = 66,
235+
quickSuggestionsDelay = 67,
236+
readOnly = 68,
237+
renderControlCharacters = 69,
238+
renderIndentGuides = 70,
239+
renderFinalNewline = 71,
240+
renderLineHighlight = 72,
241+
renderWhitespace = 73,
242+
revealHorizontalRightPadding = 74,
243+
roundedSelection = 75,
244+
rulers = 76,
245+
scrollbar = 77,
246+
scrollBeyondLastColumn = 78,
247+
scrollBeyondLastLine = 79,
248+
selectionClipboard = 80,
249+
selectionHighlight = 81,
250+
selectOnLineNumbers = 82,
251+
semanticHighlighting = 83,
252+
showFoldingControls = 84,
253+
showUnused = 85,
254+
snippetSuggestions = 86,
255+
smoothScrolling = 87,
256+
stopRenderingLineAfter = 88,
257+
suggest = 89,
258+
suggestFontSize = 90,
259+
suggestLineHeight = 91,
260+
suggestOnTriggerCharacters = 92,
261+
suggestSelection = 93,
262+
tabCompletion = 94,
263+
useTabStops = 95,
264+
wordSeparators = 96,
265+
wordWrap = 97,
266+
wordWrapBreakAfterCharacters = 98,
267+
wordWrapBreakBeforeCharacters = 99,
268+
wordWrapColumn = 100,
269+
wordWrapMinified = 101,
270+
wrappingIndent = 102,
271+
wrappingAlgorithm = 103,
272+
editorClassName = 104,
273+
pixelRatio = 105,
274+
tabFocusMode = 106,
275+
layoutInfo = 107,
276+
wrappingInfo = 108
276277
}
277278

278279
/**

src/vs/editor/contrib/comment/blockCommentCommand.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageCo
1515
export class BlockCommentCommand implements ICommand {
1616

1717
private readonly _selection: Selection;
18+
private readonly _insertSpace: boolean;
1819
private _usedEndToken: string | null;
1920

20-
constructor(selection: Selection) {
21+
constructor(selection: Selection, insertSpace: boolean) {
2122
this._selection = selection;
23+
this._insertSpace = insertSpace;
2224
this._usedEndToken = null;
2325
}
2426

@@ -53,7 +55,7 @@ export class BlockCommentCommand implements ICommand {
5355
return true;
5456
}
5557

56-
private _createOperationsForBlockComment(selection: Range, startToken: string, endToken: string, model: ITextModel, builder: IEditOperationBuilder): void {
58+
private _createOperationsForBlockComment(selection: Range, startToken: string, endToken: string, insertSpace: boolean, model: ITextModel, builder: IEditOperationBuilder): void {
5759
const startLineNumber = selection.startLineNumber;
5860
const startColumn = selection.startColumn;
5961
const endLineNumber = selection.endLineNumber;
@@ -91,25 +93,21 @@ export class BlockCommentCommand implements ICommand {
9193

9294
if (startTokenIndex !== -1 && endTokenIndex !== -1) {
9395
// Consider spaces as part of the comment tokens
94-
if (startTokenIndex + startToken.length < startLineText.length) {
95-
if (startLineText.charCodeAt(startTokenIndex + startToken.length) === CharCode.Space) {
96-
// Pretend the start token contains a trailing space
97-
startToken = startToken + ' ';
98-
}
96+
if (insertSpace && startTokenIndex + startToken.length < startLineText.length && startLineText.charCodeAt(startTokenIndex + startToken.length) === CharCode.Space) {
97+
// Pretend the start token contains a trailing space
98+
startToken = startToken + ' ';
9999
}
100100

101-
if (endTokenIndex > 0) {
102-
if (endLineText.charCodeAt(endTokenIndex - 1) === CharCode.Space) {
103-
// Pretend the end token contains a leading space
104-
endToken = ' ' + endToken;
105-
endTokenIndex -= 1;
106-
}
101+
if (insertSpace && endTokenIndex > 0 && endLineText.charCodeAt(endTokenIndex - 1) === CharCode.Space) {
102+
// Pretend the end token contains a leading space
103+
endToken = ' ' + endToken;
104+
endTokenIndex -= 1;
107105
}
108106
ops = BlockCommentCommand._createRemoveBlockCommentOperations(
109107
new Range(startLineNumber, startTokenIndex + startToken.length + 1, endLineNumber, endTokenIndex + 1), startToken, endToken
110108
);
111109
} else {
112-
ops = BlockCommentCommand._createAddBlockCommentOperations(selection, startToken, endToken);
110+
ops = BlockCommentCommand._createAddBlockCommentOperations(selection, startToken, endToken, this._insertSpace);
113111
this._usedEndToken = ops.length === 1 ? endToken : null;
114112
}
115113

@@ -144,15 +142,15 @@ export class BlockCommentCommand implements ICommand {
144142
return res;
145143
}
146144

147-
public static _createAddBlockCommentOperations(r: Range, startToken: string, endToken: string): IIdentifiedSingleEditOperation[] {
145+
public static _createAddBlockCommentOperations(r: Range, startToken: string, endToken: string, insertSpace: boolean): IIdentifiedSingleEditOperation[] {
148146
let res: IIdentifiedSingleEditOperation[] = [];
149147

150148
if (!Range.isEmpty(r)) {
151149
// Insert block comment start
152-
res.push(EditOperation.insert(new Position(r.startLineNumber, r.startColumn), startToken + ' '));
150+
res.push(EditOperation.insert(new Position(r.startLineNumber, r.startColumn), startToken + (insertSpace ? ' ' : '')));
153151

154152
// Insert block comment end
155-
res.push(EditOperation.insert(new Position(r.endLineNumber, r.endColumn), ' ' + endToken));
153+
res.push(EditOperation.insert(new Position(r.endLineNumber, r.endColumn), (insertSpace ? ' ' : '') + endToken));
156154
} else {
157155
// Insert both continuously
158156
res.push(EditOperation.replace(new Range(
@@ -176,7 +174,7 @@ export class BlockCommentCommand implements ICommand {
176174
return;
177175
}
178176

179-
this._createOperationsForBlockComment(this._selection, config.blockCommentStartToken, config.blockCommentEndToken, model, builder);
177+
this._createOperationsForBlockComment(this._selection, config.blockCommentStartToken, config.blockCommentEndToken, this._insertSpace, model, builder);
180178
}
181179

182180
public computeCursorState(model: ITextModel, helper: ICursorStateComputerData): Selection {

0 commit comments

Comments
 (0)