Perform completion against a list but each match is split on a delimiter. Only the portion of the match being completed is shown as the completion suggestions. This is useful if you match against strings that are hierarchical in nature and have a common delimiter. A
(
self,
text: str,
line: str,
begidx: int,
endidx: int,
match_against: Iterable[str],
delimiter: str,
)
| 2059 | return Completions(items=matches, is_sorted=not sort) |
| 2060 | |
| 2061 | def delimiter_complete( |
| 2062 | self, |
| 2063 | text: str, |
| 2064 | line: str, |
| 2065 | begidx: int, |
| 2066 | endidx: int, |
| 2067 | match_against: Iterable[str], |
| 2068 | delimiter: str, |
| 2069 | ) -> Completions: |
| 2070 | """Perform completion against a list but each match is split on a delimiter. |
| 2071 | |
| 2072 | Only the portion of the match being completed is shown as the completion suggestions. |
| 2073 | This is useful if you match against strings that are hierarchical in nature and have a |
| 2074 | common delimiter. |
| 2075 | |
| 2076 | An easy way to illustrate this concept is path completion since paths are just directories/files |
| 2077 | delimited by a slash. If you are completing items in /home/user you don't get the following |
| 2078 | as suggestions: |
| 2079 | |
| 2080 | /home/user/file.txt /home/user/program.c |
| 2081 | /home/user/maps/ /home/user/cmd2.py |
| 2082 | |
| 2083 | Instead you are shown: |
| 2084 | |
| 2085 | file.txt program.c |
| 2086 | maps/ cmd2.py |
| 2087 | |
| 2088 | For a large set of data, this can be visually more pleasing and easier to search. |
| 2089 | |
| 2090 | Another example would be strings formatted with the following syntax: company::department::name |
| 2091 | In this case the delimiter would be :: and the user could easily narrow down what they are looking |
| 2092 | for if they were only shown suggestions in the category they are at in the string. |
| 2093 | |
| 2094 | :param text: the string prefix we are attempting to match (all matches must begin with it) |
| 2095 | :param line: the current input line with leading whitespace removed |
| 2096 | :param begidx: the beginning index of the prefix text |
| 2097 | :param endidx: the ending index of the prefix text |
| 2098 | :param match_against: the list being matched against |
| 2099 | :param delimiter: what delimits each portion of the matches (ex: paths are delimited by a slash) |
| 2100 | :return: a Completions object |
| 2101 | """ |
| 2102 | basic_completions = self.basic_complete(text, line, begidx, endidx, match_against) |
| 2103 | if not basic_completions: |
| 2104 | return Completions() |
| 2105 | |
| 2106 | match_strings = basic_completions.to_strings() |
| 2107 | |
| 2108 | # Calculate what portion of the match we are completing |
| 2109 | common_prefix = su.common_prefix(match_strings) |
| 2110 | prefix_tokens = common_prefix.split(delimiter) |
| 2111 | display_token_index = len(prefix_tokens) - 1 |
| 2112 | |
| 2113 | # Remove from each match everything after where the user is completing. |
| 2114 | # This approach can result in duplicates so we will filter those out. |
| 2115 | unique_results: dict[str, str] = {} |
| 2116 | |
| 2117 | allow_finalization = True |
| 2118 | for cur_match in match_strings: |