Skip to content

Commit

Permalink
✨ feat: *-translate.xml enhance
Browse files Browse the repository at this point in the history
  • Loading branch information
imyuyu committed Jun 10, 2024
1 parent 4697c3d commit 2e2a650
Show file tree
Hide file tree
Showing 49 changed files with 1,038 additions and 127 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

## [Unreleased]

### Added
- Support cache translation reference redirection
- Added language injections: 'SQLToy SQL ID' and 'SQLToy TRANSLATE ID'
- 支持`*-translate.xml`文件的缓存定义跳转至java代码`lightDao.getTranslateCache``new CacheMatchFilter().cacheName("dictKeyName")``new Translate("dictKeyName")`
- 支持`*-translate.xml`文件内部`Checker`属性的`cache`值跳转到缓存定义。`<sql-increment-checker cache="dictKeyName">`、`<sql-translate
cache="dictKeyName">`
- 支持`*.sql.xml`文件`sql`定义中`cache`值跳转到缓存定义.`<sql id="sys_findStaffInfo"><translate cache="dictKeyName" columns="DICT_KEY" /></sql>`
- 以上所有xml中的`cache`属性,java中的`cacheName`均支持自动完成、自动提示
- `sqlId``cacheName`的引用均增加了下划线高亮.
- 增加了`SQLToy SQL ID``SQLToy TRANSLATE ID`两个语言注入,可以在任意字符串位置将某个字符串注入为`sqlId``cacheName`,注入后效果一致

### Changed
- Refactoring indexes while supporting SQLId and cache translation
- 重构了文件索引以同时支持`sqlId`和缓存翻译
- 优化部分代码匹配机制,提高性能及内存消耗

## [0.4.3] - 2024-05-28

