Module: VER::Methods::SmartAutoindent
- Defined in:
- config/plugin/smart_autoindent.rb
Class Method Summary collapse
- .indent_settings(text) ⇒ Object
- .indented_newline(text, record = text) ⇒ Object
- .newline(text) ⇒ Object
- .newline_above(text) ⇒ Object
- .newline_below(text) ⇒ Object
Class Method Details
.indent_settings(text) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'config/plugin/smart_autoindent.rb', line 117 def self.indent_settings(text) return {} unless text.load_preferences indent_settings = {} text.preferences.each do |pref| settings = pref[:settings] indent_settings[:increase] ||= settings[:increaseIndentPattern] indent_settings[:decrease] ||= settings[:decreaseIndentPattern] indent_settings[:indent_next] ||= settings[:indentNextLinePattern] indent_settings[:unindented] ||= settings[:unIndentedLinePattern] end [:increase, :decrease, :indent_next, :unindented].each do |key| if value = indent_settings[key] indent_settings[key] = Regexp.new(value) else indent_settings.delete(key) end end return indent_settings end |
.indented_newline(text, record = text) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'config/plugin/smart_autoindent.rb', line 36 def self.indented_newline(text, record = text) settings = indent_settings(text) increase, decrease = settings.values_at(:increase, :decrease) unless increase || decrease record.insert :insert, "\n" return end ref_line = text.get( "insert - 1 lines linestart", "insert - 1 lines lineend" ) ref = ref_line[/^\s*/].size / 2 ref_inc = increase ? ref_line.scan(increase).size : 0 ref_dec = decrease ? ref_line.scan(decrease).size : 0 cur_line = text.get( "insert linestart", "insert lineend" ) cur_inc = increase ? cur_line.scan(increase).size : 0 cur_dec = decrease ? cur_line.scan(decrease).size : 0 # we strip whitespace from previous empty lines, but it's still added to # the current one, so we use it as reference. if ref_line.empty? ref = cur_line[/^\s*/].size / 2 end pattern = [ if ref_inc != 0 && ref_dec != 0; '=' elsif ref_inc != 0; '+' elsif ref_dec != 0; '-' else; '?' end, if cur_inc != 0 && cur_dec != 0; '=' elsif cur_inc != 0; '+' elsif cur_dec != 0; '-' else; '?' end ].join cur_indent, next_indent = case pattern when '++'; [ref + 1, ref + 2] when '+-'; [ref, ref] when '+='; [ref, ref] when '+?'; [ref + 1, ref + 1] when '-+'; [ref, ref] when '--'; [ref - 1, ref - 2] when '-='; [ref, ref] when '-?'; [ref, ref] when '=+'; [ref + 1, ref + 1] when '=-'; [ref, ref] when '=='; [ref, ref] when '=?'; [ref + 1, ref + 1] when '?+'; [ref, ref + 1] when '?-'; [ref - 1, ref - 2] when '?='; [ref - 1, ref] when '??'; [ref, ref] end if text.index('insert - 1 line') == text.index('insert') # first line, replace is futile next_indent -= 1 elsif cur_line =~ /\S/ cur_indent = [0, cur_indent].max record.replace( 'insert linestart', 'insert lineend', (' ' * (cur_indent * 2)) << cur_line.lstrip ) else # that was an empty line record.replace('insert linestart', 'insert lineend', '') end next_indent = [0, next_indent].max record.insert('insert lineend', "\n#{' ' * (next_indent * 2)}") end |
.newline(text) ⇒ Object
4 5 6 7 8 9 10 |
# File 'config/plugin/smart_autoindent.rb', line 4 def self.newline(text) if text..autoindent indented_newline(text) else Insert.newline(text) end end |
.newline_above(text) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'config/plugin/smart_autoindent.rb', line 22 def self.newline_above(text) Undo.record text do |record| if text.index(:insert).y > 1 Move.prev_line(text) newline_below(text) else record.insert('insert linestart', "\n") text.mark_set(:insert, 'insert - 1 line') Control.clean_line(text, 'insert - 1 line', record) text.minor_mode(:control, :insert) end end end |
.newline_below(text) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'config/plugin/smart_autoindent.rb', line 12 def self.newline_below(text) if text..autoindent text.mark_set('insert', 'insert lineend') newline(text) text.minor_mode(:control, :insert) else Insert.newline_below(text) end end |