C/C++ plugin - notifications improvement

This commit is contained in:
Gregory Bednov 2025-01-05 02:01:13 +03:00
commit b115b5d9f0
4 changed files with 32 additions and 13 deletions

View file

@ -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<PluginBundle> {
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))
}
}

View file

@ -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,10 +37,25 @@ 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 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)
}
}
}
class CppLanguageServerFactory : LanguageServerFactory {

View file

@ -11,6 +11,8 @@
<depends>org.jetbrains.plugins.textmate</depends>
<extensions defaultExtensionNs="com.intellij">
<notificationGroup id="C/C++ notifications"
displayType="BALLOON" />
<fileType
name="C File"
extensions="c"
@ -18,7 +20,7 @@
fieldName="INSTANCE"
implementationClass="org.gregorybednov.clsp.CFileType"/>
<!-- FUCKING $YourFileLanguage!!!!! -->
<!-- F*** $YourFileLanguage!!!!! I spent f**ing week trying to understand what's wrong!!!-->
<editorHighlighterProvider
filetype="C File"
implementationClass="org.jetbrains.plugins.textmate.language.syntax.highlighting.TextMateEditorHighlighterProvider" />

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.4, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px"
width="16" height="16" viewBox="0 0 306 344.35" enable-background="new 0 0 306 344.35" xml:space="preserve">
<path fill="#00599C" d="M302.107,258.262c2.401-4.159,3.893-8.845,3.893-13.053V99.14c0-4.208-1.49-8.893-3.892-13.052L153,172.175
L302.107,258.262z"/>
@ -19,7 +18,7 @@
221,177.841 232.334,177.841 232.334,189.175 243.666,189.175 243.666,177.841 255,177.841 "/>
</g>
<g>
<polygon fill="#FFFFFF" points="297.5,166.508 286.166,166.508 286.166,155.175 274.834,155.175 274.834,166.508 263.5,166.508
263.5,177.841 274.834,177.841 274.834,189.175 286.166,189.175 286.166,177.841 297.5,177.841 "/>
<polygon fill="#FFFFFF" points="297.5,166.508 274.834,166.508 263.5,166.508
263.5,177.841 286.166,177.841 297.5,177.841"/>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

Before After
Before After