Skip to content

Commit

Permalink
highlight not supported index expr in compiler v1 with quickfix
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Jul 3, 2024
1 parent c49f019 commit 4eb5ac0
Show file tree
Hide file tree
Showing 20 changed files with 122 additions and 24 deletions.
11 changes: 11 additions & 0 deletions src/main/kotlin/org/move/ide/annotator/MvSyntaxErrorAnnotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.move.ide.annotator
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import org.move.cli.settings.moveSettings
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.*
import org.move.lang.utils.Diagnostic
Expand All @@ -20,6 +21,7 @@ class MvSyntaxErrorAnnotator: MvAnnotatorBase() {
override fun visitStruct(s: MvStruct) = checkStruct(moveHolder, s)
override fun visitFunction(o: MvFunction) = checkFunction(moveHolder, o)
override fun visitSpecFunction(o: MvSpecFunction) = checkSpecFunction(moveHolder, o)
override fun visitIndexExpr(o: MvIndexExpr) = checkIndexExpr(moveHolder, o)
}
element.accept(visitor)
}
Expand Down Expand Up @@ -56,6 +58,14 @@ class MvSyntaxErrorAnnotator: MvAnnotatorBase() {
}
}

private fun checkIndexExpr(holder: MvAnnotationHolder, indexExpr: MvIndexExpr) {
if (!indexExpr.project.moveSettings.enableIndexExpr) {
Diagnostic
.IndexExprIsNotAllowed(indexExpr)
.addToHolder(holder)
}
}

private fun checkStruct(holder: MvAnnotationHolder, struct: MvStruct) {
val native = struct.native ?: return
val errorRange = TextRange.create(native.startOffset, struct.structKw.endOffset)
Expand Down Expand Up @@ -154,6 +164,7 @@ class MvSyntaxErrorAnnotator: MvAnnotatorBase() {
}
}

@Suppress("CompanionObjectInExtension")
companion object {
private val INTEGER_WITH_SUFFIX_REGEX =
Regex("([0-9a-zA-Z_]+)(u[0-9]{1,4})")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.move.lang.core.psi.*
class WrapWithParensExprFix(castExpr: MvCastExpr) : DiagnosticIntentionFix<MvCastExpr>(castExpr) {
override fun generatePreview(project: Project, previewDescriptor: ProblemDescriptor): IntentionPreviewInfo =
IntentionPreviewInfo.EMPTY

override fun getFamilyName(): String = "Wrap cast with ()"
override fun getText(): String = "Wrap cast with ()"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.move.ide.inspections

import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
import com.intellij.codeInspection.*
import com.intellij.model.SideEffectGuard
import com.intellij.model.SideEffectGuard.EffectType.EXEC
import com.intellij.model.SideEffectGuard.EffectType.SETTINGS
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
Expand Down Expand Up @@ -107,6 +110,9 @@ abstract class DiagnosticIntentionFix<T : PsiElement>(element: T) :
override fun generatePreview(project: Project, previewDescriptor: ProblemDescriptor): IntentionPreviewInfo =
IntentionPreviewInfo.EMPTY

override fun generatePreview(project: Project, editor: Editor, file: PsiFile): IntentionPreviewInfo =
IntentionPreviewInfo.EMPTY

override fun getFamilyName(): String = text

override fun isAvailable(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.move.ide.inspections.fixes

import com.intellij.model.SideEffectGuard
import com.intellij.model.SideEffectGuard.EffectType.SETTINGS
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import io.ktor.http.*
import org.move.cli.settings.moveSettings
import org.move.ide.inspections.DiagnosticIntentionFix
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.RESOURCE_CONTROL

enum class CompilerV2Feat(val title: String) {
INDEXING("Index notation"),
RESOURCE_CONTROL("Resource access control");
}

class EnableCompilerV2FeatureFix(
element: PsiElement,
val feature: CompilerV2Feat
):
DiagnosticIntentionFix<PsiElement>(element) {

override fun getText(): String =
"Enable ${feature.title.quote()} feature of Aptos Move V2 Compiler in the plugin settings"

override fun invoke(project: Project, file: PsiFile, element: PsiElement) {
@Suppress("UnstableApiUsage")
SideEffectGuard.checkSideEffectAllowed(SETTINGS)
project.moveSettings.modify {
when (feature) {
INDEXING -> it.enableIndexExpr = true
RESOURCE_CONTROL -> it.enableResourceAccessControl = true
}
}
}
}
13 changes: 13 additions & 0 deletions src/main/kotlin/org/move/lang/utils/Diagnostic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import org.move.ide.annotator.MvAnnotationHolder
import org.move.ide.annotator.fixes.ItemSpecSignatureFix
import org.move.ide.annotator.fixes.WrapWithParensExprFix
import org.move.ide.annotator.pluralise
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.EnableCompilerV2FeatureFix
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.endOffset
import org.move.lang.core.psi.ext.itemSpecBlock
Expand Down Expand Up @@ -170,6 +172,17 @@ sealed class Diagnostic(
)
}
}

class IndexExprIsNotAllowed(indexExpr: MvIndexExpr): Diagnostic(indexExpr) {

override fun prepare(): PreparedAnnotation {
return PreparedAnnotation(
ERROR,
"Index operator is not supported in Aptos Move V1 outside specs",
fixes = listOf(EnableCompilerV2FeatureFix(element, INDEXING))
)
}
}
}

