Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/main/kotlin/insyncwithfoo/ryecharm/ruff/Tooltips.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import insyncwithfoo.ryecharm.configurations.ruff.RuffConfigurations
import insyncwithfoo.ryecharm.toHTML


internal fun RuffConfigurations.getFormattedTooltip(message: String, rule: RuleCode?): String {
val rulePossiblyWithLink = when (rule != null && renderTooltips) {
true -> "[$rule](${DocumentationURI.ruffRule(rule)})"
else -> rule
internal fun RuffConfigurations.getFormattedTooltip(message: String, ruleName: String?, ruleCode: String?): String {
val label = when (ruleCode != null && ruleName != null) {
true -> "${ruleCode}: $ruleName"
else -> ruleName ?: ruleCode
}
val uri = (ruleName ?: ruleCode)?.let { DocumentationURI.ruffRule(it) }

val rulePossiblyWithLink = when (uri != null && renderTooltips) {
true -> "[$label](${uri})"
else -> label
}
val formatted = tooltipFormat % Pair(message, rulePossiblyWithLink)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
internal val ruleSelector = """(?<linter>[A-Z]+)(?<number>[0-9]*)""".toRegex()


internal val ruleName = "[a-z0-9]+(?>-[a-z0-9]+)*".toRegex()


private val enabledRulesArray = """(?x)
linter\.rules\.enabled\h*=\h*\[(?<list>[^\[\]]*)]
""".toRegex()
Expand Down Expand Up @@ -78,7 +81,11 @@


internal val String.isRuleSelector: Boolean
get() = ruleSelector.matchEntire(this) != null
get() = ruleSelector.matches(this)


internal val String.isRuleName: Boolean

Check warning on line 87 in src/main/kotlin/insyncwithfoo/ryecharm/ruff/documentation/RuleDocumentation.kt

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused symbol

Property "isRuleName" is never used
get() = ruleName.matches(this)


internal val RuleSelectorOrName.isPylintCodePrefix: Boolean
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/insyncwithfoo/ryecharm/ruff/linting/Check.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.intellij.openapi.project.Project
import insyncwithfoo.ryecharm.Command
import insyncwithfoo.ryecharm.deserializationError
import insyncwithfoo.ryecharm.isSuccessful
import insyncwithfoo.ryecharm.parseAsJSONStrictly
import insyncwithfoo.ryecharm.parseAsJSONLeniently
import insyncwithfoo.ryecharm.processTimeout
import insyncwithfoo.ryecharm.runInBackground
import insyncwithfoo.ryecharm.unknownError
Expand All @@ -27,7 +27,7 @@ internal fun Project.runCheckCommand(command: Command): List<Diagnostic>? {
}

val results = try {
output.stdout.parseAsJSONStrictly<List<Diagnostic>>()
output.stdout.parseAsJSONLeniently<List<Diagnostic>>()
} catch (error: SerializationException) {
deserializationError(command, output, error)
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ internal enum class Severity {
}


// https://github.com/astral-sh/ruff/blob/2daaf29d90/crates/ruff_db/src/diagnostic/render/json.rs#L227
// https://github.com/astral-sh/ruff/blob/e6856de97d/crates/ruff_db/src/diagnostic/render/json.rs#L229
@Serializable
internal data class Diagnostic(
@SerialName("code")
val id: DiagnosticID,
val id: DiagnosticID = DiagnosticID.None,
@SerialName("name")
val ruleName: String? = null,
val url: String?,
val message: String,
val severity: Severity? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ internal class RuffAnnotator : ExternalAnnotator<InitialInfo, AnnotationResult>(
val message = diagnostic.message
val builder = holder.newAnnotation(highlightSeverity, message)

val tooltip = configurations.getFormattedTooltip(diagnostic.message, diagnostic.id.ruleCode)
val tooltip = configurations.getFormattedTooltip(
diagnostic.message,
diagnostic.ruleName,
diagnostic.id.ruleCode
)
val range = document.getOffsetRange(diagnostic.oneBasedRange)
val noqaOffset = diagnostic.getNoqaOffset(document)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ internal class DiagnosticsSupport(project: Project) : LspDiagnosticsSupport() {
override fun shouldAskServerForDiagnostics(file: VirtualFile) =
configurations.letNativeClientPullDiagnostics

override fun getTooltip(diagnostic: Diagnostic): String {
val rule = diagnostic.id.ruleCode
val message = diagnostic.message

return configurations.getFormattedTooltip(message, rule)
}
override fun getTooltip(diagnostic: Diagnostic) =
configurations.getFormattedTooltip(
message = diagnostic.message,
ruleName = null,
ruleCode = diagnostic.id.ruleCode
)

override fun getHighlightSeverity(diagnostic: Diagnostic) =
super.getHighlightSeverity(diagnostic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ internal class DiagnosticFeature : LSPDiagnosticFeature() {
override fun isEnabled(file: PsiFile) =
configurations.linting

override fun getTooltip(diagnostic: Diagnostic): String {
val rule = diagnostic.id.ruleCode
val message = diagnostic.message

return configurations.getFormattedTooltip(message, rule)
}
override fun getTooltip(diagnostic: Diagnostic) =
configurations.getFormattedTooltip(
message = diagnostic.message,
ruleName = null,
ruleCode = diagnostic.id.ruleCode
)

override fun getHighlightSeverity(diagnostic: Diagnostic) =
super.getHighlightSeverity(diagnostic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import com.intellij.openapi.editor.Editor
import insyncwithfoo.ryecharm.DocumentationURI
import insyncwithfoo.ryecharm.message
import insyncwithfoo.ryecharm.ruff.documentation.getRuleDocumentationByFullCode
import insyncwithfoo.ryecharm.ruff.documentation.getRuleDocumentationByRuleName
import insyncwithfoo.ryecharm.ruff.documentation.isRuleSelector
import insyncwithfoo.ryecharm.toHTML
import insyncwithfoo.ryecharm.toHTMLInReadAction
import kotlinx.coroutines.runBlocking


private const val IGNORE_THIS_LINK = true
private const val PROCEED_TO_CALL_GET_DESCRIPTION = false


Expand All @@ -23,13 +23,13 @@ private const val PROCEED_TO_CALL_GET_DESCRIPTION = false
internal class RuleTooltipLinkHandler : TooltipLinkHandler() {

override fun handleLink(refSuffix: String, editor: Editor) =
when (refSuffix.isRuleSelector) {
true -> PROCEED_TO_CALL_GET_DESCRIPTION
else -> IGNORE_THIS_LINK
}
PROCEED_TO_CALL_GET_DESCRIPTION

override fun getDescription(refSuffix: String, editor: Editor) = runBlocking {
editor.project?.getRuleDocumentationByFullCode(refSuffix)?.toHTML()
when (refSuffix.isRuleSelector) {
true -> editor.project?.getRuleDocumentationByFullCode(refSuffix)?.toHTMLInReadAction()
else -> editor.project?.getRuleDocumentationByRuleName(refSuffix)?.toHTMLInReadAction()
}
}

override fun getDescriptionTitle(refSuffix: String, editor: Editor) =
Expand Down
Loading