Change only a specific option with clang-format in C++ source file - clang-format

I need to change only PointerAlignment with clang-format and leave all the other options unchanged. Is it possible?
I tried the following file:
BasedOnStyle: Google
IndentWidth: 4
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
it changes PointerAlignment, but applies Google style. Also I tried
BasedOnStyle: none
but it does not work.

Related

How to avoid formatting for XML, SQL etc. raw strings in clang-format?

I have some C++ code with embedded XML like this:
QLatin1String test()
{
return QLatin1String(R"XML(
<ui language="c++">
<widget class="Test" name="dialogbuttonbox">
<property name="text">
<string>DialogButtonBox</string>
</property>
</widget>
</ui>)XML");
}
When formatting this with
clang-format --style=WebKit test1.cpp
Then the xml will be formatted to be broken, because of the definition of RawStringFormat:
If no style has been defined in the .clang-format file for the
specific language, a predefined style given by ‘BasedOnStyle’ is used.
If ‘BasedOnStyle’ is not found, the formatting is based on llvm style.
So I tried the following within .clang-format:
RawStringFormats:
- Language: None
Delimiters: ['XML']
DisableFormat: true
Which results into the following error:
.clang-format:177:15: error: unknown enumerated scalar - Language: None
What I really want to avoid is to add comments to the source code to switch formatting off/on:
// clang-format off
// clang-format on
So my question is how to switch off formatting for XML, SQL or whatever is not supported by clang-format raw strings?
Also what about the above unknown enumerated scalar message? Does it mean that Language: None ist not allowed? So maybe I have a feature request?
I had a similar issue.
My fix was to split the XML sections into their own files.
The clang formater then doesn't touch them and your standard VS Code formater will be used.
I then needed to set the maxLineWidth to 0 which means no new lines because of column length.
.vscode/settings.json
"xml.format.maxLineWidth": 0

geany custom filetype .svrf for syntax highlighting

