我正在尝试创建一个小函数来搜索整个应用程序中的字符串。
我得到了这段代码,但它没有多大帮助
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'.
答案 0 :(得分:2)
最简单的方法是直接使用MethodCollector(参见MethodCollector>> methodsSelect:)
| mc pattern |
pattern := '*',searchString,'*'.
mc := MethodCollector new.
mc browseSelect: (mc methodsSelect: [:m | pattern match: m getSource]).
MethodCollector已经负责迭代方法,不需要自己动手。 MethodCollector还定义了组合查询的方法,因此您还可以将查询限制为某个包中的方法。
答案 1 :(得分:0)
要搜索整个源代码,您可以执行以下操作
searchAll := [ :searchedString |
(Object withAllSubclasses collect: [ :cls |
cls methodDictionary values select: [ :method |
(method getSource findString: searchedString startingAt: 1) > 0
]
]) inject: #() into: [ :arr :each | arr, each ]
]
Object withAllSubclasses
将选择系统中的所有课程method 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"'