Commit Graph

23 Commits

Author SHA1 Message Date
Ivan Donchevskii
c5e4d73431 ClangFormat: Fix crashes when new project is created
Indenter is used by wizards so make sure it works.

Change-Id: If26eae322e26bf2e32cb86acf45e7b3d33b67e98
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2019-02-22 13:05:43 +00:00
Ivan Donchevskii
5792291520 ClangFormat: Format edited chunks of file on save
The similar implementation to the one in Beautifier plugin
with the difference that the clangformat indenter logic is used
and only modified chunks are formatted.

That means that all code which was not touched will stay in the
initial condition.

Change-Id: I47b11eb99852454ed0031ef6cfc6dbed1ecd390d
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2019-02-05 08:36:16 +00:00
Ivan Donchevskii
536b733f29 ClangFormat: Format more code while typing
With the extra option "Format while typing" checked try to format
text before the current position without breaking the current input.
To accomplish that we make proper choices which replacements to apply.

The advantage of this change is to decrease the need to manually
format code which is just written.

Some minor bugs are fixed during the testing of this change.

Change-Id: Ibed569042e6c46a881e7a83b1882cf2bb23b3626
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2019-02-05 08:36:06 +00:00
Ivan Donchevskii
80fb0178fd ClangFormat: Introduce check to format code instead of indenting
Ctrl+I with the new check will reformat the selected code or
the current line instead.

Change-Id: Ia5a72c4a09621034d0dfe463f669fe1ca36b0b5f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2019-01-31 10:25:05 +00:00
Ivan Donchevskii
d7058e1afe ClangFormat: Refactor indenter to allow ClangFormat unit-tests
We do not build texteditor files in unit-tests so some tricks
were required to make ClangFormatIndenter available.

First simple unit-test proofs it builds and runs.

Change-Id: I81d5ea099bd27fd1c1ed8b5b7877299dcc62a67f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
2019-01-22 09:52:15 +00:00
Ivan Donchevskii
73ac3b928c ClangFormat: Try to add ')' in case of no indentation
clang-format has problems with unfinished () blocks.
In case when the first formatting try failed it makes sense
to try again after appending ')' to the end of the line.

In case it does not solve the initial issue it does not
make it worse anyways.

Examples where this approach helps:

