Class: Textbringer::RubyMode

Inherits:
ProgrammingMode show all
Defined in:
lib/textbringer/modes/ruby_mode.rb

Defined Under Namespace

Classes: PartialLiteralAnalyzer

Constant Summary

Constants inherited from ProgrammingMode

ProgrammingMode::PROGRAMMING_MODE_MAP

Constants inherited from Mode

Mode::DEFAULT_SYNTAX_TABLE

Constants included from Commands

Commands::CLIPBOARD_AVAILABLE, Commands::CTAGS, Commands::HELP_RING, Commands::ISEARCH_MODE_MAP, Commands::ISEARCH_STATUS, Commands::KEYBOARD_MACROS, Commands::REGISTERS, Commands::RE_SEARCH_STATUS, Commands::UNIVERSAL_ARGUMENT_MAP

Constants included from Utils

Utils::COMPLETION, Utils::HOOKS, Utils::Y_OR_N_MAP

Instance Attribute Summary

Attributes inherited from Mode

#buffer

Instance Method Summary collapse

Methods inherited from ProgrammingMode

#indent_line, #indent_region, #reindent_then_newline_and_indent

Methods inherited from Mode

define_generic_command, define_local_command, define_syntax, inherited, list, #name, #syntax_table

Methods included from Commands

[], #command_help, command_table, #current_prefix_arg, define_command, #execute_keyboard_macro, #get_tags, #isearch_done, #isearch_mode, #isearch_pre_command_hook, #isearch_prompt, #isearch_repeat, #isearch_repeat_backward, #isearch_repeat_forward, #isearch_search, list, #match_beginning, #match_end, #match_string, #number_prefix_arg, #prefix_numeric_value, #read_keyboard_macro, #read_register, #replace_match, undefine_command, #universal_argument_mode

Methods included from Utils

add_hook, background, complete_for_minibuffer, message, next_tick, next_tick!, read_buffer, read_char, read_command_name, read_encoding, read_event, read_file_name, read_from_minibuffer, read_key_sequence, read_single_char, received_keyboard_quit?, remove_hook, ruby_install_name, run_hooks, self_insert_and_exit_minibuffer, set_transient_map, show_exception, sit_for, sleep_for, y_or_n?, yes_or_no?

Constructor Details

#initialize(buffer) ⇒ RubyMode

Returns a new instance of RubyMode.



90
91
92
93
94
# File 'lib/textbringer/modes/ruby_mode.rb', line 90

def initialize(buffer)
  super(buffer)
  @buffer[:indent_level] = CONFIG[:ruby_indent_level]
  @buffer[:indent_tabs_mode] = CONFIG[:ruby_indent_tabs_mode]
end

Instance Method Details

#backward_definition(n = number_prefix_arg || 1) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/textbringer/modes/ruby_mode.rb', line 117

def backward_definition(n = number_prefix_arg || 1)
  tokens = Ripper.lex(@buffer.to_s).reverse
  @buffer.beginning_of_line
  n.times do |i|
    tokens = tokens.drop_while { |(l, _), e, t|
      l >= @buffer.current_line ||
        e != :on_kw || /\A(?:class|module|def)\z/ !~ t
    }
    (line,), = tokens.first
    if line.nil?
      @buffer.beginning_of_buffer
      break
    end
    @buffer.goto_line(line)
    tokens = tokens.drop(1)
  end
  while /\s/ =~ @buffer.char_after
    @buffer.forward_char
  end
end

#compile(cmd = read_from_minibuffer("Compile: ", default: default_compile_command)) ⇒ Object



138
139
140
141
142
# File 'lib/textbringer/modes/ruby_mode.rb', line 138

def compile(cmd = read_from_minibuffer("Compile: ",
                                       default: default_compile_command))
  shell_execute(cmd, buffer_name: "*Ruby compile result*",
                mode: BacktraceMode)
end

#default_compile_commandObject



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/textbringer/modes/ruby_mode.rb', line 148

def default_compile_command
  @buffer[:ruby_compile_command] ||
    if File.exist?("Rakefile")
      prefix = File.exist?("Gemfile") ? "bundle exec " : ""
      prefix + "rake"
    elsif @buffer.file_name
      ruby_install_name + " " + @buffer.file_name
    else
      nil
    end
end

#forward_definition(n = number_prefix_arg || 1) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/textbringer/modes/ruby_mode.rb', line 96

def forward_definition(n = number_prefix_arg || 1)
  tokens = Ripper.lex(@buffer.to_s)
  @buffer.forward_line
  n.times do |i|
    tokens = tokens.drop_while { |(l, _), e, t|
      l < @buffer.current_line ||
        e != :on_kw || /\A(?:class|module|def)\z/ !~ t
    }
    (line,), = tokens.first
    if line.nil?
      @buffer.end_of_buffer
      break
    end
    @buffer.goto_line(line)
    tokens = tokens.drop(1)
  end
  while /\s/ =~ @buffer.char_after
    @buffer.forward_char
  end
end

#symbol_patternObject



144
145
146
# File 'lib/textbringer/modes/ruby_mode.rb', line 144

def symbol_pattern
  /[\p{Letter}\p{Number}_$@!?]/
end

#toggle_testObject



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
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/textbringer/modes/ruby_mode.rb', line 160

def toggle_test
  case @buffer.file_name
  when %r'(.*)/test/(.*/)?test_(.*?)\.rb\z'
    base = $1
    namespace = $2
    name = $3
    if namespace
      paths = Dir.glob("#{base}/{lib,app}/**/#{namespace}#{name}.rb")
      if !paths.empty?
        find_file(paths.first)
        return
      end
    end
    paths = Dir.glob("#{base}/{lib,app}/**/#{name}.rb")
    if !paths.empty?
      find_file(paths.first)
      return
    end
    raise EditorError, "Test subject not found"
  when %r'(.*)/(?:lib|app)/(.*/)?(.*?)\.rb\z'
    base = $1
    namespace = $2
    name = $3
    if namespace
      paths = Dir.glob("#{base}/test/**/#{namespace}test_#{name}.rb")
      if !paths.empty?
        find_file(paths.first)
        return
      end
    end
    paths = Dir.glob("#{base}/test/**/test_#{name}.rb")
    if !paths.empty?
      find_file(paths.first)
      return
    end
    raise EditorError, "Test not found"
  else
    raise EditorError, "Unknown file type"
  end
end