Class: Textbringer::RubyMode

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

Constant Summary

Constants inherited from ProgrammingMode

ProgrammingMode::PROGRAMMING_MODE_MAP

Constants included from Commands

Commands::CLIPBOARD_AVAILABLE, Commands::CTAGS, Commands::ISEARCH_MODE_MAP, Commands::ISEARCH_STATUS, 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, #newline_and_reindent

Methods inherited from Mode

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

Methods included from Commands

#current_prefix_arg, define_command, #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, #replace_match, undefine_command, #universal_argument_mode

Methods included from Utils

add_hook, complete_for_minibuffer, message, read_buffer, read_char, read_command_name, read_file_name, read_from_minibuffer, 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.



74
75
76
77
78
# File 'lib/textbringer/modes/ruby_mode.rb', line 74

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/textbringer/modes/ruby_mode.rb', line 101

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



122
123
124
125
126
# File 'lib/textbringer/modes/ruby_mode.rb', line 122

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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/textbringer/modes/ruby_mode.rb', line 132

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 " + @buffer.file_name
    else
      nil
    end
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/textbringer/modes/ruby_mode.rb', line 80

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



128
129
130
# File 'lib/textbringer/modes/ruby_mode.rb', line 128

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

#toggle_testObject



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
179
180
181
182
183
# File 'lib/textbringer/modes/ruby_mode.rb', line 144

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