Skip to content

Commit

Permalink
release 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzlmjj committed Aug 8, 2018
1 parent 27cec20 commit 84f8c77
Show file tree
Hide file tree
Showing 18 changed files with 297 additions and 161 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
/build
/captures
.externalNativeBuild

*.apk
/src/test
/out
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ dependencies {
}

group 'me.majiajie'
version '1.0'
version '1.1'
6 changes: 0 additions & 6 deletions src/main/java/asd.java

This file was deleted.

44 changes: 21 additions & 23 deletions src/main/kotlin/action/GenerateFindViewAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,43 @@ class GenerateFindViewAction : AnAction() {
// 获取焦点文件
val psiFile = e.getData(LangDataKeys.PSI_FILE)
if (psiFile == null) {
Messages.showWarningDialog(project, "文件无法识别", "异常")
Messages.showWarningDialog(project, "No focus file", "ERROE")
return
}

when (psiFile.fileType.name.toUpperCase()) {
"KOTLIN" -> {
if (checkSelectedText(e, project)) {
showGenerateKotlinCodeDialog(project,psiFile,searchFileAndGetElementList(project, psiFile))
if (checkSelectedText(e)) {
showGenerateKotlinCodeDialog(project, psiFile, searchFileAndGetElementList(project, psiFile))
} else {
Messages.showErrorDialog("Layout file name is not selected", "ERROE")
}
}
"JAVA" -> {
if (checkSelectedText(e, project)) {
showGenerateJavaCodeDialog(project,psiFile,searchFileAndGetElementList(project, psiFile))
if (checkSelectedText(e)) {
showGenerateJavaCodeDialog(project, psiFile, searchFileAndGetElementList(project, psiFile))
} else {
Messages.showErrorDialog("Layout file name is not selected", "ERROE")
}
}
"XML" -> {
showGenerateXMLDialog(psiFile, psiFile.getAndroidViewIds())
}
else -> Messages.showWarningDialog(project, "不支持的文件类型: " + psiFile.fileType.name, "警告")
else -> Messages.showWarningDialog(project, "This file type (${psiFile.fileType.name}) is not supported", "Warning")
}
}

/**
* 检查是否选中字符串
*/
private fun checkSelectedText(e: AnActionEvent, project: Project): Boolean {
private fun checkSelectedText(e: AnActionEvent): Boolean {
// 获取选中内容
val editor = e.getData(PlatformDataKeys.EDITOR) ?: return false

val model = editor.selectionModel
mSelectedText = model.selectedText

if (TextUtils.isEmpty(mSelectedText)) {
Messages.showErrorDialog(project, "未选择layout文件名", "错误")
return false
}

return true
return !TextUtils.isEmpty(mSelectedText)
}

/**
Expand All @@ -82,20 +81,19 @@ class GenerateFindViewAction : AnAction() {

val file = AndroidLayoutUtils.findLayoutResourceFile(psiFile, project, "$mSelectedText.xml")

if (file == null) {
Messages.showErrorDialog(project, "未找到选中的布局文件", "错误")
return ArrayList()
return if (file == null) {
ArrayList()
} else {
// 解析布局文件中所有View的ID
file.getAndroidViewIds()
}

// 解析布局文件中所有View的ID
return file.getAndroidViewIds()
}

/**
* 显示生成代码的Dialog(Kotlin)
*/
private fun showGenerateKotlinCodeDialog(project: Project,psiFile: PsiFile,elements: ArrayList<Element>) {
val dialog = KotlinDialog(project,psiFile,elements)
private fun showGenerateKotlinCodeDialog(project: Project, psiFile: PsiFile, elements: ArrayList<Element>) {
val dialog = KotlinDialog(project, psiFile, elements)
dialog.pack()
dialog.isVisible = true
}
Expand All @@ -112,8 +110,8 @@ class GenerateFindViewAction : AnAction() {
/**
* 显示生成代码的Dialog(JAVA)
*/
private fun showGenerateJavaCodeDialog(project: Project,psiFile: PsiFile,elements: ArrayList<Element>) {
val dialog = JavaDialog(elements)
private fun showGenerateJavaCodeDialog(project: Project, psiFile: PsiFile, elements: ArrayList<Element>) {
val dialog = JavaDialog(project, psiFile, elements)
dialog.pack()
dialog.isVisible = true
}
Expand Down
14 changes: 3 additions & 11 deletions src/main/kotlin/extensions/DataExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package extensions
import bean.Element
import bean.ViewInfo
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiField
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.KtPsiFactory
import java.awt.Toolkit
Expand Down Expand Up @@ -30,16 +33,6 @@ fun String.firstToUpperCase(): String {
*/
fun List<Element>.toViewInfoList() = this.map { ViewInfo(true, it) }

/**
* 转换成KtProperty
*/
fun List<ViewInfo>.toKtProperty(project: Project, addM: Boolean, isPrivate: Boolean, rootView: String): List<KtProperty> {
val ktPsiFactory = KtPsiFactory(project)
return this.filter { it.isChecked }.map {
ktPsiFactory.createProperty(it.getKTString(addM, isPrivate, rootView))
}
}

/**
* 生成Kotlin代码
*/
Expand Down Expand Up @@ -82,7 +75,6 @@ fun List<ViewInfo>.gengrateJavaCode(addM: Boolean, rootView: String, isPrivate:
val findViewStr = infos.joinToString("\n") { it.getJavaFindViewString(addM, isTarget26, rootView) }

return "$fieldStr\n\n$findViewStr"

}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/main/kotlin/extensions/PsiExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import com.intellij.psi.xml.XmlTag
import utils.AndroidLayoutUtils
import java.util.ArrayList

/**
* Psi文件处理扩展
*/


/**
* 在指定范围内寻找文件
Expand Down
83 changes: 83 additions & 0 deletions src/main/kotlin/helper/JavaFileWriteHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package helper

import bean.ViewInfo
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.util.PsiTreeUtil

/**
* Java文件写入帮助类
*/
class JavaFileWriteHelper<T>(project: Project,
private val psiFile: PsiFile,
private val viewInfos: List<ViewInfo>,
private val addM: Boolean,
private val isPrivate: Boolean,
private val rootView: String,
private val isTarget26: Boolean
) : WriteCommandAction.Simple<T>(project, psiFile) {

override fun run() {
val psiClass = PsiTreeUtil.findChildOfAnyType(psiFile, PsiClass::class.java)
if (psiClass == null) {
throw RuntimeException("Java class not found")
} else {
val psiElementFactory = JavaPsiFacade.getElementFactory(project)
// 已经存在的变量
val oldFieldNameList = psiClass.allFields.map { it.name }
// 生成变量
val fieldList = getJavaField(psiElementFactory)
// 添加变量
fieldList.forEach {
// 已经存在同名变量就不添加
if (!oldFieldNameList.contains(it.name)) {
psiClass.add(it)
}
}

// 检查是否存在'initView()'方法
val initMethod = psiClass.findMethodsByName("initView", true).firstOrNull { it.parameters.isEmpty() }
if (initMethod != null) {
getJavaStatement(psiElementFactory).forEach {
initMethod.body?.add(it)
}
} else {// 不存在就创建initView方法
val method = createInitViewMethod(psiElementFactory)
psiClass.add(method)
}
}
}

/**
* 获取成Java代码变量
*/
private fun getJavaField(psiElementFactory: PsiElementFactory)
: List<PsiField> {
return viewInfos.filter { it.isChecked }
.map { it.getJavaFieldString(addM, isPrivate) }
.map { psiElementFactory.createFieldFromText(it, psiFile) }
}

/**
* 获取Java参数变量
*/
private fun getJavaStatement(psiElementFactory: PsiElementFactory)
: List<PsiStatement> {
return viewInfos.filter { it.isChecked }
.map { it.getJavaFindViewString(addM, isTarget26, rootView) }
.map { psiElementFactory.createStatementFromText(it, psiFile) }
}

/**
* 创建initView方法
*/
private fun createInitViewMethod(psiElementFactory: PsiElementFactory): PsiMethod {
return psiElementFactory.createMethodFromText(
"""public void initView(){
${viewInfos.filter { it.isChecked }.map { it.getJavaFindViewString(addM, isTarget26, rootView) }.joinToString("\n") { it }}
}
""".trimIndent(), psiFile)
}

}
81 changes: 81 additions & 0 deletions src/main/kotlin/helper/KtFileWriteHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package helper

import bean.ViewInfo
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.*

/**
* Kt类文件写入帮助
*/
class KtFileWriteHelper<T>(project: Project,
private val psiFile: PsiFile,
private val viewInfos: List<ViewInfo>,
private val addM: Boolean,
private val isPrivate: Boolean,
private val rootView: String)
: WriteCommandAction.Simple<T>(project, psiFile) {

override fun run() {
val ktClass: KtClass? = PsiTreeUtil.findChildOfAnyType(psiFile, KtClass::class.java)

val ktBoday = ktClass?.getBody()
if (ktBoday == null) {
throw RuntimeException("kotlin class body not found")
} else {
val propertyList = getKtPropertyList()

val firstProperty: PsiElement? = ktBoday.declarations.firstOrNull { it is KtProperty }
writeFile(KtPsiFactory(project), ktBoday, propertyList, firstProperty
?: ktBoday.lBrace!!, firstProperty == null)
}
}

/**
* 写入
*/
private fun writeFile(ktPsiFactory: KtPsiFactory, ktBoday: KtClassBody, list: List<KtProperty>, psiElement: PsiElement, toAfter: Boolean) {

var index = 0
val last = list.size - 1

// 已经存在的变量
val oldPropertyNames = ktBoday.declarations.filter { it is KtProperty }.map { it.name }

var e = psiElement
for (i in 0..last) {
val p = list[i]
if (!oldPropertyNames.contains(p.name)) {
if (toAfter) {// 往后添加变量,就添加一个空行
e = ktBoday.addAfter(p, e)
ktBoday.addBefore(ktPsiFactory.createNewLine(2), e)
} else {
e = ktBoday.addBefore(p, e)
}
index = i + 1
break
}
}

for (i in index..last) {
val p = list[i]
if (!oldPropertyNames.contains(p.name)) {
e = ktBoday.addAfter(p, e)
}
}
}

/**
* 获取KtProperty
*/
private fun getKtPropertyList(): List<KtProperty> {
val ktPsiFactory = KtPsiFactory(project)
return viewInfos.filter { it.isChecked }.map {
ktPsiFactory.createProperty(it.getKTString(addM, isPrivate, rootView))
}
}

}
Loading

0 comments on commit 84f8c77

Please sign in to comment.