### Added
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ version = properties("pluginVersion").get()
// Configure project's dependencies
repositories {
mavenLocal()
maven {
/*maven {
url = uri("https://maven.aliyun.com/repository/public")
}
}*/
mavenCentral()
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.imyuyu
pluginName = SQLToy Integration
pluginRepositoryUrl = https://github.com/imyuyu/sqltoy-idea-plugin
# SemVer format -> https://semver.org
pluginVersion = 0.4.3
pluginVersion = 0.5.0

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 222
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.imyuyu.sqltoy

import com.github.imyuyu.sqltoy.model.highlighting.jam.SQLToyTranslateFindUsagesHandler
import com.github.imyuyu.sqltoy.util.XmlUtil
import com.intellij.find.findUsages.FindUsagesHandler
import com.intellij.find.findUsages.FindUsagesHandlerFactory
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile

Check warning on line 10 in src/main/kotlin/com/github/imyuyu/sqltoy/SQLToyFindUsagesHandlerFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive

class SQLToyFindUsagesHandlerFactory: FindUsagesHandlerFactory() {

override fun canFindUsages(element: PsiElement): Boolean {
val module = ModuleUtilCore.findModuleForPsiElement(element);

Check warning on line 15 in src/main/kotlin/com/github/imyuyu/sqltoy/SQLToyFindUsagesHandlerFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
val containingFile: VirtualFile = element.containingFile.virtualFile
return XmlUtil.isSqltoyXml(containingFile);

Check warning on line 17 in src/main/kotlin/com/github/imyuyu/sqltoy/SQLToyFindUsagesHandlerFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

override fun createFindUsagesHandler(element: PsiElement, forHighlightUsages: Boolean): FindUsagesHandler? {
if(XmlUtil.isTranslateXml(element)){

Check notice on line 21 in src/main/kotlin/com/github/imyuyu/sqltoy/SQLToyFindUsagesHandlerFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Return or assignment can be lifted out

'Return' can be lifted out of 'if'
//
return SQLToyTranslateFindUsagesHandler(element, forHighlightUsages);

Check warning on line 23 in src/main/kotlin/com/github/imyuyu/sqltoy/SQLToyFindUsagesHandlerFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
} else {
return null;

Check warning on line 25 in src/main/kotlin/com/github/imyuyu/sqltoy/SQLToyFindUsagesHandlerFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}
}
}
47 changes: 47 additions & 0 deletions src/main/kotlin/com/github/imyuyu/sqltoy/action/MainActionGroup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.imyuyu.sqltoy.action

import com.intellij.database.psi.DbTable
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.project.Project


/**
* create java code
*/
class MainActionGroup : AnAction() {

override fun actionPerformed(event: AnActionEvent) {
val project: Project = event.getProject() ?: return

Check notice on line 16 in src/main/kotlin/com/github/imyuyu/sqltoy/action/MainActionGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Accessor call that can be replaced with property access syntax

Use of getter method instead of property access syntax

//获取选中的PSI元素
val psiElement = event.getData(LangDataKeys.PSI_ELEMENT)
var selectDbTable: DbTable? = null
if (psiElement is DbTable) {
selectDbTable = psiElement
}
if (selectDbTable == null) {
return;

Check warning on line 25 in src/main/kotlin/com/github/imyuyu/sqltoy/action/MainActionGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

//获取选中的所有表
val psiElements = event.getData(LangDataKeys.PSI_ELEMENT_ARRAY)
if (psiElements.isNullOrEmpty()) {
return;

Check warning on line 31 in src/main/kotlin/com/github/imyuyu/sqltoy/action/MainActionGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

val dbTableList: MutableList<DbTable> = ArrayList()
for (element in psiElements) {
if (element !is DbTable) {
continue
}
dbTableList.add(element)
}
if (dbTableList.isEmpty()) {
return;

Check warning on line 42 in src/main/kotlin/com/github/imyuyu/sqltoy/action/MainActionGroup.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

// 弹出form处理
}
}
7 changes: 6 additions & 1 deletion src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/Sql.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.imyuyu.sqltoy.dom.model

import com.github.imyuyu.sqltoy.dom.model.converters.SQLToySqlIdConverter
import com.intellij.util.xml.*
import org.jetbrains.annotations.NotNull

Expand All @@ -15,7 +16,8 @@ interface Sql : DomElement {
*
* @return the id
*/
@Required
@Referencing(value = SQLToySqlIdConverter::class, soft = true)
@Required(identifier = true)
@NameValue
@Attribute("id")
fun getId(): GenericAttributeValue<String>
Expand All @@ -42,4 +44,7 @@ interface Sql : DomElement {

@SubTag("page-optimize")
fun getPageOptimize() : PageOptimize

@SubTagList("translate")
fun getTranslate(): List<TranslateConfig>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.imyuyu.sqltoy.dom.model

import com.github.imyuyu.sqltoy.dom.model.converters.SQLToyTranslateConverter
import com.intellij.util.xml.*

/**
* The interface translate element.
*/
interface TranslateConfig:DomElement{

@Referencing(SQLToyTranslateConverter::class)
@Required
@Attribute("cache")
fun getCache() : GenericAttributeValue<String>

@Required
@Attribute("cache")
fun getColumns():GenericAttributeValue<String>

@Attribute("cache-type")
fun getCacheType() : GenericAttributeValue<String>

@Attribute("split-regex")
fun getSplitRegex() : GenericAttributeValue<String>

@Attribute("link-sign")
fun getLinkSign() : GenericAttributeValue<String>

@Attribute("cache-indexs")
fun getCacheIndexs() : GenericAttributeValue<String>

@Attribute("uncached-template")
fun getUncachedTemplate() : GenericAttributeValue<String>

@Attribute("original-columns")
fun getOriginalColumns() : GenericAttributeValue<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.imyuyu.sqltoy.dom.model.converters

import com.intellij.util.xml.CustomReferenceConverter

abstract class SQLToySqlIdConverter : CustomReferenceConverter<String?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.imyuyu.sqltoy.dom.model.converters

import com.github.imyuyu.sqltoy.dom.model.Sql
import com.intellij.pom.PomTargetPsiElement
import com.intellij.pom.references.PomService
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.ResolvingHint
import com.intellij.spring.model.BeanService

Check warning on line 10 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.intellij.util.ReflectionUtil
import com.intellij.util.xml.ConvertContext
import com.intellij.util.xml.DomTarget
import com.intellij.util.xml.GenericDomValue

class SQLToySqlIdConverterImpl : SQLToySqlIdConverter() {

override fun createReferences(
value: GenericDomValue<String?>?,
element: PsiElement,
context: ConvertContext?
): Array<PsiReference> {
return arrayOf<PsiReference>(SQLIdReference(element, value));

Check warning on line 23 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon

Check notice on line 23 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unnecessary type argument

Remove explicit type arguments
}

class SQLIdReference(element:PsiElement, genericDomValue:GenericDomValue<String?>?): PsiReferenceBase<PsiElement>(element, true), ResolvingHint {

private val myGenericDomValue = genericDomValue;

Check warning on line 28 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon

override fun resolve(): PsiElement? {
val sql = getSQLIdBean();

Check warning on line 31 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
if(sql == null){

Check notice on line 32 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

If-Null return/break/... foldable to '?:'

If-Null return/break/... foldable to '?:'
return null;

Check warning on line 33 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}
val target = DomTarget.getTarget(sql);

Check warning on line 35 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
return PomService.convertToPsi(target!!);

Check warning on line 36 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

override fun canResolveTo(elementClass: Class<out PsiElement>?): Boolean {
return ReflectionUtil.isAssignable(
PomTargetPsiElement::class.java,
elementClass!!
)
}

private fun getSQLIdBean(): Sql? {
return myGenericDomValue?.getParentOfType<Sql>(Sql::class.java, false)

Check notice on line 47 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToySqlIdConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unnecessary type argument

Remove explicit type arguments
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.imyuyu.sqltoy.dom.model.converters

import com.intellij.util.xml.CustomReferenceConverter

abstract class SQLToyTranslateConverter : CustomReferenceConverter<String?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.github.imyuyu.sqltoy.dom.model.converters

import com.github.imyuyu.sqltoy.dom.model.TranslateConfig
import com.github.imyuyu.sqltoy.dom.model.translate.Checker
import com.github.imyuyu.sqltoy.indexer.SQLIdIndexHolder
import com.github.imyuyu.sqltoy.indexer.SQLToyBeanIndexType
import com.github.imyuyu.sqltoy.ui.Icons
import com.github.imyuyu.sqltoy.util.SearchUtil
import com.intellij.codeInsight.daemon.EmptyResolveMessageProvider
import com.intellij.codeInsight.highlighting.HighlightedReference
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.pom.PomTargetPsiElement
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.ResolvingHint
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.ReflectionUtil
import com.intellij.util.xml.ConvertContext
import com.intellij.util.xml.GenericDomValue

class SQLToyTranslateConverterImpl : SQLToyTranslateConverter() {

override fun createReferences(
value: GenericDomValue<String?>?,
element: PsiElement,
context: ConvertContext?
): Array<PsiReference> {
return arrayOf<PsiReference>(SQLToyTranslateReference(element, value));

Check warning on line 29 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon

Check notice on line 29 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unnecessary type argument

Remove explicit type arguments
}

class SQLToyTranslateReference(element:PsiElement, genericDomValue:GenericDomValue<String?>?): PsiReferenceBase<PsiElement>(element, true), ResolvingHint, HighlightedReference,
EmptyResolveMessageProvider {

private val myGenericDomValue = genericDomValue;

Check warning on line 35 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon

override fun resolve(): PsiElement? {
var cacheName: String? = null;

Check warning on line 38 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
val checker = getTranslateCheckerBean();

Check warning on line 39 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
if(checker != null){
//
cacheName = checker.getCache().toString();

Check warning on line 42 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

if(cacheName == null){
val translateConfigBean = getTranslateConfigBean()
if (translateConfigBean != null) {
cacheName = translateConfigBean.getCache().toString();

Check warning on line 48 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}
}

if(cacheName == null){
return null;

Check warning on line 53 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

SQLIdIndexHolder.findRecordsByQualifiedId(SQLToyBeanIndexType.TRANSLATE_ID, cacheName, element.project, SearchUtil.getSearchScope(element.project, element))
.forEach { record ->
record.getElements(element.project)
.forEach {
return it;

Check warning on line 60 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}
}

return null;

Check warning on line 64 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
/*val target = DomTarget.getTarget(sql);
return PomService.convertToPsi(target!!);*/
}

override fun getVariants(): Array<Any> {
val allIds = SQLIdIndexHolder.getAllTranslateIds(element.project, GlobalSearchScope.projectScope(element.project))
return allIds.map {
LookupElementBuilder.create(it).withIcon(Icons.XML_ICON);

Check warning on line 72 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}.toTypedArray();

Check warning on line 73 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/converters/SQLToyTranslateConverterImpl.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon
}

private fun getTranslateConfigBean(): TranslateConfig? {
return myGenericDomValue?.getParentOfType(TranslateConfig::class.java, false)
}

override fun canResolveTo(elementClass: Class<out PsiElement>?): Boolean {
return ReflectionUtil.isAssignable(
PomTargetPsiElement::class.java,
elementClass!!
)
}

private fun getTranslateCheckerBean(): Checker? {
return myGenericDomValue?.getParentOfType(Checker::class.java, false)
}

override fun getUnresolvedMessagePattern(): String {
return "Cannot resolve translate '${value}'"
}

override fun bindToElement(element: PsiElement): PsiElement {
return getElement()
}

override fun isHighlightedWhenSoft(): Boolean {
return true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ interface CacheTranslate : DomElement {
fun getSqlTranslate() : List<SqlTranslate>

@SubTagList("service-translate")
fun getRestTranslate() : List<RestTranslate>
fun getRestTranslate() : List<ServiceTranslate>

@SubTagList("rest-translate")
fun getServiceTranslate() : List<ServiceTranslate>
fun getServiceTranslate() : List<RestTranslate>

@SubTagsList("sql-translate", "service-translate", "rest-translate")
fun getAllTranslate() : List<Translate>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.github.imyuyu.sqltoy.dom.model.translate

import com.github.imyuyu.sqltoy.dom.model.ValueElement

Check warning on line 3 in src/main/kotlin/com/github/imyuyu/sqltoy/dom/model/translate/Checker.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.github.imyuyu.sqltoy.dom.model.converters.SQLToyTranslateConverter
import com.intellij.util.xml.Attribute
import com.intellij.util.xml.DomElement
import com.intellij.util.xml.GenericAttributeValue
import com.intellij.util.xml.Referencing
import com.intellij.util.xml.Required

/**
* Checker interface
*/
interface Checker : DomElement {

@Referencing(SQLToyTranslateConverter::class)
@Required
@Attribute("cache")
fun getCache() : ValueElement
fun getCache() : GenericAttributeValue<String>

@Attribute("check-frequency")
fun getCheckFrequency() : GenericAttributeValue<Int>
Expand Down
Loading

0 comments on commit 2e2a650

Please sign in to comment.