Bug 438744

Summary: Feature request: Let indentation be indentation when outcommenting lines
Product: [Applications] kate Reporter: Andreas Nordal <andreas_nordal_4>
Component: partAssignee: KWrite Developers <kwrite-bugs-null>
Status: RESOLVED FIXED    
Severity: wishlist CC: nate, waqar.17a
Priority: NOR    
Version: 21.04.1   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:

Description Andreas Nordal 2021-06-16 15:03:07 UTC
STEPS TO REPRODUCE
1. In Kwrite/Kate/KDevelop, mark the body of this C++ function and press Ctrl+D:

void f() {
  {
    ;
  }
}

EXPECTED RESULT

Preferrably this:

void f() {
  // {
  //   ;
  // }
}

Or this (not my favourite, but also fine):

void f() {
  // {
    // ;
  // }
}

OBSERVED RESULT

void f() {
//   {
//     ;
//   }
}

The "// " comment prefix is inserted before, instead of after, the indentation.

I think it would be better to do the opposite – not change the indentation level of the outcommented code.
This is to make it easy to visually follow the indentation level of an uncommented block with the surrounding code.
As it is, it is even a bit hard to see which exact indentation level the code is at when outcommented like this.
Third, it is what automatic code formatters do.

ADDITIONAL INFORMATION

The same applies to probably most languages supported by katepart, with the notable exception of python, which already does the right thing! Well, the "not my favourite, but also fine" alternative.
Comment 1 Bug Janitor Service 2022-08-18 08:15:57 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/syntax-highlighting/-/merge_requests/341
Comment 2 Waqar Ahmed 2022-08-18 10:46:06 UTC
I think providing this as default is not a good idea, see the linked MR.

However, if you want, you can change it for yourself. See the linked MR for required changes and "Syntax Highlighting" docs. You can just copy the xml file locally and modify the comment mode.
Comment 3 Bug Janitor Service 2022-08-25 06:59:04 UTC
A possibly relevant merge request was started @ https://invent.kde.org/frameworks/ktexteditor/-/merge_requests/409
Comment 4 Christoph Cullmann 2022-08-25 17:19:45 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 456819

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

https://invent.kde.org/frameworks/ktexteditor/commit/93d6b7aebe7940cc63657f6d8df141fe02dee964
Comment 5 Waqar Ahmed 2022-08-25 17:40:26 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 456819

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