From f87bc7fea92e3f50d60909389b18e7e64e8e5782 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Thu, 27 Oct 2022 13:31:34 +0200 Subject: [PATCH] Added simple custom navigation bar. --- build.gradle | 11 +-- .../navigation/M68kStructureAwareNavbar.kt | 75 +++++++++++++++++++ .../plugins/m68k/psi/utils/M68kPsiWalkUtil.kt | 10 +++ src/main/resources/META-INF/plugin.xml | 1 + 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/platon42/intellij/plugins/m68k/navigation/M68kStructureAwareNavbar.kt diff --git a/build.gradle b/build.gradle index 37c380f..4e43411 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ plugins { id 'java' - id 'org.jetbrains.intellij' version '1.8.0' - id 'org.jetbrains.kotlin.jvm' version '1.7.10' + id 'org.jetbrains.intellij' version '1.9.0' + id 'org.jetbrains.kotlin.jvm' version '1.7.20' id 'jacoco' id 'com.github.kt3k.coveralls' version '2.12.0' } @@ -43,19 +43,19 @@ compileTestKotlin { } intellij { - setVersion("2022.2") // LATEST-EAP-SNAPSHOT + setVersion("2022.2.3") // LATEST-EAP-SNAPSHOT setUpdateSinceUntilBuild(false) // setPlugins(["com.intellij.java"]) } runPluginVerifier { - ideVersions = ["IC-203.6682.168", "IC-222.3345.118", // 2020.3 - 2022.2 + ideVersions = ["IC-203.6682.168", "IC-222.4345.14", // 2020.3 - 2022.2.3 "CL-203.8084.11", // 2020.3 "CL-211.7628.27", // 2021.1 "CL-212.5712.21", // 2021.2 "CL-213.7172.20", // 2021.3.4 "CL-221.5080.224", // 2022.1 - "CL-222.3345.126"] // 2022.2 + "CL-222.3345.126"] // 2022.2.4 downloadDir = System.getProperty("user.home") + "/.gradle/caches/modules-2/files-2.1/com.jetbrains.intellij.idea/verifier" } @@ -65,6 +65,7 @@ patchPluginXml {

Full changelog available at Github project site.

""") diff --git a/src/main/java/de/platon42/intellij/plugins/m68k/navigation/M68kStructureAwareNavbar.kt b/src/main/java/de/platon42/intellij/plugins/m68k/navigation/M68kStructureAwareNavbar.kt new file mode 100644 index 0000000..cc369e6 --- /dev/null +++ b/src/main/java/de/platon42/intellij/plugins/m68k/navigation/M68kStructureAwareNavbar.kt @@ -0,0 +1,75 @@ +package de.platon42.intellij.plugins.m68k.navigation + +import com.intellij.ide.navigationToolbar.AbstractNavBarModelExtension +import com.intellij.ide.ui.UISettings +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.project.IndexNotReadyException +import com.intellij.psi.PsiElement +import com.intellij.psi.util.PsiTreeUtil +import de.platon42.intellij.plugins.m68k.M68kIcons +import de.platon42.intellij.plugins.m68k.MC68000Language +import de.platon42.intellij.plugins.m68k.psi.* +import de.platon42.intellij.plugins.m68k.psi.utils.M68kPsiWalkUtil.findStatementForElement +import org.jetbrains.annotations.Nullable +import javax.swing.Icon + +class M68kStructureAwareNavbar : AbstractNavBarModelExtension() { + + override fun getLeafElement(dataContext: DataContext): PsiElement? { + if (UISettings.getInstance().showMembersInNavigationBar) { + val psiFile = CommonDataKeys.PSI_FILE.getData(dataContext) + val editor = CommonDataKeys.EDITOR.getData(dataContext) + if (psiFile == null || !psiFile.isValid || editor == null) return null + val psiElement = psiFile.findElementAt(editor.caretModel.offset) + if (isAcceptableLanguage(psiElement)) { + try { + var statement = findStatementForElement(psiElement!!) ?: return null + do { + if (statement.localLabel != null) return statement.localLabel + if (statement.globalLabel != null) return statement.globalLabel + statement = PsiTreeUtil.getPrevSiblingOfType(statement, M68kStatement::class.java) ?: return null + } while (true) + } catch (ignored: IndexNotReadyException) { + } + } + } + return null + } + + private fun isAcceptableLanguage(psiElement: @Nullable PsiElement?) = psiElement?.language == MC68000Language.INSTANCE + + override fun getPresentableText(obj: Any?): String? { + return when (obj) { + is M68kFile -> obj.name + is M68kGlobalLabel -> obj.name + is M68kLocalLabel -> obj.name + is M68kSymbolDefinition -> obj.name + is M68kMacroDefinition -> obj.macroNameDefinition.text + else -> null + } + } + + override fun getParent(psiElement: PsiElement): PsiElement? { + if (!isAcceptableLanguage(psiElement)) return null + var statement = findStatementForElement(psiElement) ?: return null + if (statement.localLabel != null) { + do { + if (statement.globalLabel != null) return statement.globalLabel + statement = PsiTreeUtil.getPrevSiblingOfType(statement, M68kStatement::class.java) ?: return null + } while (true) + } + return psiElement.containingFile + } + + override fun getIcon(obj: Any?): Icon? { + return when (obj) { + is M68kFile -> M68kIcons.FILE + is M68kGlobalLabel -> M68kIcons.GLOBAL_LABEL + is M68kLocalLabel -> M68kIcons.LOCAL_LABEL + is M68kSymbolDefinition -> M68kIcons.SYMBOL_DEF + is M68kMacroDefinition -> M68kIcons.MACRO_DEF + else -> null + } + } +} \ No newline at end of file diff --git a/src/main/java/de/platon42/intellij/plugins/m68k/psi/utils/M68kPsiWalkUtil.kt b/src/main/java/de/platon42/intellij/plugins/m68k/psi/utils/M68kPsiWalkUtil.kt index a2c53aa..2e48435 100644 --- a/src/main/java/de/platon42/intellij/plugins/m68k/psi/utils/M68kPsiWalkUtil.kt +++ b/src/main/java/de/platon42/intellij/plugins/m68k/psi/utils/M68kPsiWalkUtil.kt @@ -5,7 +5,9 @@ import com.intellij.psi.PsiElement import com.intellij.psi.util.PsiTreeUtil import com.intellij.util.SmartList import com.intellij.util.containers.addIfNotNull +import de.platon42.intellij.plugins.m68k.psi.M68kFile import de.platon42.intellij.plugins.m68k.psi.M68kMacroDefinition +import de.platon42.intellij.plugins.m68k.psi.M68kStatement object M68kPsiWalkUtil { @@ -28,4 +30,12 @@ object M68kPsiWalkUtil { return comments.reversed() } + + fun findStatementForElement(psiElement: PsiElement): M68kStatement? { + if (psiElement is M68kStatement) return psiElement + if (psiElement.parent is M68kFile) { + return PsiTreeUtil.getPrevSiblingOfType(psiElement, M68kStatement::class.java) + } + return PsiTreeUtil.getParentOfType(psiElement, M68kStatement::class.java); + } } \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 3accb3f..f38a04f 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -21,6 +21,7 @@ implementationClass="de.platon42.intellij.plugins.m68k.parser.M68kParserDefinition"/> +