Enabling proper code generation for comments (Comment with Line/Block Comment) #3582
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Enabling "context-aware" Comment Toggling (solving #2872)
What am I doing?
Improving comment-related actions, mainly focusing on preserving proper indentation and white-spaces when toggling line/block comments.
Why?
Back in mid 2022 I opened this issue, where I exposed what I would like regarding the action "
Comment with Line Comment
" and the like, as it is a recurrent operation I do everyday. Since the default behavior is really unproductive, I need to do a combination of toggling line comment, format region and sometimes even remove a bunch of white-spaces (between the#
and the first character of the code being commented).Approach
I originally thought I would have to work with some sort of Intellij AST, and create rules sophisticated enough to produce the expected behaviors based on where the cursor is located, what the code is like on the current line and the previous and next lines. But it seems that with the
Commenter
behavior you get the appropriate thing. I was unaware that this module is available, only that it is not enabled and cannot be manipulated via settings. I have found this out as I have fiddled with the code, exploring different directions to solve the problem.Looking at the IDE configurations for other languages, I can see that the relevant options are in two regions under Editor > Code Style > WhateverLanguage:
These are the settings that I need to enable to have the expected behavior that supports proper indentation when toggling comments.
My approach involves creating a new panel (
CodeGenerationPanel.kt
) and adding settings and its manipulation viaLanguageCodeStyleSettingsProvider.kt
andElixirCodeStyleMainPanel.kt
. I haven't included aCodeSample
because it is not present in any Code Generation panel that I have seen so far (for other languages).Since there are two settings that are not available until a specific build version, I have included some logic to enable dynamically those if our IDE supports each of them.
I decided not to include the option in Wrapping and Braces, since I haven't found a case where it is necessary to fix the original issue. But I will be happy to know when we need it.
Alternative approaches considered
The first part of the above section Approach is exactly the same. This alternative approach just differs in how to implement the Settings panel for Editor > Code Style > Code Generation.
I considered enabling the code generation panel via extension providers, with an independent XML entry. However, it is a bit slower, since it goes through all providers (the
for
loop), and since the current plugin is not using this feature for theMixFormatPanel
, I am discarding it. Nevertheless, it seems it will be the way to go if we want to interact with other plugins or extensions.Adding
codeStyleSettingsProvider
toresources/META-INF/plugin.xml
:Creating the file
src/org/elixir_lang/formatter/settings/LanguageCodeGenerationSettingsProvider.kt
:Modifying
src/org/elixir_lang/formatter/settings/ElixirCodeStyleMainPanel.kt
:`
Testing
I haven't included any tests. However, I am using it in 3 WebStorm IDE and OS versions:
For the 2024.1 versions I require the changes of this other PR as well.
Some screenshots of the behavior provided by this PR. Also, considering the use cases exposed in the original issue.
By default, we provide this new panel with these options:
Then, we can modify these settings to support proper indentation:
Let's consider these use cases:
Before:
After:
Before:
After:
And considering something closer in indentation as I exposed back then for JS:
Before:
After (cursor is in the highlighted row):
Before:
After:
So, it is aware of the context.
Finally, comparing
mix format
with the IDE, manipulating it directly by doing: Toggle Line Comment + Indent + Toggle Line Comment on every line.Having a wrongly formatted file:
The produced code is the same with
mix format
than with the manual operations in the IDE:Disclaimer
This is my first time working with Intellij SDK, Kotlin, intellij-elixir and the like. So, feel free to throw any advice, refactor suggestion or alternative strategy for this PR. For instance, one of the latest changes I have done before pushing the PR, is changing the companion object of
CodeGenerationPanel
. I didn't like that the logic behindCommenterForm
was in 2 files (CodeGenerationPanel
andLanguageCodeStyleSettingsProvider
). So, I removed the logic incustomizeCodeGenerationSettings
to just call a static function present inCodeGenerationPanel
, to return the valid options for the running IDE build.I'll be happy to do any requested changes, as I have done it "in record time", studying the minimum necessary and using a pragmatic approach, because I was tired of waiting to include this functionality. I have done a quick exploration of other intellij extensions, made both in Scala and Java, and trying to extrapolate the ways to manipulate panels and extend language-styles capabilities, but trying to conform to the intellij-elixir code structure (also in Kotlin).