SmallSense__CompletionEngine.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 08 Apr 2014 21:46:51 +0200
changeset 192 f27ce6dac101
parent 187 7baeeea7d8ae
child 201 9a0df215823a
child 205 43bee6463c53
permissions -rw-r--r--
Initial support for completing selector parts. When user types map at: 1 ifA then the completer also offers to complete `ifAbsent:`, `ifAbsentPut:` ... in addition to SmallInteger methods.

"{ Package: 'jv:smallsense' }"

"{ NameSpace: SmallSense }"

Object subclass:#CompletionEngine
	instanceVariableNames:'codeView result context'
	classVariableNames:''
	poolDictionaries:''
	category:'SmallSense-Core'
!


!CompletionEngine class methodsFor:'accessing'!

exactMatcher
    "Return a match block returning true, if given selector start with given prefix"

    ^ [ :prefix :selector | selector startsWith: prefix ]

    "Created: / 08-04-2014 / 21:31:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

inexactMatcher
    "Return a match block returning true, if given prefix matches given selector"

    ^ [ :prefix :selector |
        prefix size < 5 ifTrue:[ 
            selector startsWith: prefix.  
        ] ifFalse:[ 
            | part |

            part := selector copyTo: (prefix size min: selector size).
            (prefix levenshteinTo: part) < 15
        ].
    ].

    "Created: / 08-04-2014 / 21:30:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

matcher
    "Return a match block returning true, if given prefix matches given selector"

    ^ [ :prefix :selector |
        prefix size < 5 ifTrue:[ 
            selector startsWith: prefix.  
        ] ifFalse:[ 
            | part |

            part := selector copyTo: (prefix size min: selector size).
            (prefix levenshteinTo: part) < 15
        ].
    ].

    "Created: / 02-04-2014 / 23:30:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CompletionEngine class methodsFor:'testing'!

isAbstract
    ^ self == CompletionEngine

    "Created: / 02-10-2013 / 13:11:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CompletionEngine methodsFor:'completion'!

complete: aCompletionContext
    "Compute completion for given completion context, taking all the information
     from it. Returns a CompletionResult with computed completions"

    context := aCompletionContext.
    result := CompletionResult new.
    codeView := context codeView.
    result context: context.
    ^ self complete.

    "Created: / 21-01-2014 / 23:07:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

completeFor: aCodeView2OrTextEditView
    "Compute completion for given codeView, taking all the information
     from it. Returns a CompletionResult with computed completions"

    codeView := aCodeView2OrTextEditView.
    result := CompletionResult new.

    ^ self complete.

    "Created: / 02-10-2013 / 13:24:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 03-10-2013 / 16:42:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CompletionEngine methodsFor:'completion-individual'!

addMethodsStartingWith: prefix
    ^ self addMethodsStartingWith: prefix stripOff: nil filter: nil

    "Created: / 24-07-2013 / 13:10:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 08-04-2014 / 21:36:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addMethodsStartingWith: prefix stripOff: stripoffPrefix 
    ^ self addMethodsStartingWith: prefix stripOff: stripoffPrefix filter: nil

    "Created: / 08-04-2014 / 21:36:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addMethodsStartingWith: prefix stripOff: stripoffPrefix filter: filterOrNil
    | matcher |

    matcher := stripoffPrefix isEmptyOrNil ifTrue:[ CompletionEngine inexactMatcher ] ifFalse:[ CompletionEngine exactMatcher ].
    ^ self addMethodsStartingWith: prefix stripOff: stripoffPrefix filter: filterOrNil matcher: matcher.

    "Created: / 08-04-2014 / 21:35:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addMethodsStartingWith: prefix stripOff: stripoffPrefix filter: filterOrNil matcher: matcher
    | matchPrefix selectors filter |

    selectors := Dictionary new.
    matchPrefix := stripoffPrefix isNil ifTrue:[ prefix ] ifFalse:[ stripoffPrefix , prefix ]. 
    filter := filterOrNil  isNil ifTrue:[ [:method | true ] ] ifFalse:[ filterOrNil  ].

    Smalltalk allClassesDo:[:class|
        class selectorsAndMethodsDo:[:selector :mthd |             
            (mthd isSynthetic not and:[(filter value: mthd) and:[ matcher value: matchPrefix value: selector]]) ifTrue:[
                | class skip |

                class := mthd mclass superclass.
                skip := false.
                [ skip not and:[class notNil] ] whileTrue:[
                    (class methodDictionary includesKey: selector) ifTrue:[
                        skip := true.
                    ].
                    class := class superclass.
                ].
                skip ifFalse:[
                    | classes |

                    classes := selectors at: selector ifAbsentPut:[ Set new ].
                    (classes includes: mthd mclass) ifFalse:[
                        classes add: mthd mclass.
                    ].
                ].
            ]
        ].
    ].

    selectors keysAndValuesDo: [:selector :classes|
        result add:(MethodPO 
                name:selector
                class:(classes size == 1 ifTrue:[classes anElement] ifFalse:[classes])
                stripOff: stripoffPrefix)
    ]

    "Created: / 08-04-2014 / 21:34:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CompletionEngine methodsFor:'completion-private'!

complete
    "Compute completion for `codeView`, taking all the information
     from it. Returns a CompletionResult with computed completions"        

    ^ self subclassResponsibility

    "Modified (comment): / 02-10-2013 / 13:33:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CompletionEngine class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !