-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also pass project interpreter path to executable
- Loading branch information
1 parent
4efd384
commit 83587af
Showing
4 changed files
with
141 additions
and
109 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
77 changes: 77 additions & 0 deletions
77
src/main/kotlin/com/insyncwithfoo/pyright/PyrightAnnotationApplier.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,77 @@ | ||
package com.insyncwithfoo.pyright | ||
|
||
import com.insyncwithfoo.pyright.configuration.PyrightAllConfigurations | ||
import com.intellij.lang.annotation.AnnotationBuilder | ||
import com.intellij.lang.annotation.AnnotationHolder | ||
import com.intellij.lang.annotation.HighlightSeverity | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.xml.util.XmlStringUtil | ||
|
||
|
||
private fun Document.getOffset(endpoint: PyrightDiagnosticTextRangeEndpoint) = | ||
getLineStartOffset(endpoint.line) + endpoint.character | ||
|
||
|
||
private fun Document.getStartEndRange(range: PyrightDiagnosticTextRange): TextRange { | ||
val start = getOffset(range.start) | ||
val end = getOffset(range.end) | ||
|
||
return TextRange(start, end) | ||
} | ||
|
||
|
||
private fun AnnotationHolder.makeBuilderForDiagnostic(diagnostic: PyrightDiagnostic): AnnotationBuilder { | ||
val (_, severity, message, rule) = diagnostic | ||
|
||
val tooltip = message.toPreformattedTooltip() | ||
val highlightSeverity = severity.toHighlightSeverity() | ||
|
||
val suffix = if (rule != null) " (${rule})" else "" | ||
val suffixedmessage = "$message$suffix" | ||
|
||
return newAnnotation(highlightSeverity, suffixedmessage).tooltip(tooltip) | ||
} | ||
|
||
|
||
private fun PyrightDiagnosticSeverity.toHighlightSeverity() = when (this) { | ||
PyrightDiagnosticSeverity.ERROR -> HighlightSeverity.WARNING | ||
PyrightDiagnosticSeverity.WARNING -> HighlightSeverity.WEAK_WARNING | ||
PyrightDiagnosticSeverity.INFORMATION -> HighlightSeverity.WEAK_WARNING | ||
} | ||
|
||
|
||
private fun String.toPreformattedTooltip(): String { | ||
val escapedLines = this.split("\n").map { | ||
XmlStringUtil.escapeString(it, true) | ||
} | ||
|
||
return escapedLines.joinToString("<br>") | ||
} | ||
|
||
|
||
private fun AnnotationHolder.applyDiagnostic(diagnostic: PyrightDiagnostic, document: Document) { | ||
val builder = makeBuilderForDiagnostic(diagnostic) | ||
val range = document.getStartEndRange(diagnostic.range) | ||
|
||
builder.annotateRange(range) | ||
} | ||
|
||
|
||
private fun AnnotationBuilder.annotateRange(range: TextRange) { | ||
this.needsUpdateOnTyping().range(range).create() | ||
} | ||
|
||
|
||
internal class PyrightAnnotationApplier( | ||
private val document: Document, | ||
private val output: PyrightOutput, | ||
private val configurations: PyrightAllConfigurations, | ||
private val holder: AnnotationHolder | ||
) { | ||
fun apply() { | ||
output.generalDiagnostics.forEach { | ||
holder.applyDiagnostic(it, document) | ||
} | ||
} | ||
} |
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