Hi all,

Thanks to Edward Welbourne for answering my questions.

I spent some time trying to learn how auto-indenting and auto-formatting
work in Qt Creator.
I discovered a few strange things, but I am not sure if these are bugs or
intentional design choices.
Below are my test cases and questions based on the observed behaviour.

*Case 1*

*Settings*
*Code Style*
Use ClangFormat: set
Formatting mode: Full formatting
Format while typing: unset
Format edited code on file save: unset
Use custom settings: set
Custom settings: Qt [built-in]

*Clangd*
Use clangd: set

*What happens*
*Edit -> Advanced -> Auto-indent selection*
ClangFormatBaseIndenter::autoIndent is called.
Performs both indenting and formatting because
ClangFormatIndenter::formatCodeInsteadOfIndent returns true.

*Edit -> Advanced -> Auto-format selection*
*If the text is not formatted yet:*
1. RangeFormattingRequest::sendRequest is called
2. ClangFormatBaseIndenter::autoIndent is called multiple times

I*f the text is already formatted (RangeFormattingRequest::sendRequest
makes no changes):*
RangeFormattingRequest::sendRequest is called.

*Case 2*

*Settings*
*Code Style*
Same as in Case 1

*Clangd*
Use clangd: unset

*What happens*
*Edit -> Advanced -> Auto-indent selection*
ClangFormatBaseIndenter::autoIndent is called.
Performs both indenting and formatting because
ClangFormatIndenter::formatCodeInsteadOfIndent returns true

*Edit -> Advanced -> Auto-format selection*
*Does nothing* (because !d->m_formatter in TextDocument::autoFormat is true)

*Case 3*

*Settings*

*Code Style*
Use ClangFormat: set
Formatting mode: Full formatting
Format while typing: unset
Format edited code on file save: unset
Use custom settings: set
Custom settings: LLVM [customizable]

*Code style description*
# yaml-language-server: $schema=
https://json.schemastore.org/clang-format.json
---
# Base the format rules on LLVM
BasedOnStyle: LLVM

*Clangd*
Use clangd: unset

*File naming -> License template*
/*
* A
*
* B
*/

*What happens*
*Add new -> C/C++ source file*
ClangFormatBaseIndenterPrivate::indent is called

*The contents of the new file*
/*
 * A
 *  * B
 */

The forced indent from the new file wizard corrupts the license header.

*If the header is returned to the original state, then "Auto-format
selection" is called*
/*
 * A
 *
 * B
 */

*If the header is returned to the original state, Formatting mode switched
to "Indenting only", then "Auto-format selection" is called*
/*
 * A
 *  * B
 */

*Conclusion*
Indent-only mode of ClangFormatBaseIndenterPrivate may result in incorrect
formatting.
Full formatting seems to work ok.

*Questions*
1. Why does the C++ "new file" wizard seem to ignore "Formatting mode:
indenting only vs full formatting" settings?
In my opinion, it should either avoid formatting the file at all (because
the file contains nothing but a license header) or respect the formatting
mode settings.

2. Do I understand correctly that initially Qt Creator had 2 separate
concepts: indenting and formatting?
But since there is no such distinction in clang-format, it causes some
complications.

3. Why does "Auto-format selection" action cause so many formatting passes
(language server + multiple ClangFormatBaseIndenter::autoIndent passes)?
Is it a bug or a design choice?

4. Is it correct that "Auto-format selection" does nothing when clangd is
disabled?

5. Why is formatting integrated into ClangFormatForwardingIndenter class?
At the same time, only LanguageClientFormatter is derived from
TextEditor::Formatter.

6. When "Full formatting" is enabled, "Auto-indent selection" causes the
selected text to be auto-indented + auto-formatted
(because ClangFormatIndenter::formatCodeInsteadOfIndent returns true).
Is this correct? The user asks for indenting, not formatting.

Regards,
Valentin Gorelov.
-- 
Development mailing list
[email protected]
https://lists.qt-project.org/listinfo/development

Reply via email to