From b115b5d9f0351f0824baec599c09851787f1823f Mon Sep 17 00:00:00 2001 From: Gregory Bednov Date: Sun, 5 Jan 2025 02:01:13 +0300 Subject: [PATCH] C/C++ plugin - notifications improvement --- .../clsp/CTextMateBundleProvider.kt | 5 ---- .../org/gregorybednov/clsp/CppLanguage.kt | 27 +++++++++++++++++-- src/main/resources/META-INF/plugin.xml | 4 ++- src/main/resources/icons/cpplogo.svg | 9 +++---- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/org/gregorybednov/clsp/CTextMateBundleProvider.kt b/src/main/kotlin/org/gregorybednov/clsp/CTextMateBundleProvider.kt index 258bc59..ba511e5 100644 --- a/src/main/kotlin/org/gregorybednov/clsp/CTextMateBundleProvider.kt +++ b/src/main/kotlin/org/gregorybednov/clsp/CTextMateBundleProvider.kt @@ -1,18 +1,13 @@ package org.gregorybednov.clsp -import com.intellij.openapi.application.PluginPathManager import org.jetbrains.plugins.textmate.api.TextMateBundleProvider import org.jetbrains.plugins.textmate.api.TextMateBundleProvider.PluginBundle import kotlin.io.path.Path -import kotlin.io.path.exists class CTextMateBundleProvider : TextMateBundleProvider { override fun getBundles(): List { return listOf( PluginBundle("c/c++", Path("/Users/gregorybednov/Downloads/better-cpp-syntax")) ) - val textmateDir = PluginPathManager.getPluginResource(this::class.java, "textmate") ?: return emptyList() - val path = textmateDir.toPath() - return listOf(PluginBundle("better-cpp-syntax", path)) } } \ No newline at end of file diff --git a/src/main/kotlin/org/gregorybednov/clsp/CppLanguage.kt b/src/main/kotlin/org/gregorybednov/clsp/CppLanguage.kt index 7748b14..93e838b 100644 --- a/src/main/kotlin/org/gregorybednov/clsp/CppLanguage.kt +++ b/src/main/kotlin/org/gregorybednov/clsp/CppLanguage.kt @@ -2,13 +2,21 @@ package org.gregorybednov.clsp import com.intellij.execution.configurations.GeneralCommandLine import com.intellij.lang.Language +import com.intellij.notification.NotificationGroupManager +import com.intellij.notification.NotificationType import com.intellij.openapi.fileTypes.LanguageFileType import com.intellij.openapi.project.Project import com.intellij.openapi.util.IconLoader.getIcon +import com.intellij.util.EnvironmentUtil import com.redhat.devtools.lsp4ij.LanguageServerFactory +import com.redhat.devtools.lsp4ij.LanguageServerManager import com.redhat.devtools.lsp4ij.server.OSProcessStreamConnectionProvider import com.redhat.devtools.lsp4ij.server.StreamConnectionProvider +import java.io.File +import java.nio.file.Paths import javax.swing.Icon +import kotlin.io.path.pathString + object CppLanguage : Language("C++") { private fun readResolve(): Any = CppLanguage @@ -29,9 +37,24 @@ class CppFileType private constructor() : LanguageFileType(CppLanguage.INSTANCE) } class CppLanguageServer(project: Project) : OSProcessStreamConnectionProvider() { + private fun findExecutableInPATH(executable: String) = + EnvironmentUtil.getEnvironmentMap().values.flatMap { it.split(File.pathSeparator) } + .map { File(Paths.get(it, executable).pathString) }.find { it.exists() && it.canExecute() }?.path + init { - val commandLine = GeneralCommandLine("clangd") - super.setCommandLine(commandLine) + val clangdPath = findExecutableInPATH("clangd") + if (clangdPath.isNullOrEmpty()) { + NotificationGroupManager.getInstance().getNotificationGroup("C/C++ notifications").createNotification( + "C++ LSP", + "LSP server clangd not found. Make sure it is installed properly (and is available in PATH)," + + "and restart the IDE.", + NotificationType.ERROR).notify(project) + LanguageServerManager.getInstance(project).stop("CppLanguageServer") + } else { + val commandLine = GeneralCommandLine("clangd") + commandLine.setWorkDirectory(project.basePath) + super.setCommandLine(commandLine) + } } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 63738c4..1813763 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -11,6 +11,8 @@ org.jetbrains.plugins.textmate + - + diff --git a/src/main/resources/icons/cpplogo.svg b/src/main/resources/icons/cpplogo.svg index d9449cf..71f3958 100644 --- a/src/main/resources/icons/cpplogo.svg +++ b/src/main/resources/icons/cpplogo.svg @@ -1,7 +1,6 @@ - - @@ -15,11 +14,11 @@ c18.152,0,35.085-9.776,44.191-25.515l44.143,25.543C223.142,254.644,189.294,274.175,153,274.175z"/> - - +