Class: Textbringer::CMode

Inherits:
ProgrammingMode show all
Defined in:
lib/textbringer/modes/c_mode.rb,
sig/lib/textbringer/mode.rbs,
sig/lib/textbringer/modes/c_mode.rbs

Constant Summary collapse

KEYWORDS =
%w(
  auto break case char const continue default double do
  else enum extern float for goto if inline int long
  register restrict return short signed sizeof static struct
  switch typedef union unsigned void volatile while _Bool
  _Complex _Imaginary
)
TOKEN_NAMES =
[
  :preprocessing_directive,
  :comment,
  :keyword,
  :identifier,
  :constant,
  :string_literal,
  :punctuator,
  :space,
  :partial_comment,
  :unknown
]
TOKEN_REGEXP =
/\G(?:
(?<preprocessing_directive>
  ^[ \t\f\v]*(?:\#|%:).*(?:\\\n.*)*[^\\]\n
) |
(?<comment>
  (?<multiline_comment> \/\* (?> (?:.|\n)*? \*\/ ) ) |
  (?<singleline_comment> \/\/ .*(?:\\\n.*)*(?<!\\)\n )
) |
(?<partial_comment>
  (?<partial_multiline_comment> \/\* (?:.|\n)* ) |
  (?<partial_singleline_comment> \/\/ .*? \\\n (?:.|\n)* )
) |
(?<keyword>
  (?: #{KEYWORDS.join("|")} ) \b
) |
(?<constant>
  (?<floating_constant>
(?<decimal_floating_constant>
  (?<fractional_constant>
    (?<digit_sequence> [0-9]+ )? \. \g<digit_sequence> |
    \g<digit_sequence> \. )
      (?<exponent_part> [eE] [+\-]? \g<digit_sequence> )?
      (?<floating_suffix> [flFL] )?
) |
(?<hexadecimal_floating_constant>
  (?<hexadecimal_prefix> 0x | 0X )
      (?<hexadecimal_fractional_constant>
        (?<hexadecimal_digit_sequence> [0-9a-fA-F]+ )? \.
            \g<hexadecimal_digit_sequence> |
        \g<hexadecimal_digit_sequence> \. )
      (?<binary_exponent_part> [pP] [+\-]? \g<digit_sequence> )
      \g<floating_suffix>? |
  \g<hexadecimal_prefix> \g<hexadecimal_digit_sequence>
      \g<binary_exponent_part> \g<floating_suffix>?
)
  ) |
  (?<integer_constant>
(?<decimal_constant> [1-9][0-9]* )
(?<integer_suffix>
  (?<unsigned_suffix> [uU] ) (?<long_suffix> [lL] )?
  \g<unsigned_suffix> (?<long_long_suffix> ll | LL )?
  \g<long_suffix> \g<unsigned_suffix>?
  \g<long_long_suffix> \g<unsigned_suffix>?
)? |
(?<hexadecimal_constant>
  \g<hexadecimal_prefix> \g<hexadecimal_digit_sequence> )
      \g<integer_suffix>? |
(?<octal_constant> 0 (?<octal_digit> [0-7] )* )
    \g<integer_suffix>?
  ) |
  (?<character_constant>
' (?<c_char_sequence>
    (?<c_char>
      [^'\\\r\n] |
      (?<escape_sequence>
        (?<simple_escape_sequence> \\ ['"?\\abfnrtv] ) |
        (?<octal_escape_sequence> \\ \g<octal_digit>{1,3} ) |
        (?<hexadecimal_escape_sequence>
          \\x \g<hexadecimal_digit_sequence> ) |
        (?<universal_character_name>
          \\u[0-9a-fA-F]{4} |
          \\U[0-9a-fA-F]{8} )
      )
    )+
  ) ' |
L' \g<c_char_sequence> '
  )
) |
(?<string_literal>
  " (?<s_char_sequence>
  (?<s_char> [^"\\\r\n] | \g<escape_sequence> )+ ) " |
  L" \g<s_char_sequence>? "
) |
(?<identifier>
  (?<identifier_nondigit>
[_a-zA-Z] |
\g<universal_character_name> )
    (?: \g<identifier_nondigit> | [0-9] )*
) |
(?<punctuator>
  \[   |   \]   |   \(   |   \)   |   \{   |   \}   |
  \.\.\.   |   \.   |
  \+\+   |   \+=   |   \+   |
  ->   |   --   |   -=   |   -   |
  \*=   |   \*   |
  \/=   |   \/   |
  &&   |   &=   |   &   |
  \|\|   |   \|=   |   \|   |
  !=   |   !   |
  ~   |
  ==   |   =   |
  \^=   |   \^   |
  <:   |   <%   |   <<=   |   <<   |   <=   |   <   |
  >>=   |   >>   |   >=   |   >   |
  \?   |   ;   |
  :>   |   :   |
  ,   |
  \#\#   |   \#   |
  %>   |   %:%:   |   %:   |   %=   |   %
) |
(?<space>
  \s+
) |
(?<unknown>.)
)/x

Constants inherited from Mode

Mode::DEFAULT_SYNTAX_TABLE

Constants included from Commands

Textbringer::Commands::CLIPBOARD_AVAILABLE, Textbringer::Commands::COMPLETION_POPUP_STATUS, Textbringer::Commands::CTAGS, Textbringer::Commands::EMAIL_REGEXP, Textbringer::Commands::HELP_RING, Textbringer::Commands::ISEARCH_STATUS, Textbringer::Commands::ISPELL_STATUS, Textbringer::Commands::ISPELL_WORD_REGEXP, Textbringer::Commands::KEYBOARD_MACROS, Textbringer::Commands::LSP_DOCUMENT_VERSIONS, Textbringer::Commands::LSP_STATUS, Textbringer::Commands::REGISTERS, Textbringer::Commands::RE_SEARCH_STATUS, Textbringer::Commands::URI_REGEXP

Constants included from Utils

Utils::COMPLETION, Utils::EXPRESSION_COMPLETOR, Utils::EXPRESSION_COMPLETOR_OPTIONS, Utils::HOOKS

Instance Attribute Summary

Attributes inherited from Mode

#buffer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ProgrammingMode

define_generic_command, #indent_line, #indent_new_comment_line, #indent_region, inherited, #reindent_then_newline_and_indent

Methods inherited from FundamentalMode

inherited

Methods inherited from Mode

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

Methods included from Commands

[], #buffer_uri, #command_help, command_table, #completion_popup_done, completion_popup_mode_active?, #completion_popup_pre_command_hook, #completion_popup_start, #current_prefix_arg, define_command, #ensure_ispell_active, #execute_keyboard_macro, #get_tags, #insert_completion, #isearch_done, #isearch_mode, #isearch_mode?, #isearch_pre_command_hook, #isearch_prompt, #isearch_repeat, #isearch_repeat_backward, #isearch_repeat_forward, #isearch_search, #ispell_done, #ispell_forward, #ispell_mode, #keymap_bindings, list, #lsp_after_set_visited_file_name_hook, #lsp_close_signature_window, #lsp_completion_context, #lsp_find_file_hook, #lsp_open_document, #lsp_position, #lsp_setup_buffer_hooks, #lsp_show_signature_window, #lsp_signature_pre_command_hook, #lsp_text_document_sync_kind, #lsp_utf16_length, #match_beginning, #match_end, #match_string, #message_misspelled, #number_prefix_arg, #prefix_numeric_value, #read_input_method_name, #read_keyboard_macro, #read_register, #read_theme_name, #replace_match, undefine_command, #universal_argument_mode

Methods included from Utils

add_hook, background, complete_for_minibuffer, delete_completions_window, foreground, foreground!, get_hooks, message, read_buffer, read_char, read_command_name, read_encoding, read_event, read_expression, read_file_name, read_from_minibuffer, read_key_sequence, read_object, read_single_char, received_keyboard_quit?, remove_hook, ruby_install_name, run_hooks, run_hooks_in, self_insert_and_exit_minibuffer, set_transient_map, show_exception, sit_for, sleep_for, with_clean_env, y_or_n?, yes_or_no?

Constructor Details

#initialize(buffer) ⇒ CMode

Returns a new instance of CMode.

Parameters:



41
42
43
44
45
# File 'lib/textbringer/modes/c_mode.rb', line 41

def initialize(buffer)
  super(buffer)
  @buffer[:indent_level] = CONFIG[:c_indent_level]
  @buffer[:indent_tabs_mode] = CONFIG[:c_indent_tabs_mode]
end

Class Method Details

.define_syntaxRegexp

Parameters:

  • (Symbol)
  • (Regexp)

Returns:

  • (Regexp)


28
# File 'sig/lib/textbringer/mode.rbs', line 28

def self.define_syntax: (Symbol, Regexp) -> Regexp

Instance Method Details

#calculate_indentationInteger #calculate_indentationnil

Overloads:

  • #calculate_indentationInteger

    Returns:

    • (Integer)
  • #calculate_indentationnil

    Returns:

    • (nil)


204
205
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/textbringer/modes/c_mode.rb', line 204

def calculate_indentation
  if @buffer.current_line == 1
    return 0
  end
  @buffer.save_excursion do
    @buffer.beginning_of_line
    if @buffer.looking_at?(/[ \t]*(?:#|%:)/)
      return 0
    end
    bol_pos = @buffer.point
    s = @buffer.substring(@buffer.point_min, @buffer.point).b
    tokens = lex(s)
    _, event, = tokens.last
    if event == :partial_comment
      return nil
    end
    line, column, event, text = find_nearest_beginning_token(tokens)
    if event == :punctuator && text == "("
      return column + 1
    end
    if line
      @buffer.goto_line(line)
    else
      (ln, _), ev = tokens.reverse_each.drop_while { |(l, _), e, t|
        l >= @buffer.current_line - 1 || e == :space
      }.first
      if ev == :comment
        @buffer.goto_line(ln)
      else
        @buffer.backward_line
      end
    end
    @buffer.looking_at?(/[ \t]*/)
    base_indentation = @buffer.match_string(0).
      gsub(/\t/, " " * @buffer[:tab_width]).size
    @buffer.goto_char(bol_pos)
    if line.nil? ||
        @buffer.looking_at?(/[ \t]*([}\])]|:>|%>)/)
      indentation = base_indentation
    else
      indentation = base_indentation + @buffer[:indent_level]
    end
    if @buffer.looking_at?(/[ \t]*(?:case.*|default):/)
      indentation += @buffer[:c_case_label_offset]
    elsif @buffer.looking_at?(/[ \t]*[_a-zA-Z0-9\\]+:/)
      indentation += @buffer[:c_label_offset]
    end
    indent_continued_statement(indentation, tokens, line)
  end
end

#comment_startObject



37
38
39
# File 'lib/textbringer/modes/c_mode.rb', line 37

def comment_start
  "//"
end

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

Parameters:

  • (String)

Returns:

  • (nil)


47
48
49
50
51
# File 'lib/textbringer/modes/c_mode.rb', line 47

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

#default_compile_commandString

Returns:

  • (String)


57
58
59
# File 'lib/textbringer/modes/c_mode.rb', line 57

def default_compile_command
  "make"
end

#find_nearest_beginning_token(arg0) ⇒ Array[untyped] #find_nearest_beginning_token(arg0) ⇒ nil

Overloads:

  • #find_nearest_beginning_token(arg0) ⇒ Array[untyped]

    Parameters:

    • arg0 (Array[untyped])

    Returns:

    • (Array[untyped])
  • #find_nearest_beginning_token(arg0) ⇒ nil

    Parameters:

    • arg0 (Array[untyped])

    Returns:

    • (nil)


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/textbringer/modes/c_mode.rb', line 284

def find_nearest_beginning_token(tokens)
  stack = []
  (tokens.size - 1).downto(0) do |i|
    (line, column), event, raw_text = tokens[i]
    text = CANONICAL_PUNCTUATORS[raw_text]
    case event
    when :punctuator
      if BLOCK_BEG.key?(text)
        stack.push(text)
      elsif BLOCK_END.key?(text)
        if stack.empty?
          return line, column, event, text
        end
        if stack.last != BLOCK_END[text]
          raise EditorError, "#{@buffer.name}:#{line}: Unmatched #{text}"
        end
        stack.pop
      end
    end
  end
  return nil
end

#indent_continued_statement(arg0, arg1, arg2) ⇒ Integer #indent_continued_statement(arg0, arg1, arg2) ⇒ Integer

Overloads:

  • #indent_continued_statement(arg0, arg1, arg2) ⇒ Integer

    Parameters:

    • arg0 (Integer)
    • arg1 (Array[untyped])
    • arg2 (Integer)

    Returns:

    • (Integer)
  • #indent_continued_statement(arg0, arg1, arg2) ⇒ Integer

    Parameters:

    • arg0 (Integer)
    • arg1 (Array[untyped])
    • arg2 (nil)

    Returns:

    • (Integer)


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/textbringer/modes/c_mode.rb', line 255

def indent_continued_statement(indentation, tokens ,line)
  if line && !@buffer.looking_at?(/[ \t]*\{/)
    _, last_event, last_text =
      tokens.reverse_each.drop_while { |(l, _), e, t|
        l == @buffer.current_line || e == :space || e == :comment
      }.first
    if last_event != :preprocessing_directive &&
        /[:;{}]/ !~ last_text
      indentation + @buffer[:c_continued_statement_offset]
    else
      indentation
    end
  else
    indentation
  end
end

#lex(arg0) ⇒ Array[untyped] #lex(arg0) ⇒ nil

Overloads:

  • #lex(arg0) ⇒ Array[untyped]

    Parameters:

    • arg0 (String)

    Returns:

    • (Array[untyped])
  • #lex(arg0) ⇒ nil

    Parameters:

    • arg0 (String)

    Returns:

    • (nil)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/textbringer/modes/c_mode.rb', line 180

def lex(s)
  tokens = []
  line = 1
  column = 0
  s.scan(TOKEN_REGEXP) do
    text = $&
    token_name = TOKEN_NAMES.find { |name| $~[name] }
    if text.empty?
      raise EditorError, "Empty token: (#{line},#{column}) #{$~.inspect}"
    end
    tokens.push([[line, column], token_name, CANONICAL_PUNCTUATORS[text]])
    lf_count = text.count("\n")
    if lf_count > 0
      line += lf_count
      column = text.slice(/[^\n]*\z/).size
    else
      column += text.size
    end
  end
  tokens
end

#symbol_patternRegexp

Returns:

  • (Regexp)


53
54
55
# File 'lib/textbringer/modes/c_mode.rb', line 53

def symbol_pattern
  /[A-Za-z0-9\_\\]/
end