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

Formatting while typing similar to Visual Studio IDE #6561

Closed
TryerGit opened this issue Nov 21, 2020 · 9 comments
Closed

Formatting while typing similar to Visual Studio IDE #6561

TryerGit opened this issue Nov 21, 2020 · 9 comments
Assignees
Labels
Feature: Code Formatting Language Service more info needed The issue report is not actionable in its current state

Comments

@TryerGit
Copy link

TryerGit commented Nov 21, 2020

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 <Ctrl+Shift+i>, 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 <Ctrl+Shift+i> 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.

@Colengms
Copy link
Contributor

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: #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 #1419 ? Does vcFormat provide the behavior you are looking for? If not, are there specific changes to these features you are suggesting?

@Colengms Colengms self-assigned this Nov 24, 2020
@Colengms Colengms added Language Service Feature: Code Formatting more info needed The issue report is not actionable in its current state labels Nov 24, 2020
@TryerGit
Copy link
Author

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!

@sean-mcmanus
Copy link
Contributor

@TryerGit I've filed a new issue on the 3rd issue (seems like it could be a widespread formatting bug): #6576

@sean-mcmanus
Copy link
Contributor

@TryerGit Changing the formatOnType setting to true fixed it.

@TryerGit
Copy link
Author

@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 TryerGit reopened this Nov 25, 2020
@sean-mcmanus
Copy link
Contributor

sean-mcmanus commented Nov 25, 2020

@TryerGit clang-format formatting mode only supports ';' for format on type. A related issue is #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.

@sean-mcmanus
Copy link
Contributor

I filed a new issue to track the else issue: #6582

@TryerGit
Copy link
Author

@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.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 10, 2021
@sean-mcmanus
Copy link
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Feature: Code Formatting Language Service more info needed The issue report is not actionable in its current state
Projects
None yet
Development

No branches or pull requests

3 participants