Xinonix
December 30, 2021, 2:28pm
1
Some times when I run Auto Format (Ctrl+T) the first (2) line(s) of a file get deleted, e.g. the become blank.
Ctrl+Z restores these lines, but sometimes i had done so many updates before I started compile that I was left wondering why these errors occured.
The Auto Format does not always kick in when I initiate it, it never works
Do you have "Auto Save" disabled in your preferences?
There is a similar known bug with the autoformat which only happens for me with the auto save off:
opened 01:40PM - 11 Oct 21 UTC
closed 03:19PM - 08 Jun 22 UTC
conclusion: resolved
topic: code
type: imperfection
topic: language server
## Describe the bug
🐛 **Tools > Auto Format** sometimes deletes arbitrary con… tent of the sketch when the "Auto save" preference is turned off.
## To Reproduce
Steps to reproduce the behavior:
1. Select **File > Preferences** from the Arduino IDE menus.
1. Uncheck the box next to **[] Auto save**.
1. Click the <kbd>OK</kbd> button.
1. Select **File > New** from the Arduino IDE menus.
- This step is not essential. It's just an easy way to get some standardized code to test with.
1. Add three blank lines in the `setup` function:
```cpp
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
```
- The issue is not restricted to this exact code configuration. I have reproduced it with other code, including code that doesn't require legitimate line removal of the formatter tool. But this code is quick and easy to produce and results in dramatically bad changes.
1. Do an Auto Format (via either <kbd>Ctrl</kbd>+<kbd>T</kbd> or **Tools > Auto Format**).
🐛 Some of the sketch may have been deleted. leaving it looking like this:
```cpp
void setup() {
// put your setup code here, to run once:
}
v
}
```
If not, start again from step (4) (the issue is somewhat intermittent).
## Expected behavior
Auto format only makes the changes dictated by the ClangFormat configuration.
## Desktop
- OS: Windows 10, Ubuntu 20.04
- Version: 2.0.0-beta.12
Date: 2021-10-07T07:55:54.325Z
CLI Version: 0.19.1 alpha [718bbbf2]
## Additional context
If you make any significant number of attempts to reproduce the issue, you will likely also encounter the intermittent problem of the formatter ceasing to work altogether (perhaps https://github.com/arduino/arduino-ide/issues/396). Once you open a new window the formatter should start working again.
I have never been able to reproduce the issue with the "Auto save" preference enabled.
This may be a duplicate of https://github.com/arduino/arduino-ide/issues/45 but I have not managed to reproduce that specific deletion and it doesn't mention a dependency on the "Auto save" configuration.
Originally reported by @UKHeliBob at https://forum.arduino.cc/t/auto-format-settings/698381/14
The problem is that the formatting is handled by the Arduino Language Server . In order to provide its other capabilities (code suggestions, problem detection, editor hover, go to ..., peek), the language server must be aware of the complete sketch program, which means processing all the libraries it uses after every change. While it is busy doing that processing it is not able to multitask to format the sketch, so the formatting is delayed until the processing operations complete.
Auto save is on, and the error can be provoked:
I have a second tab with a .h file,
Ctrl+T on that tab always empties the first line and inserts onother blank line. Very consistent behaviour.
I added 2 comment lines at the top of the file so I do no longer lose the #includes ...
Thanks so much for the additional information @Xinonix .
I am able to reproduce the issue and have submitted a report to the Arduino IDE developers:
opened 06:06AM - 10 Jan 22 UTC
closed 03:14PM - 08 Jun 22 UTC
conclusion: resolved
type: imperfection
topic: language server
## Describe the bug
In addition to the `.ino` file extension of the Arduino p… rogramming language, Arduino sketches may contain files of other programming languages, indicated by the appropriate file extension (e.g., `.c`, `.cpp`, `.h`).
:bug: When the Arduino IDE's Auto Format feature is used to format a sketch file with one of these file extensions other than `.ino`, the first line of the sketch is erased.
## To Reproduce
1. Select **File > New** from the Arduino IDE menus.
1. Click the downward pointing triangle button on the right side of the tab bar.
1. Select "**New Tab**" from the menu.
1. In the "**Name for new file**" field, type a filename that ends in `.c`, `.cpp`, or `.h` (e.g., `SomeTab.h`).
1. Click the <kbd>OK</kbd> button.
1. Add some code that is not correctly formatted. For example:
```
// unformatted line
```
(inappropriate leading space)
1. Select **Tools > Auto Format** from the Arduino IDE menus.
:bug: The first line of the file is erased.
## Expected behavior
Format the code of any supported sketch file without deleting any code.
## Desktop
- OS: Windows 10
- Version: 2.0.0-rc3-snapshot.4c831c6
Date: 2022-01-05T11:13:17.867Z
- Arduino CLI Version: 0.20.2 [13783819]
- Arduino Language Server version: 0.6.0
- clangd version: 13.0.0
## Additional context
The issue does not occur in `.ino` files.
---
It seems similar to https://github.com/arduino/arduino-ide/issues/543 at first glance, but it is not the same issue.
---
Language server logs: [SomeSketch.zip](https://github.com/arduino/arduino-ide/files/7837032/SomeSketch.zip)
---
Originally reported at https://forum.arduino.cc/t/ctrl-t-deletes-first-line-s-of-file/941177
If you have a GitHub account, you can subscribe to that issue to get notifications of any new developments.
system
Closed
July 9, 2022, 6:09am
5
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.