Skip to content

Commit d5064ee

Browse files
committed
Fix Bug: Import R File package name incorrect
1 parent f0d346e commit d5064ee

File tree

5 files changed

+35
-14
lines changed

5 files changed

+35
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Change Log
22

33

4+
## [v1.4.4](https://plugins.jetbrains.com/plugin/10907-mvpautocodeplus)(2020-9-27)
5+
- Fix Bug: Import R File package name incorrect
6+
47
## [v1.4.3](https://plugins.jetbrains.com/plugin/10907-mvpautocodeplus)(2020-6-4)
58
- Adapte Android studio 4.0
69

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ plugins {
1515
id 'org.jetbrains.intellij' version '0.4.2'
1616
}
1717

18-
version '1.4.3'
18+
version '1.4.4'
1919
group 'com.longforus'
2020

2121
apply plugin: 'java'
@@ -77,14 +77,15 @@ dependencies {
7777
testCompile fileTree(dir: "$StudioCompilePath/plugins/java/lib", include: ['*.jar'])
7878
testCompile fileTree(dir: "$StudioCompilePath/lib", include: ['*.jar'])
7979
// compile "com.jetbrains.intellij.java:java-psi:192.7142.36"
80-
compileOnly "com.android.tools.build:gradle:3.6.1"
80+
// compileOnly "com.android.tools.build:gradle:3.6.1"
81+
compileOnly "com.android.tools.build:gradle:4.0.1"
8182
// compileOnly "com.jetbrains.intellij.java:java:192.7142.36"
8283
// compile 'com.squareup:javapoet:1.11.0'
8384
// testCompile group: 'junit', name: 'junit', version: '4.12'
8485
}
8586

