Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the formatting of a code block name extendable #13817

Merged
merged 1 commit into from Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions IPython/core/compilerop.py
Expand Up @@ -116,6 +116,21 @@ def get_code_name(self, raw_code, transformed_code, number):
"""
return code_name(transformed_code, number)

def format_code_name(self, name):
"""Return a user-friendly label and name for a code block.

Parameters
----------
name : str
The name for the code block returned from get_code_name

Returns
-------
A (label, name) pair that can be used in tracebacks, or None if the default formatting should be used.
"""
if name in self._filename_map:
return "Cell", "In[%s]" % self._filename_map[name]

def cache(self, transformed_code, number=0, raw_code=None):
"""Make a name for a block of code, and cache the code.

Expand Down
26 changes: 16 additions & 10 deletions IPython/core/ultratb.py
Expand Up @@ -173,7 +173,7 @@ def _format_traceback_lines(lines, Colors, has_colors: bool, lvals):

def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):
"""
Format filename lines with `In [n]` if it's the nth code cell or `File *.py` if it's a module.
Format filename lines with custom formatting from caching compiler or `File *.py` by default

Parameters
----------
Expand All @@ -184,23 +184,29 @@ def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):
ColorScheme's normal coloring to be used.
"""
ipinst = get_ipython()

if ipinst is not None and file in ipinst.compile._filename_map:
file = "[%s]" % ipinst.compile._filename_map[file]
if (
ipinst is not None
and (data := ipinst.compile.format_code_name(file)) is not None
):
label, name = data
if lineno is None:
tpl_link = f"Cell {ColorFilename}In {{file}}{ColorNormal}"
tpl_link = f"{{label}} {ColorFilename}{{name}}{ColorNormal}"
else:
tpl_link = f"Cell {ColorFilename}In {{file}}, line {{lineno}}{ColorNormal}"
tpl_link = (
f"{{label}} {ColorFilename}{{name}}, line {{lineno}}{ColorNormal}"
)
else:
file = util_path.compress_user(
label = "File"
name = util_path.compress_user(
py3compat.cast_unicode(file, util_path.fs_encoding)
)
if lineno is None:
tpl_link = f"File {ColorFilename}{{file}}{ColorNormal}"
tpl_link = f"{{label}} {ColorFilename}{{name}}{ColorNormal}"
else:
tpl_link = f"File {ColorFilename}{{file}}:{{lineno}}{ColorNormal}"
# can we make this the more friendly ", line {{lineno}}", or do we need to preserve the formatting with the colon?
tpl_link = f"{{label}} {ColorFilename}{{name}}:{{lineno}}{ColorNormal}"

return tpl_link.format(file=file, lineno=lineno)
return tpl_link.format(label=label, name=name, lineno=lineno)

#---------------------------------------------------------------------------
# Module classes
Expand Down