-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
Enabling proper code generation for comments (Comment with Line/Block Comment) #3582
Enabling proper code generation for comments (Comment with Line/Block Comment) #3582
Conversation
Thank you so much for this, I will look at this right after the 2024.1/2 push. 😍 |
This looks great, fantastic work and I absolutely love the thoroughness in your description. A requirement of this repository is that all commits to be signed. If you could do this, we can push it in with 18.0.0, otherwise it'll be 18.0.1 -- no rush. |
85da1ba
to
a4d1e13
Compare
Thank you for your words. Please, push again your changes here as somehow trying to sign my first commit got a weird scenario. I don't know why it said "merge conflicts" when just trying to sign that commit (no code changes, no edit commit message). I copied the changes, just in case, but somehow they don't contain all commits.
So I am probably tired, but definitely couldn't sign without merge conflicts. Amending the commit e0bee82 got me in merge conflicts with CodeGenerationPanel.kt, specifically changes you have done later (why?). I tried in 2 ways, pasting my original file, or pasting your changes, and in the end this got weird. So I prefer to step back and wait for your advice. |
…nt with Line/Block Comment)
…n the version of elixir sdk
* Update gradlewrapper to v7.6.4 ./gradlew wrapper --gradle-version=7.6.4 To fix gradle issue: gradle/gradle#27156 * Remove use of `FileUtil.FILE_HASHING_STRATEGY` from Intellij FileUtil, it was removed in 2024.1. (Note, this is only used in the jps-build test suite). This also removes references to Trove THashSet, and no longer stores File directly in sets or collections. See JetBrains/intellij-community@560e8fc * Add support for 2024.1 IDEs (and runs tests correctly against 2024.1) * Update usages group wording for 2024.1 * Fix more sdk configuration commits in application RW thread, fixes compatibility with IDEs v2024.1 * Fix whitespace in tests due to 2024.1 change I believe 2024.1 changed how the Usages work. In 2023.x: ```kotlin val usages = myFixture.testFindUsagesUsingAction("module_attribute_usage.ex", "kernel.ex") .map { it as UsageInfo2UsageAdapter } ``` In the debugger shows: ``` 0 = {ReadWriteAccessUsageInfo2UsageAdapter@19239} "4|def usage, |do|: |@|module_attribute" 1 = {ReadWriteAccessUsageInfo2UsageAdapter@19240} "2|@|module_attribute| |1" ``` For 2024.1, this shows: ``` 0 = {ReadWriteAccessUsageInfo2UsageAdapter@19421} "2| |@module_attribute| 1" 1 = {ReadWriteAccessUsageInfo2UsageAdapter@19422} "4| def usage, do: |@module_attribute" ``` I believe it not shows the whitespace for the file, where previously it didn't. * pin versions * use BasePlatformTestCase to stop warning * add do block match test * set 241.0 as the version, to fix certain intellij warnings * fix key * revert tests * use thread --------- Co-authored-by: Josh Taylor <joshuataylorx@gmail.com>
a4d1e13
to
f624052
Compare
@joshuataylor I gave another try and signed the commits. Since it seems it rewrites history, you might need to re-sign the following ones. I checked the code changes for the final 3 files we wanted to integrate, is the same as before signing (CodeGenerationPanel, ElixirCodeStyleMainPanel and LanguageCodeStyleSettingsProvider). However, it tracks more changes now. Nevertheless, in |
Sorry, missed the last comment - I'll get this in ASAP for 18.0.1 |
Thank you so much for your hard work on this! When I get some time, I'll add these to the documentation. I've released v18.0.1 with these changes. ❤️ https://github.com/KronicDeth/intellij-elixir/releases/tag/v18.0.1 |
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).