8687
patchPluginXml {
87-
changeNotes """<h3>Adapte Android studio 4.0.</h3>
88+
changeNotes """<h3>Fix Bug: Import R File package name incorrect</h3>
8889
<br/>
8990
<a href="https://github.com/longforus/MvpAutoCodePlus/blob/master/CHANGELOG.md"><b>Full Changelog History</b></a>"""
9091
}

src/main/kotlin/com/longforus/mvpautocodeplus/MainAction.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import com.intellij.util.PlatformIcons
1515
import com.longforus.mvpautocodeplus.config.ItemConfigBean
1616
import com.longforus.mvpautocodeplus.maker.*
1717
import com.longforus.mvpautocodeplus.ui.EnterKeywordDialog
18+
import org.jetbrains.android.dom.manifest.Manifest
1819
import org.jetbrains.android.facet.AndroidFacet
20+
import org.jetbrains.android.facet.AndroidRootUtil
21+
import org.jetbrains.android.util.AndroidUtils
1922
import org.jetbrains.kotlin.psi.KtFile
2023
import com.intellij.openapi.application.runWriteAction as runWriteAction1
2124

@@ -25,14 +28,18 @@ import com.intellij.openapi.application.runWriteAction as runWriteAction1
2528
* Description :
2629
*/
2730

28-
class MainAction : AnAction("Generate MVP Code", "auto make mvp code", PlatformIcons.CLASS_ICON), WriteActionAware {
31+
open class MainAction : AnAction("Generate MVP Code", "auto make mvp code", PlatformIcons.CLASS_ICON), WriteActionAware {
2932
var project: Project? = null
30-
lateinit var mSelectedState: PropertiesComponent
31-
fun createFile(enterName: String, templateName: String, dir: PsiDirectory, superImplName: String, contract: PsiFile? = null, fileName: String = enterName): Pair<PsiFile?,
33+
private lateinit var mSelectedState: PropertiesComponent
34+
var curAppPackage:String? = null
35+
36+
private fun createFile(enterName: String, templateName: String, dir: PsiDirectory, superImplName: String, contract: PsiFile? = null, fileName: String = enterName): Pair<PsiFile?,
3237
PsiClass?> {
3338
var clazz: PsiClass? = null
3439
val template = TemplateMaker.getTemplate(templateName, project!!) ?: return null to null
35-
val liveTemplateDefaultValues = TemplateParamFactory.getParam4TemplateName(templateName, enterName, superImplName, contract, mSelectedState)
40+
val liveTemplateDefaultValues = TemplateParamFactory.getParam4TemplateName(templateName, enterName, superImplName, contract, mSelectedState).toMutableMap().also {
41+
it["CUR_APP_PACKAGE"] = curAppPackage
42+
}
3643
val psiFile = createFileFromTemplate(fileName, template, dir, null, false, liveTemplateDefaultValues, mSelectedState.getValue(COMMENT_AUTHOR))
3744
if (!templateName.contains("Contract")) {
3845
val openFile = FileEditorManager.getInstance(project!!).openFile(psiFile!!.virtualFile, false)
@@ -77,6 +84,13 @@ class MainAction : AnAction("Generate MVP Code", "auto make mvp code", PlatformI
7784
mSelectedState = it.state
7885
val module = ModuleUtil.findModuleForFile(dir.virtualFile, project!!)
7986
val facet = AndroidFacet.getInstance(module!!)
87+
if (facet != null) {
88+
AndroidRootUtil.getManifestFileForCompiler(facet)?.let {
89+
AndroidUtils.loadDomElement(facet.module, it, Manifest::class.java)?.let {
90+
curAppPackage = it.getPackage()?.value
91+
}
92+
}
93+
}
8094
runWriteAction1 {
8195
if (it.isJava) {
8296
doJavaCreate(it, dir, facet)
@@ -138,7 +152,7 @@ class MainAction : AnAction("Generate MVP Code", "auto make mvp code", PlatformI
138152
}
139153

140154

141-
fun getSubDir(dir: PsiDirectory, dirName: String): PsiDirectory {
155+
private fun getSubDir(dir: PsiDirectory, dirName: String): PsiDirectory {
142156
return if (dir.name == CONTRACT) {
143157
if (dirName == CONTRACT) {
144158
dir
@@ -162,7 +176,7 @@ class MainAction : AnAction("Generate MVP Code", "auto make mvp code", PlatformI
162176
presentation.isEnabled = enabled
163177
}
164178

165-
protected fun isAvailable(dataContext: DataContext): Boolean {
179+
private fun isAvailable(dataContext: DataContext): Boolean {
166180
val project = CommonDataKeys.PROJECT.getData(dataContext)
167181
val view = LangDataKeys.IDE_VIEW.getData(dataContext)
168182
return project != null && view != null && view.directories.isNotEmpty()

src/main/kotlin/com/longforus/mvpautocodeplus/TemplateCons.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ public interface TemplateCons {
2727
" interface View : ${VN}${VG}{}\n" + " interface ${P_OR_M} : ${PN}${PG}{}\n" + "}\n";
2828

2929
String COMMON_IMPL_TP_CONTENT_JAVA =
30-
"#if (${PACKAGE_NAME} != \"\")package ${PACKAGE_NAME};#end\n" + "\n" + "import ${CONTRACT};\n" + "import ${PACKAGE_NAME}.R;\n" + "#if (${IMPL} != \"\")import ${IMPL};#end\n" + "\n" + "\n" + "/**\n" +
30+
"#if (${PACKAGE_NAME} != \"\")package ${PACKAGE_NAME};#end\n" + "\n" + "import ${CONTRACT};\n" + "import ${CUR_APP_PACKAGE}.R;\n" + "#if (${IMPL} != \"\")import ${IMPL};#end\n" + "\n" + "\n" + "/**\n" +
3131
" * @describe \n" + " * @author ${USER}\n" + " * @date ${DATE} ${TIME}\n" + " * \t\t\t\t\t\t\t\t - generate by MvpAutoCodePlus plugin.\n" + " */\n" + "\n" +
3232
"public class ${NAME}${IMPL_TYPE} #if(${IMPL}!=\"\") extends ${IMPL}${VG}#end implements I${NAME}Contract.${TYPE} {\n" + "\n" + "}\n" + "\n";
3333

34-
//todo R文件的报名应该是清单里面的报名
3534

3635
String COMMON_IMPL_TP_CONTENT_KOTLIN =
37-
"#if (${PACKAGE_NAME} != \"\")package ${PACKAGE_NAME}#end\n" + "\n" + "import ${CONTRACT}\n" + "import ${PACKAGE_NAME}.R\n" + "#if (${IMPL} != \"\")import ${IMPL}#end\n" + "\n" + "/**\n" +
36+
"#if (${PACKAGE_NAME} != \"\")package ${PACKAGE_NAME}#end\n" + "\n" + "import ${CONTRACT}\n" + "import ${CUR_APP_PACKAGE}.R\n" + "#if (${IMPL} != \"\")import ${IMPL}#end\n" + "\n" + "/**\n" +
3837
" * @describe \n" + " * @author ${USER}\n" + " * @date ${DATE} ${TIME}\n" + " * \t\t\t\t\t\t\t\t - generate by MvpAutoCodePlus plugin.\n" + " */\n" + "\n" +
3938
"class ${NAME}${IMPL_TYPE} :#if (${IMPL_NP} != \"\") ${IMPL_NP}${VG}(),#end ${CONTRACT_NP}.${TYPE} {\n" + "\n" + "}\n" + "\n";
4039
}

src/main/kotlin/com/longforus/mvpautocodeplus/maker/LayoutCreator.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import org.jetbrains.android.util.AndroidUtils
2121
* @author longforus
2222
* @date 2020/3/18 16:07
2323
*/
24+
25+
26+
27+
2428
@Throws(Exception::class)
2529
fun doCreateLayoutFile(ic: ItemConfigBean,element: PsiClass?, project: Project, facet: AndroidFacet, isJava: Boolean,isActivity:Boolean = true): PsiElement? {
2630
return if (element == null) {
@@ -67,9 +71,9 @@ fun createLayoutFileForActivityOrFragment(ic: ItemConfigBean,facet: AndroidFacet
6771
layoutFileOriginName, resDirectory.findSubdirectory("layout")!!,
6872
rootLayoutName,
6973
ResourceFolderType.LAYOUT.getName(), false)
70-
74+
//生成布局返回代码,暂时无法解决kotlin代码编辑的问题
7175
// val layoutFileName = layoutFile?.name
72-
// val onCreateMethods = activityClass.findMethodsByName("getLayoutId", false)//todo 生成viewBinding
76+
// val onCreateMethods = activityClass.findMethodsByName("getLayoutId", false)//viewBinding点不好用
7377
// if (onCreateMethods.size != 1) {
7478
// return
7579
// }

0 commit comments

Comments
 (0)