Class: CodeTree
Class Method Summary collapse
- .add_menu(item) ⇒ Object
- .as_indented ⇒ Object
-
.bulletize(list) ⇒ Object
Turn list into bulleted string (adding + or - based on ending in slash).
- .definite_code_tree_root(line) ⇒ Object
- .definite_dir_tree_root(line) ⇒ Object
-
.determine_code_from_path(path) ⇒ Object
Rules for constructing code from path - examine path consisting of where C-.
- .do_kill_indented(options = {}) ⇒ Object
- .draw_exception(exception, code = nil) ⇒ Object
-
.escape(l) ⇒ Object
Always escape backslashes, single-quotes, #‘s.
- .extract_class(l) ⇒ Object
- .extract_method(l) ⇒ Object
-
.handles?(list = nil) ⇒ Boolean
Determine whether we should handle it.
- .kill_rest ⇒ Object
- .kill_siblings(options = {}) ⇒ Object
-
.launch(options = {}) ⇒ Object
Mapped to C-.
- .layout_menu ⇒ Object
- .maybe_code_tree_root(line) ⇒ Object
- .menu ⇒ Object
- .menus ⇒ Object
- .no_search_option ⇒ Object
- .paramify(l) ⇒ Object
- .quote_search_option ⇒ Object
- .remove_slashes(*args) ⇒ Object
- .returned_to_s(returned) ⇒ Object
- .run(code, options = {}) ⇒ Object
- .tree_search_option ⇒ Object
Class Method Details
.add_menu(item) ⇒ Object
8 9 10 11 |
# File 'lib/xiki/code_tree.rb', line 8 def self. item @menus ||= [] @menus << item unless @menus.member? item end |
.as_indented ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/xiki/code_tree.rb', line 391 def self.as_indented prefix = Keys.prefix :clear=>1 indent = Line.indent.length # If at left margin or current line followed by child if prefix != :u && (prefix == :- || indent == 0 || indent == Line.indent(Line.value(2)).length - 2) orig = View.cursor left = Line.left Line.next ignore, right = Tree.sibling_bounds :cross_blank_lines=>1 View.cursor = orig elsif prefix == :u left, ignore1, ignore2, right = Tree.sibling_bounds else left, right = Tree.sibling_bounds :cross_blank_lines=>1 end View.cursor = left $el.set_mark right Effects.blink :left=>left, :right=>right Clipboard.copy "0" end |
.bulletize(list) ⇒ Object
Turn list into bulleted string (adding + or - based on ending in slash)
466 467 468 |
# File 'lib/xiki/code_tree.rb', line 466 def self.bulletize list (list.map{|l| l =~ /\/$/ ? "+ #{l}" : "- #{l}"}.join("\n")) + "\n" end |
.definite_code_tree_root(line) ⇒ Object
453 454 455 |
# File 'lib/xiki/code_tree.rb', line 453 def self.definite_code_tree_root line line =~ /^[\s+-]*[A-Z].+\..+[\/)]$/ end |
.definite_dir_tree_root(line) ⇒ Object
461 462 463 |
# File 'lib/xiki/code_tree.rb', line 461 def self.definite_dir_tree_root line line =~ /^[\s+-]*\// end |
.determine_code_from_path(path) ⇒ Object
Rules for constructing code from path
-
examine path consisting of where C-. occurred and all its ancestors
-
root of tree is first ancestor with Class.method
-
invoke first ancestor method (using its params)
-
append all ancestor data nodes as params
-
include self if a data node
-
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/xiki/code_tree.rb', line 244 def self.determine_code_from_path path data = [] clazz = metho = nil metho_maybe = clazz_maybe = nil i = -1 path.reverse.each do |l| # Climb up path i += 1 l.sub! /^@/, '' # Remove delegate, so it's respected metho_tmp = self.extract_method(l) clazz_tmp = self.extract_class(l) l.sub(/^(\s+)[+-] /, "\\1") # Remove bullets is_root = i + 1 == path.size code_node = false if metho_tmp # If line has method if clazz_tmp # If line has class if is_root || self.definite_code_tree_root(l) metho ||= metho_tmp code_node = true clazz ||= clazz_tmp break if clazz else # Use this if nothing else is found metho_maybe ||= metho_tmp clazz_maybe ||= clazz_tmp end else # Otherwise, it can be a method metho ||= metho_tmp code_node = true end end if ! code_node # If not code node, must be data node # Prepend to data list data << self.paramify(l) end end unless clazz # If no code found return nil unless clazz_maybe metho = metho_maybe clazz = clazz_maybe data = [] end # If one line, return it literally if i == 0 return Line.without_label(:line=>path[-1]).sub(/\/$/, '') end # Extract any params from after method method_with_params, params = metho.match(/(\w+\??)(.*)/)[1..2] params.sub!(/^\((.*)\)$/, "\\1") # Remove parens if they're there # Surround with curly brackets if it's a hash, and no curly brackets already # Assumes all params are in a hash, which may not be right if params =~ /=>/ && params !~ /\}$/ params = " {#{params.strip}}" end # If last parameter was |..., make it be all the lines if data.first =~ /^ *\|/ data.first.replace( self.escape( Tree.siblings(:all=>true).map{|i| "#{i[/^ *\| ?(.*)/, 1]}\n"}.join('') )) end # If any data nodes, pass as params if ! data.empty? data.reverse! data.map! {|a| "\"#{a}\""} params << ", " + data.join(", ") end # TODO Get rid of comma if there is one params.sub!(/^, /, '') "#{clazz}.#{method_with_params}(#{params})" end |
.do_kill_indented(options = {}) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/xiki/code_tree.rb', line 376 def self.do_kill_indented ={} if Keys.up? || [:cross_blank_lines] left, right = Tree.sibling_bounds :cross_blank_lines=>1 else left, ignore1, ignore2, right = Tree.sibling_bounds end View.cursor = left $el.set_mark right Effects.blink :left=>left, :right=>right # unless options[:dont_cross_blank_lines] View.delete left, right end |
.draw_exception(exception, code = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/xiki/code_tree.rb', line 48 def self.draw_exception exception, code=nil = exception. # TODO: pay attention to this option? # Ol << "options[:suggest_args]: #{options[:suggest_args].inspect}" # TODO # For now, don't do it! # - How to not show "arg1" etc # - when ArgumentError is code deeper than the immediate one # if exception.is_a? ArgumentError # count, required = message.match(/\((\d+) for (\d+)\)/)[1..2] # if count < required # We can't add sample args if too many were supplied # return (count.to_i+1..required.to_i).to_a.map{|o| "arg#{o}"}.join('/')+"/" # end # end if exception.is_a? RuntimeError # If it was in the format of tree output, just show it # Some messages start with "path:line:...: " at beginning again .sub!(/.+?: /, '') if =~ /in `/ return if =~ /\A[>|+-] / # /^- / return View.prompt($1) if( =~ /\A\.prompt (.+)/) return View.flash($1) if( =~ /\A\.flash (.+)/) end backtrace = exception.backtrace[0..8].join("\n").gsub(/^/, ' ') + "\n" # If path in message, move it to the stack trace if =~ /(.+\d:in `.+'): (.+)/m path, = $1, $2 backtrace = " #{path}\n#{backtrace}" end = =~ /\n/ ? "\n"+.strip.gsub(/^/, " | ").gsub(/^( +\|) ([+-])/, "\\1\\2") : " #{}" txt = "" txt << "- tried to run: #{code}\n" if code txt << "- error:#{}\n- backtrace:\n#{backtrace}" txt end |
.escape(l) ⇒ Object
Always escape backslashes, single-quotes, #‘s
349 350 351 352 353 354 |
# File 'lib/xiki/code_tree.rb', line 349 def self.escape l l = l.gsub("\\", "\\\\\\\\") l.gsub!("\"", "\\\\\"") l.gsub!("#", "\\\\#") l end |
.extract_class(l) ⇒ Object
323 324 325 |
# File 'lib/xiki/code_tree.rb', line 323 def self.extract_class l l[/^([A-Z][A-Za-z0-9]*)\.[a-z_]/, 1] end |
.extract_method(l) ⇒ Object
327 328 329 330 331 332 333 |
# File 'lib/xiki/code_tree.rb', line 327 def self.extract_method l l = l.sub(/^[+-] [\w -]+?: /, '').sub(/^[+-] /, '') # Remove bullets # Either class or bol result = l[/^[A-Z][A-Za-z0-9]*\.([a-z_].*)/, 1] || # Class and method l[/^\.([a-z].*)/, 1] # Method at beginning of line result ? result.sub(/\/$/, '') : nil end |
.handles?(list = nil) ⇒ Boolean
Determine whether we should handle it
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/xiki/code_tree.rb', line 206 def self.handles? list=nil list ||= Tree.construct_path(:list => true) # Use current line by default code_tree_root = nil index = list.size - 1 # If @..., don't bother with rest of path list = [list[-1]] if list[-1][/^ *([+-] )?@/] list.reverse.each do |l| # If it has a char that wouldn't be in a file, must be code tree #return index if l =~ /[\[\{=]/ # If last one was suspected as root, confirm we're not a dir (must be a file if parent is a dir) if code_tree_root if l =~ /\/$/ # Dir means it was a file that looked like code # This means never interpret classes indented under foo/ code_tree_root = nil else return index + 1 # Must be legit, so return our index end end # If function call, it might be the root if l =~ /^[+-]? ?@?[A-Z][A-Za-z0-9]*\.[a-z_]/ code_tree_root = index end index -= 1 end code_tree_root end |
.kill_rest ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/xiki/code_tree.rb', line 416 def self.kill_rest prefix = Keys.prefix(:clear=>true) column = View.column right1 = Line.left left2 = Line.left 2 Line.next if Search.backward("^$", :go_anyway=>true) # Go to beginning of paragraph left1 = Line.left Search.forward("^$", :go_anyway=>true) # Go to end of paragraph right2 = Line.left if prefix.is_a?(Fixnum) View.cursor = left2 right2 = Line.left(prefix + 1) end View.delete left2, right2 # Always delete after Line.previous # Optionally delete before View.delete(left1, right1) if prefix == :u end |
.kill_siblings(options = {}) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/xiki/code_tree.rb', line 357 def self.kill_siblings ={} prefix = Keys.prefix :clear=>true left1, right1, left2, right2 = Tree.sibling_bounds # If number, adjust if prefix.is_a?(Fixnum) left2 = Line.left prefix + 1 elsif prefix == :u right2 = left2 elsif prefix == :uu right1 = left1 end View.delete left2, right2 View.delete left1, right1 end |
.launch(options = {}) ⇒ Object
Mapped to C-.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/xiki/code_tree.rb', line 14 def self.launch ={} line = Line.without_indent return Search.open_file_and_method line if Keys.open? # If as+open, just navigato to it! FileTree.extra_line_if_end_of_file Tree.plus_to_minus_maybe orig = Location.new Line.to_left path = [:path] || Tree.construct_path(:list=>true) path.each do |l| # if '- .xx:/", get rid of trailing slash l.sub!(/^([+-] .*\..+)\/$/, "\\1") end # Determine code to eval code = self.determine_code_from_path path orig.go self.run code, end |
.layout_menu ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/xiki/code_tree.rb', line 194 def self. View. if Keys.prefix_u? buffer = "*CodeTree CodeTree menu" if View.buffer_open? buffer # If open, switch to it View.to_buffer(buffer) else # Else open it Launcher.open("- CodeTree.menu/") end end |
.maybe_code_tree_root(line) ⇒ Object
457 458 459 |
# File 'lib/xiki/code_tree.rb', line 457 def self.maybe_code_tree_root line line =~ /^[\s+-]*[A-Z].+\.[a-z_]/ end |
.menu ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/xiki/code_tree.rb', line 180 def self. l = [] ObjectSpace.each_object(Class) do |c| next unless c.respond_to?(:menu) l << c.to_s end l.map! {|c| c.sub(/^#.+::/, '')} (l + ).sort.each do |c| next if ["CodeTree"].member?(c) puts "+ #{c}.menu/" end "" end |
.menus ⇒ Object
6 |
# File 'lib/xiki/code_tree.rb', line 6 def self.; @menus; end |
.no_search_option ⇒ Object
449 450 451 |
# File 'lib/xiki/code_tree.rb', line 449 def self.no_search_option ":code_tree_option_no_search\n" end |
.paramify(l) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/xiki/code_tree.rb', line 335 def self.paramify l l = Line.without_label(:line=>l) l = self.escape(l) if l =~ /^\|/ # If |..., escape single-quotes # l.gsub!("'", "\\'") else l.gsub! ', ', '", "' end l end |
.quote_search_option ⇒ Object
445 446 447 |
# File 'lib/xiki/code_tree.rb', line 445 def self.quote_search_option ":quote_search_option\n" end |
.remove_slashes(*args) ⇒ Object
470 471 472 473 474 |
# File 'lib/xiki/code_tree.rb', line 470 def self.remove_slashes *args args.each do |arg| arg.sub! /\/$/, '' if arg end end |
.returned_to_s(returned) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/xiki/code_tree.rb', line 35 def self.returned_to_s returned if returned.is_a? String # Join and bulletize returned elsif returned.is_a? Array # Join and bulletize returned.map{|l| "#{l =~ /\/$/ ? '+' : '-'} #{l}\n"}.join('') elsif returned.is_a? Hash (returned.map{|k, v| v =~ /\/$/ ? "+ #{k}: #{v}" : "- #{k}: #{v}"}.join("\n")) + "\n" else returned.to_s.strip end end |
.run(code, options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/xiki/code_tree.rb', line 96 def self.run code, ={} b = View.buffer orig = Location.new orig_left = $el.point returned, stdout, e = Code.eval(code) # Eval code # If no stdout (and something was returned), print return value if ! stdout.nonempty? and ! returned.nil? stdout = self.returned_to_s returned else $el.(returned.to_s) if returned and (!returned.is_a?(String) or returned.size < 500) end stdout = self.draw_exception(e, code) if e buffer_changed = b != View.buffer # Remember whether we left the buffer # Insert output if there was any if stdout.nonempty? # Pull out flags if stdout =~ /^:code_tree_option_tree_search\n/ [:tree_search] = true stdout.sub! /.+\n/, '' # Remove option elsif stdout =~ /^:quote_search_option\n/ [:quote_search] = true stdout.sub! /.+\n/, '' # Remove option elsif stdout =~ /^:code_tree_option_no_search\n/ [:no_search] = true stdout.sub! /.+\n/, '' # Remove option end ended_up = Location.new # Go back to where we were before running code orig.go indent = Line.indent Line.to_left Line.next left = $el.point # Move what they printed over to left margin initally, in case they haven't stdout = TextUtil.unindent(stdout) stdout.sub!(/\n\n\z/, "\n") # Remove any double linebreaks at end stdout.gsub!(/^/, "#{indent} ") View << stdout # Insert output right = $el.point orig.go # Move cursor back ended_up.go # End up where script took us # These are deprecated - eventually pull them out if [:tree_search] # If they want to do a tree search $el.goto_char left FileTree.select_next_file Tree.search(:left=>left, :right=>right, :recursive=>true) return end if [:quote_search] # If they want to do a tree search $el.goto_char left Search.forward "|" Line.to_beginning Tree.search(:left=>left, :right=>right, :recursive_quotes=>true) return end # If script didn't move us (line or buffer), do incremental search if ![:no_search] && !buffer_changed && $el.point == orig_left # TODO No search if there aren't more than 3 lines $el.goto_char left Line.to_words # Determine how to search based on output! Tree.search_appropriately left, right, stdout elsif [:no_search] Line.to_beginning :down=>1 end end end |
.tree_search_option ⇒ Object
441 442 443 |
# File 'lib/xiki/code_tree.rb', line 441 def self.tree_search_option ":code_tree_option_tree_search\n" end |