From def6fe306f2a71fa57960fb67936045426da68d6 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Sat, 29 Oct 2022 13:10:55 +0200 Subject: [PATCH] Added assembler directives to code completion. --- build.gradle | 2 + .../asm/M68kDirectiveCompletionContributor.kt | 27 ++++++++ .../M68kLocalLabelDefCompletionContributor.kt | 2 +- src/main/resources/META-INF/plugin.xml | 1 + .../M68kDirectiveCompletionContributorTest.kt | 63 +++++++++++++++++++ .../M68kMnemonicCompletionContributorTest.kt | 4 +- .../M68kMacroCallCompletionContributorTest.kt | 2 +- 7 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributor.kt create mode 100644 src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributorTest.kt diff --git a/build.gradle b/build.gradle index 5ee55a4..c3c9084 100644 --- a/build.gradle +++ b/build.gradle @@ -67,6 +67,8 @@ patchPluginXml {
  • Bugfix: addq/subq for address register stated it would affect the condition codes, which it in fact doesn't.
  • New: Added simple custom navigation bar.
  • New: Added folding support for functions and macro definitions. +
  • New: Added assembler directives to code completion (only lower-case except for other directives like IF + and MACRO, which are only suggested for upper-case).

    Full changelog available at Github project site.

    """) diff --git a/src/main/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributor.kt b/src/main/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributor.kt new file mode 100644 index 0000000..8687a90 --- /dev/null +++ b/src/main/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributor.kt @@ -0,0 +1,27 @@ +package de.platon42.intellij.plugins.m68k.asm + +import com.intellij.codeInsight.completion.* +import com.intellij.codeInsight.lookup.LookupElementBuilder +import com.intellij.patterns.PlatformPatterns +import com.intellij.util.ProcessingContext +import de.platon42.intellij.plugins.m68k.M68kIcons +import de.platon42.intellij.plugins.m68k.psi.M68kTypes + +class M68kDirectiveCompletionContributor : CompletionContributor() { + + companion object { + val DIRECTIVES = + listOf(AssemblerDirectives.dataDirectives, AssemblerDirectives.plainDirectives, AssemblerDirectives.otherDirectives.map(String::uppercase)) + .flatten() + .toSortedSet() + .map { PrioritizedLookupElement.withPriority(LookupElementBuilder.create(it).withIcon(M68kIcons.MNEMONIC), 1.5) } + } + + init { + extend(CompletionType.BASIC, PlatformPatterns.psiElement(M68kTypes.MACRO_INVOCATION), object : CompletionProvider() { + override fun addCompletions(parameters: CompletionParameters, context: ProcessingContext, resultSet: CompletionResultSet) { + resultSet.addAllElements(DIRECTIVES) + } + }) + } +} \ No newline at end of file diff --git a/src/main/java/de/platon42/intellij/plugins/m68k/refs/M68kLocalLabelDefCompletionContributor.kt b/src/main/java/de/platon42/intellij/plugins/m68k/refs/M68kLocalLabelDefCompletionContributor.kt index 51931d0..a288628 100644 --- a/src/main/java/de/platon42/intellij/plugins/m68k/refs/M68kLocalLabelDefCompletionContributor.kt +++ b/src/main/java/de/platon42/intellij/plugins/m68k/refs/M68kLocalLabelDefCompletionContributor.kt @@ -32,7 +32,7 @@ class M68kLocalLabelDefCompletionContributor : CompletionContributor() { } referencedLocalLabels.removeAll(definedLocalLabels) resultSet.addAllElements( - if (parameters.originalPosition?.text == ".") { + if (parameters.originalPosition?.text?.startsWith(".") == true) { referencedLocalLabels.map { LookupElementBuilder.create(it.removePrefix(".")) } } else { referencedLocalLabels.map(LookupElementBuilder::create) diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 29e6add..61cde09 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -28,6 +28,7 @@ + diff --git a/src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributorTest.kt b/src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributorTest.kt new file mode 100644 index 0000000..95f6022 --- /dev/null +++ b/src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kDirectiveCompletionContributorTest.kt @@ -0,0 +1,63 @@ +package de.platon42.intellij.plugins.m68k.asm + +import com.intellij.testFramework.fixtures.CodeInsightTestFixture +import de.platon42.intellij.jupiter.LightCodeInsightExtension +import de.platon42.intellij.jupiter.MyFixture +import de.platon42.intellij.plugins.m68k.AbstractM68kTest +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@ExtendWith(LightCodeInsightExtension::class) +internal class M68kDirectiveCompletionContributorTest : AbstractM68kTest() { + + @Test + internal fun completion_shows_plain_directive(@MyFixture myFixture: CodeInsightTestFixture) { + myFixture.configureByText( + "completeme.asm", """ + inc + """ + ) + myFixture.completeBasic() + assertThat(myFixture.lookupElementStrings).containsExactlyInAnyOrder("include", "incbin", "incdir") + } + + @Test + internal fun completion_shows_other_directive(@MyFixture myFixture: CodeInsightTestFixture) { + myFixture.configureByText( + "completeme.asm", """ + IF + """ + ) + myFixture.completeBasic() + assertThat(myFixture.lookupElementStrings).containsExactlyInAnyOrder( + "IF", + "IFB", + "IFC", + "IFD", + "IFEQ", + "IFGE", + "IFGT", + "IFLE", + "IFLT", + "IFMACROD", + "IFMACROND", + "IFNB", + "IFNC", + "IFND", + "IFNE", + "ENDIF" + ) + } + + @Test + internal fun completion_shows_data_directives(@MyFixture myFixture: CodeInsightTestFixture) { + myFixture.configureByText( + "completeme.asm", """ + dc + """ + ) + myFixture.completeBasic() + assertThat(myFixture.lookupElementStrings).containsExactlyInAnyOrder("dc", "dc.b", "dc.w", "dc.l", "dcb", "dcb.b", "dcb.w", "dcb.l", "data_c") + } +} \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kMnemonicCompletionContributorTest.kt b/src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kMnemonicCompletionContributorTest.kt index 29da551..d991045 100644 --- a/src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kMnemonicCompletionContributorTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/m68k/asm/M68kMnemonicCompletionContributorTest.kt @@ -30,7 +30,7 @@ label: """ ) myFixture.completeBasic() - assertThat(myFixture.lookupElementStrings).hasSameElementsAs(M68kIsa.mnemonics) + assertThat(myFixture.lookupElementStrings).containsAnyElementsOf(M68kIsa.mnemonics) } @Test @@ -41,6 +41,6 @@ label: """ ) myFixture.completeBasic() - assertThat(myFixture.lookupElementStrings).hasSameElementsAs(M68kIsa.mnemonics) + assertThat(myFixture.lookupElementStrings).containsAnyElementsOf(M68kIsa.mnemonics) } } \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/m68k/refs/M68kMacroCallCompletionContributorTest.kt b/src/test/java/de/platon42/intellij/plugins/m68k/refs/M68kMacroCallCompletionContributorTest.kt index d69f6c6..cd373c3 100644 --- a/src/test/java/de/platon42/intellij/plugins/m68k/refs/M68kMacroCallCompletionContributorTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/m68k/refs/M68kMacroCallCompletionContributorTest.kt @@ -27,6 +27,6 @@ noppy MACRO """ ) myFixture.completeBasic() - assertThat(myFixture.lookupElementStrings).containsExactlyInAnyOrder("nop", "not", "noppy") + assertThat(myFixture.lookupElementStrings).containsExactlyInAnyOrder("nop", "not", "noppy", "cnop") } } \ No newline at end of file