feat(gui): add run, check and format script actions
This commit is contained in:
+27
-2
@@ -3,11 +3,14 @@ package jadx.plugins.script.ide
|
||||
import jadx.plugins.script.runner.ScriptEval
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.jetbrains.kotlin.scripting.ide_services.compiler.KJvmReplCompilerWithIdeServices
|
||||
import kotlin.script.experimental.api.ReplAnalyzerResult
|
||||
import kotlin.script.experimental.api.ReplCompletionResult
|
||||
import kotlin.script.experimental.api.ResultWithDiagnostics
|
||||
import kotlin.script.experimental.api.ScriptDiagnostic
|
||||
import kotlin.script.experimental.api.SourceCode
|
||||
import kotlin.script.experimental.api.SourceCodeCompletionVariant
|
||||
import kotlin.script.experimental.api.analysisDiagnostics
|
||||
import kotlin.script.experimental.api.renderedResultType
|
||||
import kotlin.script.experimental.api.valueOrNull
|
||||
import kotlin.script.experimental.host.toScriptSource
|
||||
import kotlin.script.experimental.jvm.util.toSourceCodePosition
|
||||
@@ -19,14 +22,30 @@ data class ScriptCompletionResult(
|
||||
val reports: List<ScriptDiagnostic>
|
||||
)
|
||||
|
||||
class JadxScriptAutoComplete(private val scriptName: String) {
|
||||
data class ScriptAnalyzeResult(
|
||||
val errors: List<ScriptDiagnostic>,
|
||||
val renderType: String?,
|
||||
val reports: List<ScriptDiagnostic>
|
||||
)
|
||||
|
||||
class ScriptCompiler(private val scriptName: String) {
|
||||
private val replCompiler = KJvmReplCompilerWithIdeServices()
|
||||
private val compileConf = ScriptEval().buildCompileConf()
|
||||
|
||||
fun complete(code: String, cursor: Int): ScriptCompletionResult {
|
||||
val result = complete(code.toScriptSource(scriptName), cursor)
|
||||
return ScriptCompletionResult(
|
||||
completions = result.valueOrNull()?.toList() ?: listOf(),
|
||||
completions = result.valueOrNull()?.toList() ?: emptyList(),
|
||||
reports = result.reports
|
||||
)
|
||||
}
|
||||
|
||||
fun analyze(code: String, cursor: Int): ScriptAnalyzeResult {
|
||||
val result = analyze(code.toScriptSource(scriptName), cursor)
|
||||
val analyzerResult = result.valueOrNull()
|
||||
return ScriptAnalyzeResult(
|
||||
errors = analyzerResult?.get(ReplAnalyzerResult.analysisDiagnostics)?.toList() ?: emptyList(),
|
||||
renderType = analyzerResult?.get(ReplAnalyzerResult.renderedResultType),
|
||||
reports = result.reports
|
||||
)
|
||||
}
|
||||
@@ -36,4 +55,10 @@ class JadxScriptAutoComplete(private val scriptName: String) {
|
||||
replCompiler.complete(code, cursor.toSourceCodePosition(code), compileConf)
|
||||
}
|
||||
}
|
||||
|
||||
private fun analyze(code: SourceCode, cursor: Int): ResultWithDiagnostics<ReplAnalyzerResult> {
|
||||
return runBlocking {
|
||||
replCompiler.analyze(code, cursor.toSourceCodePosition(code), compileConf)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user