Skip to content

Commit 33266fc

Browse files
committed
Bang on readline completion
Since [ is no longer in set_completer_delims, we have to split the line on the last one of these before we complete. For example "Integrate[Si" has to split of "Si" to complete against, but prepend "Integrate[" to any completions. The same thing is true for symbol names.
1 parent 0df8634 commit 33266fc

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

mathicsscript/termshell.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
)
3030

3131
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")
3636

3737
from colorama import init as colorama_init
3838
from term_background import is_dark_background
@@ -54,6 +54,12 @@
5454

5555
HISTFILE = osp.expanduser("~/.mathicsscript_hist")
5656

57+
RL_COMPLETER_DELIMS_WITH_BRACE = (
58+
" \t\n_~!@#%^&*()-=+{]}|;:'\",<>/?"
59+
)
60+
RL_COMPLETER_DELIMS = (
61+
" \t\n_~!@#%^&*()-=+[{]}\\|;:'\",<>/?"
62+
)
5763

5864

5965
from mathics.core.parser import LineFeeder, FileLineFeeder
@@ -83,12 +89,8 @@ def __init__(
8389

8490

8591
# 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)
9294

9395
inputrc = pathlib.Path(__file__).parent.absolute() / "inputrc"
9496
read_init_file(inputrc)
@@ -178,44 +180,56 @@ def rl_read_line(self, prompt):
178180

179181
def complete_symbol_name(self, text, state):
180182
try:
181-
match = re.match(r"^.*\\\[([A-Z][a-z]*)$", text)
183+
match = re.match(r"^(.*\\\[)([A-Z][a-z]*)$", text)
182184
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)
184186
return self._complete_symbol_name(text, state)
185-
except Exception:
187+
188+
except Exception as e:
186189
# any exception thrown inside the completer gets silently
187190
# thrown away otherwise
188191
print("Unhandled error in readline completion")
192+
except:
193+
raise
189194

190-
def _complete_named_characters(self, prefix, state):
195+
def _complete_named_characters(self, prefix, text, state):
191196
"""prefix is the text after \[. Return a list of named character names.
192197
"""
193198
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)
195201
try:
196202
return self.completion_candidates[state]
197203
except IndexError:
198204
return None
199205

200-
201206
def _complete_symbol_name(self, text, state):
202207
# The readline module calls this function repeatedly,
203208
# increasing 'state' each time and expecting one string to be
204209
# returned per call.
205-
206210
if state == 0:
207211
self.completion_candidates = self.get_completion_candidates(text)
208-
209212
try:
210213
return self.completion_candidates[state]
211214
except IndexError:
212215
return None
213216

214217
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 []
216230
if "`" not in text:
217231
matches = [strip_context(m) for m in matches]
218-
return matches
232+
return [prefix + m for m in matches]
219233

220234
def reset_lineno(self):
221235
self.lineno = 0

0 commit comments

Comments
 (0)