Skip to content

"ctrl+T" changes the braces position I HATE that #94

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

Closed
Master811129 opened this issue Sep 4, 2020 · 9 comments
Closed

"ctrl+T" changes the braces position I HATE that #94

Master811129 opened this issue Sep 4, 2020 · 9 comments
Assignees
Labels
conclusion: declined Will not be worked on topic: code Related to content of the project itself topic: language server Related to the Arduino Language Server type: imperfection Perceived defect in any part of project

Comments

@Master811129
Copy link

after auto-fomating in Arduino Pro IDE. It changes the braces position (but legacy IDE doesn't):
image

@ubidefeo
Copy link

ubidefeo commented Sep 5, 2020

hi @Master811129
this is a setting in Clangd format that defaults to such behaviour

the Java IDE does not use ClangD for formatting.
I'm sure this can be tuned in the future, because everyone likes their style.
Arduino sketches are formatted according to a very specific set of rules in Astyle, so I guess that this behaviour will have to be tuned to reflect the one in the Java IDE.
Let's check wtih @per1234 and see what he has to say

@kittaakos
Copy link
Contributor

after auto-fomating in Arduino Pro IDE. It changes the braces position (but legacy IDE doesn't):

Yes, this is a reasonable request. However, we have to figure out how users can/will customize it.
Let's investigate the BraceWrappingFlags options from here. Here is an online clangd formatter with preview.

@rsora rsora transferred this issue from arduino/arduino-pro-ide Mar 1, 2021
@cmaglie cmaglie removed the type: bug label Sep 16, 2021
@rsora rsora added the type: imperfection Perceived defect in any part of project label Sep 22, 2021
@per1234 per1234 added topic: language server Related to the Arduino Language Server topic: code Related to content of the project itself labels Oct 11, 2021
@tigoe
Copy link
Member

tigoe commented Oct 29, 2021

+1 for leaving it the way the Java IDE did it, as that is what Arduino users are familiar with.

@per1234 per1234 self-assigned this Nov 12, 2021
@per1234
Copy link
Contributor

per1234 commented Jun 7, 2022

Background

Hi @Master811129. As mentioned above, we switched from the Artistic Style code formatter used by Arduino IDE 1.x to a new code formatting tool: ClangFormat.

I believe this was the right decision since ClangFormat is one of the most popular formatters and because it is actively maintained as part of the LLVM project, while Artistic Style is no longer actively maintained (note the developer even recommends ClangFormat in that statement).

Current status

As also mentioned above, at the time you wrote this the formatter was using the default configuration (since the team had not yet had time to work on a custom configuration for the beta IDE), which had significant differences from the Arduino IDE 1.x formatter configuration.

Since that time, I was given the opportunity to create a custom Arduino style configuration. I dedicated myself to making every possible effort to exactly reproduce the behavior of the IDE 1.x formatter.

Unfortunately, due to differences in the two tools, it was not possible to achieve an exact match. I'm sorry to say that the issue you reported here is one of those few remaining discrepancies.

Configuration file

The good news is that ClangFormat is very configurable (far more so than Artistic Style). It is now possible to define your own custom configuration in two different ways:

Global scope

If you add a .clang-format configuration file to either of the following locations, the Arduino IDE will always use it instead of the Arduino default configuration:

Windows

C:\Users\<username>\.arduinoIDE\

- OR -

C:\Users\<username>\AppData\Local\Arduino15\

Linux

~/.arduinoIDE/

- OR -

~/.arduino15/

macOS

~/.arduinoIDE

- OR -

~/Library/Arduino15/

Sketch scope

If you add a .clang-format configuration file to the root of a sketch, the Arduino IDE will use that configuration when formatting that sketch. This file has precedence over a global formatter configuration file.

Configuration

If you prefer "broken" braces, you can set that via the BreakBeforeBraces configuration option:

https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormatStyleOptions.html#:~:text=BreakBeforeBraces,-(BraceBreakingStyle)

❗ Please note that the custom configuration file completely overrides the Arduino default configuration, rather than merging with it. Any configuration option you don't set in your custom file will be set to the ClangFormat default value.

Here is Arduino's default configuration file in case you would like to use it as a base for your custom configuration:

---
Language: Cpp
# LLVM is the default style setting, used when a configuration option is not set here
BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: false
AlignEscapedNewlines: DontAlign
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: Empty
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: No
BinPackArguments: true
BinPackParameters: true
# Only used when "BreakBeforeBraces" set to "Custom"
BraceWrapping:
  AfterCaseLabel: false
  AfterClass: false
  AfterControlStatement: Never
  AfterEnum: false
  AfterFunction: false
  AfterNamespace: false
  #AfterObjCDeclaration:
  AfterStruct: false
  AfterUnion: false
  AfterExternBlock: false
  BeforeCatch: false
  BeforeElse: false
  BeforeLambdaBody: false
  BeforeWhile: false
  IndentBraces: false
  SplitEmptyFunction: false
  SplitEmptyRecord: false
  SplitEmptyNamespace: false
# Java-specific
#BreakAfterJavaFieldAnnotations:
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: false
ColumnLimit: 0
# "" matches none
CommentPragmas: ""
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
Cpp11BracedListStyle: false
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
# Docs say "Do not use this in config files". The default (LLVM 11.0.1) is "false".
#ExperimentalAutoDetectBinPacking:
FixNamespaceComments: false
ForEachMacros: []
IncludeBlocks: Preserve
IncludeCategories: []
# "" matches none
IncludeIsMainRegex: ""
IncludeIsMainSourceRegex: ""
IndentCaseBlocks: true
IndentCaseLabels: true
IndentExternBlock: Indent
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
# Java-specific
#JavaImportGroups:
# JavaScript-specific
#JavaScriptQuotes:
#JavaScriptWrapImports
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ""
MacroBlockEnd: ""
# Set to a large number to effectively disable
MaxEmptyLinesToKeep: 100000
NamespaceIndentation: None
NamespaceMacros: []
# Objective C-specific
#ObjCBinPackProtocolList:
#ObjCBlockIndentWidth:
#ObjCBreakBeforeNestedBlockParam:
#ObjCSpaceAfterProperty:
#ObjCSpaceBeforeProtocolList
PenaltyBreakAssignment: 1
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 1
PenaltyBreakFirstLessLess: 1
PenaltyBreakString: 1
PenaltyBreakTemplateDeclaration: 1
PenaltyExcessCharacter: 1
PenaltyReturnTypeOnItsOwnLine: 1
# Used as a fallback if alignment style can't be detected from code (DerivePointerAlignment: true)
PointerAlignment: Right
RawStringFormats: []
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
StatementMacros: []
TabWidth: 2
TypenameMacros: []
# Default to LF if line endings can't be detected from the content (DeriveLineEnding).
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros: []

At this point, we have done everything in our power to mitigate this issue so I will close it.

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2022
@per1234 per1234 added the conclusion: declined Will not be worked on label Jun 7, 2022
@tigoe
Copy link
Member

tigoe commented Jun 7, 2022

I didn't quite follow your description of how to switch from clang to artistic style, but I would like to request that this be made as easy as possible. The artistic style is one of the things I quite liked about the Processing style we started with, and I will miss it when I finally have to switch to 2.0.

@per1234
Copy link
Contributor

per1234 commented Jun 8, 2022

I didn't quite follow your description of how to switch from clang to artistic style

There is nothing at all the user needs to do to switch. It is integrated into the Arduino IDE 2.x. Simply use the auto format functionality just as you do with Arduino IDE 1.x.

The information I shared above is for any advanced users who happen to have their own formatting style preferences (e.g., "broken" instead of "attached" braces) and want to configure the Arduino IDE 2.x formatter to work differently than it does by default.

I would like to request that this be made as easy as possible

We plan to clearly and thoroughly document the formatter configuration system (something that was never done for Arduino IDE 1.x). I have left #42 open so that we can track that remaining task.

@tigoe
Copy link
Member

tigoe commented Jun 8, 2022

I'm still getting some strange behavior on the autoformat. Steps to repeat it on Windows 11 and 2.0.0 rc6:

  1. Type in the following (wrong spacing shown):
void setup() {
  // put your setup code here, to run once:
for (int p = 2; p < 6; p++) {
pinMode(p, OUTPUT);
}
Serial.begin(9600);
}
  1. type ctrl-T
  2. nothing happens.
  3. Switch to another app
  4. Switch back to Arduino IDE
  5. file auto-formats to the following:
void setup() {
  // put your setup code here, to run once:
  for (int p = 2; p < 6; p++) {
    pinMode(p, OUTPUT);
  }
  Serial.begin(9600);
}

So the artistic formatting seems to be back, but it doesn't do it on ctrl-T, it does it on app switching -- or perhaps on some timed file update? Can't tell, but I'll try on MacOS this afternoon.

@per1234
Copy link
Contributor

per1234 commented Jun 8, 2022

Thanks for testing @tigoe.

A very important improvement was made to the formatter system last week: #1019. That should fix the problem you encountered (the cause is explained here: #1009).

That fix is currently only available from the nightly build of the Arduino IDE. The download links to the nightly build are here:

https://github.com/arduino/arduino-ide#nightly-builds

@tigoe
Copy link
Member

tigoe commented Jun 8, 2022

Beautiful!

Test: Wrote a complex sketch with no tabs. Every few seconds it would auto-tab correctly for the artistic format. When I reached the end, I ctrl-T and it all formatted immediately and correctly.

Nice work, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: declined Will not be worked on topic: code Related to content of the project itself topic: language server Related to the Arduino Language Server type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

8 participants