enum class Severity {
Expand Down
11 changes: 3 additions & 8 deletions src/main/kotlin/org/move/utils/tests/MvTestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ package org.move.utils.tests

import com.intellij.codeInspection.InspectionProfileEntry
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.psi.PsiElement
import com.intellij.testFramework.UsefulTestCase
import com.intellij.testFramework.enableInspectionTool
import org.intellij.lang.annotations.Language
import org.move.cli.settings.MvProjectSettingsService
import org.move.cli.settings.moveSettings
import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Feat.RESOURCE_CONTROL
import org.move.ide.inspections.fixes.CompilerV2Feat
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.RESOURCE_CONTROL
import org.move.utils.tests.base.MvTestCase
import org.move.utils.tests.base.TestCase
import org.move.utils.tests.base.findElementsWithDataAndOffsetInEditor
Expand All @@ -33,10 +32,6 @@ annotation class DebugMode(val enabled: Boolean)
@Retention(AnnotationRetention.RUNTIME)
annotation class WithEnabledInspections(vararg val inspections: KClass<out InspectionProfileEntry>)

enum class CompilerV2Feat {
INDEXING, RESOURCE_CONTROL;
}

@Inherited
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.move.ide.annotator

import org.move.ide.colors.MvColor
import org.move.utils.tests.CompilerV2Feat.RESOURCE_CONTROL
import org.move.ide.inspections.fixes.CompilerV2Feat.RESOURCE_CONTROL
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.annotation.AnnotatorTestCase

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.move.ide.annotator.syntaxErrors.compilerV2

import org.move.ide.annotator.MvSyntaxErrorAnnotator
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.annotation.AnnotatorTestCase

class IndexExprTest: AnnotatorTestCase(MvSyntaxErrorAnnotator::class) {
@CompilerV2Features()
fun `test vector index expr in not allowed in main code with compiler v1`() = checkWarnings("""
module 0x1::m {
fun main() {
let v = vector[1, 2];
<error descr="Index operator is not supported in Aptos Move V1 outside specs">v[1]</error>;
}
}
""")

@CompilerV2Features()
fun `test resource index expr in not allowed in main code with compiler v1`() = checkWarnings("""
module 0x1::m {
fun main() {
let v = vector[1, 2];
<error descr="Index operator is not supported in Aptos Move V1 outside specs">v[1]</error>;
}
}
""")

@CompilerV2Features(INDEXING)
fun `test no error with index expr in compiler v2`() = checkWarnings("""
module 0x1::m {
struct S has key { field: u8 }
fun main() {
let v1 = vector[1, 2];
v1[1];
S[@0x1].field = 11;
}
}
""")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.move.ide.inspections

import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.annotation.InspectionTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.move.ide.inspections

import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.annotation.InspectionTestBase

class MvTypeCheckInspectionTest: InspectionTestBase(MvTypeCheckInspection::class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.move.ide.inspections

import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.annotation.InspectionTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.move.ide.inspections

import org.intellij.lang.annotations.Language
import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.FileTreeBuilder
import org.move.utils.tests.annotation.InspectionProjectTestBase
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.move.ide.inspections

import org.intellij.lang.annotations.Language
import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.annotation.InspectionTestBase

class ReplaceWithMethodCallInspectionTest: InspectionTestBase(ReplaceWithMethodCallInspection::class) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.move.ide.inspections.compilerV2

import org.move.ide.inspections.MvTypeCheckInspection
import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.annotation.InspectionTestBase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.move.lang.completion

import org.move.utils.tests.CompilerV2Feat.RESOURCE_CONTROL
import org.move.ide.inspections.fixes.CompilerV2Feat.RESOURCE_CONTROL
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.completion.CompletionTestCase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.move.lang.parser

import org.move.utils.tests.CompilerV2Feat.RESOURCE_CONTROL
import org.move.ide.inspections.fixes.CompilerV2Feat.RESOURCE_CONTROL
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.parser.MvParsingTestCase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.move.lang.resolve

import org.move.utils.tests.CompilerV2Feat.RESOURCE_CONTROL
import org.move.ide.inspections.fixes.CompilerV2Feat.RESOURCE_CONTROL
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.resolve.ResolveTestCase

Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/org/move/lang/resolve/ResolveTypesTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.move.lang.resolve

import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.resolve.ResolveTestCase

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.move.lang.types

import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.types.TypificationTestCase

class ExpressionTypeInferenceTest: TypificationTestCase() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.move.lang.types.compilerV2

import org.move.utils.tests.CompilerV2Feat.INDEXING
import org.move.ide.inspections.fixes.CompilerV2Feat.INDEXING
import org.move.utils.tests.CompilerV2Features
import org.move.utils.tests.types.TypificationTestCase

Expand Down

0 comments on commit 4eb5ac0

Please sign in to comment.