Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2025 Enaium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.enaium.jimmer.buddy.extensions.search

import cn.enaium.jimmer.buddy.utility.I18n
import cn.enaium.jimmer.buddy.utility.isJimmerGeneratedFile
import com.intellij.find.FindModel
import com.intellij.find.impl.FindInProjectExtension
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.GlobalSearchScope

/**
* @author Enaium
*/
class FindInProjectGeneratedSourceFilter : FindInProjectExtension, DumbAware {
override fun initModelFromContext(model: FindModel, dataContext: DataContext): Boolean {
val project = CommonDataKeys.PROJECT.getData(dataContext) ?: return false
if (model.isCustomScope || model.directoryName != null || model.moduleName != null) {
return false
}

model.setProjectScope(false)
model.setCustomScope(true)
model.setCustomScope(JimmerGeneratedFreeProjectSearchScope(project))
model.setCustomScopeName(I18n.message("scope.nonGeneratedProject"))
return true
}
}

private class JimmerGeneratedFreeProjectSearchScope(
private val currentProject: Project
) : GlobalSearchScope(currentProject) {
override fun contains(file: VirtualFile): Boolean {
if (isJimmerGeneratedFile(file.path)) {
return false
}

val fileIndex = ProjectFileIndex.getInstance(currentProject)
return fileIndex.isInProject(file)
}

override fun isSearchInModuleContent(aModule: Module): Boolean {
return true
}

override fun isSearchInLibraries(): Boolean {
return false
}

override fun getDisplayName(): String {
return I18n.message("scope.nonGeneratedProject")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2025 Enaium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.enaium.jimmer.buddy.extensions.search

import cn.enaium.jimmer.buddy.utility.isJimmerGeneratedFile
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.GeneratedSourcesFilter
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.usages.Usage
import com.intellij.usages.rules.GeneratedSourceUsageFilter
import com.intellij.usages.rules.UsageFilteringRule
import com.intellij.usages.rules.UsageFilteringRuleProvider
import com.intellij.usages.rules.UsageInFile
import com.intellij.usages.UsageTarget

/**
* @author Enaium
*/
class GeneratedSourceFilter : GeneratedSourcesFilter(), GeneratedSourceUsageFilter {
override fun isAvailable(): Boolean {
return true
}

override fun isGeneratedSource(file: VirtualFile, project: Project): Boolean {
return isJimmerGeneratedFile(file.path)
}

override fun isGeneratedSource(usage: Usage, project: Project): Boolean {
val file = (usage as? UsageInFile)?.file ?: return false
return isGeneratedSource(file, project)
}
}

/**
* @author Enaium
*/
class GeneratedSourceUsageFilteringRuleProvider : UsageFilteringRuleProvider {
@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")
override fun getActiveRules(project: Project): Array<UsageFilteringRule> {
return arrayOf(GeneratedSourceUsageFilteringRule)
}
}

private object GeneratedSourceUsageFilteringRule : UsageFilteringRule {
override fun getRuleId(): String {
return "JimmerBuddy.GeneratedSource"
}

override fun isVisible(usage: Usage, targets: Array<UsageTarget>): Boolean {
val file = (usage as? UsageInFile)?.file ?: return true
return !isJimmerGeneratedFile(file.path)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2025 Enaium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.enaium.jimmer.buddy.extensions.search

import cn.enaium.jimmer.buddy.utility.I18n
import cn.enaium.jimmer.buddy.utility.isJimmerGeneratedFile
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.psi.PsiFile
import com.intellij.psi.search.scope.packageSet.CustomScopesProvider
import com.intellij.psi.search.scope.packageSet.NamedScope
import com.intellij.psi.search.scope.packageSet.NamedScopesHolder
import com.intellij.psi.search.scope.packageSet.PackageSet

/**
* @author Enaium
*/
class GeneratedSourceScopeProvider : CustomScopesProvider, DumbAware {
override fun getCustomScopes(): List<NamedScope> {
return listOf(
NamedScope(
I18n.message("scope.nonGeneratedProject"),
NonGeneratedProjectPackageSet
)
)
}
}

private object NonGeneratedProjectPackageSet : PackageSet {
override fun contains(file: PsiFile, holder: NamedScopesHolder): Boolean {
val virtualFile = file.virtualFile ?: file.originalFile.virtualFile ?: return false
if (isJimmerGeneratedFile(virtualFile.path)) {
return false
}
val fileIndex = ProjectFileIndex.getInstance(holder.project)
return fileIndex.isInProject(virtualFile)
}

override fun createCopy(): PackageSet {
return this
}

override fun getText(): String {
return "project without generated jimmer artifacts"
}

override fun getNodePriority(): Int {
return 0
}
}
29 changes: 28 additions & 1 deletion core/src/main/kotlin/cn/enaium/jimmer/buddy/utility/utitlity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,44 @@ fun isGradleProject(path: Path): Boolean {
return path.resolve("build.gradle.kts").exists() || path.resolve("build.gradle").exists()
}

private val generatedDirectoryNames = setOf(
"generated",
"generated-sources",
".apt_generated",
)

private val jimmerGeneratedFileSuffixes = setOf(
"Draft",
"Fetcher",
"FetcherDsl",
"Props",
)

fun isGeneratedFile(path: Path): Boolean {
var current: Path? = path.parent
while (current != null) {
if (current.name == "generated") {
if (current.name in generatedDirectoryNames) {
return true
}
current = current.parent
}
return false
}

fun isGeneratedFile(path: String): Boolean {
return path.split('/', '\\').any { it in generatedDirectoryNames }
}

fun isJimmerGeneratedFile(path: String): Boolean {
if (!isGeneratedFile(path)) {
return false
}

val fileName = path.replace('\\', '/').substringAfterLast('/')
val sourceName = fileName.substringBeforeLast('.', fileName)
return jimmerGeneratedFileSuffixes.any { sourceName.endsWith(it) }
}

fun Project.findProjects(): Set<Path> {
return modules.flatMap { it.sourceRoots.mapNotNull { findProjectDir(it.toNioPath()) } }.toSet()
}
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/messages/BuddyBundles.properties
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ setting.generateWhenStartup=Generate when startup
setting.autoGenerate=Auto generate
setting.autoGenerateDelay=Delay:
setting.srcSets=Source Sets:
scope.nonGeneratedProject=Jimmer: Project Files Without Generated Draft/Fetcher/Props
inlay.tuple.name=Name
inlay.select.serial=Serial
inlay.baseTable.name=Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ setting.generateWhenStartup=启动时生成
setting.autoGenerate=自动生成
setting.autoGenerateDelay=延迟:
setting.srcSets=源代码集:
scope.nonGeneratedProject=Jimmer:排除生成的 Draft/Fetcher/Props
inlay.tuple.name=名称
inlay.select.serial=序号
inlay.baseTable.name=名称
Expand Down
5 changes: 5 additions & 0 deletions since/shared/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<backgroundPostStartupActivity implementation="cn.enaium.jimmer.buddy.extensions.BuddyStartupActivity"/>
<projectCloseHandler implementation="cn.enaium.jimmer.buddy.extensions.BuddyProjectCloseHandler"/>
<psi.treeChangeListener implementation="cn.enaium.jimmer.buddy.extensions.BuddyPsiTreeChange"/>
<customScopesProvider implementation="cn.enaium.jimmer.buddy.extensions.search.GeneratedSourceScopeProvider"/>
<findInProjectExtension implementation="cn.enaium.jimmer.buddy.extensions.search.FindInProjectGeneratedSourceFilter"/>
<generatedSourcesFilter implementation="cn.enaium.jimmer.buddy.extensions.search.GeneratedSourceFilter"/>
<generatedSourceUsageFilter implementation="cn.enaium.jimmer.buddy.extensions.search.GeneratedSourceFilter"/>
<usageFilteringRuleProvider implementation="cn.enaium.jimmer.buddy.extensions.search.GeneratedSourceUsageFilteringRuleProvider"/>
<statusBarWidgetFactory implementation="cn.enaium.jimmer.buddy.extensions.status.StatusBarItemFactory"
id="cn.enaium.jimmer.buddy.extensions.status.StatusBarItemFactory"/>
<toolWindow factoryClass="cn.enaium.jimmer.buddy.extensions.window.BuddyToolWindow" id="Jimmer Buddy"
Expand Down