Module: LineCache
- Defined in:
- lib/linecache.rb,
lib/version.rb
Overview
module LineCache
A module to read and cache lines of a Ruby program.
Defined Under Namespace
Classes: LineCacheInfo
Constant Summary collapse
- VERSION =
'0.45.1'- @@file_cache =
The file cache. The key is a name as would be given by Ruby for __FILE__. The value is a LineCacheInfo object.
{}
- @@script_cache =
{}
- @@ruby_highlighter =
Used for CodeRay syntax highlighting
nil- @@file2file_remap =
Maps a string filename (a String) to a key in @@file_cache (a String).
One important use of @@file2file_remap is mapping the a full path of a file into the name stored in @@file_cache or given by Ruby’s __FILE__. Applications such as those that get input from users, may want canonicalize a file name before looking it up. This map gives a way to do that.
Another related use is when a template system is used. Here we’ll probably want to remap not only the file name but also line ranges. Will probably use this for that, but I’m not sure.
{}
- @@file2file_remap_lines =
{}
- @@script2file =
{}
Class Method Summary collapse
-
.cache(filename, reload_on_change = false) ⇒ Object
Older routine - for compability.
-
.cache_file(filename, reload_on_change = false, opts = {}) ⇒ Object
Cache filename if it’s not already cached.
-
.cache_script(script, opts = {}) ⇒ Object
Cache script if it’s not already cached.
-
.cached?(file_or_script) ⇒ Boolean
Return true if file_or_script is cached.
-
.cached_files ⇒ Object
Return an array of cached file names.
- .cached_script?(filename) ⇒ Boolean
-
.checkcache(filename = nil, opts = false) ⇒ Object
Discard cache entries that are out of date.
-
.clear_file_cache(filename = nil) ⇒ Object
Clear the file cache entirely.
-
.clear_file_format_cache ⇒ Object
Remove syntax-formatted lines in the cache.
-
.clear_script_cache ⇒ Object
Clear the script cache entirely.
- .empty?(filename) ⇒ Boolean
-
.getline(file_or_script, line_number, opts = true) ⇒ Object
Get line
line_numberfrom file namedfilename. -
.getlines(filename, opts = false) ⇒ Object
Read lines of
filenameand cache the results. - .highlight_string(string, output_type) ⇒ Object
-
.path(filename) ⇒ Object
Return full filename path for filename.
- .remap_file(from_file, to_file) ⇒ Object
- .remap_file_lines(from_file, to_file, range, start) ⇒ Object
- .remove_script_temps ⇒ Object
-
.sha1(filename) ⇒ Object
Return SHA1 of filename.
-
.size(file_or_script) ⇒ Object
Return the number of lines in filename.
-
.stat(filename) ⇒ Object
Return File.stat in the cache for filename.
-
.trace_line_numbers(filename, reload_on_change = false) ⇒ Object
Return an Array of breakpoints in filename.
- .unmap_file(file) ⇒ Object (also: map_file)
- .unmap_file_line(file, line) ⇒ Object
-
.update_cache(filename, opts = false) ⇒ Object
Update a cache entry.
-
.update_script_cache(script, opts) ⇒ Object
Update a cache entry.
Class Method Details
.cache(filename, reload_on_change = false) ⇒ Object
Older routine - for compability. Cache filename if it’s not already cached. Return the expanded filename for it in the cache or nil if we can’t find the file.
216 217 218 219 220 221 222 |
# File 'lib/linecache.rb', line 216 def cache(file_or_script, reload_on_change=false) if file_or_script.kind_of?(String) cache_file(file_or_script, reload_on_change) else cache_script(file_or_script) end end |
.cache_file(filename, reload_on_change = false, opts = {}) ⇒ Object
Cache filename if it’s not already cached. Return the expanded filename for it in the cache or nil if we can’t find the file.
227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/linecache.rb', line 227 def cache_file(filename, reload_on_change=false, opts={}) if @@file_cache.member?(filename) checkcache(filename) if reload_on_change else opts[:use_script_lines] = true update_cache(filename, opts) end if @@file_cache.member?(filename) @@file_cache[filename].path else nil end end |
.cache_script(script, opts = {}) ⇒ Object
Cache script if it’s not already cached.
206 207 208 209 210 211 |
# File 'lib/linecache.rb', line 206 def cache_script(script, opts={}) if !@@script_cache.member?(script) update_script_cache(script, opts) end script end |
.cached?(file_or_script) ⇒ Boolean
Return true if file_or_script is cached
259 260 261 262 263 264 265 |
# File 'lib/linecache.rb', line 259 def cached?(file_or_script) if file_or_script.kind_of?(String) @@file_cache.member?(unmap_file(file_or_script)) else cached_script?(file_or_script) end end |
.cached_files ⇒ Object
Return an array of cached file names
157 158 159 |
# File 'lib/linecache.rb', line 157 def cached_files() @@file_cache.keys end |
.cached_script?(filename) ⇒ Boolean
267 268 269 |
# File 'lib/linecache.rb', line 267 def cached_script?(filename) SCRIPT_LINES__.member?(unmap_file(filename)) end |
.checkcache(filename = nil, opts = false) ⇒ Object
Discard cache entries that are out of date. If filename is nil all entries in the file cache @@file_cache are checked. If we don’t have stat information about a file, which can happen if the file was read from __SCRIPT_LINES but no corresponding file is found, it will be kept. Return a list of invalidated filenames. nil is returned if a filename was given but not found cached.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/linecache.rb', line 167 def checkcache(filename=nil, opts=false) use_script_lines = if opts.kind_of?(Hash) opts[:use_script_lines] else opts end if !filename filenames = @@file_cache.keys() elsif @@file_cache.member?(filename) filenames = [filename] else return nil end result = [] for filename in filenames next unless @@file_cache.member?(filename) path = @@file_cache[filename].path if File.exist?(path) cache_info = @@file_cache[filename].stat stat = File.stat(path) if cache_info if stat && (cache_info.size != stat.size or cache_info.mtime != stat.mtime) result << filename update_cache(filename, opts) end else result << filename update_cache(filename, opts) end end end return result end |
.clear_file_cache(filename = nil) ⇒ Object
Clear the file cache entirely.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/linecache.rb', line 113 def clear_file_cache(filename=nil) if filename if @@file_cache[filename] @@file_cache.delete(filename) end else @@file_cache = {} @@file2file_remap = {} @@file2file_remap_lines = {} end end |
.clear_file_format_cache ⇒ Object
Remove syntax-formatted lines in the cache. Use this when you change the CodeRay syntax or Token formatting and want to redo how files may have previously been syntax marked.
129 130 131 132 133 134 135 136 |
# File 'lib/linecache.rb', line 129 def clear_file_format_cache @@file_cache.each_pair do |fname, cache_info| cache_info.lines.each_pair do |format, lines| next if :plain == format @@file_cache[fname].lines[format] = nil end end end |
.clear_script_cache ⇒ Object
Clear the script cache entirely.
152 153 154 |
# File 'lib/linecache.rb', line 152 def clear_script_cache() @@script_cache = {} end |
.empty?(filename) ⇒ Boolean
271 272 273 274 |
# File 'lib/linecache.rb', line 271 def empty?(filename) filename=unmap_file(filename) !!@@file_cache[filename].lines[:plain] end |
.getline(file_or_script, line_number, opts = true) ⇒ Object
Get line line_number from file named filename. Return nil if there was a problem. If a file named filename is not found, the function will look for it in the $: array.
Examples:
lines = LineCache::getline('/tmp/myfile.rb')
# Same as above
$: << '/tmp'
lines = LineCache.getlines('myfile.rb')
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/linecache.rb', line 287 def getline(file_or_script, line_number, opts=true) reload_on_change = if opts.kind_of?(Hash) opts[:reload_on_change] else opts end lines = if file_or_script.kind_of?(String) filename = unmap_file(file_or_script) filename, line_number = unmap_file_line(filename, line_number) getlines(filename, opts) else script_getlines(file_or_script) end if lines and (1..lines.size) === line_number return lines[line_number-1] else return nil end end |
.getlines(filename, opts = false) ⇒ Object
Read lines of filename and cache the results. However filename was previously cached use the results from the cache. Return nil if we can’t get lines
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/linecache.rb', line 312 def getlines(filename, opts=false) if opts.kind_of?(Hash) reload_on_change, use_script_lines = [opts[:reload_on_change], opts[:use_script_lines]] else reload_on_change, use_script_lines = [opts, false] opts = {:reload_on_change => reload_on_change} end checkcache(filename) if reload_on_change format = opts[:output] || :plain if @@file_cache.member?(filename) lines = @@file_cache[filename].lines if opts[:output] && !lines[format] lines[format] = highlight_string(lines[:plain].join(''), format).split(/\n/) end return lines[format] else opts[:use_script_lines] = true update_cache(filename, opts) if @@file_cache.member?(filename) return @@file_cache[filename].lines[format] else return nil end end end |
.highlight_string(string, output_type) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/linecache.rb', line 340 def highlight_string(string, output_type) require 'rubygems' begin require 'coderay' require 'term/ansicolor' rescue LoadError return string end @@ruby_highlighter ||= CodeRay::Duo[:ruby, output_type] @@ruby_highlighter.encode(string) end |
.path(filename) ⇒ Object
Return full filename path for filename
353 354 355 356 357 358 |
# File 'lib/linecache.rb', line 353 def path(filename) return unless filename.kind_of?(String) filename = unmap_file(filename) return nil unless @@file_cache.member?(filename) @@file_cache[filename].path end |
.remap_file(from_file, to_file) ⇒ Object
360 361 362 363 |
# File 'lib/linecache.rb', line 360 def remap_file(from_file, to_file) @@file2file_remap[from_file] = to_file cache_file(to_file) end |
.remap_file_lines(from_file, to_file, range, start) ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/linecache.rb', line 365 def remap_file_lines(from_file, to_file, range, start) range = (range..range) if range.kind_of?(Fixnum) to_file = from_file unless to_file if @@file2file_remap_lines[to_file] # FIXME: need to check for overwriting ranges: whether # they intersect or one encompasses another. @@file2file_remap_lines[to_file] << [from_file, range, start] else @@file2file_remap_lines[to_file] = [[from_file, range, start]] end end |
.remove_script_temps ⇒ Object
104 105 106 107 108 |
# File 'lib/linecache.rb', line 104 def remove_script_temps @@script2file.values.each do |filename| File.unlink(filename) if File.exist?(filename) end end |
.sha1(filename) ⇒ Object
Return SHA1 of filename.
379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/linecache.rb', line 379 def sha1(filename) filename = unmap_file(filename) return nil unless @@file_cache.member?(filename) return @@file_cache[filename].sha1.hexdigest if @@file_cache[filename].sha1 sha1 = Digest::SHA1.new @@file_cache[filename].lines[:plain].each do |line| sha1 << line + "\n" end @@file_cache[filename].sha1 = sha1 sha1.hexdigest end |
.size(file_or_script) ⇒ Object
Return the number of lines in filename
393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/linecache.rb', line 393 def size(file_or_script) cache(file_or_script) if file_or_script.kind_of?(String) file_or_script = unmap_file(file_or_script) return nil unless @@file_cache.member?(file_or_script) @@file_cache[file_or_script].lines[:plain].length else return nil unless @@script_cache.member?(file_or_script) @@script_cache[file_or_script].lines[:plain].length end end |
.stat(filename) ⇒ Object
Return File.stat in the cache for filename.
406 407 408 409 |
# File 'lib/linecache.rb', line 406 def stat(filename) return nil unless @@file_cache.member?(filename) @@file_cache[filename].stat end |
.trace_line_numbers(filename, reload_on_change = false) ⇒ Object
Return an Array of breakpoints in filename. The list will contain an entry for each distinct line event call so it is possible (and possibly useful) for a line number appear more than once.
416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/linecache.rb', line 416 def trace_line_numbers(filename, reload_on_change=false) fullname = cache(filename, reload_on_change) return nil unless fullname e = @@file_cache[filename] unless e.line_numbers e.line_numbers = TraceLineNumbers.lnums_for_str_array(e.lines[:plain]) e.line_numbers = false unless e.line_numbers end e.line_numbers end |
.unmap_file(file) ⇒ Object Also known as: map_file
428 429 430 |
# File 'lib/linecache.rb', line 428 def unmap_file(file) @@file2file_remap[file] ? @@file2file_remap[file] : file end |
.unmap_file_line(file, line) ⇒ Object
433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/linecache.rb', line 433 def unmap_file_line(file, line) if @@file2file_remap_lines[file] @@file2file_remap_lines[file].each do |from_file, range, start| if range === line from_file = from_file || file return [from_file, start+line-range.begin] end end end return [file, line] end |
.update_cache(filename, opts = false) ⇒ Object
Update a cache entry. If something’s wrong, return nil. Return true if the cache was updated and false if not. If use_script_lines is true, use that as the source for the lines of the file
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/linecache.rb', line 463 def update_cache(filename, opts=false) if opts.kind_of?(Hash) use_script_lines = opts[:use_script_lines] else use_script_lines = opts opts = {:use_script_lines => use_script_lines} end return nil unless filename @@file_cache.delete(filename) path = File.(filename) if use_script_lines list = [filename] list << @@file2file_remap[path] if @@file2file_remap[path] list.each do |name| if !SCRIPT_LINES__[name].nil? && SCRIPT_LINES__[name] != true begin stat = File.stat(name) rescue stat = nil end raw_lines = SCRIPT_LINES__[name] lines = {:plain => raw_lines} lines[opts[:output]] = highlight_string(raw_lines.join, opts[:output]).split(/\n/) if opts[:output] @@file_cache[filename] = LineCacheInfo.new(stat, nil, lines, path, nil) @@file2file_remap[path] = filename return true end end end if File.exist?(path) stat = File.stat(path) elsif File.basename(filename) == filename # try looking through the search path. stat = nil for dirname in $: path = File.join(dirname, filename) if File.exist?(path) stat = File.stat(path) break end end return false unless stat end begin fp = File.open(path, 'r') raw_string = fp.read fp.rewind lines = {:plain => fp.readlines} fp.close() lines[opts[:output]] = highlight_string(raw_string, opts[:output]).split(/\n/) if opts[:output] rescue ## print '*** cannot open', path, ':', msg return nil end @@file_cache[filename] = LineCacheInfo.new(File.stat(path), nil, lines, path, nil) @@file2file_remap[path] = filename return true end |
.update_script_cache(script, opts) ⇒ Object
Update a cache entry. If something is wrong, return nil. Return true if the cache was updated and false if not.
447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/linecache.rb', line 447 def update_script_cache(script, opts) # return false unless script_is_eval?(script) # string = opts[:string] || script.eval_source lines = {:plain => string.split(/\n/)} lines[opts[:output]] = highlight_string(string, opts[:output]) if opts[:output] @@script_cache[script] = LineCacheInfo.new(nil, nil, lines, nil, opts[:sha1], opts[:compiled_method]) return true end |