Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Protobuf plugin by kanro #166

Merged
merged 11 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 25 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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)
fun environment(key: String) = providers.environmentVariable(key)
Expand Down Expand Up @@ -189,13 +191,35 @@ tasks {
password = environment("PRIVATE_KEY_PASSWORD")
}

withType<PrepareSandboxTask> {
val vendor = project.findProperty("buf.protobuf.vendor")?.toString()
?: System.getenv("BUF_PROTOBUF_VENDOR") ?: "official"
logger.info("Current Protobuf Plugin vendor: $vendor")
val disabledPluginsFile = project.layout.buildDirectory.dir(configDir).get().file("disabled_plugins.txt").asFile
devkanro marked this conversation as resolved.
Show resolved Hide resolved

doLast {
disabledPluginsFile.ensureParentDirsCreated()
disabledPluginsFile.writeText(
buildString {
if (vendor == "devkanro") {
appendLine("idea.plugin.protoeditor")
appendLine("com.intellij.grpc")
} else {
appendLine("io.kanro.idea.plugin.protobuf")
}
},
)
}
}

publishPlugin {
dependsOn("patchChangelog")
token = environment("PUBLISH_TOKEN")
// 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 {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pluginSinceBuild=223
pluginUntilBuild=999.*
# IntelliJ Platform Properties -> https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties
platformType=IU
platformVersion=2022.3.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, 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ 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
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
Expand All @@ -32,10 +32,14 @@ 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 = protobufLanguage()?.let {
PsiModificationTracker.getInstance(project).forLanguage(it)
}


override fun getModificationCount(): Long {
var modificationCount : Long = protoModificationTracker.modificationCount
var modificationCount: Long =
protoModificationTracker?.modificationCount ?: 0
runReadAction {
for (configFile in BufConfig.CONFIG_FILES) {
FilenameIndex.getVirtualFilesByName(configFile, GlobalSearchScope.projectScope(project)).forEach {
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/build/buf/intellij/annotator/BufAnalyzePass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -101,7 +101,7 @@ class BufAnalyzePass(
}

override fun doApplyInformationToEditor() {
if (file !is PbFile) return
if (!file.isProtobufFile()) return

if (annotationInfo == null) {
disposable = appService
Expand All @@ -126,8 +126,6 @@ class BufAnalyzePass(
}
})
}

override fun canEat(update: Update?): Boolean = true
}

if (isUnitTestMode) {
Expand All @@ -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 ->
Expand Down Expand Up @@ -226,7 +224,7 @@ class BufAnalyzePassFactory(
val LAST_ANALYZE_MOD_COUNT = Key.create<Long>("build.buf.analyze.mod_count")

fun shouldRunPass(file: PsiFile): Boolean {
if (file !is PbFile) {
if (!file.isProtobufFile()) {
return false
}
val analyzeModTracker = file.project.service<BufAnalyzeModificationTracker>()
Expand Down Expand Up @@ -255,7 +253,7 @@ fun MessageBus.createDisposableOnAnyPsiChange(): Disposable {
}

fun AnnotationHolder.createAnnotationsForFile(
file: PbFile,
file: PsiFile,
annotationResult: BufAnalyzeResult
) {
val doc = file.viewProvider.document
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -34,7 +34,7 @@ class BufFormatterService : AsyncDocumentFormattingService() {
override fun getFeatures(): Set<FormattingService.Feature> = 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -37,7 +37,7 @@ class BufNotInstalledInspection : LocalInspectionTool() {
}

override fun checkFile(file: PsiFile, manager: InspectionManager, isOnTheFly: Boolean): Array<ProblemDescriptor>? {
if (file !is PbFile) return null
if (!file.isProtobufFile()) return null
val bufExecutable = BufCLIUtils.getConfiguredBufExecutable(file.project)
if (bufExecutable != null) return null
return arrayOf(
Expand Down
60 changes: 60 additions & 0 deletions src/main/kotlin/build/buf/intellij/vendor/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading