Skip to content

Commit 4822fa5

Browse files
rNozashleysommerjoshuataylor
authored
Enabling proper code generation for comments (Comment with Line/Block Comment) (#3582)
* feat: enabling proper code generation for comments (toggleable, Comment with Line/Block Comment) * Add the correct ERL and elixir arguments for starting IEx depending on the version of elixir sdk * Bundle latest OtpErlang.jar from JInterface v1.14 for OTP v26 * Infer OTP_RELEASE & ERLANG_SDK_HOME if no environment variable is set (#3600) * Add compatibility for 2024.1 IDEs (#3569) * 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> * Add 18.0.0 changelog, fix version (#3602) * support only the newest version. * remove tests for now, kind of pointless --------- Co-authored-by: Ashley Sommer <ashleysommer@gmail.com> Co-authored-by: Josh Taylor <joshuataylorx@gmail.com>
1 parent 029b5d0 commit 4822fa5

File tree

4 files changed

+116
-16
lines changed

4 files changed

+116
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.elixir_lang.formatter.settings
2+
3+
import com.intellij.application.options.CodeStyleAbstractPanel
4+
import com.intellij.application.options.codeStyle.CommenterForm
5+
import com.intellij.lang.Language
6+
import com.intellij.openapi.editor.colors.EditorColorsScheme
7+
import com.intellij.openapi.editor.highlighter.EditorHighlighter
8+
import com.intellij.openapi.fileTypes.FileType
9+
import com.intellij.openapi.fileTypes.FileTypeEditorHighlighterProviders
10+
import com.intellij.openapi.util.NlsContexts.TabTitle
11+
import com.intellij.psi.codeStyle.CodeStyleSettings
12+
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable.CommenterOption
13+
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider
14+
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider.SettingsType
15+
import com.intellij.ui.IdeBorderFactory
16+
import com.intellij.ui.components.JBScrollPane
17+
import com.intellij.util.ui.JBInsets
18+
import org.elixir_lang.ElixirFileType
19+
import org.elixir_lang.ElixirLanguage
20+
import javax.swing.BoxLayout
21+
import javax.swing.JComponent
22+
import javax.swing.JPanel
23+
24+
class CodeGenerationPanel(settings: CodeStyleSettings) : CodeStyleAbstractPanel(settings) {
25+
26+
private val panel: JPanel
27+
private val myCommenterForm: CommenterForm = CommenterForm(ElixirLanguage)
28+
private var myPanel: JComponent? = null
29+
30+
init {
31+
panel = JPanel().apply {
32+
layout = BoxLayout(this, BoxLayout.Y_AXIS)
33+
border = IdeBorderFactory.createEmptyBorder(JBInsets(0, 10, 10, 10))
34+
add(myCommenterForm.commenterPanel)
35+
}
36+
}
37+
38+
override fun getFileType(): FileType = ElixirFileType.INSTANCE
39+
40+
override fun getTabTitle(): @TabTitle String = "Code Generation"
41+
42+
override fun getDefaultLanguage(): Language = ElixirLanguage
43+
44+
override fun getRightMargin(): Int = 47
45+
46+
override fun getPreviewText(): String = LanguageCodeStyleSettingsProvider.forLanguage(ElixirLanguage)!!
47+
.getCodeSample(SettingsType.LANGUAGE_SPECIFIC)!!
48+
49+
override fun apply(settings: CodeStyleSettings) {
50+
if (isModified(settings)) myCommenterForm.apply(settings)
51+
}
52+
53+
override fun isModified(settings: CodeStyleSettings): Boolean {
54+
return myCommenterForm.isModified(settings)
55+
}
56+
57+
fun getPanelInner(): JComponent {
58+
return panel
59+
}
60+
61+
override fun resetImpl(settings: CodeStyleSettings) {
62+
myCommenterForm.reset(settings)
63+
}
64+
65+
override fun getPanel(): JComponent? {
66+
if (myPanel == null) {
67+
val contentPanel = getPanelInner()
68+
myPanel = JBScrollPane(contentPanel)
69+
}
70+
return myPanel!!
71+
}
72+
73+
override fun createHighlighter(colors: EditorColorsScheme): EditorHighlighter {
74+
val fileType: FileType = getFileType()
75+
return FileTypeEditorHighlighterProviders.INSTANCE.forFileType(fileType)
76+
.getEditorHighlighter(null, fileType, null, colors)
77+
}
78+
79+
companion object {
80+
fun getSupportedCommenterStandardOptionNames(): MutableList<String> {
81+
val supportedCommenterStandardOptionNames = mutableListOf<String>().apply {
82+
add(CommenterOption.LINE_COMMENT_AT_FIRST_COLUMN.name)
83+
add(CommenterOption.LINE_COMMENT_ADD_SPACE.name)
84+
add(CommenterOption.LINE_COMMENT_ADD_SPACE_ON_REFORMAT.name)
85+
add(CommenterOption.BLOCK_COMMENT_AT_FIRST_COLUMN.name)
86+
add(CommenterOption.BLOCK_COMMENT_ADD_SPACE.name)
87+
}
88+
return supportedCommenterStandardOptionNames
89+
}
90+
}
91+
}

src/org/elixir_lang/formatter/settings/ElixirCodeStyleMainPanel.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import org.elixir_lang.ElixirLanguage
77
class ElixirCodeStyleMainPanel(currentSettings: CodeStyleSettings?, baseSettings: CodeStyleSettings) :
88
TabbedLanguageCodeStylePanel(ElixirLanguage, currentSettings, baseSettings) {
99

10-
override fun initTabs(settings: CodeStyleSettings?) {
10+
override fun initTabs(settings: CodeStyleSettings) {
1111
addTab(MixFormatPanel(settings))
1212
addIndentOptionsTab(settings)
1313
addSpacesTab(settings)
1414
addWrappingAndBracesTab(settings)
15+
addTab(CodeGenerationPanel(settings))
1516
// DO NOT have Blank Lines tab
1617
}
1718
}

src/org/elixir_lang/formatter/settings/LanguageCodeStyleSettingsProvider.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import com.intellij.application.options.CodeStyleAbstractPanel
55
import com.intellij.application.options.IndentOptionsEditor
66
import com.intellij.application.options.SmartIndentOptionsEditor
77
import com.intellij.lang.Language
8+
import com.intellij.openapi.util.BuildNumber
89
import com.intellij.psi.codeStyle.*
910
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider
11+
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable.*
1012
import org.elixir_lang.ElixirLanguage
1113
import org.elixir_lang.code_style.CodeStyleSettings
1214

@@ -27,12 +29,16 @@ class LanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider() {
2729
SettingsType.SPACING_SETTINGS -> customizeSpaceSettings(consumer)
2830
SettingsType.WRAPPING_AND_BRACES_SETTINGS -> customizeWrappingAndBracesSettings(consumer)
2931
SettingsType.LANGUAGE_SPECIFIC -> customizeLanguageSpecific(consumer)
32+
SettingsType.COMMENTER_SETTINGS -> customizeCodeGenerationSettings(consumer)
3033
SettingsType.BLANK_LINES_SETTINGS,
31-
SettingsType.INDENT_SETTINGS,
32-
SettingsType.COMMENTER_SETTINGS -> Unit
34+
SettingsType.INDENT_SETTINGS -> Unit
3335
}
3436
}
3537

38+
private fun customizeCodeGenerationSettings(consumer: CodeStyleSettingsCustomizable) {
39+
consumer.showStandardOptions(*CodeGenerationPanel.getSupportedCommenterStandardOptionNames().toTypedArray())
40+
}
41+
3642
private fun customizeSpaceSettings(consumer: CodeStyleSettingsCustomizable) {
3743
consumer.showStandardOptions( // SPACE_BEFORE_PARENTHESES group
3844
/* SPACE_BEFORE_METHOD_PARENTHESES - Disabled because space between function name and call arguments

src/org/elixir_lang/sdk/elixir/Type.kt

+15-13
Original file line numberDiff line numberDiff line change
@@ -531,21 +531,23 @@ ELIXIR_SDK_HOME
531531
return if (!project.isDisposed) {
532532
/* ModuleUtilCore.findModuleForPsiElement can fail with NullPointerException if the
533533
ProjectFileIndex.SERVICE.getInstance(Project) returns {@code null}, so check that the
534-
ProjectFileIndex is available first */
534+
ProjectFileIndex is available first
535+
*/
535536
if (ProjectFileIndex.SERVICE.getInstance(project) != null) {
536-
val module = try {
537-
ReadAction.compute<Module, Throwable> {
538-
ModuleUtilCore.findModuleForPsiElement(psiElement)
537+
ApplicationManager.getApplication().executeOnPooledThread(Callable {
538+
ReadAction.compute<Sdk?, Throwable> {
539+
try {
540+
val module = ModuleUtilCore.findModuleForPsiElement(psiElement)
541+
if (module != null) {
542+
mostSpecificSdk(module)
543+
} else {
544+
mostSpecificSdk(project)
545+
}
546+
} catch (_: AlreadyDisposedException) {
547+
null
548+
}
539549
}
540-
} catch (_: AlreadyDisposedException) {
541-
null
542-
}
543-
544-
if (module != null) {
545-
mostSpecificSdk(module)
546-
} else {
547-
mostSpecificSdk(project)
548-
}
550+
}).get()
549551
} else {
550552
mostSpecificSdk(project)
551553
}

0 commit comments

Comments
 (0)