Skip to content

Commit 4e94692

Browse files
committed
fix minor things
1 parent 3f3935d commit 4e94692

File tree

10 files changed

+145
-47
lines changed

10 files changed

+145
-47
lines changed

src/main/kotlin/org/move/cli/MoveProjectsSyncTask.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class MoveProjectsSyncTask(
139139
projects: MutableList<MoveProject>,
140140
context: SyncContext
141141
) {
142-
val projectRoot = moveTomlFile.parent?.toNioPathOrNull() ?: error("cannot be invalid path")
142+
val packageRoot = moveTomlFile.parent?.toNioPathOrNull() ?: error("cannot be invalid path")
143143
var (moveProject, rootMoveToml) =
144144
runReadAction {
145145
val tomlFile = moveTomlFile.toTomlFile(project)!!
@@ -156,7 +156,7 @@ class MoveProjectsSyncTask(
156156
)
157157
} else {
158158
context.runWithChildProgress("Fetch dependencies") { childContext ->
159-
val taskResult = fetchDependencyPackages(childContext, projectRoot)
159+
val taskResult = fetchDependencyPackages(childContext, packageRoot)
160160
if (taskResult is TaskResult.Err) {
161161
moveProject = moveProject.copy(
162162
fetchDepsStatus = UpdateStatus.UpdateFailed("Failed to fetch dependency packages")
@@ -204,17 +204,16 @@ class MoveProjectsSyncTask(
204204
}
205205

206206
private fun fetchDependencyPackages(
207-
childContext: SyncContext, projectRoot: Path
207+
childContext: SyncContext, packageRoot: Path
208208
): TaskResult<ProcessOutput> {
209-
val skipLatest = project.moveSettings.skipFetchLatestGitDeps
210209
val aptos = project.getAptosCli(parentDisposable = this)
211210
return when {
212211
aptos == null -> TaskResult.Err("Invalid Aptos CLI configuration")
213212
else -> {
214213
val aptosProcessOutput =
215214
aptos.fetchPackageDependencies(
216-
projectRoot,
217-
skipLatest,
215+
project,
216+
packageRoot,
218217
runner = {
219218
// populate progress bar
220219
addProcessListener(object: AnsiEscapedProcessAdapter() {

src/main/kotlin/org/move/cli/runConfigurations/AptosRunState.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import com.intellij.execution.process.KillableColoredProcessHandler
66
import com.intellij.execution.process.ProcessHandler
77
import com.intellij.execution.process.ProcessTerminatedListener
88
import com.intellij.execution.runners.ExecutionEnvironment
9+
import com.intellij.openapi.util.Disposer
910
import org.move.cli.MoveFileHyperlinkFilter
1011
import org.move.cli.runConfigurations.CommandConfigurationBase.CleanConfiguration
1112

1213
abstract class AptosRunStateBase(
13-
environment: ExecutionEnvironment, val cleanConfiguration: CleanConfiguration.Ok
14+
environment: ExecutionEnvironment,
15+
val cleanConfiguration: CleanConfiguration.Ok
1416
): CommandLineState(environment) {
1517

1618
val project = environment.project
@@ -21,7 +23,11 @@ abstract class AptosRunStateBase(
2123
val generalCommandLine =
2224
commandLine.toGeneralCommandLine(cleanConfiguration.aptosPath, emulateTerminal = true)
2325
val handler = KillableColoredProcessHandler(generalCommandLine)
24-
consoleBuilder.console.attachToProcess(handler)
26+
27+
val console = consoleBuilder.console
28+
Disposer.register(this.environment, console)
29+
console.attachToProcess(handler)
30+
2531
ProcessTerminatedListener.attach(handler) // shows exit code upon termination
2632
return handler
2733
}

src/main/kotlin/org/move/cli/runConfigurations/CommandConfigurationBase.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.intellij.execution.configurations.*
66
import com.intellij.execution.runners.ExecutionEnvironment
77
import com.intellij.openapi.options.advanced.AdvancedSettings
88
import com.intellij.openapi.project.Project
9+
import com.intellij.openapi.util.Disposer
910
import com.intellij.openapi.util.NlsContexts
1011
import com.intellij.util.execution.ParametersListUtil
1112
import org.jdom.Element
@@ -17,6 +18,7 @@ import org.move.cli.runConfigurations.test.AptosTestRunState
1718
import org.move.cli.settings.aptosCliPath
1819
import org.move.cli.writePath
1920
import org.move.cli.writeString
21+
import org.move.openapiext.rootPluginDisposable
2022
import org.move.stdext.exists
2123
import java.nio.file.Path
2224

@@ -55,6 +57,11 @@ abstract class CommandConfigurationBase(
5557

5658
override fun getState(executor: Executor, environment: ExecutionEnvironment): AptosRunStateBase? {
5759
val config = clean().ok ?: return null
60+
61+
// environment is disposable to which all internal RunState disposables are connected
62+
// todo: find shorter living disposable?
63+
Disposer.register(project.rootPluginDisposable, environment)
64+
5865
return if (showTestToolWindow(config.cmd)) {
5966
AptosTestRunState(environment, config)
6067
} else {

src/main/kotlin/org/move/cli/runConfigurations/aptos/Aptos.kt

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import com.intellij.util.execution.ParametersListUtil
1313
import org.move.cli.MoveProject
1414
import org.move.cli.MvConstants
1515
import org.move.cli.runConfigurations.AptosCommandLine
16+
import org.move.cli.settings.moveSettings
1617
import org.move.openapiext.*
1718
import org.move.openapiext.common.isUnitTestMode
19+
import org.move.stdext.CollectionBuilder
1820
import org.move.stdext.RsResult
1921
import org.move.stdext.RsResult.Err
2022
import org.move.stdext.RsResult.Ok
@@ -61,41 +63,31 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
6163
}
6264

6365
fun fetchPackageDependencies(
64-
projectDir: Path,
65-
skipLatest: Boolean,
66+
project: Project,
67+
packageRoot: Path,
6668
runner: CapturingProcessHandler.() -> ProcessOutput = { runProcessWithGlobalProgress() }
6769
): AptosProcessResult<Unit> {
6870
val commandLine =
6971
AptosCommandLine(
7072
subCommand = "move compile",
71-
arguments = listOfNotNull(
72-
"--skip-fetch-latest-git-deps".takeIf { skipLatest }
73-
),
74-
workingDirectory = projectDir
73+
arguments = compileArguments(project),
74+
workingDirectory = packageRoot
7575
)
7676
return executeAptosCommandLine(commandLine, colored = true, runner = runner)
7777
}
7878

79-
fun checkProject(args: AptosCompileArgs): RsResult<ProcessOutput, RsProcessExecutionException.Start> {
79+
fun checkProject(
80+
project: Project,
81+
args: AptosCompileArgs
82+
): RsResult<ProcessOutput, RsProcessExecutionException.Start> {
8083
// val useClippy = args.linter == ExternalLinter.CLIPPY
8184
// && !checkNeedInstallClippy(project, args.cargoProjectDirectory)
8285
// val checkCommand = if (useClippy) "clippy" else "check"
8386
val extraArguments = ParametersListUtil.parse(args.extraArguments)
8487
val commandLine =
8588
AptosCommandLine(
8689
"move compile",
87-
buildList {
88-
// add("--message-format=json")
89-
if ("--skip-fetch-latest-git-deps" !in extraArguments) {
90-
add("--skip-fetch-latest-git-deps")
91-
}
92-
if (args.enableMove2) {
93-
if ("--move-2" !in extraArguments) {
94-
add("--move-2")
95-
}
96-
}
97-
addAll(ParametersListUtil.parse(args.extraArguments))
98-
},
90+
arguments = compileArguments(project) { addAll(extraArguments) },
9991
args.moveProjectDirectory,
10092
)
10193
return executeCommandLine(commandLine).ignoreExitCode()
@@ -207,6 +199,23 @@ data class Aptos(val cliLocation: Path, val parentDisposable: Disposable?): Disp
207199
return Ok(aptosProcessOutput)
208200
}
209201

202+
private fun compileArguments(
203+
project: Project,
204+
builder: (CollectionBuilder<String>.() -> Unit)? = null
205+
): List<String> {
206+
val settings = project.moveSettings
207+
val initialArguments = buildList { builder?.let { this.it() } }
208+
return buildList {
209+
addAll(initialArguments)
210+
if (settings.enableMove2 && "--move-2" !in initialArguments) {
211+
add("--move-2")
212+
}
213+
if (settings.skipFetchLatestGitDeps && "--skip-fetch-latest-git-deps" !in initialArguments) {
214+
add("--skip-fetch-latest-git-deps")
215+
}
216+
}
217+
}
218+
210219
override fun dispose() {}
211220
}
212221

src/main/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatus.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package org.move.cli.runConfigurations.aptos
22

33
import com.fasterxml.jackson.core.JacksonException
44
import com.fasterxml.jackson.databind.DeserializationFeature
5+
import com.fasterxml.jackson.databind.JsonNode
56
import com.fasterxml.jackson.databind.ObjectMapper
67
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
78
import org.intellij.lang.annotations.Language
9+
import org.move.stdext.blankToNull
810

911
sealed class AptosExitStatus(val message: String) {
1012
class Result(message: String): AptosExitStatus(message)
@@ -14,22 +16,23 @@ sealed class AptosExitStatus(val message: String) {
1416
companion object {
1517
@Throws(JacksonException::class)
1618
fun fromJson(@Language("JSON") json: String): AptosExitStatus {
17-
val parsedResult = JSON_MAPPER.readValue(json, AptosJsonResult::class.java)
18-
return when {
19-
parsedResult.Error != null -> Error(parsedResult.Error)
20-
parsedResult.Result != null -> Result(parsedResult.Result)
21-
else -> Malformed(json)
19+
val rootNode = JSON_MAPPER.readTree(json)
20+
if (rootNode.has("Result")) {
21+
return Result(nodeToText(rootNode.get("Result")))
2222
}
23+
if (rootNode.has("Error")) {
24+
return Error(nodeToText(rootNode.get("Error")))
25+
}
26+
return Malformed(json)
27+
}
28+
29+
private fun nodeToText(jsonNode: JsonNode): String {
30+
// blank for containers
31+
return jsonNode.asText().blankToNull() ?: jsonNode.toString()
2332
}
2433
}
2534
}
2635

27-
@Suppress("PropertyName")
28-
private data class AptosJsonResult(
29-
val Result: String?,
30-
val Error: String?
31-
)
32-
3336
private val JSON_MAPPER: ObjectMapper = ObjectMapper()
3437
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
3538
.registerKotlinModule()

src/main/kotlin/org/move/ide/annotator/RsExternalLinterUtils.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object RsExternalLinterUtils {
106106

107107
override fun run(indicator: ProgressIndicator) {
108108
widget?.inProgress = true
109-
future.complete(check(aptosCli, args))
109+
future.complete(check(project, aptosCli, args))
110110
}
111111

112112
override fun onFinished() {
@@ -118,13 +118,14 @@ object RsExternalLinterUtils {
118118
}
119119

120120
private fun check(
121+
project: Project,
121122
aptosCli: Aptos,
122123
aptosCompileArgs: AptosCompileArgs
123124
): RsExternalLinterResult? {
124125
ProgressManager.checkCanceled()
125126
val started = Instant.now()
126127
val output = aptosCli
127-
.checkProject(aptosCompileArgs)
128+
.checkProject(project, aptosCompileArgs)
128129
.unwrapOrElse { e ->
129130
LOG.error(e)
130131
return null

src/main/kotlin/org/move/ide/refactoring/MvImportOptimizer.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ class MvImportOptimizer : ImportOptimizer {
8080
private fun removeCurlyBracesIfPossible(rootUseSpeck: MvUseSpeck, psiFactory: MvPsiFactory) {
8181
val itemUseSpeck = rootUseSpeck.useGroup?.asTrivial ?: return
8282

83-
val newUseSpeck = psiFactory.useSpeck("0x1::dummy::call")
83+
val newUseSpeck = psiFactory.useSpeck("0x1::dummy::call as mycall")
8484
val newUseSpeckPath = newUseSpeck.path
8585
newUseSpeckPath.path?.replace(rootUseSpeck.path)
8686
itemUseSpeck.path.identifier?.let { newUseSpeckPath.identifier?.replace(it) }
8787

88-
val useAlias = itemUseSpeck.useAlias
89-
if (useAlias != null) {
90-
newUseSpeck.add(useAlias)
88+
val oldUseAlias = itemUseSpeck.useAlias
89+
if (oldUseAlias != null) {
90+
newUseSpeck.useAlias?.replace(oldUseAlias)
91+
} else {
92+
newUseSpeck.useAlias?.delete()
9193
}
9294

9395
rootUseSpeck.replace(newUseSpeck)

src/main/kotlin/org/move/openapiext/utils.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ inline fun <R> Project.nonBlocking(crossinline block: () -> R, crossinline uiCon
208208
}
209209

210210
@Service(PROJECT)
211-
class RootPluginDisposable: Disposable {
212-
override fun dispose() {}
213-
}
211+
class RootPluginDisposable(val project: Project): Disposable.Default
214212

215213
val Project.rootPluginDisposable get() = this.service<RootPluginDisposable>()
216214

src/test/kotlin/org/move/cli/runConfigurations/aptos/AptosExitStatusTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,23 @@ class AptosExitStatusTest: MvTestBase() {
3737
)
3838
check(status is AptosExitStatus.Malformed)
3939
}
40+
41+
fun `test parse array of items`() {
42+
val status = AptosExitStatus.fromJson(
43+
"""
44+
{
45+
"Result": [
46+
"0000000000000000000000000000000000000000000000000000000000000001::system_addresses",
47+
"0000000000000000000000000000000000000000000000000000000000000001::guid"
48+
]
49+
}
50+
}
51+
"""
52+
)
53+
check(status is AptosExitStatus.Result)
54+
check(status.message ==
55+
"[\"0000000000000000000000000000000000000000000000000000000000000001::system_addresses\"," +
56+
"\"0000000000000000000000000000000000000000000000000000000000000001::guid\"]",
57+
{ status.message })
58+
}
4059
}

src/test/kotlin/org/move/ide/refactoring/optimizeImports/OptimizeImportsTest.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,32 @@ module 0x1::main {
321321
}
322322
""")
323323

324+
fun `test unused import with Self as`() = doTest("""
325+
module 0x1::pool {
326+
struct X1 {}
327+
public fun create_pool() {}
328+
}
329+
module 0x1::main {
330+
use 0x1::pool::{Self as mypool, X1};
331+
332+
fun main() {
333+
mypool::create_pool();
334+
}
335+
}
336+
""", """
337+
module 0x1::pool {
338+
struct X1 {}
339+
public fun create_pool() {}
340+
}
341+
module 0x1::main {
342+
use 0x1::pool::Self as mypool;
343+
344+
fun main() {
345+
mypool::create_pool();
346+
}
347+
}
348+
""")
349+
324350
fun `test duplicate self import`() = doTest("""
325351
module 0x1::pool {
326352
struct X1 {}
@@ -377,5 +403,33 @@ module 0x1::main {
377403
// let _a = string::utf8(b"hello");
378404
// }
379405
//}
406+
// """)
407+
408+
// todo
409+
// fun `test vector import should be test_only if used only in tests`() = doTest("""
410+
//module 0x1::vector {
411+
// public fun call() {}
412+
//}
413+
//module 0x1::main {
414+
// use 0x1::vector;
415+
//
416+
// #[test]
417+
// fun test_main() {
418+
// vector::call();
419+
// }
420+
//}
421+
// """, """
422+
//module 0x1::vector {
423+
// public fun call() {}
424+
//}
425+
//module 0x1::main {
426+
// #[test_only]
427+
// use 0x1::vector;
428+
//
429+
// #[test]
430+
// fun test_main() {
431+
// vector::call();
432+
// }
433+
//}
380434
// """)
381435
}

0 commit comments

Comments
 (0)