Module: LineCache
- Defined in:
- lib/linecache2.rb,
lib/linecache2/colors.rb,
lib/linecache2/version.rb
Overview
Reopen main module to define the library version
Defined Under Namespace
Classes: LineCacheInfo
Constant Summary collapse
- ColorScheme =
{ light: CodeRay::Encoders::Terminal::TOKEN_COLORS.clone, # Adapted from the above dark: {:debug=>"\e[1;37;44m", :annotation=>"\e[33m", :attribute_name=>"\e[35m", :attribute_value=>"\e[31m", :binary=>{:self=>"\e[31m", :char=>"\e[1;31m", :delimiter=>"\e[1;31m"}, :char=>{:self=>"\e[35m", :delimiter=>"\e[1;35m"}, :class=> '', # "\e[1;36m", :class_variable=> '', # "\e[0;33m", :color=> '', # "\e[32m", :comment=>{:self=>"\e[3;31m", :char=>"\e[0;36m", :delimiter=>"\e[0;36m"}, :constant=>"\e[0;36;4m", :decorator=>"\e[35m", :definition=>"\e[1;33m", :directive=>"\e[33m", :docstring=>"\e[31m", :doctype=>"\e[1;33m", :done=>"\e[1;30;2m", :entity=>"\e[31m", :error=>"\e[1;37;41m", :exception=>"\e[1;31m", :float=>"\e[1;35m", :function=>"\e[1;33m", :global_variable=>"\e[1;32m", :hex=>"\e[1;36m", :id=>"\e[1;33m", :include=>"\e[31m", :integer=>"\e[1;34m", :imaginary=>"\e[1;34m", :important=>"\e[1;31m", :key=>{:self=>"\e[35m", :char=>"\e[1;35m", :delimiter=>"\e[1;35m"}, :keyword=>"\e[32m", :label=>"\e[1;33m", :local_variable=>"\e[33m", :namespace=>"\e[1;35m", :octal=>"\e[1;33m", :predefined=>"\e[36m", :predefined_constant=>"\e[1;36m", :predefined_type=>"\e[1;32m", :preprocessor=>"\e[1;36m", :pseudo_class=>"\e[1;33m", :regexp=> {:self=>"\e[35m", :delimiter=>"\e[1;35m", :modifier=>"\e[35m", :char=>"\e[1;35m"}, :reserved=>"\e[32m", :shell=> {:self=>"\e[33m", :char=>"\e[1;33m", :delimiter=>"\e[1;33m", :escape=>"\e[1;33m"}, :string=> {:self=>"\e[33m", :modifier=>"\e[1;31m", :char=>"\e[1;35m", :delimiter=>"\e[1;36m", :escape=>"\e[1;31m"}, :symbol=>{:self=>"\e[33m", :delimiter=>"\e[1;33m"}, :tag=>"\e[32m", :type=>"\e[1;33m", :value=>"\e[36m", :variable=>"\e[33m", :insert=>{:self=>"\e[42m", :insert=>"\e[1;32;42m", :eyecatcher=>"\e[102m"}, :delete=>{:self=>"\e[41m", :delete=>"\e[1;31;41m", :eyecatcher=>"\e[101m"}, :change=>{:self=>"\e[44m", :change=>"\e[37;44m"}, :head=>{:self=>"\e[45m", :filename=>"\e[37;45m"}, :method=>"\e[3;33m", :escape=>nil} # Extend by adding keys for your own style }
- VERSION =
'1.4.0'.freeze
- @@file_cache =
The file cache. The key is a name as would be given by Ruby for __FILE__. The value is a LineCacheInfo object.
{}
- @@iseq_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 =
{}
- @@iseq2file =
{}
Class Method Summary collapse
-
.cache(file_or_iseq, reload_on_change = false) ⇒ Object
Cache file name or iseq object if it’s not already cached.
-
.cache_file(filename, reload_on_change = false, opts = {}) ⇒ Object
Cache filename if it’s not already cached.
-
.cache_iseq(iseq, opts = {}) ⇒ Object
Cache iseq if it’s not already cached.
-
.cached?(file_or_iseq) ⇒ Object
Return true if file_or_iseq is cached.
-
.cached_files ⇒ Object
Return an array of cached file names.
- .cached_script?(filename) ⇒ Object
-
.checkcache(filename = nil, opts = {}) ⇒ 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_iseq_cache ⇒ Object
Clear the iseq cache entirely.
- .empty?(filename) ⇒ Object
-
.getline(file_or_iseq, line_number, opts = {}) ⇒ Object
Get line
line_numberfrom file namedfilename. -
.getlines(filename, opts = {}) ⇒ Object
Read lines of
filenameand cache the results. - .highlight_string(string, output_type, style = :light) ⇒ Object
-
.iseq_getlines(iseq, opts = {}) ⇒ Object
Read lines of
iseqand cache the results. - .iseq_is_eval?(iseq) ⇒ Object
- .map_file(file) ⇒ Object
- .map_file_line(file, line) ⇒ Object
- .map_iseq(iseq) ⇒ Object
-
.path(filename) ⇒ Object
Return full filename path for filename.
- .remap_file(to_file, from_file) ⇒ Object
- .remap_file_lines(from_file, to_file, range, start) ⇒ Object
- .remove_iseq_temps ⇒ Object
-
.sha1(filename) ⇒ Object
Return SHA1 of filename.
-
.size(file_or_iseq) ⇒ 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.
-
.update_cache(filename, opts = {}) ⇒ Object
Update a cache entry.
-
.update_iseq_cache(iseq, opts) ⇒ Object
Update a cache entry.
Class Method Details
.cache(file_or_iseq, reload_on_change = false) ⇒ Object
Cache file name or iseq object if it’s not already cached. Return the expanded filename for it in the cache if a filename, or the iseq, or nil if we can’t find the file.
199 200 201 202 203 204 205 |
# File 'lib/linecache2.rb', line 199 def cache(file_or_iseq, reload_on_change=false) if file_or_iseq.kind_of?(String) cache_file(file_or_iseq, reload_on_change) else cache_iseq(file_or_iseq) 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.
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/linecache2.rb', line 211 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_iseq(iseq, opts = {}) ⇒ Object
Cache iseq if it’s not already cached.
189 190 191 192 193 194 |
# File 'lib/linecache2.rb', line 189 def cache_iseq(iseq, opts={}) if !@@iseq_cache.member?(iseq) update_iseq_cache(iseq, opts) end iseq end |
.cached?(file_or_iseq) ⇒ Object
Return true if file_or_iseq is cached
226 227 228 229 230 231 232 |
# File 'lib/linecache2.rb', line 226 def cached?(file_or_iseq) if file_or_iseq.kind_of?(String) @@file_cache.member?(map_file(file_or_iseq)) else cached_iseq?(file_or_iseq) end end |
.cached_files ⇒ Object
Return an array of cached file names
146 147 148 |
# File 'lib/linecache2.rb', line 146 def cached_files() @@file_cache.keys end |
.cached_script?(filename) ⇒ Object
235 236 237 |
# File 'lib/linecache2.rb', line 235 def cached_script?(filename) SCRIPT_LINES__.member?(map_file(filename)) end |
.checkcache(filename = nil, opts = {}) ⇒ 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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/linecache2.rb', line 156 def checkcache(filename=nil, opts={}) 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.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/linecache2.rb', line 127 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.
117 118 119 120 121 122 123 124 |
# File 'lib/linecache2.rb', line 117 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_iseq_cache ⇒ Object
Clear the iseq cache entirely.
140 141 142 |
# File 'lib/linecache2.rb', line 140 def clear_iseq_cache() @@iseq_cache = {} end |
.empty?(filename) ⇒ Object
240 241 242 243 |
# File 'lib/linecache2.rb', line 240 def empty?(filename) filename=map_file(filename) @@file_cache[filename].lines.empty? end |
.getline(file_or_iseq, line_number, opts = {}) ⇒ 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')
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/linecache2.rb', line 257 def getline(file_or_iseq, line_number, opts={}) lines = if file_or_iseq.kind_of?(String) filename = map_file(file_or_iseq) filename, line_number = map_file_line(filename, line_number) getlines(filename, opts) else iseq_getlines(file_or_iseq) end if lines and (1..lines.size) === line_number return lines[line_number-1] else return nil end end |
.getlines(filename, opts = {}) ⇒ 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
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/linecache2.rb', line 301 def getlines(filename, opts={}) filename = map_file(filename) checkcache(filename) if opts[:reload_on_change] format = opts[:output] || :plain style = opts[:style] || :light if @@file_cache.member?(filename) lines = @@file_cache[filename].lines if opts[:output] && !lines[format] lines[format] = highlight_string(lines[:plain].join(''), format, style.to_sym ).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, style = :light) ⇒ Object
326 327 328 329 330 331 332 333 334 335 |
# File 'lib/linecache2.rb', line 326 def highlight_string(string, output_type, style=:light) # coderay just has one module variable that it uses in colorizing # for terminals. No "style" parameters.. So we need to smash/set # that variable before encoding. CodeRay::Encoders::Terminal::TOKEN_COLORS.merge!(LineCache::ColorScheme[style]) if LineCache::ColorScheme.member?(style.to_sym) @@ruby_highlighter = CodeRay::Duo[:ruby, output_type] @@ruby_highlighter.encode(string) end |
.iseq_getlines(iseq, opts = {}) ⇒ Object
Read lines of iseq and cache the results. However iseq was previously cached use the results from the cache. Return nil if we can’t get lines
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/linecache2.rb', line 276 def iseq_getlines(iseq, opts={}) return nil unless iseq.kind_of? RubyVM::InstructionSequence format = opts[:output] || :plain line_formats = if @@iseq_cache.member?(iseq) @@iseq_cache[iseq].lines else update_iseq_cache(iseq, opts) if @@iseq_cache.member?(iseq) @@iseq_cache[iseq].lines else nil end end return nil unless line_formats if format != :plain && !line_formats[format] highlight_string(line_formats[:plain].join('')).split(/\n/) else line_formats[format] end end |
.iseq_is_eval?(iseq) ⇒ Object
446 447 448 |
# File 'lib/linecache2.rb', line 446 def iseq_is_eval?(iseq) !!iseq.eval_source end |
.map_file(file) ⇒ Object
412 413 414 |
# File 'lib/linecache2.rb', line 412 def map_file(file) @@file2file_remap[file] ? @@file2file_remap[file] : file end |
.map_file_line(file, line) ⇒ Object
433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/linecache2.rb', line 433 def map_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 [map_file(file), line] end |
.map_iseq(iseq) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/linecache2.rb', line 416 def map_iseq(iseq) if @@iseq2file[iseq] @@iseq2file[iseq] else # Doc says there's new takes an optional string parameter # But it doesn't work for me sha1 = Digest::SHA1.new string = iseq.eval_source sha1 << iseq.eval_source tempfile = Tempfile.new(["eval-#{sha1.hexdigest[0...7]}-", '.rb']) tempfile.open.puts(string) tempfile.close @@iseq2file[iseq] = tempfile.path tempfile.path end end |
.path(filename) ⇒ Object
Return full filename path for filename
338 339 340 341 342 343 |
# File 'lib/linecache2.rb', line 338 def path(filename) return unless filename.kind_of?(String) filename = map_file(filename) return nil unless @@file_cache.member?(filename) @@file_cache[filename].path end |
.remap_file(to_file, from_file) ⇒ Object
345 346 347 |
# File 'lib/linecache2.rb', line 345 def remap_file(to_file, from_file) @@file2file_remap[to_file] = from_file end |
.remap_file_lines(from_file, to_file, range, start) ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/linecache2.rb', line 349 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_iseq_temps ⇒ Object
105 106 107 108 109 |
# File 'lib/linecache2.rb', line 105 def remove_iseq_temps @@iseq2file.values.each do |filename| File.unlink(filename) if File.exist?(filename) end end |
.sha1(filename) ⇒ Object
Return SHA1 of filename.
362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/linecache2.rb', line 362 def sha1(filename) filename = map_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_iseq) ⇒ Object
Return the number of lines in filename
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/linecache2.rb', line 376 def size(file_or_iseq) cache(file_or_iseq) if file_or_iseq.kind_of?(String) file_or_iseq = map_file(file_or_iseq) return nil unless @@file_cache.member?(file_or_iseq) @@file_cache[file_or_iseq].lines[:plain].length else return nil unless @@iseq_cache.member?(file_or_iseq) @@iseq_cache[file_or_iseq].lines.length @@script_cache[file_or_iseq].lines[:plain].length end end |
.stat(filename) ⇒ Object
Return File.stat in the cache for filename.
390 391 392 393 |
# File 'lib/linecache2.rb', line 390 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.
400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/linecache2.rb', line 400 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 |
.update_cache(filename, opts = {}) ⇒ Object
Update a cache entry. If something’s wrong, return nil. Return true if the cache was updated and false if not. If opts is true, use that as the source for the lines of the file
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 |
# File 'lib/linecache2.rb', line 469 def update_cache(filename, opts={}) return nil unless filename @@file_cache.delete(filename) path = File.(filename) 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() style = opts[:style] || :light lines[opts[:output]] = highlight_string(raw_string, opts[:output], style).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_iseq_cache(iseq, opts) ⇒ Object
Update a cache entry. If something is wrong, return nil. Return true if the cache was updated and false if not.
453 454 455 456 457 458 459 460 461 462 463 |
# File 'lib/linecache2.rb', line 453 def update_iseq_cache(iseq, opts) return false unless iseq_is_eval?(iseq) style = opts[:style] || :light string = opts[:string] || iseq.eval_source lines = {:plain => string.split(/\n/)} lines[opts[:output]] = highlight_string(string, opts[:output], style) if opts[:output] @@iseq_cache[iseq] = LineCacheInfo.new(nil, nil, lines, nil, opts[:sha1]) return true end |