if (a<press CR>

or

if (a <press CR>&& b

Change-Id: Iacc35af79a7b19c1d512d141423d7549788a1a22
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2019-01-21 11:59:34 +00:00
Ivan Donchevskii
6b5b94d7ca ClangFormat: Remove reference from const parameter
Offset is never changed anymore in this function.

Change-Id: I5a38fcbd983bbd855b9d72888d67125276a6bf34
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2019-01-09 15:27:05 +00:00
Ivan Donchevskii
97d3858720 ClangTools: Apply indention/formatting after fix-its
Fix-its do not follow the current formatting style therefore
apply indention/formatting by using indenter.

This change will work best when ClangFormat plugin is
enabled to not only reindent line but also format it.

Task-number: QTCREATORBUG-21448
Change-Id: I8bcafcf49f3118aff7840326547e7a24052469b2
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-12-21 08:04:56 +00:00
Ivan Donchevskii
89b83aab2a ClangFormat: Return replacements from 'format' call
We want to have the results of the 'format' call in order to
have understanding what has changed and if the existing offsets
have to be adjusted.

One of the possible use cases is the formatting after fix-its.
If we apply several fix-its in the same file we need to know
after each of them if the further ones are affected and shift them.

Change-Id: I17e8f4dbcf6d36224ab7da9e11035cf3be3d3125
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-12-11 07:50:10 +00:00
Ivan Donchevskii
87762eb8ee ClangFormat: Fix the end offset for the reformat call
The intention was to add 200 lines to the current
position, not 200 characters.

Change-Id: I5b4de24077fc354611fdeba2d6747c239a3f9d5f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
2018-12-05 08:04:35 +00:00
Ivan Donchevskii
f1634df8b3 ClangFormat: Trim space-only lines to properly indent them
Do not add indentation to the existing spaces in the line
but trim the line first and then indent it as an empty line.

Change-Id: I0e95ab5e4550410e55abfb12362035751c47b0b3
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-11-27 10:20:17 +00:00
Ivan Donchevskii
386535cf18 ClangFormat: Fix iteration through text blocks
Document might be edited between consecutive currentBlock.next()
calls which is unsafe. Let's instead operate with block numbers.

Fixes: QTCREATORBUG-21521
Change-Id: I6863a77a32f5583269e1c796f38afb45cc9e7f77
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
2018-11-27 10:20:09 +00:00
Ivan Donchevskii
e9d0083ccd ClangFormat: Do not format text but indent only
Provide the separate infrastructure for the formatting
but use it only when QTC_FORMAT_INSTEAD_OF_INDENT is
provided in run environment.

Fixes: QTCREATORBUG-21447
Fixes: QTCREATORBUG-21459
Change-Id: I1ad6fe23f5de17016c0c7b18749c6977fc03a22b
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
2018-11-14 09:29:41 +00:00
Ivan Donchevskii
9a85ce88a7 ClangFormat: Use relevant settings for each file
Existing Clang Format settings may not follow the
project/global pattern but can be expected to be used
for the indentation/formatting.

So let's proceed with UI for global/project settings
but use global settings only if there's no configuration
found for the current file.

Change-Id: I87c25ab3feb7e2e3deb0290848088657783cf972
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-11-14 09:29:30 +00:00
Ivan Donchevskii
0e5c7f51fa ClangFormat: Synchronize with C++ code style settings
And remove UI for default code style settings because
it does not affect anything when ClangFormat plugin is
enabled.

Change-Id: Ie348b7d2691b09ea2b4868da987f2a27347ea0f3
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-11-08 14:23:06 +00:00
Ivan Donchevskii
0a29e12d0c ClangFormat: Fix text indentation after inserted snippet
Limit the range of text which is indented to the last
non-empty character inside the block.

Fixes: QTCREATORBUG-21385
Change-Id: If9bc271e7ecf81482cb682efd0f09770e6fa02f5
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-11-08 14:19:01 +00:00
Ivan Donchevskii
918713a059 ClangFormat: Make more readable helper functions
Change-Id: I25828a17104a778d4be156bf2a2879cefbd08691
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-10-24 12:23:49 +00:00
Ivan Donchevskii
cafd5ced1a Clang: Significantly improve ClangFormat plugin usability
What's new:
1. New LibFormat option is used to prevent lines shrink,
   which allows to drop most of tricks used before for that purpose.
2. Cached UTF-8 source code is used to improve performance
3. Improved error handling.
4. Slightly improved UI.

Change-Id: I4605200fa103167369a40650b2e1ad2c61e8133b
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-10-17 07:16:04 +00:00
Ivan Donchevskii
e0d7d03720 Clang: Fix ClangFormat build with clang-7
Change-Id: I0859b7aad1a49f8d46ee4b32cc32039cfa0b482b
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
2018-10-05 11:08:27 +00:00
Tobias Hunger
74813fba3a ClangFormat: Fix unused parameter warning
Change-Id: Ib2d46e88a236a8ac7706a9827535b664e9f08792
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
2018-09-12 13:58:40 +00:00
Tobias Hunger
a5f63f4aa0 ClangFormat: Fix warnings about signed/unsigned int
Change-Id: I5cff5054d3440ee6fb0f262e6306ae525afa94c9
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
2018-09-12 13:58:33 +00:00
Tobias Hunger
5462527c0d ClangFormat: Fix warnings about fallthrough
Change-Id: Iaa2effe38153844552e2b1baa9d9d5336ed886ab
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
2018-09-12 13:58:06 +00:00
Ivan Donchevskii
ca84a51f86 Clang: Use clang-format for indentation
This is the new experimental plugin based on LibFormat.

It replaces the default indenter for CppEditorDocument
and applies clang-format after the CR or the set of 'electric'
characters.

Uses the global .clang-format kept in QtC settings or
the one for current project. Both can be configured.

For indentation some style modifications and code manipulations
are done to prevent line shrinking when it's not expected.

Manual indentation uses unmodified style from .clang-format file.

Change-Id: I6279b805e418e1804b553efa615f5c843f395a58
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
2018-09-12 07:59:30 +00:00