I have a similar issue;
I copied and edited filetype_extensions.conf in my ~/.config/geany adding:
CALIBRE=*.rul;*.svrf;*.SVRF;*.cal;
Then under ~/.config/geany/filedefs I created following files:
filetypes.CALIBRE.conf ==> my custom filetypes
filetypes.commmon ==> I wanted specific colored named_styles
# For complete documentation of this file, please see Geany's main documentation
[styling]
comment=svrf_comment
key=svrf_keyword_comment,bold
[settings]
# default extension used when saving files
extension=svrf
lexer_filetype=NONE
[keywords]
# all items must be in one line
svrf=EXT ENC INT EXPAND
# the following characters are these which a "word" can contains, see documentation
#wordchars=_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
# single comments, like / in this file
comment_single=//
# multiline comments
#comment_open=/*
#comment_close=*/
# set to false if a comment character/string should start at column 0 of a line, true uses any
# indentation of the line, e.g. setting to true causes the following on pressing CTRL+d
#command_example();
# setting to false would generate this
# command_example();
# This setting works only for single line comments
comment_use_indent=true
# context action command (please see Geany's main documentation for details)
context_action_cmd=
[indentation]
#width=4
# 0 is spaces, 1 is tabs, 2 is tab & spaces
#type=1
[build-menu]
# %f will be replaced by the complete filename
# %e will be replaced by the filename without extension
# (use only one of it at one time)
#FT_02_LB=_Lint
#FT_02_CM=jshint "%f"
#FT_02_WD=
#error_regex=([^:]+): line ([0-9]+), col ([0-9]+)
However when I open an svrf file type my custom filetypes is not recognized (no specific color while I chose some styling).
If I choose [styling=C] and lexer_filetype=C I am getting color for "C" code...
I also tried [styling] and lexer_filtype=NONE, but once again my custom highlight is not recognized.
I alread ready geany manual, as well as looked as some post but none of them is completely answering this subject (on the 2nd overflow link user has mapped to existing filetype hence he's not getting behavior he had wished).
geany custom filetype .sass for syntax highlighting
Geany: Syntax highlighting for custom filetype for SOME words
Do you have any idea on how to solve this issue?

splitting parameters rather than declaration

I'm trying to get clang-format to do my bidding, and stumbling on one point. I want my C function declaration to be formatted as:
sensor_t *sensor_enable_alarm(sensor_t *sensor,
float lo_thresh,
float hi_thresh);
but with my current settings, clang-format wants to format it as:
sensor_t *
sensor_enable_alarm(sensor_t *sensor, float lo_thresh, float hi_thresh);
I've initialized my .clang-format from llvm via:
clang-format -style=llvm -dump-config > .clang-format
and modified just the BinPackParameters to be false:
BinPackParameters: false
What additional tweaks to .clang-format do I need?
This can be done by setting PenaltyReturnTypeOnItsOwnLine to a larger value. The default value for LLVM style is 60. Try 200, which is the default for Google, Mozilla, and Chromium styles.
For example, clang-format -style='{BasedOnStyle: LLVM, BinPackParameters: false, PenaltyReturnTypeOnItsOwnLine: 200}' yourfile.cpp gives this output:
sensor_t *sensor_enable_alarm(sensor_t *sensor,
float lo_thresh,
float hi_thresh);
As always, see the documentation for more information. However, unfortunately, there really isn't much there to explain the "Penalty" values.

What does ellipsis at end of .clang-format mean?

When running:
$ clang-format -style=Google -dump-config > .clang-format
The file is appended by a ellipsis (...).
TabWidth: 8
UseTab: Never
...
Does it have any significance? Can I delete it? Someone is asking what it means in a code review.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html says:
The .clang-format file uses YAML format
http://www.yaml.org/refcard.html says:
'...': Document terminator.
Some more from http://yaml.org/spec/1.2/spec.html:
YAML uses three dashes (“---”) to separate directives from document
content. This also serves to signal the start of a document if no
directives are present. Three dots ( “...”) indicate the end of a
document without starting a new one, for use in communication
channels.

Atom text editor remove trailing whitespace on save

I use Sublime text. Now I am trying Atom. When I save any file in sublime text it does not include any trailing blank line. But saving any file in Atom leaves a trailing blank line. How do I force Atom not to leave trailing white spaces?
Under your Atom Preferences go to Packages tab and search for whitespace. Click on the whitespace package and uncheck Ensure Single Trailing Newline option
On global level this can be changed using settings in Whitespace package, but if you want to disable it for a specific language you have to use syntax-scoped properties in your config.cson.
'.text.html.php': # php overrides
whitespace:
ensureSingleTrailingNewline: false
removeTrailingWhitespace: false
'.source.ruby': # ruby overrides
whitespace:
ensureSingleTrailingNewline: false
removeTrailingWhitespace: false
To see the scope of language go to Packages tab and search for your language.
Click on the settings of the language package and you can see the scope:
Go to packages and find "whitespace", go to it's settings and uncheck the last checkbox.
Settings
Checkbox
To add to Dan Moldavan's answer.
I experienced this issue when working on a Rails Application.
I added a .editorconfig file with the following properties:
# editorconfig.org
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false
And I added a .gitattributes file with the following properties:
# Enforce Unix newlines
* text=auto eol=lf
And then my Atom Editor threw a problem:
1 problem affecting .gitattributes
whitespace: It is possible that the "whitespace"-package prevents the following properties from working reliably: insert_final_newline, trim_trailing_whitespace. You may try reconfiguring or disabling the "whitespace"-package to solve regarding issues.
Here's how I fixed it:
Open your Atom Editor
Go to Edit > Preferences > Packages
Type in whitespace
Click on the package that shows up
Untick the following:
Ensure Single Trailing Newline
Ignore Whitespace On Current Line
Leave Ignore Whitespace Only Lines unticked
Save and close the settings.
That's all.
I hope this helps

Resources