From d76438fb92dcdc61980298b0b2032d32f96e26aa Mon Sep 17 00:00:00 2001 From: caixuan Date: Wed, 20 Dec 2023 18:38:45 +0800 Subject: [PATCH 01/10] Support protobuf plugin by kanro --- build.gradle.kts | 18 ++++++ gradle.properties | 6 +- .../BufAnalyzeModificationTracker.kt | 7 ++- .../buf/intellij/annotator/BufAnalyzePass.kt | 14 ++--- .../buf/intellij/annotator/BufAnalyzeUtils.kt | 4 +- .../intellij/fixes/IgnoreBufIssueQuickFix.kt | 4 +- .../intellij/formatter/BufFormatterService.kt | 4 +- .../inspections/BufAnalyzeInspection.kt | 4 +- .../inspections/BufNotInstalledInspection.kt | 4 +- .../kotlin/build/buf/intellij/vendor/Utils.kt | 46 +++++++++++++++ .../jetbrains}/BufProtoFileResolver.kt | 3 +- .../vendor/kanro/BufProtoFileResolver.kt | 57 +++++++++++++++++++ .../kanro-protobuf-plugin-support.xml | 5 ++ .../official-protobuf-plugin-support.xml | 5 ++ src/main/resources/META-INF/plugin.xml | 6 +- 15 files changed, 159 insertions(+), 28 deletions(-) create mode 100644 src/main/kotlin/build/buf/intellij/vendor/Utils.kt rename src/main/kotlin/build/buf/intellij/{resolve => vendor/jetbrains}/BufProtoFileResolver.kt (97%) create mode 100644 src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt create mode 100644 src/main/resources/META-INF/kanro-protobuf-plugin-support.xml create mode 100644 src/main/resources/META-INF/official-protobuf-plugin-support.xml diff --git a/build.gradle.kts b/build.gradle.kts index 5224e0d4..8ce50d62 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ import org.jetbrains.changelog.Changelog import org.jetbrains.changelog.markdownToHTML +import org.jetbrains.kotlin.gradle.internal.ensureParentDirsCreated fun properties(key: String) = providers.gradleProperty(key) fun environment(key: String) = providers.environmentVariable(key) @@ -189,6 +190,23 @@ tasks { password = environment("PRIVATE_KEY_PASSWORD") } + prepareSandbox { + doLast { + val file = project.layout.buildDirectory.file("idea-sandbox/config/disabled_plugins.txt").get().asFile + file.ensureParentDirsCreated() + file.writeText( + buildString { + // Comment this to use Protobuf plugin by JetBrains + // appendLine("idea.plugin.protoeditor") + // appendLine("com.intellij.grpc") + + // Comment this to use Protobuf plugin by Kanro + appendLine("io.kanro.idea.plugin.protobuf") + }, + ) + } + } + publishPlugin { dependsOn("patchChangelog") token = environment("PUBLISH_TOKEN") diff --git a/gradle.properties b/gradle.properties index a2794588..d106ffa0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,14 +7,14 @@ pluginRepositoryUrl=https://github.com/bufbuild/intellij-buf pluginVersion=0.2.1 # See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # for insight into build numbers and IntelliJ Platform versions. -pluginSinceBuild=223 +pluginSinceBuild=233 pluginUntilBuild=999.* # IntelliJ Platform Properties -> https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties platformType=IU -platformVersion=2022.3.3 +platformVersion=2023.3 # Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html # Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22 -platformPlugins=idea.plugin.protoeditor, org.jetbrains.plugins.yaml +platformPlugins=idea.plugin.protoeditor, io.kanro.idea.plugin.protobuf:1.7.40, org.jetbrains.plugins.yaml # Gradle Releases -> https://github.com/gradle/gradle/releases gradleVersion=8.4 # Opt-out flag for bundling Kotlin standard library -> https://jb.gg/intellij-platform-kotlin-stdlib diff --git a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt index 5dd6f0ef..9b56e954 100644 --- a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt +++ b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt @@ -16,6 +16,7 @@ package build.buf.intellij.annotator import build.buf.intellij.config.BufConfig import build.buf.intellij.settings.BufProjectSettingsService +import build.buf.intellij.vendor.protobufLanguage import com.intellij.openapi.application.runReadAction import com.intellij.openapi.components.Service import com.intellij.openapi.components.service @@ -25,6 +26,7 @@ import com.intellij.protobuf.lang.PbLanguage import com.intellij.psi.search.FilenameIndex import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.util.PsiModificationTracker +import io.kanro.idea.plugin.protobuf.lang.ProtobufLanguage /** * Project level service to keep track of all changes to .proto files, Buf configuration files, and the plugin config. @@ -32,10 +34,11 @@ import com.intellij.psi.util.PsiModificationTracker */ @Service(Service.Level.PROJECT) class BufAnalyzeModificationTracker(private val project: Project) : ModificationTracker { - private val protoModificationTracker = PsiModificationTracker.getInstance(project).forLanguage(PbLanguage.INSTANCE) + private val protoModificationTracker = PsiModificationTracker.getInstance(project).forLanguage(protobufLanguage()!!) override fun getModificationCount(): Long { - var modificationCount : Long = protoModificationTracker.modificationCount + var modificationCount: Long = + protoModificationTracker.modificationCount runReadAction { for (configFile in BufConfig.CONFIG_FILES) { FilenameIndex.getVirtualFilesByName(configFile, GlobalSearchScope.projectScope(project)).forEach { diff --git a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzePass.kt b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzePass.kt index c7f49756..738a6f85 100644 --- a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzePass.kt +++ b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzePass.kt @@ -18,6 +18,7 @@ import build.buf.intellij.BufPluginService import build.buf.intellij.fixes.IgnoreBufIssueQuickFix import build.buf.intellij.model.BufIssue import build.buf.intellij.settings.bufSettings +import build.buf.intellij.vendor.isProtobufFile import com.intellij.codeHighlighting.DirtyScopeTrackingHighlightingPassFactory import com.intellij.codeHighlighting.MainHighlightingPassFactory import com.intellij.codeHighlighting.TextEditorHighlightingPass @@ -46,7 +47,6 @@ import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.util.Disposer import com.intellij.openapi.util.Key import com.intellij.openapi.util.TextRange -import com.intellij.protobuf.lang.psi.PbFile import com.intellij.psi.PsiFile import com.intellij.psi.impl.AnyPsiChangeListener import com.intellij.psi.impl.PsiManagerImpl @@ -75,7 +75,7 @@ class BufAnalyzePass( override fun doCollectInformation(progress: ProgressIndicator) { annotationHolder.clear() - if (file !is PbFile) return + if (!file.isProtobufFile()) return if (!myProject.bufSettings.state.backgroundLintingEnabled && !myProject.bufSettings.state.backgroundBreakingEnabled) return val contentRootForFile = ProjectFileIndex.getInstance(myProject) @@ -101,7 +101,7 @@ class BufAnalyzePass( } override fun doApplyInformationToEditor() { - if (file !is PbFile) return + if (!file.isProtobufFile()) return if (annotationInfo == null) { disposable = appService @@ -126,8 +126,6 @@ class BufAnalyzePass( } }) } - - override fun canEat(update: Update?): Boolean = true } if (isUnitTestMode) { @@ -138,7 +136,7 @@ class BufAnalyzePass( } private fun doApply(annotationResult: BufAnalyzeResult) { - if (file !is PbFile || !file.isValid) return + if (!file.isProtobufFile() || !file.isValid) return try { @Suppress("UnstableApiUsage") annotationHolder.runAnnotatorWithContext(file) { _, holder -> @@ -226,7 +224,7 @@ class BufAnalyzePassFactory( val LAST_ANALYZE_MOD_COUNT = Key.create("build.buf.analyze.mod_count") fun shouldRunPass(file: PsiFile): Boolean { - if (file !is PbFile) { + if (!file.isProtobufFile()) { return false } val analyzeModTracker = file.project.service() @@ -255,7 +253,7 @@ fun MessageBus.createDisposableOnAnyPsiChange(): Disposable { } fun AnnotationHolder.createAnnotationsForFile( - file: PbFile, + file: PsiFile, annotationResult: BufAnalyzeResult ) { val doc = file.viewProvider.document diff --git a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeUtils.kt b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeUtils.kt index aa522dba..99af971b 100644 --- a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeUtils.kt +++ b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeUtils.kt @@ -21,6 +21,7 @@ import build.buf.intellij.model.BufIssue import build.buf.intellij.settings.BufCLIUtils import build.buf.intellij.settings.bufSettings import build.buf.intellij.status.BufCLIWidget +import build.buf.intellij.vendor.isProtobufFile import com.intellij.execution.process.ProcessAdapter import com.intellij.execution.process.ProcessEvent import com.intellij.execution.process.ProcessOutputType @@ -40,7 +41,6 @@ import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.progress.Task import com.intellij.openapi.project.Project import com.intellij.openapi.wm.WindowManager -import com.intellij.protobuf.lang.psi.PbFile import com.intellij.psi.PsiDocumentManager import com.intellij.psi.util.PsiModificationTracker import kotlinx.coroutines.Dispatchers @@ -94,7 +94,7 @@ object BufAnalyzeUtils { FileDocumentManager.getInstance() .saveDocuments { document -> val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document) ?: return@saveDocuments false - psiFile is PbFile || BufConfig.CONFIG_FILES.contains(psiFile.name) + psiFile.isProtobufFile() || BufConfig.CONFIG_FILES.contains(psiFile.name) } val statusBar = WindowManager.getInstance().getStatusBar(project) statusBar?.getWidget(BufCLIWidget.ID) as? BufCLIWidget diff --git a/src/main/kotlin/build/buf/intellij/fixes/IgnoreBufIssueQuickFix.kt b/src/main/kotlin/build/buf/intellij/fixes/IgnoreBufIssueQuickFix.kt index cc1b9ec9..2e16dce7 100644 --- a/src/main/kotlin/build/buf/intellij/fixes/IgnoreBufIssueQuickFix.kt +++ b/src/main/kotlin/build/buf/intellij/fixes/IgnoreBufIssueQuickFix.kt @@ -16,10 +16,10 @@ package build.buf.intellij.fixes import build.buf.intellij.BufBundle import build.buf.intellij.config.BufConfig +import build.buf.intellij.vendor.isProtobufFile import com.intellij.codeInsight.intention.impl.BaseIntentionAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project -import com.intellij.protobuf.lang.psi.PbFile import com.intellij.psi.PsiFile import com.intellij.psi.PsiFileFactory import com.intellij.psi.PsiManager @@ -33,7 +33,7 @@ class IgnoreBufIssueQuickFix(private val type: String) : BaseIntentionAction() { override fun getText(): String = BufBundle.getMessage("buf.quickfix.ignore.issue") - override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean = file is PbFile + override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean = file.isProtobufFile() override fun startInWriteAction(): Boolean = true diff --git a/src/main/kotlin/build/buf/intellij/formatter/BufFormatterService.kt b/src/main/kotlin/build/buf/intellij/formatter/BufFormatterService.kt index a3af0165..bf41309b 100644 --- a/src/main/kotlin/build/buf/intellij/formatter/BufFormatterService.kt +++ b/src/main/kotlin/build/buf/intellij/formatter/BufFormatterService.kt @@ -17,6 +17,7 @@ package build.buf.intellij.formatter import build.buf.intellij.BufBundle import build.buf.intellij.annotator.BufAnalyzeUtils import build.buf.intellij.settings.bufSettings +import build.buf.intellij.vendor.isProtobufFile import com.intellij.codeInsight.actions.ReformatCodeProcessor import com.intellij.formatting.service.AsyncDocumentFormattingService import com.intellij.formatting.service.AsyncFormattingRequest @@ -25,7 +26,6 @@ import com.intellij.openapi.command.CommandProcessor import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.util.Disposer import com.intellij.openapi.vfs.VfsUtil -import com.intellij.protobuf.lang.psi.PbFile import com.intellij.psi.PsiFile import com.intellij.psi.formatter.FormatterUtil import kotlinx.coroutines.runBlocking @@ -34,7 +34,7 @@ class BufFormatterService : AsyncDocumentFormattingService() { override fun getFeatures(): Set = emptySet() override fun canFormat(file: PsiFile): Boolean = - file is PbFile && file.project.bufSettings.state.useBufFormatter && getFormattingReason() == FormattingReason.ReformatCode + file.isProtobufFile() && file.project.bufSettings.state.useBufFormatter && getFormattingReason() == FormattingReason.ReformatCode override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask? { val context = request.context diff --git a/src/main/kotlin/build/buf/intellij/inspections/BufAnalyzeInspection.kt b/src/main/kotlin/build/buf/intellij/inspections/BufAnalyzeInspection.kt index c4d91223..3ed1d9f0 100644 --- a/src/main/kotlin/build/buf/intellij/inspections/BufAnalyzeInspection.kt +++ b/src/main/kotlin/build/buf/intellij/inspections/BufAnalyzeInspection.kt @@ -17,6 +17,7 @@ package build.buf.intellij.inspections import build.buf.intellij.BufBundle import build.buf.intellij.BufPluginService import build.buf.intellij.annotator.* +import build.buf.intellij.vendor.isProtobufFile import com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl import com.intellij.codeInspection.* import com.intellij.lang.annotation.AnnotationSession @@ -25,7 +26,6 @@ import com.intellij.openapi.application.runReadAction import com.intellij.openapi.components.service import com.intellij.openapi.roots.ProjectFileIndex import com.intellij.openapi.util.Disposer -import com.intellij.protobuf.lang.psi.PbFile import com.intellij.psi.PsiFile class BufAnalyzeInspection : GlobalSimpleInspectionTool() { @@ -48,7 +48,7 @@ class BufAnalyzeInspection : GlobalSimpleInspectionTool() { globalContext: GlobalInspectionContext, problemDescriptionsProcessor: ProblemDescriptionsProcessor ) { - if (file !is PbFile) return + if (!file.isProtobufFile()) return val project = manager.project val disposable = project.messageBus.createDisposableOnAnyPsiChange() .also { Disposer.register(appService, it) } diff --git a/src/main/kotlin/build/buf/intellij/inspections/BufNotInstalledInspection.kt b/src/main/kotlin/build/buf/intellij/inspections/BufNotInstalledInspection.kt index 086b743b..f0a308c4 100644 --- a/src/main/kotlin/build/buf/intellij/inspections/BufNotInstalledInspection.kt +++ b/src/main/kotlin/build/buf/intellij/inspections/BufNotInstalledInspection.kt @@ -16,11 +16,11 @@ package build.buf.intellij.inspections import build.buf.intellij.BufBundle import build.buf.intellij.settings.BufCLIUtils +import build.buf.intellij.vendor.isProtobufFile import com.intellij.codeInspection.InspectionManager import com.intellij.codeInspection.LocalInspectionTool import com.intellij.codeInspection.ProblemDescriptor import com.intellij.codeInspection.ProblemHighlightType -import com.intellij.protobuf.lang.psi.PbFile import com.intellij.psi.PsiFile import org.jetbrains.annotations.NotNull @@ -37,7 +37,7 @@ class BufNotInstalledInspection : LocalInspectionTool() { } override fun checkFile(file: PsiFile, manager: InspectionManager, isOnTheFly: Boolean): Array? { - if (file !is PbFile) return null + if (!file.isProtobufFile()) return null val bufExecutable = BufCLIUtils.getConfiguredBufExecutable(file.project) if (bufExecutable != null) return null return arrayOf( diff --git a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt new file mode 100644 index 00000000..f86a48c7 --- /dev/null +++ b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt @@ -0,0 +1,46 @@ +package build.buf.intellij.vendor + +import com.intellij.lang.Language +import com.intellij.psi.PsiFile + +private val officialProtobufFile by lazy { + try { + Class.forName("com.intellij.protobuf.lang.psi.PbFile") + } catch (e: ClassNotFoundException) { + null + } +} + +private val officialProtobufLanguage: Language? by lazy { + try { + val clazz = Class.forName("com.intellij.protobuf.lang.PbLanguage") + clazz.getField("INSTANCE").get(null) as Language + } catch (e: ClassNotFoundException) { + null + } +} + +private val kanroProtobufFile by lazy { + try { + Class.forName("io.kanro.idea.plugin.protobuf.lang.psi.ProtobufFile") + } catch (e: ClassNotFoundException) { + null + } +} + +private val kanroProtobufLanguage: Language? by lazy { + try { + val clazz = Class.forName("io.kanro.idea.plugin.protobuf.lang.ProtobufLanguage") + clazz.kotlin.objectInstance as Language + } catch (e: ClassNotFoundException) { + null + } +} + +fun PsiFile.isProtobufFile(): Boolean { + return (officialProtobufFile?.isInstance(this) ?: false) || (kanroProtobufFile?.isInstance(this) ?: false) +} + +fun protobufLanguage(): Language? { + return officialProtobufLanguage ?: kanroProtobufLanguage +} \ No newline at end of file diff --git a/src/main/kotlin/build/buf/intellij/resolve/BufProtoFileResolver.kt b/src/main/kotlin/build/buf/intellij/vendor/jetbrains/BufProtoFileResolver.kt similarity index 97% rename from src/main/kotlin/build/buf/intellij/resolve/BufProtoFileResolver.kt rename to src/main/kotlin/build/buf/intellij/vendor/jetbrains/BufProtoFileResolver.kt index 4319ca2e..58e321ca 100644 --- a/src/main/kotlin/build/buf/intellij/resolve/BufProtoFileResolver.kt +++ b/src/main/kotlin/build/buf/intellij/vendor/jetbrains/BufProtoFileResolver.kt @@ -12,10 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -package build.buf.intellij.resolve +package build.buf.intellij.vendor.jetbrains import build.buf.intellij.config.BufConfig import build.buf.intellij.index.BufModuleIndex +import build.buf.intellij.resolve.BufRootsProvider import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile diff --git a/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt b/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt new file mode 100644 index 00000000..cb2fbbf2 --- /dev/null +++ b/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt @@ -0,0 +1,57 @@ +package build.buf.intellij.vendor.kanro + +import build.buf.intellij.config.BufConfig +import build.buf.intellij.index.BufModuleIndex +import build.buf.intellij.resolve.BufRootsProvider +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiElement +import com.intellij.psi.search.FilenameIndex +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.psi.search.GlobalSearchScopesCore +import com.intellij.psi.search.ProjectScope +import io.kanro.idea.plugin.protobuf.lang.root.ProtobufRoot +import io.kanro.idea.plugin.protobuf.lang.root.ProtobufRootProvider + +class BufProtoRootProvider : ProtobufRootProvider { + override fun id(): String = "bufRoot2" + + override fun roots(context: PsiElement): List { + val project = context.project + val roots = mutableListOf() + for (bufConfig in FilenameIndex.getVirtualFilesByName( + BufConfig.BUF_YAML, ProjectScope.getProjectScope(project) + )) { + val parent = bufConfig.parent ?: continue + roots.add(ProtobufRoot("bufCurrentModule", parent)) + } + for (mod in BufModuleIndex.getAllProjectModules(project)) { + val modName = "${mod.remote}/${mod.owner}/${mod.repository}:${mod.commit}" + val modFolderV2Path = BufRootsProvider.getOrCreateModuleCacheFolderV2(mod) + if (modFolderV2Path != null) { + roots.add(ProtobufRoot(modName, modFolderV2Path)) + continue + } + val modFolder = BufRootsProvider.findModuleCacheFolderV1(mod) ?: continue + roots.add(ProtobufRoot(modName, modFolder)) + } + return roots + } + + override fun searchScope(context: PsiElement): GlobalSearchScope? { + val project = context.project + val roots = ArrayList() + for (mod in BufModuleIndex.getAllProjectModules(project)) { + val v2Root = BufRootsProvider.getOrCreateModuleCacheFolderV2(mod) + if (v2Root != null) { + roots.add(v2Root) + continue + } + BufRootsProvider.findModuleCacheFolderV1(mod)?.let { roots.add(it) } + } + return if (roots.isNotEmpty()) { + GlobalSearchScopesCore.directoriesScope(project, true, *roots.toTypedArray()) + } else { + GlobalSearchScope.allScope(project) + } + } +} diff --git a/src/main/resources/META-INF/kanro-protobuf-plugin-support.xml b/src/main/resources/META-INF/kanro-protobuf-plugin-support.xml new file mode 100644 index 00000000..fcda23db --- /dev/null +++ b/src/main/resources/META-INF/kanro-protobuf-plugin-support.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/main/resources/META-INF/official-protobuf-plugin-support.xml b/src/main/resources/META-INF/official-protobuf-plugin-support.xml new file mode 100644 index 00000000..4c62aaec --- /dev/null +++ b/src/main/resources/META-INF/official-protobuf-plugin-support.xml @@ -0,0 +1,5 @@ + + + + + \ 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 c5a20c20..a926a342 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -5,8 +5,9 @@ Buf com.intellij.modules.platform - idea.plugin.protoeditor org.jetbrains.plugins.yaml + idea.plugin.protoeditor + io.kanro.idea.plugin.protobuf @@ -40,7 +41,4 @@ - - - From 2deddb7897979b2bf801d506180d43af93d3695c Mon Sep 17 00:00:00 2001 From: caixuan Date: Wed, 20 Dec 2023 18:57:48 +0800 Subject: [PATCH 02/10] Update change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9024c15a..504e5ec2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Unreleased +- Support [Protobuf plugin](https://github.com/devkanro/intellij-protobuf-plugin) by devkanro ## 0.2.1 - 2023-08-18 - Migrate to Kotlin UI DSL v2 in https://github.com/bufbuild/intellij-buf/pull/138 From 8086c181e7179419d72ca230a386d8da8293ffe6 Mon Sep 17 00:00:00 2001 From: caixuan Date: Thu, 21 Dec 2023 10:26:49 +0800 Subject: [PATCH 03/10] Add license header --- src/main/kotlin/build/buf/intellij/vendor/Utils.kt | 14 ++++++++++++++ .../intellij/vendor/kanro/BufProtoFileResolver.kt | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt index f86a48c7..596d9444 100644 --- a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt +++ b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt @@ -1,3 +1,17 @@ +// Copyright 2022-2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package build.buf.intellij.vendor import com.intellij.lang.Language diff --git a/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt b/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt index cb2fbbf2..a7bf6053 100644 --- a/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt +++ b/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt @@ -1,3 +1,17 @@ +// Copyright 2022-2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package build.buf.intellij.vendor.kanro import build.buf.intellij.config.BufConfig From 9c3f7f9a8b19bb8d590fe543f9704656d3160d86 Mon Sep 17 00:00:00 2001 From: caixuan Date: Mon, 25 Dec 2023 18:23:33 +0800 Subject: [PATCH 04/10] Fix tests --- build.gradle.kts | 24 ++++++++++++------- .../kotlin/build/buf/intellij/vendor/Utils.kt | 4 ++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8ce50d62..40db730e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ import org.jetbrains.changelog.Changelog import org.jetbrains.changelog.markdownToHTML +import org.jetbrains.intellij.tasks.PrepareSandboxTask import org.jetbrains.kotlin.gradle.internal.ensureParentDirsCreated fun properties(key: String) = providers.gradleProperty(key) @@ -190,18 +191,22 @@ tasks { password = environment("PRIVATE_KEY_PASSWORD") } - prepareSandbox { + withType { + val vendor = project.findProperty("buf.protobuf.vendor")?.toString() + ?: System.getenv("BUF_PROTOBUF_VENDOR") ?: "official" + logger.info("Current Protobuf Plugin vendor: $vendor") + val file = project.layout.buildDirectory.dir(configDir).get().file("disabled_plugins.txt").asFile + doLast { - val file = project.layout.buildDirectory.file("idea-sandbox/config/disabled_plugins.txt").get().asFile file.ensureParentDirsCreated() file.writeText( buildString { - // Comment this to use Protobuf plugin by JetBrains - // appendLine("idea.plugin.protoeditor") - // appendLine("com.intellij.grpc") - - // Comment this to use Protobuf plugin by Kanro - appendLine("io.kanro.idea.plugin.protobuf") + if (vendor == "devkanro") { + appendLine("idea.plugin.protoeditor") + appendLine("com.intellij.grpc") + } else { + appendLine("io.kanro.idea.plugin.protobuf") + } }, ) } @@ -213,7 +218,8 @@ tasks { // The pluginVersion is based on the SemVer (https://semver.org) and supports pre-release labels, like 2.1.7-alpha.3 // Specify pre-release label to publish the plugin in a custom Release Channel automatically. Read more: // https://plugins.jetbrains.com/docs/intellij/deployment.html#specifying-a-release-channel - channels = properties("pluginVersion").map { listOf(it.split('-').getOrElse(1) { "default" }.split('.').first()) } + channels = + properties("pluginVersion").map { listOf(it.split('-').getOrElse(1) { "default" }.split('.').first()) } } runPluginVerifier { diff --git a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt index 596d9444..8baed0a2 100644 --- a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt +++ b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt @@ -52,9 +52,9 @@ private val kanroProtobufLanguage: Language? by lazy { } fun PsiFile.isProtobufFile(): Boolean { - return (officialProtobufFile?.isInstance(this) ?: false) || (kanroProtobufFile?.isInstance(this) ?: false) + return (kanroProtobufFile?.isInstance(this) ?: false) || (officialProtobufFile?.isInstance(this) ?: false) } fun protobufLanguage(): Language? { - return officialProtobufLanguage ?: kanroProtobufLanguage + return kanroProtobufLanguage ?: officialProtobufLanguage } \ No newline at end of file From 59e467c31d31820fb396968a2b4530511f74e723 Mon Sep 17 00:00:00 2001 From: caixuan Date: Wed, 27 Dec 2023 20:56:20 +0800 Subject: [PATCH 05/10] Fix tests --- src/main/kotlin/build/buf/intellij/vendor/Utils.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt index 8baed0a2..146d64e3 100644 --- a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt +++ b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt @@ -52,9 +52,9 @@ private val kanroProtobufLanguage: Language? by lazy { } fun PsiFile.isProtobufFile(): Boolean { - return (kanroProtobufFile?.isInstance(this) ?: false) || (officialProtobufFile?.isInstance(this) ?: false) + return ((officialProtobufFile?.isInstance(this) ?: false || kanroProtobufFile?.isInstance(this) ?: false)) } fun protobufLanguage(): Language? { - return kanroProtobufLanguage ?: officialProtobufLanguage + return officialProtobufLanguage ?: kanroProtobufLanguage } \ No newline at end of file From 3fd03a9164c26072b7f3d1272568d6edeec75df8 Mon Sep 17 00:00:00 2001 From: caixuan Date: Thu, 28 Dec 2023 10:28:18 +0800 Subject: [PATCH 06/10] Fix comments --- .../intellij/annotator/BufAnalyzeModificationTracker.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt index 9b56e954..af8fea33 100644 --- a/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt +++ b/src/main/kotlin/build/buf/intellij/annotator/BufAnalyzeModificationTracker.kt @@ -22,11 +22,9 @@ import com.intellij.openapi.components.Service import com.intellij.openapi.components.service import com.intellij.openapi.project.Project import com.intellij.openapi.util.ModificationTracker -import com.intellij.protobuf.lang.PbLanguage import com.intellij.psi.search.FilenameIndex import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.util.PsiModificationTracker -import io.kanro.idea.plugin.protobuf.lang.ProtobufLanguage /** * Project level service to keep track of all changes to .proto files, Buf configuration files, and the plugin config. @@ -34,11 +32,14 @@ import io.kanro.idea.plugin.protobuf.lang.ProtobufLanguage */ @Service(Service.Level.PROJECT) class BufAnalyzeModificationTracker(private val project: Project) : ModificationTracker { - private val protoModificationTracker = PsiModificationTracker.getInstance(project).forLanguage(protobufLanguage()!!) + private val protoModificationTracker = protobufLanguage()?.let { + PsiModificationTracker.getInstance(project).forLanguage(it) + } + override fun getModificationCount(): Long { var modificationCount: Long = - protoModificationTracker.modificationCount + protoModificationTracker?.modificationCount ?: 0 runReadAction { for (configFile in BufConfig.CONFIG_FILES) { FilenameIndex.getVirtualFilesByName(configFile, GlobalSearchScope.projectScope(project)).forEach { From 0cc68d3197b665725b06764f5b373eaa65009501 Mon Sep 17 00:00:00 2001 From: caixuan Date: Thu, 28 Dec 2023 10:34:00 +0800 Subject: [PATCH 07/10] Upgrade protobuf plugin version --- gradle.properties | 2 +- .../build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index d106ffa0..0840ce67 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,7 +14,7 @@ platformType=IU platformVersion=2023.3 # Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html # Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22 -platformPlugins=idea.plugin.protoeditor, io.kanro.idea.plugin.protobuf:1.7.40, org.jetbrains.plugins.yaml +platformPlugins=idea.plugin.protoeditor, io.kanro.idea.plugin.protobuf:1.7.50, org.jetbrains.plugins.yaml # Gradle Releases -> https://github.com/gradle/gradle/releases gradleVersion=8.4 # Opt-out flag for bundling Kotlin standard library -> https://jb.gg/intellij-platform-kotlin-stdlib diff --git a/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt b/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt index a7bf6053..d849b745 100644 --- a/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt +++ b/src/main/kotlin/build/buf/intellij/vendor/kanro/BufProtoFileResolver.kt @@ -51,7 +51,7 @@ class BufProtoRootProvider : ProtobufRootProvider { return roots } - override fun searchScope(context: PsiElement): GlobalSearchScope? { + override fun searchScope(context: PsiElement): GlobalSearchScope { val project = context.project val roots = ArrayList() for (mod in BufModuleIndex.getAllProjectModules(project)) { From 5f36fab689f1d0416630212e7acf2f064ca1bbfe Mon Sep 17 00:00:00 2001 From: Kanro Date: Thu, 4 Jan 2024 13:08:04 +0800 Subject: [PATCH 08/10] =?UTF-8?q?=F0=9F=94=80=20Update=20build.gradle.kts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Philip K. Warren --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 40db730e..12404eda 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -195,7 +195,7 @@ tasks { val vendor = project.findProperty("buf.protobuf.vendor")?.toString() ?: System.getenv("BUF_PROTOBUF_VENDOR") ?: "official" logger.info("Current Protobuf Plugin vendor: $vendor") - val file = project.layout.buildDirectory.dir(configDir).get().file("disabled_plugins.txt").asFile + val disabledPluginsFile = project.layout.buildDirectory.dir(configDir).get().file("disabled_plugins.txt").asFile doLast { file.ensureParentDirsCreated() From 8044f5fcb573c51fdd2e52e059e2115f101868cc Mon Sep 17 00:00:00 2001 From: caixuan Date: Tue, 9 Jan 2024 16:22:30 +0800 Subject: [PATCH 09/10] Fix comments --- build.gradle.kts | 4 ++-- gradle.properties | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 12404eda..a85a2486 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -198,8 +198,8 @@ tasks { val disabledPluginsFile = project.layout.buildDirectory.dir(configDir).get().file("disabled_plugins.txt").asFile doLast { - file.ensureParentDirsCreated() - file.writeText( + disabledPluginsFile.ensureParentDirsCreated() + disabledPluginsFile.writeText( buildString { if (vendor == "devkanro") { appendLine("idea.plugin.protoeditor") diff --git a/gradle.properties b/gradle.properties index 0840ce67..eb6047a1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,14 +7,14 @@ pluginRepositoryUrl=https://github.com/bufbuild/intellij-buf pluginVersion=0.2.1 # See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # for insight into build numbers and IntelliJ Platform versions. -pluginSinceBuild=233 +pluginSinceBuild=223 pluginUntilBuild=999.* # IntelliJ Platform Properties -> https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties platformType=IU -platformVersion=2023.3 +platformVersion=2022.3 # Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html # Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22 -platformPlugins=idea.plugin.protoeditor, io.kanro.idea.plugin.protobuf:1.7.50, org.jetbrains.plugins.yaml +platformPlugins=idea.plugin.protoeditor, io.kanro.idea.plugin.protobuf:1.6.32, org.jetbrains.plugins.yaml # Gradle Releases -> https://github.com/gradle/gradle/releases gradleVersion=8.4 # Opt-out flag for bundling Kotlin standard library -> https://jb.gg/intellij-platform-kotlin-stdlib From 0147f84ebfb666949204217112d9bd66ff5ec1cb Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Tue, 23 Jan 2024 08:18:06 -0800 Subject: [PATCH 10/10] newline at end of files --- src/main/kotlin/build/buf/intellij/vendor/Utils.kt | 2 +- src/main/resources/META-INF/kanro-protobuf-plugin-support.xml | 2 +- .../resources/META-INF/official-protobuf-plugin-support.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt index 146d64e3..ae276c85 100644 --- a/src/main/kotlin/build/buf/intellij/vendor/Utils.kt +++ b/src/main/kotlin/build/buf/intellij/vendor/Utils.kt @@ -57,4 +57,4 @@ fun PsiFile.isProtobufFile(): Boolean { fun protobufLanguage(): Language? { return officialProtobufLanguage ?: kanroProtobufLanguage -} \ No newline at end of file +} diff --git a/src/main/resources/META-INF/kanro-protobuf-plugin-support.xml b/src/main/resources/META-INF/kanro-protobuf-plugin-support.xml index fcda23db..18de99d1 100644 --- a/src/main/resources/META-INF/kanro-protobuf-plugin-support.xml +++ b/src/main/resources/META-INF/kanro-protobuf-plugin-support.xml @@ -2,4 +2,4 @@ - \ No newline at end of file + diff --git a/src/main/resources/META-INF/official-protobuf-plugin-support.xml b/src/main/resources/META-INF/official-protobuf-plugin-support.xml index 4c62aaec..3059f222 100644 --- a/src/main/resources/META-INF/official-protobuf-plugin-support.xml +++ b/src/main/resources/META-INF/official-protobuf-plugin-support.xml @@ -2,4 +2,4 @@ - \ No newline at end of file +