Skip to content

Instantly share code, notes, and snippets.

@mkremins
Created September 29, 2015 17:19
Show Gist options
  • Save mkremins/aa35746df32a5695f9bd to your computer and use it in GitHub Desktop.
Save mkremins/aa35746df32a5695f9bd to your computer and use it in GitHub Desktop.
Text completions with match highlighting
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