Sometimes clang-format does this :
SomeType VariableName[] = {Thing1,
Thing2,
Thing3}
and sometimes clang-format does this :
SomeType VariableName[] = {
Thing1,
Thing2,
Thing3}
and a single character change can make it switch between.
Is there any way to control which it does?
I'm building from the latest git source, so the latest options are available.
According to this answer, clang-format in some step puts as much as possible on a single line, and applies the ColumnLimit on that.
That would explain the switching between behaviors.
One way to prevent this could be to set ColumnLimit to 0, with the cost of removing all automatic wrapping. I think it's worth the cost, I'm sure other don't agree.
clang-format provide a way for user to specify a single property when format the code, such as whether tab is allowed, what is the tabwidth.
You could use the following way to tell clang-format to use a customized property.
$clang-format -style="{BaseonStype: llvm, IndentWidth: 8}"
$clang-format -style=HAND_WRITTEN_FORMAT_FILENAME
$clang-format -style=llvm #builtin styles.
You could get an idea about what property you could customize in the file from line 171 to line 266.
Related
I have a typescript project formatted with prettier. My issue is when I save code like this:
const thing = await call({ // comment
a: 1
});
Prettier moves the comment to a new line which is not what I want.
const thing = await call({
// comment
a: 1
});
How do I prevent this from happening while retaining all other format settings?
EDIT: I do not want to use another formatter or // prettier-ignore. I need this done from the config file .prettierrc.json
This is a known issue with Prettier. If you're looking for the corresponding bug/feature request, it's here: https://github.com/prettier/prettier/issues/807
I'm pretty sure there is no way to configure this in .prettierrc, as prettier is opinionated. Though this kind of moving of comments to new line only happens when it appears beside opening brackets. If it really annoys you, you could change to a different linter, such as eslint, which to my knowledge does not do the above.
In my experience, you can specify certain areas of your code for prettier to ignore in the .prettierignore which uses the same syntax as a .gitignore. You can also ignore specific comments as this section of their documentation.
I have a large code base with indentation that is very inconsistent.
I finally got some buy in from developers for running a code formatter.
I would like to start with just fixing the indentation because indentation is a lighter touch than running clang-format with a style.
I like correcting indentation only because when you do the indentation only and then run git diff --ignore-space-at-eol --ignore-space-change --ignore-all-space you get zero diff lines.
I want to avoid a situation where the formatted code is somehow worse and so people avoid any future attempts at making our codebase better via something like clang-format. In our case at least we can agree on spaces-only, tabs are 4-spaces. So improving only the indentation can only be a good thing.
Eclipse has a feature to "correct indentation" (via Menu --> Source --> Correct Indentation):
Correct Indentation
Applies simple indentation rules to the current selection, or the line containing the cursor, if there is no selection
Ctrl+I
Eclipse's "Correct Indentation" does just the indentation but it is not a shell command and i want/need a shell command so that I can run the command on all the source code files.
Is there a way to use clang-format to do "indentation only"? if yes, how?
For example with space-only, 4-spaces.
Clang-format always works with a default format. You can just customize it. If you don't specify a style the clang-format default is chosen. [1],[2]
Unfortunately, you cannot necessarily fix only indentation.
In the comments to your question KamilCuk suggested to use indent probably referring to https://www.gnu.org/software/indent/
I thought about configuring a custom style which does only indentation but, while going over the style options there are unfortunately some which might alter the code-base, depending on how it looks, like AllowShortIfStatementsOnASingleLine
This disallows the co-existence of
if (a)
return ;
else {
return;
}
if (b) return ;
else {
return;
}
So, it might be possible that you find a certain configuration which works for your code-base, but this would be highly specific and brittle.
[1]
The configuration file can consist of several sections each having different Language: parameter denoting the programming language this section of the configuration is targeted at. See the description of the Language option below for the list of supported languages. The first section may have no language set, it will set the default style options for all lanugages. Configuration sections for specific language will override options set in the default section.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configuring-style-with-clang-format
[2]
This section lists the supported style options. Value type is specified for each option. For enumeration types possible values are specified both as a C++ enumeration member (with a prefix, e.g. LS_Auto), and as a value usable in the configuration (without a prefix: Auto).
BasedOnStyle (string)
The style used for all options not specifically set in the configuration.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#configurable-format-style-options
I don't have a Shell script for you that can do this on all source files, however, I use VSCode that allows me to specify the clang format fallback style in the settings.json so that every time I save my files, it applys the same formatting to each one consistently. Here is an example of my settings.json C_Cpp.clang_format_fallbackStyle that applys an indent width of 4.
"C_Cpp.clang_format_fallbackStyle": " {BasedOnStyle: Google, AllowShortCaseLabelsOnASingleLine: true, AlignConsecutiveDeclarations: true, AllowShortFunctionsOnASingleLine: All, AlignTrailingComments: true, Language: Cpp, AlwaysBreakAfterReturnType: None, PenaltyReturnTypeOnItsOwnLine: 9999, PointerAlignment: Left, SortIncludes: true, IndentWidth: 4, ColumnLimit: 0, BreakBeforeBraces: Allman, SpacesBeforeTrailingComments: 5, AlignAfterOpenBracket: true, AlignConsecutiveAssignments: true, AlignConsecutiveMacros : true}",
https://clang.llvm.org/docs/ClangFormatStyleOptions.html This documentation will explain the different parameters and values for this option. For your questions specifically, I would look at "IndentWidth" and "UseTab".
Is there a way to disable the printWidth rule warning in prettier?
I want to be able to determine my own line length for readability. In certain cases I want a line break, and in other cases I don't.
I tried this in my .prettierrc file :
{
"singleQuote": true,
"printWidth" : "off"
}
But this does not work.
The short answer is no, you cannot disable it entirely.
There are, however, a few workarounds, but they have caveats.
To quote an answer from this issue on github: https://github.com/prettier/prettier/issues/3468.
printWidth is not a rule but an input into the algorithm they use to generate their output. Meaning, it is required to be there.
One workaround is to set printWidth to a really high number, but although this will prevent lines from breaking, changing this property will affect your entire codebase, causing other lines throughout it to combine into a single line, which is most likely not desired.
Your second option is to disable prettier for a block of code with the // prettier-ignore syntax. The downside to this is that you'll disable all prettier features for this section of the code. Also, I personally don't find it very "clean" to have comments like that all over your code.
You can read about how to use the ignore functionality here: https://prettier.io/docs/en/ignore.html
If you want to disable prettier rules once, just do:
// prettier-ignore
const date = new Date() // prettier disabled on this line
I am using WebStorm to make React application.
Firstly, when I use JSX tag attribute, it automatically creates curly brace. How can I disable this option?
Secondly, when our source code was modified, many IDE shows us that this file is changed. In VSCode it's done like this:
but WebStorm is not. So I can't know whether this file has changed or not. How can I set this?
Firstly, when I use JSX tag attribute, it automatically creates curly brace. How can I disable this option?
See https://stackoverflow.com/a/46949738/783119 -- should explain the situation (so I do not repeat the same here)
Secondly, when our source code was modified, many IDE shows us that this file is changed.
Settings/Preferences
Editor | General | Editor Tabs
Enable Mark modified tabs with asterisk option.
I've decided to document the code for my project. I found this tool Doxygen online and downloaded it.
But when I try to actually create an HTML file, it just displays the project contents and none of the special comments above the function.
I tried all types of comments - /*! ... */ , /** ... */ , ///,//!
It displays only the name of a function and its definition in the .h file, like this:
How can I fix this? How can I enable Doxygen to display the special comments too?
If I understand correctly, you are just using these special markers on code comments placed in the usual way inside functions? Check that your front-end isn't using
HIDE_IN_BODY_DOCS
If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any documentation blocks found inside the body of a function. If set to NO these blocks will be appended to the function's detailed documentation block.
The default value is: NO.
You also probably want to enable
EXTRACT_ALL
If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in documentation are documented, even if no documentation was available. Private class members and static file members will be hidden unless the EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES.
Note
This will also disable the warnings about undocumented members that are normally produced when WARNINGS is set to YES.
The default value is: NO.
I haven't specifically tested whether comments in a function body count as "documentation was available", but if you don't have parameter documentation in the doxygen format, I'd definitely turn on EXTRACT_ALL.
Doxygen by itself is a very efficient tool, but you have to learn how to use it. The key is the configuration file. The command:
doxygen -g
Will generate for you a default one in the current folder. You then need to edit it, either directly using any text editor, either through a GUI program that is name doxywizard. The default options values are usually a good start, but maybe you switched something ?
This commenting style should work:
/// define foo
#define foo 42
/// a truely efficient function
void foobar();
struct A {
int b; ///< this is something
};