Autoformat doesn't work when I add .clang-format file

I want to change the autoformat per the clang guidelines and even if my code has only
TabWidth: 4
in it then the autoformatter simply does nothing. I use windows and put it in the .arduinoIDE and since it stops it from working, I'm guessing it reading it, but nothing happens when I use Ctrl T. Remove the file and it totally re-formats my code :frowning: So it works, just not when I try to configure it.

Hi @simonwakley. I'm going to ask you to attach the .clang-format file you are using to a forum reply here. It might be there is something about the file that is causing this problem.

I'll provide instructions:

  1. Rename the file from .clang-format to .clang-format.txt
    This is necessary because the forum software only allows attachments with certain file extensions.
  2. Open a forum reply here by clicking the Reply button.
  3. Click the "Upload" icon (image) on the post composer toolbar:
    image
  4. Select the .txt file that contains the output.
  5. Click the Open button.
  6. Click the Reply button to publish the post.

Alternatively, instead of using the "Upload" icon on the post composer toolbar as described in steps (3) - (5) above, you can simply drag and drop the .txt file onto the post composer field to attach it.

Here's the file - just a couple of simple lines in it. Surely it there's a issue it should say something... I am assuming that I can change just those things I want to in this file.
.clang-format.txt (41 Bytes)

I agree that it would be nice.

Why don't you get / create the default one and modify it? Instructions can be found here: Clang setup; where is .clang-format.

As you know, assumption is the mother of all evil :wink:

I installed your file and then started the Arduino IDE from the command line in order to get a convenient log output printed in the terminal as I use the IDE.

I can see the error when I do an Auto Format:

root INFO Formatting file:///c%3A/Users/per/Documents/Arduino/foo/foo.ino [Range: {"startLineNumber":1,"startColumn":1,"endLineNumber":5,"endColumn":1}]
root ERROR Error: Error executing "C:\ide 2\tip\142-f3ef95c\resources\app\node_modules\arduino-ide-extension\build\clang-format.exe" -style=file:"\Users\per\.arduinoIDE\.clang-format": \Users\per\.arduinoIDE\.clang-format:5:1: error: Found invalid tab character in indentation

^^^^^^^^
\Users\per\.arduinoIDE\.clang-format:4:1: error: Map value must not be empty
IndentWidth: 4
^~~~~~~~~~~
Error reading \Users\per\.arduinoIDE\.clang-format: invalid argument
    at ChildProcess.<anonymous> (C:\ide 2\tip\142-f3ef95c\resources\app\node_modules\arduino-ide-extension\lib\node\exec-util.js:53:31)
    at ChildProcess.emit (node:events:394:28)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
root ERROR Request format failed with error: Error executing "C:\ide 2\tip\142-f3ef95c\resources\app\node_modules\arduino-ide-extension\build\clang-format.exe" -style=file:"\Users\per\.arduinoIDE\.clang-format": \Users\per\.arduinoIDE\.clang-format:5:1: error: Found invalid tab character in indentation

^^^^^^^^
\Users\per\.arduinoIDE\.clang-format:4:1: error: Map value must not be empty
IndentWidth: 4
^~~~~~~~~~~
Error reading \Users\per\.arduinoIDE\.clang-format: invalid argument
root ERROR Failed to execute command:

The cause of this error is a stray tab on the last line in your .clang-format file. This file is written in the YAML language, which uses whitespace for structure (similar to Python), so you have to be careful about your use of whitespace.

After removing that tab, and trying another auto format, we get to the next error:

root INFO Formatting file:///c%3A/Users/per/Documents/Arduino/foo/foo.ino [Range: {"startLineNumber":1,"startColumn":1,"endLineNumber":5,"endColumn":1}]
root ERROR Error: Error executing "C:\ide 2\tip\142-f3ef95c\resources\app\node_modules\arduino-ide-extension\build\clang-format.exe" -style=file:"\Users\per\.arduinoIDE\.clang-format": \Users\per\.arduinoIDE\.clang-format:3:1: error: unknown key 'UseTabs'
UseTabs: Always
^~~~~~~
Error reading \Users\per\.arduinoIDE\.clang-format: invalid argument
    at ChildProcess.<anonymous> (C:\ide 2\tip\142-f3ef95c\resources\app\node_modules\arduino-ide-extension\lib\node\exec-util.js:53:31)
    at ChildProcess.emit (node:events:394:28)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
root ERROR Request format failed with error: Error executing "C:\ide 2\tip\142-f3ef95c\resources\app\node_modules\arduino-ide-extension\build\clang-format.exe" -style=file:"\Users\per\.arduinoIDE\.clang-format": \Users\per\.arduinoIDE\.clang-format:3:1: error: unknown key 'UseTabs'
UseTabs: Always
^~~~~~~
Error reading \Users\per\.arduinoIDE\.clang-format: invalid argument
root ERROR Failed to execute command:

The key name is UseTab, not UseTabs:

https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormatStyleOptions.html#:~:text=UseTab%20(UseTabStyle)

Remove the "s" and you have a valid ClangFormat configuration file.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.