Bug 456819 - Commenting out code in C++ source files adds // to the very beginning of the line
Summary: Commenting out code in C++ source files adds // to the very beginning of the ...
Status: RESOLVED FIXED
Alias: None
Product: frameworks-ktexteditor
Classification: Frameworks and Libraries
Component: indentation (show other bugs)
Version: unspecified
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-17 12:49 UTC by Ahmad Samir
Modified: 2022-08-25 17:40 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ahmad Samir 2022-07-17 12:49:56 UTC
For example:
    if (!iconNo.isNull()) {
        buttonNoGui.setIconName(iconNo);
    }

if I toggle comment on the middle line (or all of them), it becomes:
     if (!iconNo.isNull()) {
//         buttonNoGui.setIconName(iconNo);
     }

the // characters are added to the very beginning of the line; which works, but interferes with clang-format, which will then change that to:
     if (!iconNo.isNull()) {
         //         buttonNoGui.setIconName(iconNo);
     }

This can be avoided if the // characters are added like this to begin with:
    if (!iconNo.isNull()) {
        // buttonNoGui.setIconName(iconNo);
    }
Comment 1 Christoph Cullmann 2022-07-24 08:18:32 UTC
Does it behave better if you pass the singleLine tag the position="afterwhitespace" attribute in the XML highlighting file?
Comment 2 Ahmad Samir 2022-07-24 14:11:30 UTC
(In reply to Christoph Cullmann from comment #1)
> Does it behave better if you pass the singleLine tag the
> position="afterwhitespace" attribute in the XML highlighting file?

Yeah, that seems to have an effect:
    if (!iconNo.isNull()) {
        buttonNoGui.setIconName(iconNo);
    }

becomes:
    if (!iconNo.isNull()) {
        //buttonNoGui.setIconName(iconNo);
    }

but, of course, clang(pesky)-fromat still complains and changes it to:
        // buttonNoGui.setIconName(iconNo);

so it would probably need another change in the indenter js code to add "// " instead of just "//".
(I don't know javascript well enough to try hacking the indenter js code).
Comment 3 Ahmad Samir 2022-08-08 21:31:06 UTC
Git commit 178c4dfbf4eb5ad7c5aef4838f190f717f97766f by Ahmad Samir.
Committed on 08/08/2022 at 21:30.
Pushed by ahmadsamir into branch 'master'.

clang-format.cmake: don't change spaces after '//' in code comments

For example, if you have:
//This is a comment

now clang-format won't change it to:
// This is a comment

We're mainly interested in formatting the code, not the comments, so that
cuts down on some noise when running clang-format.

M  +5    -0    kde-modules/clang-format.cmake

https://invent.kde.org/frameworks/extra-cmake-modules/commit/178c4dfbf4eb5ad7c5aef4838f190f717f97766f
Comment 4 Bug Janitor Service 2022-08-08 21:38:55 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/extra-cmake-modules/-/merge_requests/289
Comment 5 Bug Janitor Service 2022-08-08 22:27:09 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/397
Comment 6 Ahmad Samir 2022-08-10 18:39:34 UTC
Git commit fceb6fef10932a5b6aaf698c102f05bbed04e12a by Ahmad Samir.
Committed on 10/08/2022 at 18:20.
Pushed by cullmann into branch 'master'.

KateDocument: always add space after single line comment start marker

So that if you have code:
if (foo) {
    do_something();
}

commenting out the second line, it would become (assuming the
position="afterwhitespace" option is set in c.xml):
if (foo) {
    // do_something();
}

This works better with clang-format, which insists on reformatting this:
    //do_something();
to
    // do_something();

to the version with the space, "// ".

Fix unittests.

M  +1    -1    autotests/src/katedocument_test.cpp
M  +1    -2    src/document/katedocument.cpp

https://invent.kde.org/frameworks/ktexteditor/commit/fceb6fef10932a5b6aaf698c102f05bbed04e12a
Comment 7 Bug Janitor Service 2022-08-18 08:15:58 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/syntax-highlighting/-/merge_requests/341
Comment 8 Bug Janitor Service 2022-08-25 06:59:05 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/409
Comment 9 Christoph Cullmann 2022-08-25 17:19:36 UTC
Git commit 93d6b7aebe7940cc63657f6d8df141fe02dee964 by Christoph Cullmann, on behalf of Waqar Ahmed.
Committed on 25/08/2022 at 16:14.
Pushed by cullmann into branch 'master'.

Improve singleline selection commenting

Currently, if you have a selection and "afterwhitespace" turned on for
the language that you are using and you do a selection and comment it
out it leads to a really bad result with languages like C/C++. Following
code:

```c++
for (auto x : list) {
    if (x > 1)
    	printf("%d", x);
}
```

will become

```c++
// for (auto x : list) {
    // if (x > 1)
    	// printf("%d", x);
// }
```

Which makes zero sense. With this change, the comment marker will be
placed at the smallest indent found in the selection, which can be 0.
For the above code, the result will be:

```c++
// for (auto x : list) {
//    if (x > 1)
//    	printf("%d", x);
// }
```

Comments are nicely aligned, and we keep the indentation.

This behaviour is consistent with other editors for e.g., vscode,
sublime. Its also compatible with what for e.g clang-format or prettier
do, i.e., align the comments according to indentation setting.
Related: bug 438744

M  +3    -3    autotests/src/katedocument_test.cpp
M  +44   -15   src/document/katedocument.cpp

https://invent.kde.org/frameworks/ktexteditor/commit/93d6b7aebe7940cc63657f6d8df141fe02dee964
Comment 10 Waqar Ahmed 2022-08-25 17:40:34 UTC
Git commit 656fbbcfd456f07bb7e1fc432cbe4ef6a16e0bcd by Waqar Ahmed.
Committed on 25/08/2022 at 16:28.
Pushed by waqar into branch 'master'.

Change singleLineComments to be AfterWhiteSpace for c-like langs

clang-format, the most commonly used formatter these days insists on
comments being properly indented. Similarly other formatters like
prettier also insist that comments are aligned.
Related: bug 438744

M  +1    -1    autotests/repository_test.cpp
M  +2    -2    data/syntax/c.xml
M  +2    -2    data/syntax/cpp.xml
M  +2    -2    data/syntax/cs.xml
M  +2    -2    data/syntax/d.xml
M  +2    -2    data/syntax/dart.xml
M  +2    -2    data/syntax/go.xml
M  +2    -2    data/syntax/isocpp.xml
M  +2    -2    data/syntax/java.xml
M  +2    -2    data/syntax/javascript.xml
M  +2    -2    data/syntax/php.xml
M  +2    -2    data/syntax/rust.xml
M  +2    -2    data/syntax/typescript.xml

https://invent.kde.org/frameworks/syntax-highlighting/commit/656fbbcfd456f07bb7e1fc432cbe4ef6a16e0bcd