Skip to content

Commit 7ba8554

Browse files
committed
add folding support
1 parent ee5b7ad commit 7ba8554

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies {
3636

3737
// IntelliJ Platform Gradle Plugin Dependencies Extension - read more: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-dependencies-extension.html
3838
intellijPlatform {
39-
create(providers.gradleProperty("platformType"), providers.gradleProperty("platformVersion"))
39+
// create(providers.gradleProperty("platformType"), providers.gradleProperty("platformVersion"))
4040

4141
// Plugin Dependencies. Uses `platformBundledPlugins` property from the gradle.properties file for bundled IntelliJ Platform plugins.
4242
bundledPlugins(providers.gradleProperty("platformBundledPlugins").map { it.split(',') })
@@ -48,6 +48,8 @@ dependencies {
4848
bundledModules(providers.gradleProperty("platformBundledModules").map { it.split(',') })
4949

5050
testFramework(TestFrameworkType.Platform)
51+
pycharmCommunity("2025.2")
52+
bundledPlugin("PythonCore")
5153
}
5254
}
5355

qodana.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
version: "1.0"
55
linter: jetbrains/qodana-jvm-community:2024.3
6-
projectJDK: "21"
6+
projectJDK: "17"
77
profile:
88
name: qodana.recommended
99
exclude:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// kotlin
2+
package com.github.teahousestudios.akaribotdevplugin.folding
3+
4+
import com.github.teahousestudios.akaribotdevplugin.services.JsonLookupService
5+
import com.intellij.lang.ASTNode
6+
import com.intellij.lang.folding.FoldingBuilderEx
7+
import com.intellij.lang.folding.FoldingDescriptor
8+
import com.intellij.openapi.diagnostic.thisLogger
9+
import com.intellij.openapi.editor.Document
10+
import com.intellij.openapi.project.DumbAware
11+
import com.intellij.openapi.util.TextRange
12+
import com.intellij.psi.PsiElement
13+
import com.intellij.psi.PsiFile
14+
import com.intellij.psi.util.PsiTreeUtil
15+
import com.jetbrains.python.psi.PyStringLiteralExpression
16+
import java.util.concurrent.ConcurrentHashMap
17+
18+
class LocaleStringFoldingBuilder : FoldingBuilderEx(), DumbAware {
19+
private val placeholders = ConcurrentHashMap<ASTNode, String>()
20+
21+
override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
22+
if (root !is PsiFile || !root.name.endsWith(".py", ignoreCase = true)) return emptyArray()
23+
val project = root.project
24+
val localeData = JsonLookupService.getInstance(project).getLocaleData()
25+
val descriptors = ArrayList<FoldingDescriptor>()
26+
27+
val strings = PsiTreeUtil.findChildrenOfType(root, PyStringLiteralExpression::class.java)
28+
29+
for (str in strings) {
30+
val key = str.stringValue ?: continue
31+
val value = localeData[key] ?: continue
32+
val node = str.node ?: continue
33+
// 折叠整个字符串字面量范围
34+
val range = TextRange(str.textRange.startOffset, str.textRange.endOffset)
35+
placeholders[node] = "\"$value\"" // 占位显示带引号的值
36+
descriptors.add(FoldingDescriptor(node, range))
37+
}
38+
39+
return descriptors.toTypedArray()
40+
}
41+
42+
override fun getPlaceholderText(node: ASTNode): String? {
43+
return placeholders[node]
44+
}
45+
46+
override fun isCollapsedByDefault(node: ASTNode): Boolean = true
47+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
<vendor>TeahouseStudios</vendor>
66

77
<depends>com.intellij.modules.platform</depends>
8+
<depends>com.intellij.modules.python-core-capable</depends>
9+
<depends>com.intellij.modules.python</depends>
10+
11+
812

913
<resource-bundle>messages.MyBundle</resource-bundle>
1014

1115
<extensions defaultExtensionNs="com.intellij">
1216
<completion.contributor language="any" implementationClass="com.github.teahousestudios.akaribotdevplugin.completion.JsonCompletionContributor"/>
1317
<notificationGroup id="Akaribot-Plugin" displayType="BALLOON" />
1418
<postStartupActivity implementation="com.github.teahousestudios.akaribotdevplugin.listeners.JsonFileChangeStartupActivity"/>
19+
<lang.foldingBuilder language="Python" implementationClass="com.github.teahousestudios.akaribotdevplugin.folding.LocaleStringFoldingBuilder"/>
1520
</extensions>
1621

1722
</idea-plugin>

0 commit comments

Comments
 (0)