Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option for auto closing comments #192335

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/vs/editor/common/config/editorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ export interface IEditorOptions {
* Defaults to language defined behavior.
*/
autoClosingBrackets?: EditorAutoClosingStrategy;
/**
* Options for auto closing comments.
* Defaults to language defined behavior.
*/
autoClosingComments?: EditorAutoClosingStrategy;
/**
* Options for auto closing quotes.
* Defaults to language defined behavior.
Expand Down Expand Up @@ -5011,6 +5016,7 @@ export const enum EditorOption {
ariaLabel,
ariaRequired,
autoClosingBrackets,
autoClosingComments,
screenReaderAnnounceInlineSuggestion,
autoClosingDelete,
autoClosingOvertype,
Expand Down Expand Up @@ -5205,6 +5211,20 @@ export const EditorOptions = {
description: nls.localize('autoClosingBrackets', "Controls whether the editor should automatically close brackets after the user adds an opening bracket.")
}
)),
autoClosingComments: register(new EditorStringEnumOption(
EditorOption.autoClosingComments, 'autoClosingComments',
'languageDefined' as 'always' | 'languageDefined' | 'beforeWhitespace' | 'never',
['always', 'languageDefined', 'beforeWhitespace', 'never'] as const,
{
enumDescriptions: [
'',
nls.localize('editor.autoClosingComments.languageDefined', "Use language configurations to determine when to autoclose comments."),
nls.localize('editor.autoClosingComments.beforeWhitespace', "Autoclose comments only when the cursor is to the left of whitespace."),
'',
],
description: nls.localize('autoClosingComments', "Controls whether the editor should automatically close comments after the user adds an opening comment.")
}
)),
autoClosingDelete: register(new EditorStringEnumOption(
EditorOption.autoClosingDelete, 'autoClosingDelete',
'auto' as 'always' | 'auto' | 'never',
Expand Down
31 changes: 23 additions & 8 deletions src/vs/editor/common/cursor/cursorTypeOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ITextModel } from 'vs/editor/common/model';
import { EnterAction, IndentAction, StandardAutoClosingPairConditional } from 'vs/editor/common/languages/languageConfiguration';
import { getIndentationAtPosition } from 'vs/editor/common/languages/languageConfigurationRegistry';
import { IElectricAction } from 'vs/editor/common/languages/supports/electricCharacter';
import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
import { EditorAutoClosingStrategy, EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
import { createScopedLineTokens } from 'vs/editor/common/languages/supports';
import { getIndentActionForType, getIndentForEnter, getInheritIndentForLine } from 'vs/editor/common/languages/autoIndent';
import { getEnterAction } from 'vs/editor/common/languages/enterAction';
Expand Down Expand Up @@ -565,13 +565,6 @@ export class TypeOperations {
}

private static _getAutoClosingPairClose(config: CursorConfiguration, model: ITextModel, selections: Selection[], ch: string, chIsAlreadyTyped: boolean): string | null {
const chIsQuote = isQuote(ch);
const autoCloseConfig = (chIsQuote ? config.autoClosingQuotes : config.autoClosingBrackets);
const shouldAutoCloseBefore = (chIsQuote ? config.shouldAutoCloseBefore.quote : config.shouldAutoCloseBefore.bracket);

if (autoCloseConfig === 'never') {
return null;
}

for (const selection of selections) {
if (!selection.isEmpty()) {
Expand Down Expand Up @@ -603,6 +596,28 @@ export class TypeOperations {
return null;
}

let autoCloseConfig: EditorAutoClosingStrategy;
let shouldAutoCloseBefore: (ch: string) => boolean;

const chIsQuote = isQuote(ch);
if (chIsQuote) {
autoCloseConfig = config.autoClosingQuotes;
shouldAutoCloseBefore = config.shouldAutoCloseBefore.quote;
} else {
const pairIsForComments = config.blockCommentStartToken ? pair.open.includes(config.blockCommentStartToken) : false;
if (pairIsForComments) {
autoCloseConfig = config.autoClosingComments;
shouldAutoCloseBefore = config.shouldAutoCloseBefore.comment;
} else {
autoCloseConfig = config.autoClosingBrackets;
shouldAutoCloseBefore = config.shouldAutoCloseBefore.bracket;
}
}

if (autoCloseConfig === 'never') {
return null;
}

// Sometimes, it is possible to have two auto-closing pairs that have a containment relationship
// e.g. when having [(,)] and [(*,*)]
// - when typing (, the resulting state is (|)
Expand Down
12 changes: 10 additions & 2 deletions src/vs/editor/common/cursorCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ export class CursorConfiguration {
public readonly multiCursorPaste: 'spread' | 'full';
public readonly multiCursorLimit: number;
public readonly autoClosingBrackets: EditorAutoClosingStrategy;
public readonly autoClosingComments: EditorAutoClosingStrategy;
public readonly autoClosingQuotes: EditorAutoClosingStrategy;
public readonly autoClosingDelete: EditorAutoClosingEditStrategy;
public readonly autoClosingOvertype: EditorAutoClosingEditStrategy;
public readonly autoSurround: EditorAutoSurroundStrategy;
public readonly autoIndent: EditorAutoIndentStrategy;
public readonly autoClosingPairs: AutoClosingPairs;
public readonly surroundingPairs: CharacterMap;
public readonly shouldAutoCloseBefore: { quote: (ch: string) => boolean; bracket: (ch: string) => boolean };
public readonly blockCommentStartToken: string | null;
public readonly shouldAutoCloseBefore: { quote: (ch: string) => boolean; bracket: (ch: string) => boolean; comment: (ch: string) => boolean };

private readonly _languageId: string;
private _electricChars: { [key: string]: boolean } | null;
Expand All @@ -87,6 +89,7 @@ export class CursorConfiguration {
|| e.hasChanged(EditorOption.multiCursorPaste)
|| e.hasChanged(EditorOption.multiCursorLimit)
|| e.hasChanged(EditorOption.autoClosingBrackets)
|| e.hasChanged(EditorOption.autoClosingComments)
|| e.hasChanged(EditorOption.autoClosingQuotes)
|| e.hasChanged(EditorOption.autoClosingDelete)
|| e.hasChanged(EditorOption.autoClosingOvertype)
Expand Down Expand Up @@ -125,6 +128,7 @@ export class CursorConfiguration {
this.multiCursorPaste = options.get(EditorOption.multiCursorPaste);
this.multiCursorLimit = options.get(EditorOption.multiCursorLimit);
this.autoClosingBrackets = options.get(EditorOption.autoClosingBrackets);
this.autoClosingComments = options.get(EditorOption.autoClosingComments);
this.autoClosingQuotes = options.get(EditorOption.autoClosingQuotes);
this.autoClosingDelete = options.get(EditorOption.autoClosingDelete);
this.autoClosingOvertype = options.get(EditorOption.autoClosingOvertype);
Expand All @@ -136,7 +140,8 @@ export class CursorConfiguration {

this.shouldAutoCloseBefore = {
quote: this._getShouldAutoClose(languageId, this.autoClosingQuotes, true),
bracket: this._getShouldAutoClose(languageId, this.autoClosingBrackets, false)
comment: this._getShouldAutoClose(languageId, this.autoClosingComments, false),
bracket: this._getShouldAutoClose(languageId, this.autoClosingBrackets, false),
};

this.autoClosingPairs = this.languageConfigurationService.getLanguageConfiguration(languageId).getAutoClosingPairs();
Expand All @@ -147,6 +152,9 @@ export class CursorConfiguration {
this.surroundingPairs[pair.open] = pair.close;
}
}

const commentsConfiguration = this.languageConfigurationService.getLanguageConfiguration(languageId).comments;
this.blockCommentStartToken = commentsConfiguration?.blockCommentStartToken ?? null;
}

public get electricChars() {
Expand Down