|
29 | 29 | ) |
30 | 30 |
|
31 | 31 | color_scheme = TERMINAL_COLORS.copy() |
32 | | -color_scheme[Token.Name] = "yellow" |
33 | | -color_scheme[Name.Function] = "green" |
34 | | -color_scheme[Name.NameSpace] = "brown" |
35 | | -color_scheme[Literal.Number] = "blue" |
| 32 | +color_scheme[Token.Name] = ("yellow", "ansibrightyellow") |
| 33 | +color_scheme[Name.Function] = ("ansigreen", "ansibrightgreen") |
| 34 | +color_scheme[Name.NameSpace] = ("magenta", "ansibrightmagenta") |
| 35 | +color_scheme[Literal.Number] = ("ansiblue", "ansibrightblue") |
36 | 36 |
|
37 | 37 | from colorama import init as colorama_init |
38 | 38 | from term_background import is_dark_background |
|
54 | 54 |
|
55 | 55 | HISTFILE = osp.expanduser("~/.mathicsscript_hist") |
56 | 56 |
|
| 57 | +RL_COMPLETER_DELIMS_WITH_BRACE = ( |
| 58 | + " \t\n_~!@#%^&*()-=+{]}|;:'\",<>/?" |
| 59 | + ) |
| 60 | +RL_COMPLETER_DELIMS = ( |
| 61 | + " \t\n_~!@#%^&*()-=+[{]}\\|;:'\",<>/?" |
| 62 | + ) |
57 | 63 |
|
58 | 64 |
|
59 | 65 | from mathics.core.parser import LineFeeder, FileLineFeeder |
@@ -83,12 +89,8 @@ def __init__( |
83 | 89 |
|
84 | 90 |
|
85 | 91 | # Make _ a delimiter, but not $ or ` |
86 | | - # set_completer_delims( |
87 | | - # " \t\n_~!@#%^&*()-=+[{]}\\|;:'\",<>/?" |
88 | | - # ) |
89 | | - set_completer_delims( |
90 | | - " \t\n_~!@#%^&*()-=+{]}|;:'\",<>/?" |
91 | | - ) |
| 92 | + # set_completer_delims(RL_COMPLETER_DELIMS) |
| 93 | + set_completer_delims(RL_COMPLETER_DELIMS_WITH_BRACE) |
92 | 94 |
|
93 | 95 | inputrc = pathlib.Path(__file__).parent.absolute() / "inputrc" |
94 | 96 | read_init_file(inputrc) |
@@ -178,44 +180,56 @@ def rl_read_line(self, prompt): |
178 | 180 |
|
179 | 181 | def complete_symbol_name(self, text, state): |
180 | 182 | try: |
181 | | - match = re.match(r"^.*\\\[([A-Z][a-z]*)$", text) |
| 183 | + match = re.match(r"^(.*\\\[)([A-Z][a-z]*)$", text) |
182 | 184 | if match: |
183 | | - return self._complete_named_characters(match.group(1), state) |
| 185 | + return self._complete_named_characters(match.group(1), match.group(2), state) |
184 | 186 | return self._complete_symbol_name(text, state) |
185 | | - except Exception: |
| 187 | + |
| 188 | + except Exception as e: |
186 | 189 | # any exception thrown inside the completer gets silently |
187 | 190 | # thrown away otherwise |
188 | 191 | print("Unhandled error in readline completion") |
| 192 | + except: |
| 193 | + raise |
189 | 194 |
|
190 | | - def _complete_named_characters(self, prefix, state): |
| 195 | + def _complete_named_characters(self, prefix, text, state): |
191 | 196 | """prefix is the text after \[. Return a list of named character names. |
192 | 197 | """ |
193 | 198 | if state == 0: |
194 | | - self.completion_candidates = ["\\[" + name + "]" for name in self.named_character_names if name.startswith(prefix)] |
| 199 | + self.completion_candidates = [prefix + name + "]" for name in self.named_character_names if name.startswith(text)] |
| 200 | + # self.completion_candidates = self.get_completion_symbol_candidates(prefix, text) |
195 | 201 | try: |
196 | 202 | return self.completion_candidates[state] |
197 | 203 | except IndexError: |
198 | 204 | return None |
199 | 205 |
|
200 | | - |
201 | 206 | def _complete_symbol_name(self, text, state): |
202 | 207 | # The readline module calls this function repeatedly, |
203 | 208 | # increasing 'state' each time and expecting one string to be |
204 | 209 | # returned per call. |
205 | | - |
206 | 210 | if state == 0: |
207 | 211 | self.completion_candidates = self.get_completion_candidates(text) |
208 | | - |
209 | 212 | try: |
210 | 213 | return self.completion_candidates[state] |
211 | 214 | except IndexError: |
212 | 215 | return None |
213 | 216 |
|
214 | 217 | def get_completion_candidates(self, text): |
215 | | - matches = self.definitions.get_matching_names(text + "*") |
| 218 | + |
| 219 | + brace_pos = text.rfind("[") |
| 220 | + if brace_pos >= 0: |
| 221 | + suffix = text[brace_pos+1:] |
| 222 | + prefix = text[:brace_pos+1] |
| 223 | + else: |
| 224 | + prefix = "" |
| 225 | + suffix = text |
| 226 | + try: |
| 227 | + matches = self.definitions.get_matching_names(suffix + "*") |
| 228 | + except Exception as e: |
| 229 | + return [] |
216 | 230 | if "`" not in text: |
217 | 231 | matches = [strip_context(m) for m in matches] |
218 | | - return matches |
| 232 | + return [prefix + m for m in matches] |
219 | 233 |
|
220 | 234 | def reset_lineno(self): |
221 | 235 | self.lineno = 0 |
|
0 commit comments