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

Can 'Visual Studio' format style be modified? #657

Closed
emptyVoid opened this issue Apr 25, 2017 · 19 comments
Closed

Can 'Visual Studio' format style be modified? #657

emptyVoid opened this issue Apr 25, 2017 · 19 comments
Assignees
Labels
Feature Request fixed Check the Milestone for the release in which the fix is or will be available. Language Service
Milestone

Comments

@emptyVoid
Copy link

I would like to replicate the Visual Studio 2013 formatting settings (Tools -> Options -> Text Editor -> C/C++ -> Formatting) we use in VSCode. Is it possible to modify "C_Cpp.clang_format_style": "Visual Studio" style's settings?
I've already tried to replicate them using .clang-format to no avail, cant get it to format in the same style.

@sean-mcmanus sean-mcmanus added Language Service more info needed The issue report is not actionable in its current state labels Apr 25, 2017
@sean-mcmanus
Copy link
Collaborator

Internally, the Visual Studio style setting we use is similar to "{ BasedOnStyle: LLVM, UseTab: Always, IndentWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, TabWidth: 4 }", so you could set the clang_format_style to something like that with modifications. In the pending 0.11.0 update, you can also use a custom clang_format_fallbackStyle. What formatting feature of Visual Studio are you not able to replicate with clang-format? Later clang-format versions have added more options.

@emptyVoid
Copy link
Author

First of all: not sure what I'm doing wrong, though VSCode seems to ignore .clang-format file I place in the root directory of my workspace, here's the file's contents:

BasedOnStyle: LLVM

AccessModifierOffset: -4
AllowShortIfStatementsOnASingleLine: false
BreakBeforeBraces: Allman
ColumnLimit: 0
IndentCaseLabels: false
IndentWidth: 4
SortIncludes: false
TabWidth: 4

There are no C_Cpp settings in my User Settings and I got no Workspace Settings (I'm using cpptools 0.11.0).
Please note the same file in the same place with the same workspace works just fine in Visual Studio 2013 with ClangFormat extension installed.

As per formatting features of Visual Studio: for example it lets me keep empty lines in various locations:

class Foo
{
public:

    Foo()
    {
        ...
    }
...

Whereas ClangFormat removes them.

@sean-mcmanus sean-mcmanus added external and removed more info needed The issue report is not actionable in its current state labels Apr 26, 2017
@sean-mcmanus
Copy link
Collaborator

You're on Windows? We have a known bug with .clang-format not being found on Linux/Mac, but if it's not working on Windows, then that sounds like a different problem. I just tried your settings in a .clang-format in the root of my workspace and it worked. Yeah, the empty lines thing looks like it's possibly a bug with clang-format because the MaxEmptyLinesToKeep setting isn't being used. It would probably have to be fixed in clang-format.

@emptyVoid
Copy link
Author

Oh, sorry forgot to mention, I use VSCode on Linux (Ubuntu 16.04). Is there a workaround for the said bug?

@sean-mcmanus
Copy link
Collaborator

The workaround is to change your clang_format_fallbackStyle to one of the non-Visual Studio presets, such as LLVM. That will cause clang-format itself to recognize the .clang-format file.

@emptyVoid
Copy link
Author

Thanks for the workaround!

Ok, I've compiled the major differences between Visual Studio code formatting and clang-format which affect our codebase.

First, the formatting settings for which I've found no counterparts in clang-format:

  1. Insert space before opening parenthesis of lambda argument list
  2. For empty types, move closing braces to the same line as opening braces
  3. For empty function bodies, move closing braces to the same line as opening braces
  4. Don't change spaces around assignment operators
  5. Insert space within parenthesis of ... (multiple settings allowing different spacing styles depending on the context)

Then, the formatting behavior (with no corresponding settings) which I could not replicate using clang-format:

  1. Keeps empty lines in various places
  2. Keeps multiple spaces in various places (good for alignment purposes): before macro replacement-list, in declarations
  3. Keeps spaces in angle brackets (allowing different spacing styles depending on the context)

Lastly, Visual Studio formatting is more relaxed giving some freedom to choose what should and shouldn't be enforced by the coding style; whereas clang-format's settings mostly have no "do not enforce" option.

@sean-mcmanus
Copy link
Collaborator

Okay, those seem like a lot of differences. The "Visual Studio" style we setup up was just intended as a rough approximation given the limits of clang-format. The 2 options would be to make the modifications to clang-format or to port the formatting code used in VS and use that instead of clang-format. I think the reason we choose to use clang-format for formatting was that it was cross-platform and the VS formatting code was not.

@christophe-calmejane
Copy link

Hi,

any update on this issue?
Is there a way (more than 1y later) to enable "Visual Studio" formatting in VSCode?

@bobbrow
Copy link
Member

bobbrow commented Aug 22, 2018

Unfortunately, we do not have an update on this issue. Personally, I would love to have this feature as well, but it hasn't gotten enough upvotes to make it to the top of the backlog.

@christophe-calmejane
Copy link

Where should we upvote it?

@bobbrow
Copy link
Member

bobbrow commented Aug 22, 2018

Add a +1 to the first comment in this issue.

@christophe-calmejane
Copy link

Like I just did? (I don't see yours, still you say you want this feature as well, did I miss something?) thanks!

@bobbrow
Copy link
Member

bobbrow commented Aug 22, 2018

Yes, that's correct. Since we manage the backlog, I haven't been voting on things since I can add my vote in on the fly. 😄

@sean-mcmanus
Copy link
Collaborator

It seems like this issue is tracking porting VS's formatting instead of using clang_format.

We recently added AccessModifierOffset: - to the "Visual Studio" format to more closely match VS. If anyone else has a modification we can use to our default clang_format settings to more closely match VS then let us know.

@bobbrow bobbrow added this to the 0.29.0 milestone Apr 20, 2020
@bobbrow bobbrow modified the milestones: 0.29.0, 0.30.0 May 7, 2020
@bobbrow bobbrow added the fixed Check the Milestone for the release in which the fix is or will be available. label Jul 27, 2020
@travid112
Copy link

Hey your fix broke something,
Now it is not working the .clang-format (default option) in windows 10 I just do this quick test and the extension doesn't update anything.
image

@tarik-brown
Copy link
Contributor

@travid112 Hi! Have you made sure to update the C_cpp: Formatting settings to either clangFormat or Default?

image

@sean-mcmanus
Copy link
Collaborator

sean-mcmanus commented Aug 5, 2020

@travid112 Also, are you doing Format Document or Format on Type or Format on Save? Can you provide a code sample and an example .clang-format file?

See also #5901 (i.e. set C_Cpp.clang_format_fallbackStyle to Emulated Visual Studio if you want to get the previous formatting behavior). You could also try changing the vcFormat.* settings (see #5907 ).

@sean-mcmanus
Copy link
Collaborator

sean-mcmanus commented Aug 5, 2020

@emptyVoid @christophe-calmejane https://github.com/microsoft/vscode-cpptools/releases/tag/0.30.0-insiders has support for the various Visual Studio formatting settings instead of using the "emulated" clang-format implementation. Let us know if you have any feedback on that (note: you may want to change some of the default settings, see #5907 ).

@travid112
Copy link

I actually had to install Clang formatter and configure a file to use it with VSC because it wasn't doing anything at all but well by the way I learn about it and I have it working now, thanks for the comments :)

@github-actions github-actions bot locked and limited conversation to collaborators Oct 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Feature Request fixed Check the Milestone for the release in which the fix is or will be available. Language Service
Projects
None yet
Development

No branches or pull requests

6 participants