-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add capability to display hover (#605)
* feat: add capability to display hover This is disabled by default and can be enabled in the IntelliJ registry with the key snyk.documentationHoversEnabled. * fix: minimize read lock during code vision calculation, improve annotations
- Loading branch information
1 parent
85db49f
commit fc50f9b
Showing
9 changed files
with
159 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/snyk/common/lsp/LSDocumentationTargetProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
@file:Suppress("UnstableApiUsage") | ||
|
||
package snyk.common.lsp | ||
|
||
import com.intellij.markdown.utils.convertMarkdownToHtml | ||
import com.intellij.model.Pointer | ||
import com.intellij.openapi.Disposable | ||
import com.intellij.platform.backend.documentation.DocumentationResult | ||
import com.intellij.platform.backend.documentation.DocumentationTarget | ||
import com.intellij.platform.backend.documentation.DocumentationTargetProvider | ||
import com.intellij.platform.backend.presentation.TargetPresentation | ||
import com.intellij.psi.PsiFile | ||
import icons.SnykIcons | ||
import io.snyk.plugin.isDocumentationHoverEnabled | ||
import io.snyk.plugin.toLanguageServerURL | ||
import io.snyk.plugin.ui.toolwindow.SnykPluginDisposable | ||
import org.eclipse.lsp4j.Hover | ||
import org.eclipse.lsp4j.HoverParams | ||
import org.eclipse.lsp4j.Position | ||
import org.eclipse.lsp4j.TextDocumentIdentifier | ||
import java.util.concurrent.TimeUnit | ||
|
||
class SnykDocumentationTargetPointer(private val documentationTarget: DocumentationTarget) : | ||
Pointer<DocumentationTarget> { | ||
|
||
override fun dereference(): DocumentationTarget { | ||
return documentationTarget | ||
} | ||
} | ||
|
||
class LSDocumentationTargetProvider : DocumentationTargetProvider, Disposable { | ||
private var disposed = false | ||
get() { | ||
return SnykPluginDisposable.getInstance().isDisposed() || field | ||
} | ||
|
||
fun isDisposed() = disposed | ||
|
||
override fun documentationTargets(file: PsiFile, offset: Int): MutableList<out DocumentationTarget> { | ||
val languageServerWrapper = LanguageServerWrapper.getInstance() | ||
if (disposed || !languageServerWrapper.isInitialized || !isDocumentationHoverEnabled()) return mutableListOf() | ||
|
||
val lineNumber = file.viewProvider.document.getLineNumber(offset) | ||
val lineStartOffset = file.viewProvider.document.getLineStartOffset(lineNumber) | ||
val hoverParams = HoverParams( | ||
TextDocumentIdentifier(file.virtualFile.toLanguageServerURL()), | ||
Position(lineNumber, offset - lineStartOffset) | ||
) | ||
val hover = | ||
languageServerWrapper.languageServer.textDocumentService.hover(hoverParams).get(2000, TimeUnit.MILLISECONDS) | ||
if (hover == null || hover.contents.right.value.isEmpty()) return mutableListOf() | ||
return mutableListOf(SnykDocumentationTarget(hover)) | ||
} | ||
|
||
inner class SnykDocumentationTarget(private val hover: Hover) : DocumentationTarget { | ||
override fun computeDocumentationHint(): String? { | ||
val htmlText = convertMarkdownToHtml(hover.contents.right.value) | ||
if (htmlText.isEmpty()) { | ||
return null | ||
} | ||
return htmlText.split("\n")[0] | ||
} | ||
|
||
override fun computeDocumentation(): DocumentationResult? { | ||
val htmlText = convertMarkdownToHtml(hover.contents.right.value) | ||
if (htmlText.isEmpty()) { | ||
return null | ||
} | ||
return DocumentationResult.documentation(htmlText) | ||
} | ||
|
||
override fun computePresentation(): TargetPresentation { | ||
return TargetPresentation.builder("Snyk Security").icon(SnykIcons.TOOL_WINDOW).presentation() | ||
} | ||
|
||
override fun createPointer(): Pointer<out DocumentationTarget> { | ||
return SnykDocumentationTargetPointer(this) | ||
} | ||
} | ||
|
||
|
||
override fun dispose() { | ||
disposed = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters