1+// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
2+package dev.blachut.svelte.lang.service
3+4+import com.intellij.lang.javascript.ecmascript6.TypeScriptAnnotatorCheckerProvider
5+import com.intellij.lang.typescript.compiler.TypeScriptLanguageServiceAnnotatorCheckerProvider
6+import com.intellij.lang.typescript.compiler.languageService.protocol.commands.response.TypeScriptQuickInfoResponse
7+import com.intellij.lang.typescript.lsp.JSFrameworkLspTypeScriptService
8+import com.intellij.lsp.api.LspServerDescriptor
9+import com.intellij.lsp.api.LspServerSupportProvider
10+import com.intellij.lsp.methods.HoverMethod
11+import com.intellij.openapi.project.Project
12+import com.intellij.openapi.vfs.VirtualFile
13+import com.intellij.psi.PsiElement
14+import com.intellij.psi.PsiFile
15+import java.util.concurrent.CompletableFuture
16+import java.util.concurrent.CompletableFuture.completedFuture
17+18+class SvelteLspTypeScriptService(project: Project) : JSFrameworkLspTypeScriptService(project) {
19+override fun getProviderClass(): Class<out LspServerSupportProvider> = SvelteLspServerSupportProvider::class.java
20+21+override val name = "Svelte LSP"
22+override val prefix = "Svelte"
23+override val serverVersion = svelteLanguageToolsVersion
24+25+private fun quickInfo(element: PsiElement): TypeScriptQuickInfoResponse? {
26+val server = getServer() ?: return null
27+val raw = server.invokeSynchronously(HoverMethod.create(server, element)) ?: return null
28+val response = TypeScriptQuickInfoResponse()
29+ response.displayString = raw.substring("<html><body><pre>".length, raw.length - "</pre></body></html>".length)
30+return response
31+ }
32+33+private fun processHoverResponse(raw: String): String {
34+return raw.substring("<html><body>".length, raw.length - "</body></html>".length)
35+ }
36+37+override fun getQuickInfoAt(element: PsiElement,
38+originalElement: PsiElement,
39+originalFile: VirtualFile): CompletableFuture<TypeScriptQuickInfoResponse?> =
40+ completedFuture(quickInfo(element))
41+42+override fun canHighlight(file: PsiFile): Boolean {
43+val provider = TypeScriptAnnotatorCheckerProvider.getCheckerProvider(file)
44+if (provider !is TypeScriptLanguageServiceAnnotatorCheckerProvider) return false
45+46+return isFileAcceptableForService(file.virtualFile ?: return false)
47+ }
48+49+override fun isAcceptable(file: VirtualFile) = isServiceEnabledAndAvailable(project, file)
50+51+override fun isServiceEnabledBySettings(project: Project): Boolean {
52+return isSvelteServiceEnabledBySettings(project)
53+ }
54+55+override fun getLspServerDescriptor(project: Project, file: VirtualFile): LspServerDescriptor? {
56+return getSvelteServerDescriptor(project, file)
57+ }
58+}