mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
fix: add file name checks when creating new files (fixes #1322)
This commit is contained in:
Generated
+10
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="deploymentTargetDropDown">
|
||||
<value>
|
||||
<entry key="app">
|
||||
<State />
|
||||
</entry>
|
||||
</value>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="KotlinJpsPluginSettings">
|
||||
<option name="version" value="1.9.0" />
|
||||
<option name="version" value="1.9.10" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+10
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectMigrations">
|
||||
<option name="MigrateToGradleLocalJavaHome">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
@@ -31,6 +31,7 @@ import com.itsaky.androidide.projects.IProjectManager
|
||||
import com.itsaky.androidide.resources.R
|
||||
import com.itsaky.androidide.utils.DialogUtils
|
||||
import com.itsaky.androidide.utils.Environment
|
||||
import com.itsaky.androidide.utils.ILogger
|
||||
import com.itsaky.androidide.utils.ProjectWriter
|
||||
import com.itsaky.androidide.utils.SingleTextWatcher
|
||||
import com.itsaky.androidide.utils.flashError
|
||||
@@ -62,13 +63,20 @@ class NewFileAction(context: Context, override val order: Int) :
|
||||
const val MENU_RES_PATH_REGEX = "/.*/src/.*/res/menu"
|
||||
const val DRAWABLE_RES_PATH_REGEX = "/.*/src/.*/res/drawable"
|
||||
const val JAVA_PATH_REGEX = "/.*/src/.*/java"
|
||||
|
||||
private val log = ILogger.newInstance("NewFileAction")
|
||||
}
|
||||
|
||||
override fun execAction(data: ActionData) {
|
||||
val context = data.requireActivity()
|
||||
val file = data.requireFile()
|
||||
val node = data.getTreeNode()
|
||||
createNewFile(context, node, file, false)
|
||||
try {
|
||||
createNewFile(context, node, file, false)
|
||||
} catch (e: Exception) {
|
||||
log.error(e)
|
||||
flashError(e.cause?.message ?: e.message)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNewFile(
|
||||
@@ -152,64 +160,11 @@ class NewFileAction(context: Context, override val order: Int) :
|
||||
builder.setTitle(R.string.new_java_class)
|
||||
builder.setPositiveButton(R.string.text_create) { dialogInterface, _ ->
|
||||
dialogInterface.dismiss()
|
||||
if (binding.name.isErrorEnabled) {
|
||||
flashError(R.string.msg_invalid_name)
|
||||
return@setPositiveButton
|
||||
}
|
||||
|
||||
val name: String = binding.name.editText!!.text.toString().trim()
|
||||
val autoLayout =
|
||||
binding.typeGroup.checkedButtonId == binding.typeActivity.id &&
|
||||
binding.createLayout.isChecked
|
||||
val pkgName = ProjectWriter.getPackageName(file)
|
||||
if (pkgName == null || pkgName.trim { it <= ' ' }.isEmpty()) {
|
||||
flashError(R.string.msg_get_package_failed)
|
||||
return@setPositiveButton
|
||||
}
|
||||
|
||||
val id: Int = binding.typeGroup.checkedButtonId
|
||||
val javaName = if (name.endsWith(".java")) name else "$name.java"
|
||||
val className = if (!name.contains(".")) name else name.substring(0, name.lastIndexOf("."))
|
||||
val created =
|
||||
when (id) {
|
||||
binding.typeClass.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createJavaClass(pkgName, className)
|
||||
)
|
||||
binding.typeInterface.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createJavaInterface(pkgName, className)
|
||||
)
|
||||
binding.typeEnum.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createJavaEnum(pkgName, className)
|
||||
)
|
||||
binding.typeActivity.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createActivity(pkgName, className)
|
||||
)
|
||||
else -> createFile(context, node, file, name, "")
|
||||
}
|
||||
|
||||
if (created && autoLayout) {
|
||||
val packagePath = pkgName.toString().replace(".", "/")
|
||||
createAutoLayout(context, file, name, packagePath)
|
||||
try {
|
||||
doCreateJavaFile(binding, file, context, node)
|
||||
} catch (e: Exception) {
|
||||
log.error(e)
|
||||
flashError(e.cause?.message ?: e.message)
|
||||
}
|
||||
}
|
||||
builder.setNegativeButton(android.R.string.cancel, null)
|
||||
@@ -217,6 +172,82 @@ class NewFileAction(context: Context, override val order: Int) :
|
||||
builder.create().show()
|
||||
}
|
||||
|
||||
private fun doCreateJavaFile(
|
||||
binding: LayoutCreateFileJavaBinding,
|
||||
file: File,
|
||||
context: Context,
|
||||
node: TreeNode?
|
||||
) {
|
||||
if (binding.name.isErrorEnabled) {
|
||||
flashError(R.string.msg_invalid_name)
|
||||
return
|
||||
}
|
||||
|
||||
val name: String = binding.name.editText!!.text.toString().trim()
|
||||
if (name.isBlank()) {
|
||||
flashError(R.string.msg_invalid_name)
|
||||
return
|
||||
}
|
||||
|
||||
val autoLayout =
|
||||
binding.typeGroup.checkedButtonId == binding.typeActivity.id &&
|
||||
binding.createLayout.isChecked
|
||||
val pkgName = ProjectWriter.getPackageName(file)
|
||||
if (pkgName == null || pkgName.trim { it <= ' ' }.isEmpty()) {
|
||||
flashError(R.string.msg_get_package_failed)
|
||||
return
|
||||
}
|
||||
|
||||
val id: Int = binding.typeGroup.checkedButtonId
|
||||
val javaName = if (name.endsWith(".java")) name else "$name.java"
|
||||
val className = if (!name.contains(".")) name else name.substring(0, name.lastIndexOf("."))
|
||||
val created =
|
||||
when (id) {
|
||||
binding.typeClass.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createJavaClass(pkgName, className)
|
||||
)
|
||||
|
||||
binding.typeInterface.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createJavaInterface(pkgName, className)
|
||||
)
|
||||
|
||||
binding.typeEnum.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createJavaEnum(pkgName, className)
|
||||
)
|
||||
|
||||
binding.typeActivity.id ->
|
||||
createFile(
|
||||
context,
|
||||
node,
|
||||
file,
|
||||
javaName,
|
||||
ProjectWriter.createActivity(pkgName, className)
|
||||
)
|
||||
|
||||
else -> createFile(context, node, file, name, "")
|
||||
}
|
||||
|
||||
if (created && autoLayout) {
|
||||
val packagePath = pkgName.toString().replace(".", "/")
|
||||
createAutoLayout(context, file, name, packagePath)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isValidJavaName(s: CharSequence?) =
|
||||
s == null || !SourceVersion.isName(s) || SourceVersion.isKeyword(s)
|
||||
|
||||
@@ -327,10 +358,21 @@ class NewFileAction(context: Context, override val order: Int) :
|
||||
builder.setPositiveButton(R.string.text_create) { dialogInterface, _ ->
|
||||
dialogInterface.dismiss()
|
||||
var name = binding.name.editText!!.text.toString().trim()
|
||||
if (name.isBlank()) {
|
||||
flashError(R.string.msg_invalid_name)
|
||||
return@setPositiveButton
|
||||
}
|
||||
|
||||
if (extension != null && extension.trim { it <= ' ' }.isNotEmpty()) {
|
||||
name = if (name.endsWith(extension)) name else name + extension
|
||||
}
|
||||
createFile(context, node, folder, name, content)
|
||||
|
||||
try {
|
||||
createFile(context, node, folder, name, content)
|
||||
} catch (e: Exception) {
|
||||
log.error(e)
|
||||
flashError(e.cause?.message ?: e.message)
|
||||
}
|
||||
}
|
||||
builder.setNegativeButton(android.R.string.cancel, null)
|
||||
builder.create().show()
|
||||
|
||||
Reference in New Issue
Block a user