##// END OF EJS Templates
Make the formatting of a code block name extendable...
Jason Grout -
Show More
@@ -116,6 +116,21 b' class CachingCompiler(codeop.Compile):'
116 116 """
117 117 return code_name(transformed_code, number)
118 118
119 def format_code_name(self, name):
120 """Return a user-friendly label and name for a code block.
121
122 Parameters
123 ----------
124 name : str
125 The name for the code block returned from get_code_name
126
127 Returns
128 -------
129 A (label, name) pair that can be used in tracebacks, or None if the default formatting should be used.
130 """
131 if name in self._filename_map:
132 return "Cell", "In[%s]" % self._filename_map[name]
133
119 134 def cache(self, transformed_code, number=0, raw_code=None):
120 135 """Make a name for a block of code, and cache the code.
121 136
@@ -173,7 +173,7 b' def _format_traceback_lines(lines, Colors, has_colors: bool, lvals):'
173 173
174 174 def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):
175 175 """
176 Format filename lines with `In [n]` if it's the nth code cell or `File *.py` if it's a module.
176 Format filename lines with custom formatting from caching compiler or `File *.py` by default
177 177
178 178 Parameters
179 179 ----------
@@ -184,23 +184,29 b' def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):'
184 184 ColorScheme's normal coloring to be used.
185 185 """
186 186 ipinst = get_ipython()
187
188 if ipinst is not None and file in ipinst.compile._filename_map:
189 file = "[%s]" % ipinst.compile._filename_map[file]
187 if (
188 ipinst is not None
189 and (data := ipinst.compile.format_code_name(file)) is not None
190 ):
191 label, name = data
190 192 if lineno is None:
191 tpl_link = f"Cell {ColorFilename}In {{file}}{ColorNormal}"
193 tpl_link = f"{{label}} {ColorFilename}{{name}}{ColorNormal}"
192 194 else:
193 tpl_link = f"Cell {ColorFilename}In {{file}}, line {{lineno}}{ColorNormal}"
195 tpl_link = (
196 f"{{label}} {ColorFilename}{{name}}, line {{lineno}}{ColorNormal}"
197 )
194 198 else:
195 file = util_path.compress_user(
199 label = "File"
200 name = util_path.compress_user(
196 201 py3compat.cast_unicode(file, util_path.fs_encoding)
197 202 )
198 203 if lineno is None:
199 tpl_link = f"File {ColorFilename}{{file}}{ColorNormal}"
204 tpl_link = f"{{label}} {ColorFilename}{{name}}{ColorNormal}"
200 205 else:
201 tpl_link = f"File {ColorFilename}{{file}}:{{lineno}}{ColorNormal}"
206 # can we make this the more friendly ", line {{lineno}}", or do we need to preserve the formatting with the colon?
207 tpl_link = f"{{label}} {ColorFilename}{{name}}:{{lineno}}{ColorNormal}"
202 208
203 return tpl_link.format(file=file, lineno=lineno)
209 return tpl_link.format(label=label, name=name, lineno=lineno)
204 210
205 211 #---------------------------------------------------------------------------
206 212 # Module classes
General Comments 0
You need to be logged in to leave comments. Login now