试图在Smalltalk VisualWorks中的所有类中搜索字符串?

试图在Smalltalk VisualWorks中的所有类中搜索字符串?,smalltalk,visualworks,Smalltalk,Visualworks,我试图创建一个小函数来搜索整个应用程序中的字符串 我得到了这个代码,但帮不了什么忙 aString := '\\'. class := DosFileDirectory. methodsContainingString := class methodDictionary values select: [:method | method hasLiteralSuchThat: [:lit | (lit isString and: [lit isSymbol not]) an

我试图创建一个小函数来搜索整个应用程序中的字符串

我得到了这个代码,但帮不了什么忙

aString := '\\'.
class := DosFileDirectory.
methodsContainingString := class methodDictionary values select: [:method |
    method hasLiteralSuchThat: [:lit |
        (lit isString and: [lit isSymbol not]) and:
            [lit = aString]]].
messageList := methodsContainingString collect: [ :e | MethodReference new setStandardClass: class methodSymbol: e selector ].

SystemNavigation new
    browseMessageList: messageList
    name: 'methods containing string'.

要搜索整个源代码,可以执行以下操作

searchAll := [ :searchedString |
    (Object withAllSubclasses collect: [ :cls |
        cls methodDictionary values select: [ :method |
            (method getSource findString: searchedString startingAt: 1) > 0
        ]
    ]) inject: #() into: [ :arr :each | arr, each ]
]
  • objectwithallsubclass
    将选择系统中的所有类
  • 方法getSource-findString:startingAt:
    将自己进行匹配(您可以用regexp等替换它)
  • #inject:into:
    将展平阵列(否则它是一个阵列阵列)
要执行搜索,请计算块:

matchedMethods := searchAll value: 'Answer a Paragraph' "(returns a collection of methods containing the string)"
最后,您可以检查集合,或在浏览器中打开它:

MethodCollector new
    openListBrowserOn: (matchedMethods collect: [ :each | each definition ])
    label: 'methods containing "Answer a Paragraph"'

最简单的方法是直接使用MethodCollector(请参阅MethodCollector>>methodsSelect:)

MethodCollector已经负责迭代方法,无需自己完成。MethodCollector还定义了组合查询的方法,因此您还可以将查询限制为特定包中的方法

| mc pattern |
pattern := '*',searchString,'*'. 
mc := MethodCollector new. 
mc browseSelect: (mc methodsSelect: [:m | pattern match: m getSource]).