Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for a string inside a class in Squeak smalltalk ? How about inside a package?

I searched, and searched.

I went to IRC

Hope the question is not silly. If it was, the right string to search at google would still be much appreciated

like image 415
josinalvo Avatar asked Dec 24 '11 21:12

josinalvo


2 Answers

Answering such questions with the refactoring engine is quite easy. The following code finds all occurrences of / in the system:

allCodeWithSlash := RBBrowserEnvironment new matches: '/'

From there you can further scope the search, e.g. to inside a class:

allCodeWithSlashInClass := allCodeWithSlash forClasses: (Array with: DosFileDirectory)

Or inside a package:

allCodeWithSlashInPackage := allCodeWithSlash forPackageNames: (Array with: 'Files')

If you have an UI loaded, you can open a browser on any of these search results:

allCodeWithSlashInPackage open

If you use OmniBrowser, you can also build and navigate these scopes without typing any code through the Refactoring scope menu.

like image 59
Lukas Renggli Avatar answered Oct 01 '22 17:10

Lukas Renggli


Here's an example that shows you all methods in DosFileDirectory containing the string '\'.

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'.

To search a whole package, wrap the search part in:

package := PackageOrganizer default packageNamed: packageName ifAbsent: [ self error: 'package doesn't exist' ].
package classesAndMetaClasses do: [ :class | ... ]
like image 36
Sean DeNigris Avatar answered Oct 01 '22 17:10

Sean DeNigris