diff --git a/src/main/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/file/FileHelpers.kt b/src/main/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/file/FileHelpers.kt index be1c392..36cad03 100644 --- a/src/main/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/file/FileHelpers.kt +++ b/src/main/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/file/FileHelpers.kt @@ -12,6 +12,7 @@ import com.intellij.openapi.command.CommandProcessor import com.intellij.openapi.command.WriteCommandAction import com.intellij.openapi.project.Project import com.intellij.openapi.project.guessProjectDir +import com.intellij.openapi.vfs.VfsUtilCore import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiManager @@ -159,11 +160,12 @@ object FileHelpers { * 获取所有符合生成的file */ fun getAllEntityFiles(project: Project): List> { - val pubSpecConfig = YamlHelper.getPubSpecConfig(project) + val pubSpecConfig = YamlHelper.getPubSpecConfig(project) ?: return emptyList() + val libRoot = pubSpecConfig.pubRoot.lib ?: return emptyList() val psiManager = PsiManager.getInstance(project) return FilenameIndex.getAllFilesByExt(project, "dart").filter { //不过滤entity结尾了 - (it.path.contains("${project.name}/lib/") || it.path.contains("${pubSpecConfig?.name}/lib/")) && !it.path.contains("freezed") + isUnderLibRoot(it, libRoot) && !it.path.contains("freezed") }.sortedBy { println(it.path) it.path @@ -177,7 +179,7 @@ object FileHelpers { } else { //包名 val packageName = (it.path).substringAfter("/lib/") - dartFileHelperClassGeneratorInfo to "import 'package:${pubSpecConfig?.name}/${packageName}';" + dartFileHelperClassGeneratorInfo to "import 'package:${pubSpecConfig.name}/${packageName}';" } } catch (e: Exception) { val errorString = "error file: ${it},stackTrace: ${e.stackTraceToString()}" @@ -188,6 +190,10 @@ object FileHelpers { } } + internal fun isUnderLibRoot(file: VirtualFile, libRoot: VirtualFile): Boolean { + return VfsUtilCore.isAncestor(libRoot, file, false) + } + /** * 判断项目中是否包含这个file */ diff --git a/src/test/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/MyPluginTest.kt b/src/test/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/MyPluginTest.kt index c69df04..1f61ebf 100644 --- a/src/test/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/MyPluginTest.kt +++ b/src/test/kotlin/com/github/zhangruiyu/flutterjsonbeanfactory/MyPluginTest.kt @@ -1,6 +1,7 @@ package com.github.zhangruiyu.flutterjsonbeanfactory import com.github.zhangruiyu.flutterjsonbeanfactory.action.dart_to_helper.node.GeneratorDartClassNodeToHelperInfo +import com.github.zhangruiyu.flutterjsonbeanfactory.file.FileHelpers import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess import com.intellij.testFramework.TestDataPath import com.intellij.testFramework.fixtures.BasePlatformTestCase @@ -58,4 +59,19 @@ class MapEntity { fun testRename() { myFixture.testRename("foo.xml", "foo_after.xml", "a2") } + + fun testEntityFileScopeUsesPhysicalLibRoot() { + val entityFile = myFixture.addFileToProject( + "physical-directory/lib/model/user_entity.dart", + "class UserEntity {}", + ).virtualFile + val outsideFile = myFixture.addFileToProject( + "physical-directory/test/user_entity_test.dart", + "class UserEntityTest {}", + ).virtualFile + val libRoot = entityFile.parent.parent + + assertTrue(FileHelpers.isUnderLibRoot(entityFile, libRoot)) + assertFalse(FileHelpers.isUnderLibRoot(outsideFile, libRoot)) + } }