英文:
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 # 9For 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
Nodehas achildrenmethod which can be used to traverse the syntax tree. The root node is of typeSCOPEwhich represents a lexical scope.The node types you are looking for are probably:
VCALL– a method call without receiver or argumentsCALL– a method call with explicit receiverFCALL– a method call with argumentsOPCALL– method call using an operatorHere'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) } endUsage:
each_call(ast) do |type, name, lineno| puts "#{type}: `#{name}' on line #{lineno}" endOutput:
VCALL: `foo' on line 4 CALL: `bar' on line 5 FCALL: `baz' on line 6 OPCALL: `+' on line 7This 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('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 argumentsCALL– a method call with explicit receiverFCALL– a method call with argumentsOPCALL– 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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论