-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
297 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ | |
/build | ||
/captures | ||
.externalNativeBuild | ||
|
||
*.apk | ||
/src/test | ||
/out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ dependencies { | |
} | ||
|
||
group 'me.majiajie' | ||
version '1.0' | ||
version '1.1' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.