Skip to content

Use code formatter tool directly #1009

Closed
@per1234

Description

@per1234

Describe the request

Use the code formatting tool directly for the IDE's "Auto Format" capability.

🙂 This will ensure the fastest possible response when the user triggers a code formatting operation.

Describe the current behavior

The Arduino IDE provides a sketch code formatter (e.g., Tools > Auto Format).

Formatting is currently done via the "Arduino Language Server", which also provides the code aware features such as autocompletion, problem detection, "Go to definition". This approach is convenient because a high quality code formatting capability is already available is the "clangd" C++ language server used by Arduino Language Server.

The language server features require the complete program to be known, which includes far more than the code in the sketch itself (i.e., toolchain, core, libraries). The amount of processing involved is significant, which means that there will always be some latency in the language server results.

Code formatting is different in that it only requires passing the text of the current sketch editor tab through the formatter tool, with no program processing needed.

🙁 The auto format process is unnecessarily subject to program processing latency, which causes significant delays between the user triggering an "Auto Format" operation and getting the result.

Arduino IDE version

2.0.0-rc6-snapshot-522a5c6

Operating system

Windows, Linux, macOS

Operating system version

Any

Additional context

Build infrastructure for the standalone ClangFormat code formatter tool is already in place at https://github.com/arduino/clang-static-binaries


Please let's start with a PoC and see how tricky this is


  • @per1234 to provide .clang-format configuration file

Unfortunately the main limitation is that clang-format will not accept stdin as input but requires a file, so when formatting a Tab's content we'll need to use a temp file and run it as follows

clang-format --style=/path/to/style-config.clang-format /path/to/source-file.ino

This proposal is equivalent to the approach taken by Arduino IDE 1.x


Related:

Issue checklist

  • I searched for previous requests in the issue tracker
  • I verified the feature was still missing when using the latest nightly build
  • My request contains all necessary details

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions