Class: RText::Frontend::Context
- Inherits:
-
Object
- Object
- RText::Frontend::Context
- Defined in:
- lib/rtext/frontend/context.rb
Instance Method Summary collapse
-
#extract(lines, pos) ⇒ Object
lines: all lines from the beginning up to and including the current line pos: position of the cursor in the last lines returns the extracted lines and the new position in the last line.
- #filter_lines(lines) ⇒ Object
-
#join_lines(lines, pos) ⇒ Object
when joining two lines, all whitespace after the last character of the first line is removed (after , and [); however whitespace at the end of the last of several joined lines is preserved; this way the context is correct even if the cursor is after the end of the last line (i.e. with whitespace after the last non-whitespace character).
Instance Method Details
#extract(lines, pos) ⇒ Object
lines: all lines from the beginning up to and including the current line pos: position of the cursor in the last lines returns the extracted lines and the new position in the last line
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rtext/frontend/context.rb', line 9 def extract(lines, pos) lines = filter_lines(lines) lines, new_pos = join_lines(lines, pos) non_ignored_lines = 0 array_nesting = 0 block_nesting = 0 last_element_line = 0 result = [] lines.reverse.each_with_index do |l, i| if i == 0 result.unshift(l) else non_ignored_lines += 1 case l.strip[-1..-1] when "{" if block_nesting > 0 block_nesting -= 1 elsif block_nesting == 0 result.unshift(l) last_element_line = non_ignored_lines end when "}" block_nesting += 1 when "[" if array_nesting > 0 array_nesting -= 1 elsif array_nesting == 0 result.unshift(l) end when "]" array_nesting += 1 when ":" # lable directly above element if non_ignored_lines == last_element_line + 1 result.unshift(l) end end end end [result, new_pos] end |
#filter_lines(lines) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/rtext/frontend/context.rb', line 51 def filter_lines(lines) lines.reject { |l| ls = l.strip ls[0..0] == "@" || ls[0..0] == "#" } end |
#join_lines(lines, pos) ⇒ Object
when joining two lines, all whitespace after the last character of the first line is removed (after , and [); however whitespace at the end of the last of several joined lines is preserved; this way the context is correct even if the cursor is after the end of the last line (i.e. with whitespace after the last non-whitespace character)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rtext/frontend/context.rb', line 62 def join_lines(lines, pos) outlines = [] while lines.size > 0 outlines << lines.shift while lines.size > 0 && (outlines.last =~ /,\s*$/ || (outlines.last =~ /\[\s*$/ && outlines.last =~ /,/) || (lines.first =~ /^\s*\]/ && outlines.last =~ /,/)) outlines.last.rstrip! l = lines.shift if lines.size == 0 # strip only left part, the prefix might have whitespace on the # right hand side which is relevant for the position non_ws_prefix = l[0..pos-1].lstrip pos = outlines.last.size + non_ws_prefix.size end outlines.last.concat(l.lstrip) end end [outlines, pos] end |