Skip to content

Commit 4477341

Browse files
author
cairui
committed
实现多Module变化检测,并且只生成对应Module改变
1 parent dac7433 commit 4477341

File tree

6 files changed

+103
-296
lines changed

6 files changed

+103
-296
lines changed

src/main/java/com/crzsc/plugin/actions/GenerateAction.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.crzsc.plugin.actions
22

33
import com.crzsc.plugin.utils.FileGenerator
4-
import com.crzsc.plugin.utils.FileHelper.shouldActivateFor
4+
import com.crzsc.plugin.utils.FileHelperNew.shouldActivateFor
55
import com.crzsc.plugin.utils.PluginUtils.showNotify
66
import com.intellij.openapi.actionSystem.AnAction
77
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -12,7 +12,7 @@ class GenerateAction : AnAction() {
1212
override fun actionPerformed(e: AnActionEvent) {
1313
val project = e.getData(PlatformDataKeys.PROJECT)
1414
if (shouldActivateFor(project!!)) {
15-
FileGenerator(project).generate()
15+
FileGenerator(project).generateAll()
1616
} else {
1717
showNotify("This project is not the flutter project")
1818
}

src/main/java/com/crzsc/plugin/listener/PsiTreeListener.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.crzsc.plugin.listener
22

33
import com.crzsc.plugin.utils.FileGenerator
4-
import com.crzsc.plugin.utils.FileHelper
4+
import com.crzsc.plugin.utils.FileHelperNew
55
import com.intellij.openapi.project.Project
66
import com.intellij.psi.PsiDirectory
77
import com.intellij.psi.PsiTreeChangeEvent
@@ -12,8 +12,6 @@ import kotlin.concurrent.timerTask
1212

1313
class PsiTreeListener(private val project: Project) : PsiTreeChangeListener {
1414
private val fileGenerator = FileGenerator(project)
15-
private val timer = Timer()
16-
private var timerTask: TimerTask? = null
1715

1816
override fun beforePropertyChange(event: PsiTreeChangeEvent) {
1917
}
@@ -58,19 +56,18 @@ class PsiTreeListener(private val project: Project) : PsiTreeChangeListener {
5856
}
5957

6058
private fun handleEvent(event: PsiTreeChangeEvent) {
61-
if (!FileHelper.isAutoDetectionEnable(project) || !FileHelper.shouldActivateFor(project)) {
62-
return
63-
}
64-
event.child?.let { changedFile ->
65-
changedFile.parent.castSafelyTo<PsiDirectory>()?.let { dir ->
66-
//assets目录发生改变
67-
FileHelper.getAssetsFolder(project)?.path?.let { path ->
68-
if (dir.virtualFile.path.startsWith(path)) {
69-
timerTask?.cancel()
70-
timerTask = timerTask {
71-
fileGenerator.generate()
59+
val assets = FileHelperNew.getAssets(project)
60+
for (config in assets) {
61+
if (FileHelperNew.isAutoDetectionEnable(config)) {
62+
// 该Module开启了自动检测
63+
event.child?.let { changedFile ->
64+
changedFile.parent.castSafelyTo<PsiDirectory>()?.let { dir ->
65+
//assets目录发生改变 这里延迟生成避免报错
66+
if (dir.virtualFile.path.startsWith(config.assetVFile.path)) {
67+
Timer().schedule(timerTask {
68+
fileGenerator.generateOne(config)
69+
}, 300)
7270
}
73-
timer.schedule(timerTask, 300)
7471
}
7572
}
7673
}

src/main/java/com/crzsc/plugin/provider/AssetsLineMarkerProvider.kt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.crzsc.plugin.provider
22

3-
import com.crzsc.plugin.utils.FileHelper
3+
import com.crzsc.plugin.utils.FileHelperNew
44
import com.crzsc.plugin.utils.PluginUtils.openFile
55
import com.crzsc.plugin.utils.isImageExtension
66
import com.crzsc.plugin.utils.isSvgExtension
@@ -16,6 +16,8 @@ import com.intellij.psi.util.PsiTreeUtil
1616
import com.intellij.ui.scale.ScaleContext
1717
import com.intellij.util.IconUtil
1818
import com.intellij.util.SVGLoader
19+
import io.flutter.utils.FlutterModuleUtils
20+
import org.jetbrains.kotlin.idea.util.module
1921
import javax.swing.Icon
2022
import javax.swing.ImageIcon
2123

@@ -25,22 +27,30 @@ import javax.swing.ImageIcon
2527
*/
2628
class AssetsLineMarkerProvider : LineMarkerProvider {
2729

28-
/**
29-
* Fixme 大图性能优化,添加缓存策略
30-
*/
3130
override fun getLineMarkerInfo(element: PsiElement): LineMarkerInfo<*>? {
32-
val filenameCorrect = element.containingFile.name.equals(
33-
FileHelper.getGeneratedFile(
34-
element.project
35-
).name, true
36-
) || element.containingFile.name.equals(
37-
"assets.dart", true
38-
)
31+
val module = element.module
32+
if (!FlutterModuleUtils.isFlutterModule(module)) return null
3933
val elementType = (element as? LeafPsiElement?)?.elementType?.toString()
40-
if (filenameCorrect
41-
&& elementType == "REGULAR_STRING_PART"
34+
if (elementType == "REGULAR_STRING_PART"
4235
) {
43-
return showMakeByType(element)
36+
// 这里会被多次调用 尽量减少调用次数
37+
var assetName: String? = null
38+
if (module != null) {
39+
FileHelperNew.getPubSpecConfig(module)?.let {
40+
assetName = FileHelperNew.getGeneratedFile(
41+
it
42+
).name
43+
}
44+
}
45+
val filenameCorrect = element.containingFile.name.equals(
46+
assetName, true
47+
) || element.containingFile.name.equals(
48+
"assets.dart", true
49+
)
50+
if (filenameCorrect) {
51+
// println("filenameCorrect showMakeByType : $element")
52+
return showMakeByType(element)
53+
}
4454
}
4555
return null
4656
}

src/main/java/com/crzsc/plugin/utils/FileGenerator.kt

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,65 @@ import com.intellij.psi.PsiManager
1212

1313
class FileGenerator(private val project: Project) {
1414
private val ignoreDir = listOf("2.0x", "3.0x", "Mx", "Nx")
15-
fun generate() {
15+
16+
/**
17+
* 为所有模块重新生成
18+
*/
19+
fun generateAll() {
1620
WriteCommandAction.runWriteCommandAction(project) {
1721
val assets = FileHelperNew.getAssets(project)
1822
if (assets.isEmpty()) {
1923
showNotify("Please configure your assets path in pubspec.yaml")
2024
return@runWriteCommandAction
2125
}
22-
for (entry in assets) {
23-
val module = entry.key
24-
val config = entry.value
25-
println("module $module config ${config.assetVFile}")
26-
val map = mutableMapOf<String, String>()
27-
generateFileMap(
28-
config.assetVFile,
29-
config,
30-
map,
31-
)
32-
if (map.isEmpty()) {
33-
showNotify("assets path is empty")
34-
return@runWriteCommandAction
35-
}
36-
val content = StringBuilder()
37-
content.append("///This file is automatically generated. DO NOT EDIT, all your changes would be lost.\n")
38-
val className = FileHelperNew.getGeneratedClassName(config)
39-
content.append("class $className {\n $className._();\n\n")
40-
map.toSortedMap().forEach {
41-
content.append(" static const String ${it.key} = '${it.value}';\n")
42-
}
43-
content.append("\n}\n")
44-
val psiManager = PsiManager.getInstance(project)
45-
val psiDocumentManager = PsiDocumentManager.getInstance(project)
46-
FileHelperNew.getGeneratedFile(config).let { generated ->
47-
psiManager.findFile(generated)?.let { dartFile ->
48-
psiDocumentManager.getDocument(dartFile)?.let { document ->
49-
if (document.text != content.toString()) {
50-
document.setText(content)
51-
psiDocumentManager.commitDocument(document)
52-
showNotify("$module : assets generate succeed")
53-
} else {
54-
showNotify("$module : nothing changed")
55-
}
56-
}
26+
for (config in assets) {
27+
generateWithConfig(config)
28+
}
29+
}
30+
}
31+
32+
33+
/**
34+
* 生成单个模块文件
35+
*/
36+
fun generateOne(config: ModulePubSpecConfig) {
37+
WriteCommandAction.runWriteCommandAction(project) {
38+
generateWithConfig(config)
39+
}
40+
}
41+
42+
private fun generateWithConfig(config: ModulePubSpecConfig) {
43+
val module = config.module
44+
println("module $module config ${config.assetVFile}")
45+
val map = mutableMapOf<String, String>()
46+
generateFileMap(
47+
config.assetVFile,
48+
config,
49+
map,
50+
)
51+
if (map.isEmpty()) {
52+
showNotify("assets path is empty")
53+
return
54+
}
55+
val content = StringBuilder()
56+
content.append("///This file is automatically generated. DO NOT EDIT, all your changes would be lost.\n")
57+
val className = FileHelperNew.getGeneratedClassName(config)
58+
content.append("class $className {\n $className._();\n\n")
59+
map.toSortedMap().forEach {
60+
content.append(" static const String ${it.key} = '${it.value}';\n")
61+
}
62+
content.append("\n}\n")
63+
val psiManager = PsiManager.getInstance(project)
64+
val psiDocumentManager = PsiDocumentManager.getInstance(project)
65+
FileHelperNew.getGeneratedFile(config).let { generated ->
66+
psiManager.findFile(generated)?.let { dartFile ->
67+
psiDocumentManager.getDocument(dartFile)?.let { document ->
68+
if (document.text != content.toString()) {
69+
document.setText(content)
70+
psiDocumentManager.commitDocument(document)
71+
showNotify("$module : assets generate succeed")
72+
} else {
73+
showNotify("$module : nothing changed")
5774
}
5875
}
5976
}

0 commit comments

Comments
 (0)