Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions IPython/core/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,45 +1615,41 @@ def back_unicode_name_matches(text: str) -> Tuple[str, Sequence[str]]:


@context_matcher()
def back_latex_name_matcher(context: CompletionContext):
def back_latex_name_matcher(context: CompletionContext) -> SimpleMatcherResult:
"""Match latex characters back to unicode name

Same as :any:`back_latex_name_matches`, but adopted to new Matcher API.
This does ``\\ℵ`` -> ``\\aleph``
"""
fragment, matches = back_latex_name_matches(context.text_until_cursor)
return _convert_matcher_v1_result_to_v2(
matches, type="latex", fragment=fragment, suppress_if_matches=True
)


def back_latex_name_matches(text: str) -> Tuple[str, Sequence[str]]:
"""Match latex characters back to unicode name

This does ``\\ℵ`` -> ``\\aleph``
text = context.text_until_cursor
no_match = {
"completions": [],
"suppress": False,
}

.. deprecated:: 8.6
You can use :meth:`back_latex_name_matcher` instead.
"""
if len(text)<2:
return '', ()
return no_match
maybe_slash = text[-2]
if maybe_slash != '\\':
return '', ()

return no_match

char = text[-1]
# no expand on quote for completion in strings.
# nor backcomplete standard ascii keys
if char in string.ascii_letters or char in ('"',"'"):
return '', ()
return no_match
try :
latex = reverse_latex_symbol[char]
# '\\' replace the \ as well
return '\\'+char,[latex]
return {
"completions": [SimpleCompletion(text=latex, type="latex")],
"suppress": True,
"matched_fragment": "\\" + char,
}
except KeyError:
pass
return '', ()

return no_match

def _formatparamchildren(parameter) -> str:
"""
Expand Down Expand Up @@ -2699,15 +2695,7 @@ def dict_key_matches(self, text: str) -> List[str]:
return results

@context_matcher()
def unicode_name_matcher(self, context: CompletionContext):
"""Same as :any:`unicode_name_matches`, but adopted to new Matcher API."""
fragment, matches = self.unicode_name_matches(context.text_until_cursor)
return _convert_matcher_v1_result_to_v2(
matches, type="unicode", fragment=fragment, suppress_if_matches=True
)

@staticmethod
def unicode_name_matches(text: str) -> Tuple[str, List[str]]:
def unicode_name_matcher(self, context: CompletionContext) -> SimpleMatcherResult:
"""Match Latex-like syntax for unicode characters base
on the name of the character.

Expand All @@ -2716,17 +2704,27 @@ def unicode_name_matches(text: str) -> Tuple[str, List[str]]:
Works only on valid python 3 identifier, or on combining characters that
will combine to form a valid identifier.
"""

text = context.text_until_cursor

slashpos = text.rfind('\\')
if slashpos > -1:
s = text[slashpos+1:]
try :
unic = unicodedata.lookup(s)
# allow combining chars
if ('a'+unic).isidentifier():
return '\\'+s,[unic]
return {
"completions": [SimpleCompletion(text=unic, type="unicode")],
"suppress": True,
"matched_fragment": "\\" + s,
}
except KeyError:
pass
return '', []
return {
"completions": [],
"suppress": False,
}

@context_matcher()
def latex_name_matcher(self, context: CompletionContext):
Expand Down