Created
September 29, 2015 17:19
-
-
Save mkremins/aa35746df32a5695f9bd to your computer and use it in GitHub Desktop.
Text completions with match highlighting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Debug | |
import List | |
import String | |
type alias Match = {full: String, before: String, match: String, after: String} | |
score : Match -> Int | |
score {before} = String.length before | |
match : String -> String -> Maybe Match | |
match sub sup = | |
case List.head (String.indexes sub sup) of | |
Nothing -> Nothing | |
Just start -> | |
let end = start + String.length sub | |
in Just {full = sup, | |
before = String.slice 0 start sup, | |
match = sub, | |
after = String.slice end (String.length sup) sup} | |
matches : String -> List String -> List Match | |
matches sub sups = | |
List.sort sups |> List.filterMap (match sub) |> List.sortBy score | |
_ = Debug.log "matches" (matches "foo" ["Arfoo","foo","bfoobar","fogey","golf","","fookin"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment