Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to configure line length #53

Closed
Bennett-Lynch opened this issue Jul 19, 2022 · 10 comments
Closed

Unable to configure line length #53

Bennett-Lynch opened this issue Jul 19, 2022 · 10 comments

Comments

@Bennett-Lynch
Copy link

Bennett-Lynch commented Jul 19, 2022

First of all, thanks for the great plugin! This is a huge time saver compared to manually formatting KDoc.

However, I am unable to successfully configure the line length when using the IntelliJ plugin. I have the following .editorconfig file in the root of my project:

root = true

[*.kt]
max_line_length=120

With this setting under "Code Style" enabled:

image

But whenever I format, it uses the default line length of 72, which is too short for my preferences.

  1. Am I doing anything wrong to configure the max line length?
  2. Could a setting be added to the plugin preferences to specify the line length?

Version: 1.5.1

Thanks!

@tnorbye
Copy link
Owner

tnorbye commented Jul 19, 2022

Try adding this to your .editorconfig:

[*.md]
max_line_length = 120

Note that kdoc-formatter has two separate concepts:

  • the maximum line length
  • the maximum comment width

When you have deeply nested code, this distinction doesn't matter; the comments will break at the maximum line length. But for really wide code lines, the comments will not use the full line width. This is intentional, because for readability, you don't want really long lines -- those are harder to read (it's why books are typically in portrait orientation, not landscape). And for comments, readability is the primary concern, whereas for code, structure is the main concern.

I recently (in 1.5.1) changed the default from matching the line width to being 72. This is typically the default for markdown files, as well as git commit messages. Kdoc formatter will also initialize this value from the line-width configured for markdown files (hence the above [*.md] rule). Maybe this is a bit controversial, though I do think it makes for much nicer comments. At a minimum I should make sure it's easy to configure this for non-.editorconfig projects in IntelliJ.

@Bennett-Lynch
Copy link
Author

Bennett-Lynch commented Jul 19, 2022

Thank you for the reply @tnorbye.

I tried making your suggested change, but it's still not working (still defaulting to 72). I even tried changing the file mask to [*], which also does not seem to be detected. Perhaps I'm doing something wrong, or perhaps the latest version is not honoring the config properly?

In my project root, I have a file named .editorconfig, with the following contents:

root = true

[*]
max_line_length = 120

FWIW, I'm only trying to auto-format class-level and function-level KDoc. I'm not trying to format standalone markdown files (but I could see that being valuable as well).

I don't have a strong preference on the default behavior, as long as it's configurable, but I will add my $0.02: ~72 is a very practical width for commit messages, but I think the vast majority of codebases will consider it too short for function-level documentation. Something closer to ~100 seems more conventional/modern for that.

I actually wouldn't mind being able to declare separate line widths for class-level documentation and function-level documentation. Personally, I prefer around 100 and 80 for those, respectively.

@tnorbye
Copy link
Owner

tnorbye commented Jul 20, 2022

I've attempted to fix this, in IDE plugin version 1.5.2. I haven't submitted it to JetBrains yet; can you try installing it to see if it works for you?

With this there's a new setting at the bottom of the KDoc Formatting Options panel.
Screen Shot 2022-07-19 at 18 16 20

You can find the plugin zip to install (go to the Plugins settings page, click on the settings icon and then install plugin from disk). You can find the plugin zip on the 1.5.2 release page: https://github.com/tnorbye/kdoc-formatter/releases/tag/v1.5.2

@tnorbye
Copy link
Owner

tnorbye commented Jul 20, 2022

I uploaded 1.5.2 to the plugin center and it's been approved and is public now.

@Bennett-Lynch
Copy link
Author

Bennett-Lynch commented Jul 20, 2022

Thanks @tnorbye for this quick fix! Just tested 1.5.2 and it works for my use case. Here's some feedback from testing it:

  1. I'm a little bit confused by the wording "72 characters (or the configured Markdown line length)". If I configure my Markdown line length to be 120, it still appears to use 72:
    image
  2. Unchecking the new option allows me to use my configured line length, BUT, if I change the line length, I have to restart the IDE for the plugin to recognize the new value.
  3. I think it may still be more convenient to allow the line width to be specified directly in the plugin settings. For example, I may wish to specify a line width for comments that is different than both 72 and my normal code line width.
  4. While not nearly as important, the plugin doesn't seem to support multi-line block comments (non KDoc). Understand if that's outside the scope of this plugin, but unfortunately IntelliJ still doesn't provide native support for block comments in Kotlin.
  5. If I uncheck the "collapse short comments" setting, it still collapses them.

Thanks again!

tnorbye added a commit that referenced this issue Jul 28, 2022
Misc code cleanup (such as a new FormattingTask object which
passes around state for the formatting job, such that it no
longer has to have the hacky parameter order list in the options
class etc).

Plus 1.5.5 work:
- Fixed the following bugs:
   - #53: The collapse-single-line setting does not work in the IDE
   - #69: Paragraph + list (stability), variation 2
   - #70: Multi-line @link isn't converted if there's a #
   - #71: Make plugin dynamic
   - #72: @param with brackets is not supported
   - A bug where `<p>` paragraphs following a blank line would be
     deleted (without leaving a blank paragraph separator)
- Changed heuristics around optimal or greedy line breaking in list
  items and for KDoc tag and TODO-item paragraph formatting.
- The .editorconfig support is improved. It will now pick up the nearest
  applicable .editorconfig settings for a file, but any options
  explicitly set from the command line will override the .editor config.
   - Also, the "collapse documents that fit on a single line" option,
     instead of just defaulting to true, will now use the default
     specified for the equivalent setting for Java (if set), ensuring a
     more consistent codebase. (You can also set it for Kotlin using
     `ij_kotlin_doc_do_not_wrap_if_one_line`, though that option isn't
     supported in the IDE or by the Kotlin plugin currently.)
- Preliminary support for formatting line comments and block comments
  (enabled via new flags, `--include-line-comments` and
  `--include-block-comments`.)
- Misc IDE plugin improvements
- `<pre>` tags are converted into KDoc preformatted blocks
@tnorbye
Copy link
Owner

tnorbye commented Jul 28, 2022

Thanks for the note about the "collapse short comments" thing not working -- that was an embarassing copy paste error. It's been fixed in 1.5.5, just released. Also in 1.5.3 or 4 I added a new IDE setting which lets you manually configure override line lengths (and comment widths) in the IDE plugin which will override whatever the inferred .editorconfig settings are, which I think gives you what you need -- see the last two text fields here: https://github.com/tnorbye/kdoc-formatter/blob/main/screenshot-settings.png

(I've also done some preliminary work on block and line comments. From the command line you can format whole files with it; from the IDE only interactively the comment under the caret using the dedicated format kdoc action.)

If there are other bugs or remaining things I missed here, please file new separate issues. Thanks again.

@tnorbye tnorbye closed this as completed Jul 28, 2022
@Bennett-Lynch
Copy link
Author

Thanks @tnorbye! Love the built-in line width option.

FWIW, I'm still a little confused on "line width" vs "comment width". I thought the plugin only formats KDoc (comments)? Or is it supposed to refer to //-style comments?

Regardless, I'm happy with the current behavior, where the below configuration will correctly only auto-format KDoc to 100 width:

image

@Bennett-Lynch
Copy link
Author

@tnorbye It looks like something broke between version 1.5.4 and 1.5.5 where the above config is no longer working. In 1.5.4 this would correctly format to 100 width. Now it's formatting to 72 again.

@tnorbye
Copy link
Owner

tnorbye commented Jul 29, 2022

Ugh, I see it. Embarrassing. I really should try to write a UI test for this; the formatting machinery is well covered with unit tests but not the plugin UI code. #74

@tnorbye
Copy link
Owner

tnorbye commented Jul 29, 2022

This is available in 1.5.6 now. I've uploaded it to plugins.jetbrains.com; approvals usually take around a day but you can install it manually from https://github.com/tnorbye/kdoc-formatter/releases/tag/v1.5.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants