##// 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 return code_name(transformed_code, number)
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 def cache(self, transformed_code, number=0, raw_code=None):
134 def cache(self, transformed_code, number=0, raw_code=None):
120 """Make a name for a block of code, and cache the code.
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 def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):
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 Parameters
178 Parameters
179 ----------
179 ----------
@@ -184,23 +184,29 b' def _format_filename(file, ColorFilename, ColorNormal, *, lineno=None):'
184 ColorScheme's normal coloring to be used.
184 ColorScheme's normal coloring to be used.
185 """
185 """
186 ipinst = get_ipython()
186 ipinst = get_ipython()
187
187 if (
188 if ipinst is not None and file in ipinst.compile._filename_map:
188 ipinst is not None
189 file = "[%s]" % ipinst.compile._filename_map[file]
189 and (data := ipinst.compile.format_code_name(file)) is not None
190 ):
191 label, name = data
190 if lineno is None:
192 if lineno is None:
191 tpl_link = f"Cell {ColorFilename}In {{file}}{ColorNormal}"
193 tpl_link = f"{{label}} {ColorFilename}{{name}}{ColorNormal}"
192 else:
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 else:
198 else:
195 file = util_path.compress_user(
199 label = "File"
200 name = util_path.compress_user(
196 py3compat.cast_unicode(file, util_path.fs_encoding)
201 py3compat.cast_unicode(file, util_path.fs_encoding)
197 )
202 )
198 if lineno is None:
203 if lineno is None:
199 tpl_link = f"File {ColorFilename}{{file}}{ColorNormal}"
204 tpl_link = f"{{label}} {ColorFilename}{{name}}{ColorNormal}"
200 else:
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 # Module classes
212 # Module classes
General Comments 0
You need to be logged in to leave comments. Login now