RubyMine:列出文件中调用的所有方法?

huangapple go评论57阅读模式
英文:

RubyMine: List all methods that are called in a file?

问题

我想获取调用给定文件中的所有方法的列表。澄清一下,我指的不是查找特定方法的所有用法这种更常见的任务。相反,我想收集文件内通过名称显式调用的所有方法的所有唯一名称。我知道RubyMine可以检测文件代码中的实体是否是方法调用,那么RubyMine有办法给我提供文件中调用的所有方法列表吗?

英文:

I want to get a list of all methods that are called in a given file. For clarification, I am not referring to the more common task of finding all usages for a specific method. Instead, I want to collect all unique names of any method that is explicitly called by name within a file. I know RubyMine can detect whether an entity in a file's code is a method call, so is there any way for RubyMine to give me a list of all methods called in a file?

答案1

得分: 1

以下是您要翻译的内容:

I am trying to find method calls that are explicitly written into the code text of a given file, so nothing having to do with runtime

Let's say we have this example file: (the code doesn't make any sense but its syntax is valid)

# test.rb           # 1
class MyClass       # 2
  def my_method     # 3
    foo             # 4
    self.bar        # 5
    baz(1)          # 6
    1 + 2           # 7
  end               # 8
end                 # 9

For static code analysis, you could use Ruby's AbstractSyntaxTree:

ast = RubyVM::AbstractSyntaxTree.parse_file('test.rb')
#=> #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-8:22>

The returned Node has a children method which can be used to traverse the syntax tree. The root node is of type SCOPE which represents a lexical scope.

The node types you are looking for are probably:

  • VCALL – a method call without receiver or arguments
  • CALL – a method call with explicit receiver
  • FCALL – a method call with arguments
  • OPCALL – method call using an operator

Here's a very simplistic parser:

def each_call(node, &block)
  return unless node.is_a?(RubyVM::AbstractSyntaxTree::Node)

  case node.type
  when :VCALL, :FCALL
    block.call(node.type, node.children[0], node.first_lineno)
  when :CALL, :OPCALL
    block.call(node.type, node.children[1], node.first_lineno)
  end

  node.children.each { |child_node| each_call(child_node, &block) }
end

Usage:

each_call(ast) do |type, name, lineno|
  puts "#{type}: `#{name}' on line #{lineno}"
end

Output:

VCALL: `foo' on line 4
CALL: `bar' on line 5
FCALL: `baz' on line 6
OPCALL: `+' on line 7

This should get you started in writing your own method call extractor.

英文:

> I am trying to find method calls that are explicitly written into the code text of a given file, so nothing having to do with runtime

Let's say we have this example file: (the code doesn't make any sense but its syntax is valid)

# test.rb           # 1
class MyClass       # 2
  def my_method     # 3
    foo             # 4
    self.bar        # 5
    baz(1)          # 6
    1 + 2           # 7
  end               # 8
end                 # 9

For static code analysis, you could use Ruby's AbstractSyntaxTree:

ast = RubyVM::AbstractSyntaxTree.parse_file(&#39;test.rb&#39;)
#=&gt; #&lt;RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-8:22&gt;

The returned Node has a children method which can be used to traverse the syntax tree. The root node is of type SCOPE which represents a lexical scope.

The node types you are looking for are probably:

  • VCALL – a method call without receiver or arguments
  • CALL – a method call with explicit receiver
  • FCALL – a method call with arguments
  • OPCALL – method call using an operator

Here's a very simplistic parser:

def each_call(node, &amp;block)
  return unless node.is_a?(RubyVM::AbstractSyntaxTree::Node)

  case node.type
  when :VCALL, :FCALL
    block.call(node.type, node.children[0], node.first_lineno)
  when :CALL, :OPCALL
    block.call(node.type, node.children[1], node.first_lineno)
  end

  node.children.each { |child_node| each_call(child_node, &amp;block) }
end

Usage:

each_call(ast) do |type, name, lineno|
  puts &quot;#{type}: `#{name}&#39; on line #{lineno}&quot;
end

Output:

VCALL: `foo&#39; on line 4
CALL: `bar&#39; on line 5
FCALL: `baz&#39; on line 6
OPCALL: `+&#39; on line 7

This should get you started in writing your own method call extractor.

答案2

得分: 0

无法在RubyMine中完成此操作。其中一个选项是使用解析树并获取所有方法的列表。

英文:

It's not possible to do it in RubyMine. One of the options is to use the parse tree and get the list of all methods there.

huangapple
  • 本文由 发表于 2023年7月11日 05:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定