Skip to content

Commit 7932cb7

Browse files
authored
i18n code completion
1 parent 381c221 commit 7932cb7

File tree

12 files changed

+179
-3
lines changed

12 files changed

+179
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.oldmegit.goframehelper.callUtil.i18n
2+
3+
import com.goide.inspections.core.GoCallableDescriptorSet
4+
import com.goide.inspections.core.GoMethodDescriptor
5+
6+
val I18nCallables = GoCallableDescriptorSet(
7+
setOf(
8+
// https://github.com/gogf/gf/blob/master/i18n/gi18n/gi18n_manager.go
9+
GoMethodDescriptor.of("(*github.com/gogf/gf/i18n/gi18n.Manager).T"),
10+
GoMethodDescriptor.of("(*github.com/gogf/gf/i18n/gi18n.Manager).Tf"),
11+
GoMethodDescriptor.of("(*github.com/gogf/gf/i18n/gi18n.Manager).Translate"),
12+
GoMethodDescriptor.of("(*github.com/gogf/gf/i18n/gi18n.Manager).TranslateFormat"),
13+
)
14+
)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.github.oldmegit.goframehelper.callUtil.i18n
2+
3+
import com.github.oldmegit.goframehelper.callUtil.CallUtil
4+
import com.github.oldmegit.goframehelper.callUtil.i18n.types.Json
5+
import com.github.oldmegit.goframehelper.callUtil.i18n.types.Yaml
6+
import com.github.oldmegit.goframehelper.ui.AppSettingsState
7+
import com.intellij.openapi.project.Project
8+
import com.intellij.openapi.vfs.LocalFileSystem
9+
import com.intellij.openapi.vfs.VirtualFile
10+
import com.intellij.psi.PsiElement
11+
import com.intellij.psi.PsiManager
12+
import java.io.File
13+
14+
object I18nUtil : CallUtil {
15+
private val i18nTypes = mapOf(
16+
"yaml" to Yaml,
17+
"json" to Json,
18+
)
19+
20+
override fun getData(psiElement: PsiElement): Map<String, String> {
21+
val project = psiElement.project
22+
return try {
23+
val fileMaps = getI18nFilesPath(project)
24+
val data = getKeyValue(fileMaps)
25+
data.associateWith { "" }
26+
} catch (_: Exception) {
27+
hashMapOf()
28+
}
29+
}
30+
31+
// get key and value in all file
32+
private fun getKeyValue(files: Map<PsiElement, String>): Set<String> {
33+
val map = hashSetOf<String>()
34+
35+
for ((file, extension) in files) {
36+
map += i18nTypes[extension]!!.getFileKey(file)
37+
}
38+
return map
39+
}
40+
41+
// get gf i18n folder
42+
private fun getI18nFoldersPath(project: Project): Set<String> {
43+
val settings = AppSettingsState.getInstance(project)
44+
val custom = settings.gfCustomI18nFolder
45+
return setOf(
46+
"${project.basePath}/manifest/i18n",
47+
"${project.basePath}/i18n",
48+
project.basePath + "/" + custom
49+
)
50+
}
51+
52+
private fun getI18nFilesPath(project: Project): Map<PsiElement, String> {
53+
val map = hashMapOf<PsiElement, String>()
54+
val folders = getI18nFoldersPath(project)
55+
56+
for (folder in folders) {
57+
map += scanFolder(project, folder)
58+
}
59+
return map
60+
}
61+
62+
// scan all file in folder
63+
private fun scanFolder(project: Project, folderPath: String): Map<PsiElement, String> {
64+
val map = HashMap<PsiElement, String>()
65+
val folder = File(folderPath)
66+
val files = folder.listFiles()
67+
var virtualFile: VirtualFile
68+
var psiFile: PsiElement
69+
var extension: String
70+
71+
files?.forEach { file ->
72+
if (file.isDirectory) {
73+
map += scanFolder(project, file.path)
74+
return@forEach
75+
}
76+
extension = file.extension.lowercase()
77+
if (extension in i18nTypes.keys) {
78+
virtualFile = LocalFileSystem.getInstance().findFileByPath(file.path)!!
79+
psiFile = PsiManager.getInstance(project).findFile(virtualFile)!!
80+
map[psiFile] = extension
81+
}
82+
}
83+
return map
84+
}
85+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.oldmegit.goframehelper.callUtil.cfg.types
2+
3+
import com.intellij.psi.PsiElement
4+
5+
interface I18nType {
6+
fun getFileKey(file: PsiElement): Set<String>
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.oldmegit.goframehelper.callUtil.i18n.types
2+
3+
import com.github.oldmegit.goframehelper.callUtil.cfg.types.I18nType
4+
import com.intellij.json.psi.JsonProperty
5+
import com.intellij.psi.PsiElement
6+
import com.intellij.psi.util.PsiTreeUtil
7+
8+
object Json : I18nType {
9+
override fun getFileKey(file: PsiElement): Set<String> {
10+
val data = hashSetOf<String>()
11+
val document = file.firstChild
12+
val all = PsiTreeUtil.findChildrenOfType(document, JsonProperty::class.java)
13+
14+
for (one in all) {
15+
data.add(one.getKey())
16+
}
17+
return data
18+
}
19+
20+
private fun JsonProperty.getKey(): String {
21+
return this.firstChild.text.trim('"')
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.oldmegit.goframehelper.callUtil.i18n.types
2+
3+
import com.github.oldmegit.goframehelper.callUtil.cfg.types.I18nType
4+
import com.intellij.psi.PsiElement
5+
import com.intellij.psi.util.PsiTreeUtil
6+
import org.jetbrains.yaml.psi.YAMLKeyValue
7+
8+
object Yaml : I18nType {
9+
override fun getFileKey(file: PsiElement): Set<String> {
10+
val data = hashSetOf<String>()
11+
val document = file.firstChild
12+
val all = PsiTreeUtil.findChildrenOfType(document, YAMLKeyValue::class.java)
13+
14+
for (one in all) {
15+
data.add(one.keyText)
16+
}
17+
return data
18+
}
19+
}

src/main/kotlin/com/github/oldmegit/goframehelper/contributor/Contributor.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ class Contributor: CompletionContributor() {
2121
PlatformPatterns.psiElement(GoTypes.STRING).withSuperParent(3, GoCallExpr::class.java),
2222
CallProvider()
2323
)
24+
extend(
25+
CompletionType.BASIC,
26+
PlatformPatterns.psiElement(GoTypes.RAW_STRING).withSuperParent(3, GoCallExpr::class.java),
27+
CallProvider()
28+
)
2429
}
2530
}

src/main/kotlin/com/github/oldmegit/goframehelper/provider/CallProvider.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.github.oldmegit.goframehelper.provider
22

33
import com.github.oldmegit.goframehelper.callUtil.cfg.CfgCallables
44
import com.github.oldmegit.goframehelper.callUtil.cfg.CfgUtil
5+
import com.github.oldmegit.goframehelper.callUtil.i18n.I18nUtil
6+
import com.github.oldmegit.goframehelper.callUtil.i18n.I18nCallables
57
import com.github.oldmegit.goframehelper.callUtil.orm.OrmCallables
68
import com.github.oldmegit.goframehelper.callUtil.orm.OrmUtil
79
import com.github.oldmegit.goframehelper.gf.Gf
@@ -21,6 +23,13 @@ class CallProvider : GfProvider() {
2123
OrmUtil
2224
} else if (CfgCallables.find(call, false) != null) {
2325
CfgUtil
26+
} else if (I18nCallables.find(call, false) != null) {
27+
val index = position.parent.startOffsetInParent
28+
// Restrict the second parameter
29+
if (!(index == 5 || index == 6)) {
30+
return
31+
}
32+
I18nUtil
2433
} else {
2534
return
2635
}

src/main/kotlin/com/github/oldmegit/goframehelper/ui/AppSettingsComponent.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ class AppSettingsComponent {
1414
private val customGfCli = JBTextField()
1515
private val enableApiWatch = JBCheckBox(Bundle.getMessage("setting.api.watch"))
1616
private val enableLogicWatch = JBCheckBox(Bundle.getMessage("setting.service.watch"))
17+
private val customI18nFolder = JBTextField()
1718

1819
init {
1920
panel = FormBuilder.createFormBuilder()
2021
.addLabeledComponent(JBLabel(Bundle.getMessage("setting.api.src")), apiDir, 1, false)
2122
.addLabeledComponent(JBLabel(Bundle.getMessage("setting.logic.src")), logicDir, 1, false)
2223
.addLabeledComponent(JBLabel(Bundle.getMessage("setting.custom.gfCli")), customGfCli, 1, false)
24+
.addLabeledComponent(JBLabel(Bundle.getMessage("setting.custom.i18n")), customI18nFolder, 1, false)
2325
.addComponent(enableApiWatch)
2426
.addComponent(enableLogicWatch)
2527
.addComponentFillVertically(JPanel(), 0)
@@ -55,4 +57,10 @@ class AppSettingsComponent {
5557
set(newStatus) {
5658
enableLogicWatch.setSelected(newStatus as Boolean)
5759
}
60+
61+
var gfCustomI18nFolder: String?
62+
get() = customI18nFolder.getText()
63+
set(newText) {
64+
customI18nFolder.text = newText
65+
}
5866
}

src/main/kotlin/com/github/oldmegit/goframehelper/ui/AppSettingsConfigurable.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AppSettingsConfigurable(private val project: Project) : Configurable {
2828
modified = modified or (appSettingsComponent!!.gfCustomGfCli != settings.gfCustomGfCli)
2929
modified = modified or (appSettingsComponent!!.gfEnableApiWatch != settings.gfEnableApiWatch)
3030
modified = modified or (appSettingsComponent!!.gfEnableLogicWatch != settings.gfEnableLogicWatch)
31+
modified = modified or (appSettingsComponent!!.gfCustomI18nFolder != settings.gfCustomI18nFolder)
3132
return modified
3233
}
3334

@@ -38,6 +39,7 @@ class AppSettingsConfigurable(private val project: Project) : Configurable {
3839
settings.gfCustomGfCli = appSettingsComponent!!.gfCustomGfCli!!
3940
settings.gfEnableApiWatch = appSettingsComponent!!.gfEnableApiWatch!!
4041
settings.gfEnableLogicWatch = appSettingsComponent!!.gfEnableLogicWatch!!
42+
settings.gfCustomI18nFolder = appSettingsComponent!!.gfCustomI18nFolder!!
4143
}
4244

4345
override fun reset() {
@@ -47,6 +49,7 @@ class AppSettingsConfigurable(private val project: Project) : Configurable {
4749
appSettingsComponent!!.gfCustomGfCli = settings.gfCustomGfCli
4850
appSettingsComponent!!.gfEnableApiWatch = settings.gfEnableApiWatch
4951
appSettingsComponent!!.gfEnableLogicWatch = settings.gfEnableLogicWatch
52+
appSettingsComponent!!.gfCustomI18nFolder = settings.gfCustomI18nFolder
5053
}
5154

5255
override fun disposeUIResources() {

src/main/kotlin/com/github/oldmegit/goframehelper/ui/AppSettingsState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal class AppSettingsState : PersistentStateComponent<AppSettingsState> {
1818
var gfCustomGfCli = "gf"
1919
var gfEnableApiWatch = true
2020
var gfEnableLogicWatch = true
21+
var gfCustomI18nFolder = "resource/i18n"
2122

2223
override fun getState(): AppSettingsState {
2324
return this

0 commit comments

Comments
 (0)