Class: RubyLsp::Requests::ShowSyntaxTree

Inherits:
BaseRequest
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/requests/show_syntax_tree.rb

Overview

Show syntax tree demo

Show syntax tree is a custom LSP request that displays the AST for the current document or for the current selection in a new tab.

Example

# Executing the Ruby LSP: Show syntax tree command will display the AST for the document
1 + 1
# (program (statements ((binary (int "1") + (int "1")))))

Instance Method Summary collapse

Methods included from RubyLsp::Requests::Support::Common

#create_code_lens, #defined_in_gem?, #markdown_from_index_entries, #range_from_location, #range_from_node, #self_receiver?, #visible?

Constructor Details

#initialize(document, range) ⇒ ShowSyntaxTree

Returns a new instance of ShowSyntaxTree.



24
25
26
27
28
# File 'lib/ruby_lsp/requests/show_syntax_tree.rb', line 24

def initialize(document, range)
  super(document)

  @range = range
end

Instance Method Details

#run ⇒ Object



31
32
33
34
35
36
37
# File 'lib/ruby_lsp/requests/show_syntax_tree.rb', line 31

def run
  return ast_for_range if @range

  output_string = +""
  PP.pp(@document.tree, output_string)
  output_string
end