diff --git a/src/main/kotlin/me/shedaniel/linkie/discord/MappingsQueryUtils.kt b/src/main/kotlin/me/shedaniel/linkie/discord/MappingsQueryUtils.kt index 83983f5..685835f 100644 --- a/src/main/kotlin/me/shedaniel/linkie/discord/MappingsQueryUtils.kt +++ b/src/main/kotlin/me/shedaniel/linkie/discord/MappingsQueryUtils.kt @@ -29,7 +29,12 @@ import me.shedaniel.linkie.utils.QueryContext import me.shedaniel.linkie.utils.ResultHolder object MappingsQueryUtils { - suspend fun query(mappings: MappingsContainer, searchTerm: String, vararg types: MappingsEntryType): MutableList> { + data class Result( + val results: MutableList>, + val fuzzy: Boolean, + ) + + suspend fun query(mappings: MappingsContainer, searchTerm: String, vararg types: MappingsEntryType): Result { require(types.isNotEmpty()) val context = QueryContext( provider = { mappings }, @@ -70,50 +75,52 @@ object MappingsQueryUtils { fields?.also(result::addAll) result.sortByDescending { it.score } - if (result.isEmpty()) { - runBlocking { - if (MappingsEntryType.CLASS in types) { - launch { - try { - classes = MappingsQuery.queryClasses(context.copy(accuracy = MatchAccuracy.Fuzzy)).value - } catch (e: NullPointerException) { + if (result.isNotEmpty()) { + return Result(result, false) + } + + runBlocking { + if (MappingsEntryType.CLASS in types) { + launch { + try { + classes = MappingsQuery.queryClasses(context.copy(accuracy = MatchAccuracy.Fuzzy)).value + } catch (e: NullPointerException) { - } } } - if (MappingsEntryType.METHOD in types) { - launch { - try { - methods = MappingsQuery.queryMethods(context.copy(accuracy = MatchAccuracy.Fuzzy)).value - } catch (e: NullPointerException) { + } + if (MappingsEntryType.METHOD in types) { + launch { + try { + methods = MappingsQuery.queryMethods(context.copy(accuracy = MatchAccuracy.Fuzzy)).value + } catch (e: NullPointerException) { - } } } - if (MappingsEntryType.FIELD in types) { - launch { - try { - fields = MappingsQuery.queryFields(context.copy(accuracy = MatchAccuracy.Fuzzy)).value - } catch (e: NullPointerException) { + } + if (MappingsEntryType.FIELD in types) { + launch { + try { + fields = MappingsQuery.queryFields(context.copy(accuracy = MatchAccuracy.Fuzzy)).value + } catch (e: NullPointerException) { - } } } } - classes?.also(result::addAll) - methods?.also(result::addAll) - fields?.also(result::addAll) - result.sortByDescending { it.score } + } + classes?.also(result::addAll) + methods?.also(result::addAll) + fields?.also(result::addAll) + result.sortByDescending { it.score } - if (result.isEmpty()) { - if (types.size != 1) { - MappingsQuery.errorNoResultsFound(null, searchTerm) - } else { - MappingsQuery.errorNoResultsFound(types.first(), searchTerm) - } + if (result.isEmpty()) { + if (types.size != 1) { + MappingsQuery.errorNoResultsFound(null, searchTerm) + } else { + MappingsQuery.errorNoResultsFound(types.first(), searchTerm) } } - return result + return Result(result, true) } } diff --git a/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryMappingsCommand.kt b/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryMappingsCommand.kt index dea3f2a..e938e6e 100644 --- a/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryMappingsCommand.kt +++ b/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryMappingsCommand.kt @@ -52,6 +52,7 @@ import me.shedaniel.linkie.utils.ResultHolder import me.shedaniel.linkie.utils.onlyClass import me.shedaniel.linkie.utils.valueKeeper import java.util.* +import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger import kotlin.math.ceil @@ -99,12 +100,13 @@ open class QueryMappingsCommand( } suspend fun execute(ctx: CommandContext, namespace: Namespace, version: String, searchTerm: String, types: Array) = ctx.use { + val fuzzy = AtomicBoolean(false) val maxPage = AtomicInteger(-1) val query by valueKeeper { - QueryMappingsExtensions.query(searchTerm, namespace.getProvider(version), user, message, maxPage, types) + QueryMappingsExtensions.query(searchTerm, namespace.getProvider(version), user, message, maxPage, fuzzy, types) }.initiate() message.sendPages(ctx, 0, maxPage.get()) { page -> - QueryMessageBuilder.buildMessage(this, namespace, query.value, query.mappings, page, user, maxPage.get()) + QueryMessageBuilder.buildMessage(this, searchTerm, namespace, query.value, query.mappings, page, user, maxPage.get(), fuzzy.get()) } } @@ -170,6 +172,7 @@ object QueryMappingsExtensions { user: User, message: MessageCreator, maxPage: AtomicInteger, + fuzzy: AtomicBoolean, types: Array, ): QueryResult>> { val hasWildcard: Boolean = searchTerm.substringBeforeLast('/').onlyClass() == "*" || searchTerm.onlyClass() == "*" @@ -182,8 +185,10 @@ object QueryMappingsExtensions { } return message.getCatching(user) { val mappings = provider.get() - QueryResult(mappings, query(mappings, searchTerm, *types).also { - maxPage.set(ceil(it.size / 4.0).toInt()) + QueryResult(mappings, query(mappings, searchTerm, *types).let { + maxPage.set(ceil(it.results.size / 4.0).toInt()) + fuzzy.set(it.fuzzy) + it.results }) } } diff --git a/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryTranslateMappingsCommand.kt b/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryTranslateMappingsCommand.kt index 2e709c2..a26d620 100644 --- a/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryTranslateMappingsCommand.kt +++ b/src/main/kotlin/me/shedaniel/linkie/discord/commands/QueryTranslateMappingsCommand.kt @@ -55,6 +55,7 @@ import me.shedaniel.linkie.utils.QueryResult import me.shedaniel.linkie.utils.ResultHolder import me.shedaniel.linkie.utils.dropAndTake import me.shedaniel.linkie.utils.valueKeeper +import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger class QueryTranslateMappingsCommand( @@ -132,15 +133,20 @@ class QueryTranslateMappingsCommand( } suspend fun execute(ctx: CommandContext, src: Namespace, dst: Namespace, version: String, searchTerm: String, types: Array) = ctx.use { + val fuzzy = AtomicBoolean(false) val maxPage = AtomicInteger(-1) val result by valueKeeper { - translate(QueryMappingsExtensions.query(searchTerm, src.getProvider(version), user, message, maxPage, types), dst.getProvider(version)) + translate(QueryMappingsExtensions.query(searchTerm, src.getProvider(version), user, message, maxPage, fuzzy, types), dst.getProvider(version)) }.initiate() message.sendPages(ctx, 0, maxPage.get()) { page -> basicEmbed(user) if (maxPage.get() > 1) title("List of ${result.source.name}->${result.target.name} Mappings (Page ${page + 1}/${maxPage.get()})") else title("List of ${result.source.name}->${result.target.name} Mappings") buildSafeDescription { + if (fuzzy.get()) { + append("**No results found for __${searchTerm}__. Displaying related results.**").appendLine().appendLine() + } + var isFirst = true result.value.dropAndTake(4 * page, 4).forEach { (original, translated) -> if (!isFirst) { diff --git a/src/main/kotlin/me/shedaniel/linkie/discord/commands/ValueListCommand.kt b/src/main/kotlin/me/shedaniel/linkie/discord/commands/ValueListCommand.kt index 112d3ca..3b4a59e 100644 --- a/src/main/kotlin/me/shedaniel/linkie/discord/commands/ValueListCommand.kt +++ b/src/main/kotlin/me/shedaniel/linkie/discord/commands/ValueListCommand.kt @@ -22,7 +22,6 @@ import me.shedaniel.linkie.discord.OptionlessCommand import me.shedaniel.linkie.discord.config.ConfigManager import me.shedaniel.linkie.discord.config.GuildConfig import me.shedaniel.linkie.discord.utils.CommandContext -import me.shedaniel.linkie.discord.utils.QueryMessageBuilder.buildMessage import me.shedaniel.linkie.discord.utils.addInlineField import me.shedaniel.linkie.discord.utils.basicEmbed import me.shedaniel.linkie.discord.utils.description diff --git a/src/main/kotlin/me/shedaniel/linkie/discord/utils/QueryMessageBuilder.kt b/src/main/kotlin/me/shedaniel/linkie/discord/utils/QueryMessageBuilder.kt index 793c830..e95d252 100644 --- a/src/main/kotlin/me/shedaniel/linkie/discord/utils/QueryMessageBuilder.kt +++ b/src/main/kotlin/me/shedaniel/linkie/discord/utils/QueryMessageBuilder.kt @@ -30,9 +30,13 @@ import me.shedaniel.linkie.utils.dropAndTake import me.shedaniel.linkie.utils.localiseFieldDesc object QueryMessageBuilder { - fun buildMessage(spec: EmbedCreateSpec.Builder, namespace: Namespace, results: List>, mappings: MappingsMetadata, page: Int, author: User, maxPage: Int) { + fun buildMessage(spec: EmbedCreateSpec.Builder, searchTerm: String, namespace: Namespace, results: List>, mappings: MappingsMetadata, page: Int, author: User, maxPage: Int, fuzzy: Boolean) { buildHeader(spec, mappings, page, author, maxPage) spec.buildSafeDescription { + if (fuzzy) { + append("**No results found for __${searchTerm}__. Displaying related results.**").appendLine().appendLine() + } + var isFirst = true results.dropAndTake(4 * page, 4).forEach { (value, _) -> if (isFirst) {