From 6022088afb3a5206871bd65e629262ef26f7abe1 Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Tue, 30 Aug 2022 20:08:04 +0200 Subject: [PATCH 1/2] Fix issue where implement abstract members code action doesn't show up if you don't mark the entire text. Happens in editors that are not VSCode --- .../org/javacs/kt/codeaction/quickfix/QuickFix.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt b/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt index a7048ddf4..0115eaacd 100644 --- a/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt +++ b/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt @@ -16,10 +16,18 @@ interface QuickFix { } fun diagnosticMatch(diagnostic: Diagnostic, range: Range, diagnosticTypes: Set): Boolean = - diagnostic.range.equals(range) && diagnosticTypes.contains(diagnostic.code.left) + isDiagnosticInRange(diagnostic, range) && diagnosticTypes.contains(diagnostic.code.left) + +// for a diagnostic to be in range the lines should be the same, and +// the input character range should be within the bounds of the diagnostics range. +private fun isDiagnosticInRange(diagnostic: Diagnostic, range: Range): Boolean { + val diagnosticRange = diagnostic.range + return diagnosticRange.start.line == range.start.line && diagnosticRange.end.line == range.end.line && + diagnosticRange.start.character <= range.start.character && diagnosticRange.end.character >= range.end.character +} fun diagnosticMatch(diagnostic: KotlinDiagnostic, startCursor: Int, endCursor: Int, diagnosticTypes: Set): 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) fun findDiagnosticMatch(diagnostics: List, range: Range, diagnosticTypes: Set) = diagnostics.find { diagnosticMatch(it, range, diagnosticTypes) } From a3c040e297602b8cc705c327f8a79713a0fbe17d Mon Sep 17 00:00:00 2001 From: Marie Katrine Ekeberg Date: Wed, 31 Aug 2022 17:49:17 +0200 Subject: [PATCH 2/2] Change private helper function into an extension function --- .../org/javacs/kt/codeaction/quickfix/QuickFix.kt | 11 ++--------- .../src/main/kotlin/org/javacs/kt/util/RangeUtils.kt | 8 ++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 server/src/main/kotlin/org/javacs/kt/util/RangeUtils.kt diff --git a/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt b/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt index 0115eaacd..89526bb0a 100644 --- a/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt +++ b/server/src/main/kotlin/org/javacs/kt/codeaction/quickfix/QuickFix.kt @@ -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 @@ -16,15 +17,7 @@ interface QuickFix { } fun diagnosticMatch(diagnostic: Diagnostic, range: Range, diagnosticTypes: Set): Boolean = - isDiagnosticInRange(diagnostic, range) && diagnosticTypes.contains(diagnostic.code.left) - -// for a diagnostic to be in range the lines should be the same, and -// the input character range should be within the bounds of the diagnostics range. -private fun isDiagnosticInRange(diagnostic: Diagnostic, range: Range): Boolean { - val diagnosticRange = diagnostic.range - return diagnosticRange.start.line == range.start.line && diagnosticRange.end.line == range.end.line && - diagnosticRange.start.character <= range.start.character && diagnosticRange.end.character >= range.end.character -} + range.isSubrangeOf(diagnostic.range) && diagnosticTypes.contains(diagnostic.code.left) fun diagnosticMatch(diagnostic: KotlinDiagnostic, startCursor: Int, endCursor: Int, diagnosticTypes: Set): Boolean = diagnostic.textRanges.any { it.startOffset <= startCursor && it.endOffset >= endCursor } && diagnosticTypes.contains(diagnostic.factory.name) diff --git a/server/src/main/kotlin/org/javacs/kt/util/RangeUtils.kt b/server/src/main/kotlin/org/javacs/kt/util/RangeUtils.kt new file mode 100644 index 000000000..01820c80d --- /dev/null +++ b/server/src/main/kotlin/org/javacs/kt/util/RangeUtils.kt @@ -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