scoop:scoop.ui.component:0.1.0
src/command/filter.scoop
Selected production source · src/command/filter.scoop
1
package scoop.ui.component3
import scoop.collections.sortedWith4
import scoop.comparisons.comparatorOf6
private class RankedCommand(val entry: CommandEntry, val score: Double)8
private class CommandSection(val label: CommandEntry?, val separator: CommandEntry?) {9
val items = ArrayList<RankedCommand>()10
var score = 0.011
}13
/** Pure snapshot transformation; a provided source is returned without enumeration. */14
internal fun commandResults(15
source: CommandSource,16
query: String,17
filter: CommandFilter18
): CommandSource {19
if (query.byteLength == 0L)20
return source21
when (filter) {22
case Provided -> return source;23
else -> {}24
}25
val sections = ArrayList<CommandSection>()26
var current = CommandSection(None, None)27
sections.add(current)28
var separator: CommandEntry? = None29
for (index in 0L ..< source.count) {30
val entry = source.entryAt(index)31
when (entry) {32
case Label(_, _) -> {33
current = CommandSection(Some(entry), separator)34
separator = None35
sections.add(current)36
}37
case Separator(_) -> {38
separator = Some(entry)39
current = CommandSection(None, separator)40
sections.add(current)41
}42
case Item(val item) -> {43
val score =44
when (filter) {45
case Ranked -> commandScore(item.label, query, item.keywords)46
case Custom(val score) -> score(item, query)47
case Provided -> 1.048
}49
requireComponent(score.isFinite() && score >= 0.0, "invalid command score")50
if (score > 0.0) {51
current.items.add(RankedCommand(entry, score))52
if (score > current.score)53
current.score = score54
}55
separator = None56
}57
}58
}59
val result = ArrayList<CommandEntry>()60
val sorted =61
sections.sortedWith(62
comparatorOf<CommandSection> { left, right -> right.score.compareTo(left.score) }63
)64
for (section in sorted) {65
if (section.items.size == 0L)66
continue67
when (section.separator) {68
case Some(val entry) -> if (result.size > 0L)69
result.add(entry)70
case None -> {}71
}72
when (section.label) {73
case Some(val entry) -> result.add(entry);74
case None -> {}75
}76
for (77
item in section.items.sortedWith(78
comparatorOf<RankedCommand> { left, right -> right.score.compareTo(left.score) }79
)80
)81
result.add(item.entry)82
}83
return CommandContent(result.toArray())84
}