Vscode-cpptools: Formatting while typing similar to Visual Studio IDE

Created on 21 Nov 2020  路  8Comments  路  Source: microsoft/vscode-cpptools

Type: LanguageService

Describe the bug

  • OS and Version: Linux Ubuntu 20.10
  • VS Code Version:1.51.1
  • C/C++ Extension Version: 1.1.0

Steps
E.g. 1

if(1 == 1)                                   <---- after hitting enter here,
printf("Yes\n");                             <---- next line does not autoindent

E.g. 2

if(1 == 1)                                  
    printf("Yes\n");                             <---- after manual indent, and hitting enter
    printf("No\n");                              <---- this line should not have an indent, but it does

E.g. 3

for(int i = 1; i < 10; i++){
}
   for(int k = 1; k < 10; k++){

        }       <---- typing this brace, should automatically lead to the following

for(int i = 1; i < 10; i++){
}
for(int k = 1; k < 10; k++){

}       Correct Indent!

All of these examples work and indent/format correctly automatically in Visual Studio IDE automatically.

I read through previous issue #883 but it is not clear to me whether that issue should have resolved the examples above or not.

In settings.json, I currently have

"C_Cpp.clang_format_fallbackStyle": "{BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"

I like to add that on hitting , all of these do get formatted accurately. So, this is not a bug for sure and with the above settings.json entry, formatting on hitting works just fine. This is more a question of whether this formatting can happen automatically as the user types. If that leads to a much heavier VSCode, I can understand.


Edited to add: Vim as C++ IDE with YCM and VimSpector plugins are able to correctly and automatically handle E.g. 1 and E.g. 2 above. I have not been able to get Vim to automatically handle E.g. 3 though.

Code Formatting Language Service more info needed

All 8 comments

Hi @TryerGit . The C/C++ Extension supports two format engines, clangFormat and vcFormat, settable with the "C_Cpp.formatting" setting. It sounds like you are using clang-format, as you are setting the fallbackStyle (which is only used if "C_Cpp.clang_format_style" is set to file and no .clang-format file is found).

When using clang-format, the C/C++ Extension will only format on type when ; is pressed. We ran into some issues with handling other characters on type, and are tracking this with: https://github.com/microsoft/vscode-cpptools/issues/1419

When using vcFormat, the extension will format on type when either ;, }, or Enter are pressed. However, there are some issues with this, such as:

As vcFormat has features that collide with editor.autoIndent, that setting would likely need to be disabled to get the desired behavior.

Is this issue a dupe of https://github.com/microsoft/vscode-cpptools/issues/1419 ? Does vcFormat provide the behavior you are looking for? If not, are there specific changes to these features you are suggesting?

I am able to confirm that E.g. 1 and E.g. 2 get resolved with the clangFormat and Format On Type checked. See below.

Peek 2020-11-24 10-12

However, E.g. 3 although contrived does not work completely. I agree this workflow is constructed and may not happen in normal
course of action/events.

Peek 2020-11-24 10-23

Is there a way, short of hitting Ctrl Shift i to format E.g. 3 ?

I am happy with 1 and 2 happening, so I will close this issue.

Thanks!

@TryerGit I've filed a new issue on the 3rd issue (seems like it could be a widespread formatting bug): https://github.com/microsoft/vscode-cpptools/issues/6576

@TryerGit Changing the formatOnType setting to true fixed it.

@sean-mcmanus @Colengms

Please see below an if-else construct where braces are automatically added, which, in my view should not be default.

Secondly, else does not align with if unless user manually does formatting.

I am doing this with clang-format and format on type checked/true.
Peek 2020-11-25 10-18

@TryerGit clang-format formatting mode only supports ';' for format on type. A related issue is https://github.com/microsoft/vscode-cpptools/issues/5982 .

The braces appear for else because you invoked the else snippet. VS Code invokes completion/snippets when Enter is used (I don't know how to change that). We could reduce the priority/order of the else snippet to be less than the keyword priority.

I filed a new issue to track the else issue: https://github.com/microsoft/vscode-cpptools/issues/6582

@sean-mcmanus @Colengms

I switched format style to VcFormat and when else is halftyped, it shows else keyword and else snippet is not even available. However, on pressing a single { after else (keyword), the } autocompletes.

So, this works fine for me. I have to re-evaluate why I chose clang-format over VcFormat in the first place. As of now, all of the issues I have mentioned in this thread are resolved using VcFormat and choosing else keyword instead of snippet.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

peterwoods1 picture peterwoods1  路  3Comments

SkyRiderMike picture SkyRiderMike  路  3Comments

vicatcu picture vicatcu  路  3Comments

DemoCrazer picture DemoCrazer  路  3Comments

ismadelgad11 picture ismadelgad11  路  3Comments