Class: RubyLsp::Requests::OnTypeFormatting
- Inherits:
-
BaseRequest
- Object
- SyntaxTree::Visitor
- BaseRequest
- RubyLsp::Requests::OnTypeFormatting
- Extended by:
- T::Sig
- Defined in:
- lib/ruby_lsp/requests/on_type_formatting.rb
Overview

The [on type formatting](microsoft.github.io/language-server-protocol/specification#textDocument_onTypeFormatting) request formats code as the user is typing. For example, automatically adding ‘end` to class definitions.
# Example
“‘ruby class Foo # <– upon adding a line break, on type formatting is triggered
# <-- cursor ends up here
end # <– end is automatically added “‘
Constant Summary collapse
- END_REGEXES =
T.let( [ /(if|unless|for|while|class|module|until|def|case).*/, /.*\sdo/, ], T::Array[Regexp], )
Instance Method Summary collapse
-
#initialize(document, position, trigger_character) ⇒ OnTypeFormatting
constructor
A new instance of OnTypeFormatting.
- #run ⇒ Object
Methods inherited from BaseRequest
Methods included from Support::Common
#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?
Constructor Details
#initialize(document, position, trigger_character) ⇒ OnTypeFormatting
Returns a new instance of OnTypeFormatting.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby_lsp/requests/on_type_formatting.rb', line 30 def initialize(document, position, trigger_character) super(document) scanner = document.create_scanner line_begin = position[:line] == 0 ? 0 : scanner.find_char_position({ line: position[:line] - 1, character: 0 }) @line_end = T.let(scanner.find_char_position(position), Integer) line = T.must(@document.source[line_begin..@line_end]) @indentation = T.let(find_indentation(line), Integer) @previous_line = T.let(line.strip.chomp, String) @position = position @edits = T.let([], T::Array[Interface::TextEdit]) @trigger_character = trigger_character end |
Instance Method Details
#run ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ruby_lsp/requests/on_type_formatting.rb', line 46 def run case @trigger_character when "{" handle_curly_brace if @document.syntax_error? when "|" handle_pipe if @document.syntax_error? when "\n" if (comment_match = @previous_line.match(/^#(\s*)/)) handle_comment_line(T.must(comment_match[1])) elsif @document.syntax_error? handle_statement_end end end @edits end |