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

Add command to generate EditorConfig contents from VC Format settings #6264

Closed
wants to merge 10 commits into from
11 changes: 8 additions & 3 deletions Extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
"%c_cpp.configuration.vcFormat.indent.gotoLabels.leftmostColumn.description%",
"%c_cpp.configuration.vcFormat.indent.gotoLabels.none.description%"
],
"default": "none",
"default": "oneLeft",
"description": "%c_cpp.configuration.vcFormat.indent.gotoLabels.description%",
"scope": "resource"
},
Expand Down Expand Up @@ -257,10 +257,10 @@
"description": "%c_cpp.configuration.vcFormat.indent.namespaceContents.description%",
"scope": "resource"
},
"C_Cpp.vcFormat.indent.preserveComment": {
"C_Cpp.vcFormat.indent.preserveComments": {
"type": "boolean",
"default": false,
"description": "%c_cpp.configuration.vcFormat.indent.preserveComment.description%",
"description": "%c_cpp.configuration.vcFormat.indent.preserveComments.description%",
"scope": "resource"
},
"C_Cpp.vcFormat.newLine.beforeOpenBrace.namespace": {
Expand Down Expand Up @@ -1199,6 +1199,11 @@
"title": "%c_cpp.command.vcpkgOnlineHelpSuggested.title%",
"category": "C/C++"
},
{
"command": "C_Cpp.GenerateEditorConfig",
"title": "%c_cpp.command.generateEditorConfig.title%",
"category": "C/C++"
},
{
"command": "C_Cpp.referencesViewGroupByType",
"category": "C/C++",
Expand Down
3 changes: 2 additions & 1 deletion Extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"c_cpp.command.rescanWorkspace.title": "Rescan Workspace",
"c_cpp.command.vcpkgClipboardInstallSuggested.title": "Copy vcpkg install command to clipboard",
"c_cpp.command.vcpkgOnlineHelpSuggested.title": "Visit the vcpkg help page",
"c_cpp.command.generateEditorConfig.title": "Generate EditorConfig contents from VC Format settings",
"c_cpp.configuration.formatting.description": "Configures the formatting engine",
"c_cpp.configuration.formatting.clangFormat.description": "clang-format will be used to format code.",
"c_cpp.configuration.formatting.vcFormat.description": "The Visual C++ formatting engine will be used to format code.",
Expand Down Expand Up @@ -45,7 +46,7 @@
"c_cpp.configuration.vcFormat.indent.preprocessor.none.description": "Preprocessor directives will not be formatted.",
"c_cpp.configuration.vcFormat.indent.accessSpecifiers.description": "Access specifiers are indented relative to class or struct definitions by the amount specified in the Editor: Tab Size setting",
"c_cpp.configuration.vcFormat.indent.namespaceContents.description": "Code is indented relative to its enclosing namespace by the amount specified in the Editor: Tab Size setting",
"c_cpp.configuration.vcFormat.indent.preserveComment.description": "Indentation of comments is not changed during formatting operations.",
"c_cpp.configuration.vcFormat.indent.preserveComments.description": "Indentation of comments is not changed during formatting operations.",
"c_cpp.configuration.vcFormat.newLine.beforeOpenBrace.namespace.description": "The position of opening braces for namespaces",
"c_cpp.configuration.vcFormat.newLine.beforeOpenBrace.type.description": "The position of opening braces for type definitions",
"c_cpp.configuration.vcFormat.newLine.beforeOpenBrace.lambda.description": "The position of opening braces for lambda functions",
Expand Down
8 changes: 4 additions & 4 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ export class DefaultClient implements Client {
const settings_indentPreprocessor: (string | undefined)[] = [];
const settings_indentAccessSpecifiers: boolean[] = [];
const settings_indentNamespaceContents: boolean[] = [];
const settings_indentPreserveComment: boolean[] = [];
const settings_indentPreserveComments: boolean[] = [];
const settings_formattingEngine: (string | undefined)[] = [];
const settings_newLineBeforeOpenBraceNamespace: (string | undefined)[] = [];
const settings_newLineBeforeOpenBraceType: (string | undefined)[] = [];
Expand Down Expand Up @@ -988,7 +988,7 @@ export class DefaultClient implements Client {
settings_formattingEngine.push(setting.formattingEngine);
settings_indentBraces.push(setting.vcFormatIndentBraces);
settings_indentWithinParentheses.push(setting.vcFormatIndentWithinParentheses);
settings_indentPreserveWithinParentheses.push(setting.vcFormatindentPreserveWithinParentheses);
settings_indentPreserveWithinParentheses.push(setting.vcFormatIndentPreserveWithinParentheses);
settings_indentMultiLine.push(setting.vcFormatIndentMultiLineRelativeTo);
settings_indentCaseLabels.push(setting.vcFormatIndentCaseLabels);
settings_indentCaseContents.push(setting.vcFormatIndentCaseContents);
Expand All @@ -998,7 +998,7 @@ export class DefaultClient implements Client {
settings_indentPreprocessor.push(setting.vcFormatIndentPreprocessor);
settings_indentAccessSpecifiers.push(setting.vcFormatIndentAccessSpecifiers);
settings_indentNamespaceContents.push(setting.vcFormatIndentNamespaceContents);
settings_indentPreserveComment.push(setting.vcFormatIndentPreserveComment);
settings_indentPreserveComments.push(setting.vcFormatIndentPreserveComments);
settings_newLineBeforeOpenBraceNamespace.push(setting.vcFormatNewlineBeforeOpenBraceNamespace);
settings_newLineBeforeOpenBraceType.push(setting.vcFormatNewlineBeforeOpenBraceType);
settings_newLineBeforeOpenBraceFunction.push(setting.vcFormatNewlineBeforeOpenBraceFunction);
Expand Down Expand Up @@ -1108,7 +1108,7 @@ export class DefaultClient implements Client {
preprocessor : settings_indentPreprocessor,
accesSpecifiers : settings_indentAccessSpecifiers,
namespaceContents : settings_indentNamespaceContents,
preserveComment : settings_indentPreserveComment
preserveComments : settings_indentPreserveComments
},
newLine: {
beforeOpenBrace: {
Expand Down
131 changes: 131 additions & 0 deletions Extension/src/LanguageServer/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ export function registerCommands(): void {
disposables.push(vscode.commands.registerCommand('C_Cpp.referencesViewUngroupByType', onToggleRefGroupView));
disposables.push(vscode.commands.registerCommand('C_Cpp.VcpkgClipboardInstallSuggested', onVcpkgClipboardInstallSuggested));
disposables.push(vscode.commands.registerCommand('C_Cpp.VcpkgOnlineHelpSuggested', onVcpkgOnlineHelpSuggested));
disposables.push(vscode.commands.registerCommand('C_Cpp.GenerateEditorConfig', onGenerateEditorConfig));
disposables.push(vscode.commands.registerCommand('cpptools.activeConfigName', onGetActiveConfigName));
disposables.push(vscode.commands.registerCommand('cpptools.activeConfigCustomVariable', onGetActiveConfigCustomVariable));
disposables.push(vscode.commands.registerCommand('cpptools.setActiveConfigName', onSetActiveConfigName));
Expand Down Expand Up @@ -862,6 +863,136 @@ function onEditConfiguration(): void {
}
}

function mapIndentationReferenceToEditorConfig(value: string | undefined): string {
if (value !== undefined) {
// Will never actually be undefined, as these settings have default values.
if (value === "statementBegin") {
return "statement_begin";
}
if (value === "outermostParenthesis") {
return "outermost_parenthesis";
}
}
return "innermost_parenthesis";
}

function mapIndentToEditorConfig(value: string | undefined): string {
if (value !== undefined) {
// Will never actually be undefined, as these settings have default values.
if (value === "leftmostColumn") {
return "leftmost_column";
}
if (value === "oneLeft") {
return "one_left";
}
}
return "none";
}

function mapNewOrSameLineToEditorConfig(value: string | undefined): string {
if (value !== undefined) {
// Will never actually be undefined, as these settings have default values.
if (value === "newLine") {
return "new_line";
}
if (value === "sameLine") {
return "same_line";
}
}
return "ignore";
}

function mapWrapToEditorConfig(value: string | undefined): string {
if (value !== undefined) {
// Will never actually be undefined, as these settings have default values.
if (value === "allOneLineScopes") {
return "all_one_line_scopes";
}
if (value === "oneLiners") {
return "one_liners";
}
}
return "never";
}

function generateEditorConfig(rootUri?: vscode.Uri): void {
const settings: CppSettings = new CppSettings(rootUri);

let content: string = "[*]";
content += "\ncpp_indent_braces = " + settings.vcFormatIndentBraces;
content += "\ncpp_indent_multi_line_relative_to = " + mapIndentationReferenceToEditorConfig(settings.vcFormatIndentMultiLineRelativeTo);
content += "\ncpp_indent_within_parentheses = " + settings.vcFormatIndentWithinParentheses;
content += "\ncpp_indent_preserve_within_parentheses = " + settings.vcFormatIndentPreserveWithinParentheses;
content += "\ncpp_indent_case_labels = " + settings.vcFormatIndentCaseLabels;
content += "\ncpp_indent_case_contents = " + settings.vcFormatIndentCaseContents;
content += "\ncpp_indent_case_contents_when_block = " + settings.vcFormatIndentCaseContentsWhenBlock;
content += "\ncpp_indent_lambda_braces_when_parameter = " + settings.vcFormatIndentLambdaBracesWhenParameter;
content += "\ncpp_indent_goto_labels = " + mapIndentToEditorConfig(settings.vcFormatIndentGotoLables);
content += "\ncpp_indent_preprocessor = " + mapIndentToEditorConfig(settings.vcFormatIndentPreprocessor);
content += "\ncpp_indent_access_specifiers = " + settings.vcFormatIndentAccessSpecifiers;
content += "\ncpp_indent_namespace_contents = " + settings.vcFormatIndentNamespaceContents;
content += "\ncpp_indent_preserve_comments = " + settings.vcFormatIndentPreserveComments;
content += "\ncpp_new_line_before_open_brace_namespace = " + mapNewOrSameLineToEditorConfig(settings.vcFormatNewlineBeforeOpenBraceNamespace);
content += "\ncpp_new_line_before_open_brace_type = " + mapNewOrSameLineToEditorConfig(settings.vcFormatNewlineBeforeOpenBraceType);
content += "\ncpp_new_line_before_open_brace_function = " + mapNewOrSameLineToEditorConfig(settings.vcFormatNewlineBeforeOpenBraceFunction);
content += "\ncpp_new_line_before_open_brace_block = " + mapNewOrSameLineToEditorConfig(settings.vcFormatNewlineBeforeOpenBraceBlock);
content += "\ncpp_new_line_before_open_brace_lambda = " + mapNewOrSameLineToEditorConfig(settings.vcFormatNewlineBeforeOpenBraceLambda);
content += "\ncpp_new_line_scope_braces_on_separate_lines = " + settings.vcFormatNewlineScopeBracesOnSeparateLines;
content += "\ncpp_new_line_close_brace_same_line_empty_type = " + settings.vcFormatNewlineCloseBraceSameLineEmptyType;
content += "\ncpp_new_line_close_brace_same_line_empty_function = " + settings.vcFormatNewlineCloseBraceSameLineEmptyFunction;
content += "\ncpp_new_line_before_catch = " + settings.vcFormatNewlineBeforeCatch;
content += "\ncpp_new_line_before_else = " + settings.vcFormatNewlineBeforeElse;
content += "\ncpp_new_line_before_while_in_do_while = " + settings.vcFormatNewlineBeforeWhileInDoWhile;
content += "\ncpp_space_before_function_open_parenthesis = " + settings.vcFormatSpaceBeforeFunctionOpenParenthesis;
content += "\ncpp_space_within_parameter_list_parentheses = " + settings.vcFormatSpaceWithinParameterListParentheses;
content += "\ncpp_space_between_empty_parameter_list_parentheses = " + settings.vcFormatSpaceBetweenEmptyParameterListParentheses;
content += "\ncpp_space_after_keywords_in_control_flow_statements = " + settings.vcFormatSpaceAfterKeywordsInControlFlowStatements;
content += "\ncpp_space_within_control_flow_statement_parentheses = " + settings.vcFormatSpaceWithinControlFlowStatementParentheses;
content += "\ncpp_space_before_lambda_open_parenthesis = " + settings.vcFormatSpaceBeforeLambdaOpenParenthesis;
content += "\ncpp_space_within_cast_parentheses = " + settings.vcFormatSpaceWithinCastParentheses;
content += "\ncpp_space_after_cast_close_parenthesis = " + settings.vcFormatSpaceAfterCastCloseParenthesis;
content += "\ncpp_space_within_expression_parentheses = " + settings.vcFormatSpaceWithinExpressionParentheses;
content += "\ncpp_space_before_block_open_brace = " + settings.vcFormatSpaceBeforeBlockOpenBrace;
content += "\ncpp_space_between_empty_braces = " + settings.vcFormatSpaceBetweenEmptyBraces;
content += "\ncpp_space_before_initializer_list_open_brace = " + settings.vcFormatSpaceBeforeInitializerListOpenBrace;
content += "\ncpp_space_within_initializer_list_braces = " + settings.vcFormatSpaceWithinInitializerListBraces;
content += "\ncpp_space_preserve_in_initializer_list = " + settings.vcFormatSpacePreserveInInitializerList;
content += "\ncpp_space_before_open_square_bracket = " + settings.vcFormatSpaceBeforeOpenSquareBracket;
content += "\ncpp_space_within_square_brackets = " + settings.vcFormatSpaceWithinSquareBrackets;
content += "\ncpp_space_before_empty_square_brackets = " + settings.vcFormatSpaceBeforeEmptySquareBrackets;
content += "\ncpp_space_between_empty_square_brackets = " + settings.vcFormatSpaceBetweenEmptySquareBrackets;
content += "\ncpp_space_group_square_brackets = " + settings.vcFormatSpaceGroupSquareBrackets;
content += "\ncpp_space_within_lambda_brackets = " + settings.vcFormatSpaceWithinLambdaBrackets;
content += "\ncpp_space_between_empty_lambda_brackets = " + settings.vcFormatSpaceBetweenEmptyLambdaBrackets;
content += "\ncpp_space_before_comma = " + settings.vcFormatSpaceBeforeComma;
content += "\ncpp_space_after_comma = " + settings.vcFormatSpaceAfterComma;
content += "\ncpp_space_remove_around_member_operators = " + settings.vcFormatSpaceRemoveAroundMemberOperators;
content += "\ncpp_space_before_inheritance_colon = " + settings.vcFormatSpaceBeforeInheritanceColon;
content += "\ncpp_space_before_constructor_colon = " + settings.vcFormatSpaceBeforeConstructorColon;
content += "\ncpp_space_remove_before_semicolon = " + settings.vcFormatSpaceRemoveBeforeSemicolon;
content += "\ncpp_space_after_semicolon = " + settings.vcFormatSpaceInsertAfterSemicolon;
content += "\ncpp_space_remove_around_unary_operator = " + settings.vcFormatSpaceRemoveAroundUnaryOperator;
content += "\ncpp_space_around_binary_operator = " + settings.vcFormatSpaceAroundBinaryOperator;
content += "\ncpp_space_around_assignment_operator = " + settings.vcFormatSpaceAroundAssignmentOperator;
content += "\ncpp_space_pointer_reference_alignment = " + settings.vcFormatSpacePointerReferenceAlignment;
content += "\ncpp_space_around_ternary_operator = " + settings.vcFormatSpaceAroundTernaryOperator;
content += "\ncpp_wrap_preserve_blocks = " + mapWrapToEditorConfig(settings.vcFormatWrapPreserveBlocks);
content += "\n";

vscode.workspace.openTextDocument({ content }).then((document) => {
vscode.window.showTextDocument(document, 1, false);
});
}

function onGenerateEditorConfig(): void {
onActivationEvent();
if (!isFolderOpen()) {
generateEditorConfig();
} else {
selectClient().then(client => generateEditorConfig(client.RootUri));
}
}

function onAddToIncludePath(path: string): void {
if (!isFolderOpen()) {
vscode.window.showInformationMessage(localize('add.includepath.open.first', 'Open a folder first to add to {0}', "includePath"));
Expand Down
Loading