Skip to content

Fix issue where code action doesn't show up, unless entire text marked #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.eclipse.lsp4j.Range
import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.javacs.kt.CompiledFile
import org.javacs.kt.index.SymbolIndex
import org.javacs.kt.util.isSubrangeOf
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
import org.jetbrains.kotlin.diagnostics.Diagnostic as KotlinDiagnostic

Expand All @@ -16,10 +17,10 @@ interface QuickFix {
}

fun diagnosticMatch(diagnostic: Diagnostic, range: Range, diagnosticTypes: Set<String>): Boolean =
diagnostic.range.equals(range) && diagnosticTypes.contains(diagnostic.code.left)
range.isSubrangeOf(diagnostic.range) && diagnosticTypes.contains(diagnostic.code.left)

fun diagnosticMatch(diagnostic: KotlinDiagnostic, startCursor: Int, endCursor: Int, diagnosticTypes: Set<String>): Boolean =
diagnostic.textRanges.any { it.startOffset == startCursor && it.endOffset == endCursor } && diagnosticTypes.contains(diagnostic.factory.name)
diagnostic.textRanges.any { it.startOffset <= startCursor && it.endOffset >= endCursor } && diagnosticTypes.contains(diagnostic.factory.name)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, it might be worth checking if there is a similar function for Kotlin's TextRange, this might be useful in other places too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will look further into that in a future PR. Right now I don't see how it will improve the current code without some rewrites. I might be wrong though 🙂


fun findDiagnosticMatch(diagnostics: List<Diagnostic>, range: Range, diagnosticTypes: Set<String>) =
diagnostics.find { diagnosticMatch(it, range, diagnosticTypes) }
Expand Down
8 changes: 8 additions & 0 deletions server/src/main/kotlin/org/javacs/kt/util/RangeUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.javacs.kt.util

import org.eclipse.lsp4j.Range

// checks if the current range is within the other range (same lines, within the character bounds)
fun Range.isSubrangeOf(otherRange: Range): Boolean =
otherRange.start.line == this.start.line && otherRange.end.line == this.end.line &&
otherRange.start.character <= this.start.character && otherRange.end.character >= this.end.character