Skip to content

Commit

Permalink
Merge pull request #13817 from jasongrout/formatcell
Browse files Browse the repository at this point in the history
Make the formatting of a code block name extendable
  • Loading branch information
Carreau committed Nov 28, 2022
2 parents 4ea7a41 + 1c3678b commit 47abb68
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
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

0 comments on commit 47abb68

Please sign in to comment.