Skip to content

Commit 76e9aa9

Browse files
authored
feature ReferenceGall (#15)
1 parent 5093d4d commit 76e9aa9

File tree

19 files changed

+201
-63
lines changed

19 files changed

+201
-63
lines changed

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/CallUtil.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ package com.github.oldmegit.goframehelper.callUtil
33
import com.intellij.psi.PsiElement
44

55
interface CallUtil {
6-
fun getData(psiElement: PsiElement): Map<String, String>
6+
fun getData(psiElement: PsiElement) : Map<String, PsiElement?>
7+
// get completion of tail from psi
8+
fun getPsiTail(psiElement: PsiElement?) : String
79
}

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/cfg/CfgUtil.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import java.io.File
1212
object CfgUtil : CallUtil {
1313
private val cfgTypes = mapOf(
1414
"yaml" to Yaml,
15+
"yml" to Yaml,
1516
"json" to Json,
1617
)
1718

18-
override fun getData(psiElement: PsiElement): Map<String, String> {
19+
override fun getData(psiElement: PsiElement): Map<String, PsiElement?> {
1920
val project = psiElement.project
2021
val fileMaps = getCfgFilesPath(project)
2122
return try {
@@ -25,9 +26,21 @@ object CfgUtil : CallUtil {
2526
}
2627
}
2728

29+
override fun getPsiTail(psiElement: PsiElement?): String {
30+
var ctx = ""
31+
if (psiElement == null) {
32+
return ctx
33+
}
34+
val extension = psiElement.language.associatedFileType?.defaultExtension?.lowercase()
35+
if (extension != null) {
36+
ctx = cfgTypes[extension]?.getPsiTail(psiElement).toString()
37+
}
38+
return ctx
39+
}
40+
2841
// get key and value in all file
29-
private fun getKeyValue(files: Map<PsiElement, String>): Map<String, String> {
30-
val map = hashMapOf<String, String>()
42+
private fun getKeyValue(files: Map<PsiElement, String>): Map<String, PsiElement?> {
43+
val map = hashMapOf<String, PsiElement?>()
3144

3245
for ((file, extension) in files) {
3346
map += cfgTypes[extension]!!.getFileKeyValue(file)

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/cfg/types/CfgType.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.oldmegit.goframehelper.callUtil.cfg.types
33
import com.intellij.psi.PsiElement
44

55
interface CfgType {
6-
fun getFileKeyValue(file: PsiElement): Map<String, String>
6+
fun getFileKeyValue(file: PsiElement): Map<String, PsiElement?>
77
fun getParentKeys(psiElement: PsiElement): String
8+
fun getPsiTail(psiElement: PsiElement) : String
89
}

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/cfg/types/Json.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ import com.intellij.psi.PsiElement
77
import com.intellij.psi.util.PsiTreeUtil
88

99
object Json : CfgType {
10-
override fun getFileKeyValue(file: PsiElement): Map<String, String> {
11-
val data: MutableMap<String, String> = hashMapOf()
10+
override fun getFileKeyValue(file: PsiElement): Map<String, PsiElement?> {
11+
val data: MutableMap<String, PsiElement?> = hashMapOf()
1212

1313
val document = file.firstChild
1414
val all = PsiTreeUtil.findChildrenOfType(document, JsonProperty::class.java)
1515
var k: String
16-
var v = ""
1716

1817
for (one in all) {
1918
k = getParentKeys(one) + one.getKey()
20-
if (one.value !is JsonObject && one.value !is JsonArray) {
21-
v = one.value?.text!!
22-
}
23-
data[k] = v
19+
data[k] = one
2420
}
2521
return data
2622
}
@@ -33,6 +29,17 @@ object Json : CfgType {
3329
return ""
3430
}
3531

32+
override fun getPsiTail(psiElement: PsiElement): String {
33+
var ctx = ""
34+
if (psiElement !is JsonProperty) {
35+
return ctx
36+
}
37+
if (psiElement.value !is JsonObject && psiElement.value !is JsonArray) {
38+
ctx = psiElement.value?.text!!.trim('"')
39+
}
40+
return ctx
41+
}
42+
3643
private fun JsonProperty.getKey(): String {
3744
return this.firstChild.text.trim('"')
3845
}

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/cfg/types/Yaml.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@ import org.jetbrains.yaml.psi.YAMLKeyValue
66
import org.jetbrains.yaml.psi.YAMLScalar
77

88
object Yaml : CfgType {
9-
override fun getFileKeyValue(file: PsiElement): Map<String, String> {
10-
val data: MutableMap<String, String> = hashMapOf()
9+
override fun getFileKeyValue(file: PsiElement): Map<String, PsiElement?> {
10+
val data: MutableMap<String, PsiElement?> = hashMapOf()
1111

1212
val document = file.firstChild
1313
val all = PsiTreeUtil.findChildrenOfType(document, YAMLKeyValue::class.java)
1414
var k: String
15-
var v = ""
1615

1716
for (one in all) {
1817
k = getParentKeys(one) + one.keyText
19-
if (one.value is YAMLScalar) {
20-
v = one.valueText
21-
}
22-
data[k] = v
18+
data[k] = one
2319
}
2420
return data
2521
}
@@ -31,4 +27,12 @@ object Yaml : CfgType {
3127
}
3228
return ""
3329
}
30+
31+
override fun getPsiTail(psiElement: PsiElement): String {
32+
var ctx = ""
33+
if (psiElement is YAMLKeyValue && psiElement.value is YAMLScalar) {
34+
ctx = psiElement.valueText
35+
}
36+
return ctx
37+
}
3438
}

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/i18n/I18nUtil.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.github.oldmegit.goframehelper.callUtil.i18n
22

33
import com.github.oldmegit.goframehelper.callUtil.CallUtil
4-
import com.github.oldmegit.goframehelper.callUtil.i18n.types.Json
54
import com.github.oldmegit.goframehelper.callUtil.i18n.types.Yaml
5+
import com.github.oldmegit.goframehelper.callUtil.i18n.types.Json
66
import com.github.oldmegit.goframehelper.ui.AppSettingsState
77
import com.intellij.openapi.project.Project
88
import com.intellij.openapi.vfs.LocalFileSystem
@@ -14,26 +14,30 @@ import java.io.File
1414
object I18nUtil : CallUtil {
1515
private val i18nTypes = mapOf(
1616
"yaml" to Yaml,
17+
"yml" to Yaml,
1718
"json" to Json,
1819
)
1920

20-
override fun getData(psiElement: PsiElement): Map<String, String> {
21+
override fun getData(psiElement: PsiElement): Map<String, PsiElement?> {
2122
val project = psiElement.project
2223
return try {
2324
val fileMaps = getI18nFilesPath(project)
24-
val data = getKeyValue(fileMaps)
25-
data.associateWith { "" }
25+
getKeyValue(fileMaps)
2626
} catch (_: Exception) {
2727
hashMapOf()
2828
}
2929
}
3030

31+
override fun getPsiTail(psiElement: PsiElement?): String {
32+
return ""
33+
}
34+
3135
// get key and value in all file
32-
private fun getKeyValue(files: Map<PsiElement, String>): Set<String> {
33-
val map = hashSetOf<String>()
36+
private fun getKeyValue(files: Map<PsiElement, String>): Map<String, PsiElement?> {
37+
val map = hashMapOf<String, PsiElement?>()
3438

3539
for ((file, extension) in files) {
36-
map += i18nTypes[extension]!!.getFileKey(file)
40+
map += i18nTypes[extension]!!.getFileKeyValue(file)
3741
}
3842
return map
3943
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package com.github.oldmegit.goframehelper.callUtil.cfg.types
1+
package com.github.oldmegit.goframehelper.callUtil.i18n.types
22

33
import com.intellij.psi.PsiElement
44

55
interface I18nType {
6-
fun getFileKey(file: PsiElement): Set<String>
6+
fun getFileKeyValue(file: PsiElement): Map<String, PsiElement?>
77
}

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/i18n/types/Json.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import com.intellij.psi.PsiElement
66
import com.intellij.psi.util.PsiTreeUtil
77

88
object Json : I18nType {
9-
override fun getFileKey(file: PsiElement): Set<String> {
10-
val data = hashSetOf<String>()
9+
override fun getFileKeyValue(file: PsiElement): Map<String, PsiElement?> {
10+
val data = hashMapOf<String, PsiElement?>()
1111
val document = file.firstChild
1212
val all = PsiTreeUtil.findChildrenOfType(document, JsonProperty::class.java)
1313

14+
var k: String
15+
1416
for (one in all) {
15-
data.add(one.getKey())
17+
k = one.getKey()
18+
data[k] = one
1619
}
20+
1721
return data
1822
}
1923

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/i18n/types/Yaml.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import com.intellij.psi.util.PsiTreeUtil
66
import org.jetbrains.yaml.psi.YAMLKeyValue
77

88
object Yaml : I18nType {
9-
override fun getFileKey(file: PsiElement): Set<String> {
10-
val data = hashSetOf<String>()
9+
override fun getFileKeyValue(file: PsiElement): Map<String, PsiElement?> {
10+
val data = hashMapOf<String, PsiElement?>()
11+
1112
val document = file.firstChild
1213
val all = PsiTreeUtil.findChildrenOfType(document, YAMLKeyValue::class.java)
14+
var k: String
1315

1416
for (one in all) {
15-
data.add(one.keyText)
17+
k = one.keyText
18+
data[k] = one
1619
}
1720
return data
1821
}

src/main/kotlin/com/github/oldmegit/goframehelper/callUtil/orm/OrmUtil.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.intellij.psi.util.PsiTreeUtil
1010
import com.intellij.util.ObjectUtils
1111

1212
object OrmUtil : CallUtil {
13-
override fun getData(psiElement: PsiElement): Map<String, String> {
13+
override fun getData(psiElement: PsiElement): Map<String, PsiElement?> {
1414
return try {
1515
val statement = getStatementContainDao(psiElement)
1616
val dao = getDaoByStatement(statement!!)
@@ -21,6 +21,16 @@ object OrmUtil : CallUtil {
2121
}
2222
}
2323

24+
override fun getPsiTail(psiElement: PsiElement?): String {
25+
var ctx = ""
26+
if (psiElement == null) {
27+
return ctx
28+
}
29+
val psiComment = psiElement.nextSibling.nextSibling
30+
ctx = extractTextFromComment(psiComment.text)
31+
return ctx
32+
}
33+
2434
// get statement contain XXXDao by given PsiElement
2535
private fun getStatementContainDao(psiElement: PsiElement): GoStatement? {
2636
// get direct statement
@@ -89,19 +99,19 @@ object OrmUtil : CallUtil {
8999
}
90100

91101
// get table data by XXXColumns of type GoTypeSpec
92-
private fun getTableData(columnType: GoType): Map<String, String> {
102+
private fun getTableData(columnType: GoType): Map<String, PsiElement?> {
93103
val typeSpec = columnType.resolve(ResolveState.initial()) as GoTypeSpec
94104
val fields = PsiTreeUtil.findChildrenOfType(typeSpec, GoFieldDeclaration::class.java)
95-
val data: MutableMap<String, String> = hashMapOf()
105+
val data: MutableMap<String, PsiElement?> = hashMapOf()
96106
for (field in fields) {
97107
val fieldDefinition = field.firstChild
98108
if (fieldDefinition !is GoFieldDefinition) {
99109
continue
100110
}
101111
val psiComment = field.nextSibling.nextSibling
102-
var comment = ""
112+
var comment : PsiElement? = null
103113
if (psiComment is PsiComment) {
104-
comment = extractTextFromComment(psiComment.text)
114+
comment = psiComment
105115
}
106116
data[fieldDefinition.text.camelToSnakeCase()] = comment
107117
}

0 commit comments

Comments
 (0)