From 53a5a8933846c625d18ee5ef0363d0071127b294 Mon Sep 17 00:00:00 2001 From: yin <1830959054@qq.com> Date: Mon, 10 Aug 2026 16:40:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E5=88=AB=E5=90=8D=E4=B8=8E=E7=89=A9=E7=90=86=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=E6=97=B6=E6=97=A0=E6=B3=95=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=AE=9E=E4=BD=93=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当 IDE 项目名/别名与磁盘路径不同时,原先用 project.name/lib 字符串匹配会漏扫 dart 文件。 改为基于 pubspec 的物理 lib 根目录,用 VfsUtilCore.isAncestor 判断文件范围。 --- .../flutterjsonbeanfactory/file/FileHelpers.kt | 12 +++++++++--- .../flutterjsonbeanfactory/MyPluginTest.kt | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) 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)) + } }