module SyntaxTree

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/syntax_tree.rbs

module SyntaxTree
  def self.parse: (String source) -> untyped
end

def self.format(

Parses the given source and returns the formatted source.
def self.format(
  source,
  maxwidth = DEFAULT_PRINT_WIDTH,
  base_indentation = DEFAULT_INDENTATION,
  options: Formatter::Options.new
)
  format_node(
    source,
    parse(source),
    maxwidth,
    base_indentation,
    options: options
  )
end

def self.format_file(

Parses the given file and returns the formatted source.
def self.format_file(
  filepath,
  maxwidth = DEFAULT_PRINT_WIDTH,
  base_indentation = DEFAULT_INDENTATION,
  options: Formatter::Options.new
)
  format(read(filepath), maxwidth, base_indentation, options: options)
end

def self.format_node(

Accepts a node in the tree and returns the formatted source.
def self.format_node(
  source,
  node,
  maxwidth = DEFAULT_PRINT_WIDTH,
  base_indentation = DEFAULT_INDENTATION,
  options: Formatter::Options.new
)
  formatter = Formatter.new(source, [], maxwidth, options: options)
  node.format(formatter)
  formatter.flush(base_indentation)
  formatter.output.join
end

def self.index(source)

documentation generation.
method definitions. Used to quickly provide indexing capability for IDEs or
Indexes the given source code to return a list of all class, module, and
def self.index(source)
  Index.index(source)
end

def self.index_file(filepath)

documentation generation.
definitions. Used to quickly provide indexing capability for IDEs or
Indexes the given file to return a list of all class, module, and method
def self.index_file(filepath)
  Index.index_file(filepath)
end

def self.mutation

A convenience method for creating a new mutation visitor.
def self.mutation
  visitor = MutationVisitor.new
  yield visitor
  visitor
end

def self.parse(source)

Experimental RBS support (using type sampling data from the type_fusion project).

def self.parse: (String source) -> untyped

This signature was generated using 1 sample from 1 application.

Parses the given source and returns the syntax tree.
def self.parse(source)
  parser = Parser.new(source)
  response = parser.parse
  response unless parser.error?
end

def self.parse_file(filepath)

Parses the given file and returns the syntax tree.
def self.parse_file(filepath)
  parse(read(filepath))
end

def self.read(filepath)

magic encoding comments.
Returns the source from the given filepath taking into account any potential
def self.read(filepath)
  encoding =
    File.open(filepath, "r") do |file|
      break Encoding.default_external if file.eof?
      header = file.readline
      header += file.readline if !file.eof? && header.start_with?("#!")
      Ripper.new(header).tap(&:parse).encoding
    end
  File.read(filepath, encoding: encoding)
end

def self.register_handler(extension, handler)

handler for a particular file type.
This is a hook provided so that plugins can register themselves as the
def self.register_handler(extension, handler)
  HANDLERS[extension] = handler
end

def self.search(source, query, &block)

node in the tree that matches the pattern to the given block.
Searches through the given source using the given pattern and yields each
def self.search(source, query, &block)
  pattern = Pattern.new(query).compile
  program = parse(source)
  Search.new(pattern).scan(program, &block)
end

def self.search_file(filepath, query, &block)

node in the tree that matches the pattern to the given block.
Searches through the given file using the given pattern and yields each
def self.search_file(filepath, query, &block)
  search(read(filepath), query, &block)
end