disable printWidth on prettier - prettier

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

Related

How to prevent Prettier from moving comments to new line

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.

Is there a way to use clang-format to do "indentation only"?

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

How do I control indentation of array initializers with clang-format?

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.

How to disable brackets auto closing in light table?

I start to use Light Table IDE for Clojure programming , but I can not find way to disable brackets auto closing. I suppose that something must be added to the user.behaviors file but I can not find what exactly.
This can be disabled in the user.keymap file. By default you have a map entry for adding keymappings (:+). To remove keymappings you need to add one for removing keymappings (:-). Something like this:
:- {:app {}
:editor.keys.normal {
"[" [(:editor.open-pair "[")]
"{" [(:editor.open-pair "{")]
}
}
Add more lines for other behaviours you would like to remove (open-pair, close-pair ...). Look in default.keymap to see what has been set up in :editor.keys.normal.
I don't have an answer, but I suspect the auto-paren behavior is actually implemented by CodeMirror, not Light Table per se. Maybe you'd have better luck searching for an option there?
If you find something, you might be able to enable it in Light Table by using the set-codemirror-flags behavior.

Is there anyway to rename the "Source" button to something like "HTML"?

Is there anyway to rename the "Source" button to something like "HTML", I ask this as users are confused at how to add html code using the editor?
Yes, inside of the "lang" folder you will see all of the various language files.
For my case, and probably yours, You will want to edit the file "en.js". The file is "compressed" to some degree so it may be difficult to read, but it's still not too difficult to change one string. If you do plan on changing multiple strings you will most likely want to use a service to format Javascript.
Search for the following segment of code. It was one of the very last lines in the file.
"sourcearea":{"toolbar":"Source"}
change it to
"sourcearea":{"toolbar":"HTML"}
Avoid This Method Unless Required
And as for a very unsuggested method, since you can't modify the language files for some reason, you can modify the ckeditor.js file and force a specific label.
Inside of "ckeditor.js" change the line below
a.ui.addButton("Source",{label:a.lang.sourcearea.toolbar,command:"source",toolbar:"mode,10"});
to the follow code
a.ui.addButton("Source",{label:"HTML",command:"source",toolbar:"mode,10"});
The only thing modified is the "label" value in the above line. We remove the reference to the a.language.sourcearea.toolbar and insert a string in it's place instead.