We have a custom .clang-format file in the root folder of our repository and use clang-format with -style=file for all C and C++ files.
I like to change the formatting rules of clang-format for one file only.
In particular I like to switch BinPackArguments form true to false.
I know, that I could place a .clang-format file in the corresponding folder. This option does effect all files in this folder.
Formatting it differently once is not an option, because our version control system will reject not formatted files.
I know, that I could turn off the formatter via the comment
// clang-format off
This has the drawback that the file is not formatted at all.
Is there a way to change one option for one file only (perhaps via a comment)?
Related
I want to format code using a custom style if .clang-format doesn't exist. Is there any way to do so?
I used --fallback-style but it fails.
clang-format -i --style=file --fallback-style="{BasedOnStyle: google, IndentWidth: 4, ColumnLimit: 100}" a.cpp
It raises an error:
Invalid fallback style "{BasedOnStyle: google, IndentWidth: 4, ColumnLimit: 100}
I doubt there is a command line option to use another style if .clang_format file doesn't exist.
The option -fallback-style does work only with predefined styles, which are llvm, chromium, mozilla, google, webkit, gnu, microsoft. Also there is none to skip formatiing. You can use -fallback-style=google for example.
However I would suggest other ways. Here is how clang-format finds files:
it checks for .clang-format and _clang-format files in all directories from the one which contains a file you want to format, then the parent of this directory, then the parent of this, and so on to the root directory. So there are two options:
Manually check if configuration file for clang-format exists.
Put your custom fallback style in root or home directory, so that clang-format finds your file in case it can't find it in a project
I've installed the latest clang-format plugin for Visual Studio from http://llvm.org/builds/ and I'm trying to use it with a VS solution file that's generated from CMake. I've got the following directory structure:
./
├──project/
│ ├──.clang-format
│ └──source.cpp #all sourcefiles are here
└──build/ # cmake build files
When I open the cmake-generated .sln file and press CTRL+R, CTRL+F, no formatting happens. When I set Tools->Options->LLVM/Clang->ClangFormat->Fallback Style to anything (e.g. 'LLVM') then it formats (to the default style), but not if I set fallback to 'none' - which means my .clang-format file doesn't get loaded.
I tried copying the .clang-format file all over the project directories, i.e. putting it in the project dir, sub-directories, next to the source files, in the build directory, in the build/x64 directory, etc. - but it just doesn't get picked up.
Where do I have to put the file so that it gets picked up by the plugin?
I solved this, and it had nothing to do with where you put the .clang-format file. It's fine to just put it in the root of the source directory, as I did it.
Why it didn't format at all was that a .clang-format requires either the line BasedOnStyle: LLVM (or any other default style) or to define all required parameters.
If you just have a .clang-format file with a few lines, like for example:
---
ColumnLimit: '120'
IndentWidth: '4'
Language: Cpp
Standard: Cpp11
UseTab: Never
...
then it will not do any formatting whatsoever. Probably it can't work without a full set of rules, and in this case, it doesn't know to which default settings whatsoever it should fall back to.
I am attempting to use the google.vim (and on vim.org) indent style for my c++ project, but I can't seem to load it. I installed google.vim using vundle and opened up a cpp file and noticed that my tabstops, shiftwidths, etc did not change. If you set global ts and sw parameters, are they not overwritten? How do you force a certain indention when editing a file in vim? I want everything in my vimrc file to be overwritten by the google.vim file when I edit a cpp file.
Running the :filetype command in vim reveals this information:
detection:ON plugin:ON indent:ON
The bottom line is that I don't understand how to tell vim to use my google.vim file located under ~/.vim/vundle/google.vim/indent/google.vim. From what I have read, vim detects that filetype of your current file and uses the appropriate .vim file for syntax and indentation. So would I then have to rename my google.vim file to something like cpp.vim?
In most cases I like to use a tabstop of 3 and a shiftwidth of 3, so I set these in my .vimrc file. However, when I edit a cpp file, I want all my settings to be changed from what I have set globally to what the google.vim file sets.
You must rename the file from google.vim to cpp.vim, as indicated in the description of the plugin on vim.org.
General explanation:
Adding filetype plugin indent on to your ~/.vimrc allows Vim to detect the filetype of the files you edit (usually based on the file extention) and source filetype-specific plugins and indent scripts.
The idea is simple: you edit a file with {{LANGUAGE}} filetype and Vim tries to source any ftplugin/{{LANGUAGE}}.vim and indent/{{LANGUAGE}}.vim it finds in your runtimepath.
Because the filetype of your file is cpp, Vim will blissfully ignore your google.vim indent script so… you must rename it to cpp.vim for it to work.
Or you could rename all your C++ files foobar.google and teach Vim to recognize *.google files but, well… it doesn't sound right ;-)
So I downloaded, installed, and inserted into path the clang formatting plugin. I also tested it and it works for Google (Mozilla, etc.) formatting options out of the box, yet I cannot get it working with my .clang-format file. (I've put my file into the same folder as my source file, changed its encoding into UTF-8, also tried to put it into clang install folder, add file into project, write its contents inside '{key:value}' yet formatting does not happen). So how do you feed formatting file to chrome-format extension?
My file contents:
{ BasedOnStyle: "LLVM", IndentWidth: 4 }
My file name:nm.clang-format
Go to Tools->Options->LLVM/Clang->ClangFormat and put file in the Style option field.
Then place your style file named .clang-format (this is the full filename, not an extension) either in the source file's directory or one of its parent directories. Windows Explorer won't let you create filenames with leading . so you need to go to the console for this.
If like me you got confused later on where the .clang-format was living, use procmon to track the file reads of clang-format.exe
For the record, it seems that if both "Fallback Style" and "Style" are set to "file", no formatting will happen even if the style file is at its correct location. Setting "Fallback Style" to something different than "file" (e.g. "none") helps.
In VS2019 works if the clang-format file is named as .clang-format.
It must be .clang-format, not .clang-format.txt or clang-format.txt.
I typed the source code in WordPad and saved it as addition.f90 but, unfortunately, the only options Windows provides are text files, rtf's, and so forth.
I am using the G95 compiler for Windows.
In the WordPad Save dialog, type the filename in double quotes, e.g. "addition.f90". This will override the default extension.
I suggest you use a decent text editor instead. Have a look at notepad++, VIM, GNU Emacs or similar.
To rename a file in Windows you either use the command prompt:
rename file.f90.rtf file.f90
Or you can use the Windows explorer. Make sure you set it up to display file extensions. Under Tools | Folder Options, unselect 'Hide extensions for known file types'. To rename a file, either select it and press F2, or right-click and select Rename.