@staticmethod<EOL><INDENT>def find_matches(text, collection, start_only=False, fuzzy=True, casing=None):<DEDENT>
last = last_word(text, include='<STR_LIT>')<EOL>text = last.lower()<EOL>completions = []<EOL>if fuzzy:<EOL><INDENT>regex = '<STR_LIT>'.join(map(escape, text))<EOL>pat = compile('<STR_LIT>' % regex)<EOL>for item in sorted(collection):<EOL><INDENT>r = pat.search(item.lower())<EOL>if r:<EOL><INDENT>completions.append((len(r.group()), r.start(), item))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>match_end_limit = len(text) if start_only else None<EOL>for item in sorted(collection):<EOL><INDENT>match_point = item.lower().find(text, <NUM_LIT:0>, match_end_limit)<EOL>if match_point >= <NUM_LIT:0>:<EOL><INDENT>completions.append((len(text), match_point, item))<EOL><DEDENT><DEDENT><DEDENT>if casing == '<STR_LIT>':<EOL><INDENT>casing = '<STR_LIT>' if last and last[-<NUM_LIT:1>].islower() else '<STR_LIT>'<EOL><DEDENT>def apply_case(kw):<EOL><INDENT>if casing == '<STR_LIT>':<EOL><INDENT>return kw.upper()<EOL><DEDENT>return kw.lower()<EOL><DEDENT>return (Completion(z if casing is None else apply_case(z), -len(text))<EOL>for x, y, z in sorted(completions))<EOL>
Find completion matches for the given text.
        Given the user's input text and a collection of available
        completions, find completions matching the last word of the
        text.
        If `start_only` is True, the text will match an available
        completion only at the beginning. Otherwise, a completion is
        considered a match if the text appears anywhere within it.
        yields prompt_toolkit Completion instances for any matches found
        in the collection of available completions.