added: #resourceCompletion:inEnvironment:match:ignoreCase:
authorClaus Gittinger <cg@exept.de>
Wed, 06 Jul 2011 12:19:04 +0200
changeset 4049 b5540a857436
parent 4048 f44190df8501
child 4050 df578fc47076
added: #resourceCompletion:inEnvironment:match:ignoreCase:
DoWhatIMeanSupport.st
--- a/DoWhatIMeanSupport.st	Tue Jul 05 15:35:34 2011 +0200
+++ b/DoWhatIMeanSupport.st	Wed Jul 06 12:19:04 2011 +0200
@@ -1178,6 +1178,68 @@
     "Created: / 10-08-2006 / 13:22:31 / cg"
 !
 
+resourceCompletion:aPartialResourceName inEnvironment:anEnvironment match:doMatch ignoreCase:ignoreCase
+    "given a partial resource name, return an array consisting of
+     2 entries: 1st: the longest match
+                2nd: collection consisting of matching defined resources"
+
+    |matches best lcSym isMatch|
+
+    matches := IdentitySet new.
+
+    isMatch := doMatch and:[aPartialResourceName includesMatchCharacters].
+
+    anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector |
+        eachMethod hasResource ifTrue:[
+            eachMethod resources keysDo:[:eachResourceName |
+                (isMatch 
+                    ifTrue:[ (aPartialResourceName match:eachResourceName ignoreCase:ignoreCase) ]
+                    ifFalse:[ ignoreCase 
+                                ifTrue:[ (eachResourceName asLowercase startsWith:aPartialResourceName asLowercase) ]  
+                                ifFalse:[ (eachResourceName startsWith:aPartialResourceName) ] ]
+                ) ifTrue:[
+                    matches add:eachResourceName
+                ].
+            ].
+        ].
+    ].
+    (matches isEmpty and:[ignoreCase not]) ifTrue:[
+        "/ search for case-ignoring match
+        lcSym := aPartialResourceName asLowercase.
+        anEnvironment allMethodsWithSelectorDo:[:eachMethod :eachSelector |
+            eachMethod hasResource ifTrue:[
+                eachMethod resources keysDo:[:eachResourceName |
+                    (isMatch 
+                        ifTrue:[ (aPartialResourceName match:eachResourceName ignoreCase:true) ]
+                        ifFalse:[ (eachResourceName asLowercase startsWith:lcSym) ])
+                     ifTrue:[
+                        matches add:eachResourceName
+                    ].
+                ].
+            ].
+        ].
+    ].
+
+    matches isEmpty ifTrue:[
+        ^ Array with:aPartialResourceName with:#() 
+    ].
+    matches size == 1 ifTrue:[
+        ^ Array with:matches first with:(matches asArray)
+    ].
+    matches := matches asSortedCollection.
+    best := matches longestCommonPrefix.
+    ^ Array with:best with:matches asArray
+
+    "
+     DoWhatIMeanSupport resourceCompletion:'*debug*' inEnvironment:Smalltalk match:true ignoreCase:false
+     DoWhatIMeanSupport resourceCompletion:'context' inEnvironment:Smalltalk match:true ignoreCase:false
+     DoWhatIMeanSupport resourceCompletion:'key' inEnvironment:Smalltalk match:true ignoreCase:false
+     DoWhatIMeanSupport resourceCompletion:'cont' inEnvironment:Smalltalk match:true ignoreCase:false
+    "
+
+    "Created: / 06-07-2011 / 12:04:41 / cg"
+!
+
 selectorCompletion:aPartialSymbolName inEnvironment:anEnvironment
     "given a partial selector, return an array consisting of
      2 entries: 1st: the longest match
@@ -1579,5 +1641,5 @@
 !DoWhatIMeanSupport class methodsFor:'documentation'!
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libwidg2/DoWhatIMeanSupport.st,v 1.79 2011-07-05 09:05:24 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/DoWhatIMeanSupport.st,v 1.80 2011-07-06 10:19:04 cg Exp $'
 ! !