SmallSense__JavaCompletionEngine.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 07 Aug 2014 10:30:23 +0100
changeset 267 b6fbf84b14ae
parent 252 feba6ee5c814
child 278 696843cd1f9d
permissions -rw-r--r--
Temporary commit: More work on JavaCompletionEngine

"
stx:goodies/smallsense - A productivity plugin for Smalltalk/X IDE
Copyright (C) 2013-2014 Jan Vrany

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"
"{ Package: 'stx:goodies/smallsense' }"

"{ NameSpace: SmallSense }"

AbstractJavaCompletionEngine subclass:#JavaCompletionEngine
	instanceVariableNames:'classTree methodTree'
	classVariableNames:''
	poolDictionaries:''
	category:'SmallSense-Java'
!

!JavaCompletionEngine class methodsFor:'documentation'!

copyright
"
stx:goodies/smallsense - A productivity plugin for Smalltalk/X IDE
Copyright (C) 2013-2014 Jan Vrany

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"
! !

!JavaCompletionEngine methodsFor:'completion'!

completeNode: node
    Transcript 
        show: 'Java Simple Completion on node: ';
        show: node printString;
        show: ' [';
        show: node class printString;
        showCR: ']'.

    "Created: / 20-10-2013 / 01:34:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaCompletionEngine methodsFor:'completion-individual'!

addFieldsStartingWith: prefix
    | klass |

    classTree notNil ifTrue:[
        (classTree fields ? #()) do:[:field |
            result add: (VariablePO instanceVariable: field name in: class).            
        ].
    ] ifFalse:[
        klass := class.
    ].

    [ klass notNil ] whileTrue:[
        klass instVarNames do:[:nm |
            result add: (VariablePO instanceVariable: nm in: klass).
        ].
        klass := klass superclass.
    ].

    "Created: / 03-10-2013 / 11:16:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 20-10-2013 / 02:04:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addImportsStartingWith: prefix
    | packages |

    packages := Set new.

    "/ Class imports...
    self javaClassesDo:[:cls|
        | name i |

        name := cls javaName.
        (cls isPublic and:[name startsWith: prefix]) ifTrue:[
            result add: (JavaImportPO new subject: name; klass: cls; yourself).        
            packages add: cls javaPackage.
        ].
    ].
    "/ Package imports...
    packages do:[:each |
        result add: (JavaImportPO new subject: (each , '.*'))
    ].

    "Created: / 19-10-2013 / 17:54:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 20-10-2013 / 00:35:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addLocalsStartingWith: prefix
    | queue |

    methodTree isNil ifTrue:[ ^ self ].
    methodTree scope isNil ifTrue:[ ^ self ].

    queue := OrderedCollection with: methodTree scope.
    [ queue notEmpty ] whileTrue:[
        | scope |

        scope := queue removeFirst.
        1 to: scope localIndex do:[:i|
            | nm |

            nm := (scope locals at: i) name.
            (nm startsWith: prefix) ifTrue:[
                result add: (VariablePO instanceVariable: nm in: class). 
            ].
        ].
    ].

    "Created: / 03-10-2013 / 17:46:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 20-10-2013 / 02:15:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addMethodsForReceiver: maybeReceiverToken startingWith: prefix    
    ^ self addMethodsStartingWith: prefix

    "Created: / 03-10-2013 / 17:46:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addMethodsStartingWith: prefix    
    ^ self addMethodsStartingWith: prefix stripOff: nil filter: [:m | m isJavaMethod ]

    "Created: / 03-10-2013 / 18:01:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 08-04-2014 / 21:37:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaCompletionEngine methodsFor:'completion-private'!

complete
    
    | position source parser tree finder node scope |

    position := context codeView characterPositionOfCursor.

    source := JAVA stx libjava tools Source new.
    source setContents: codeView list asStringWithoutEmphasis.
    parser := JAVA stx libjava tools parser Parser new.
    tree := parser parse: source diet: true resolve: true.

    "
    (SmallSense::ParseTreeInspector new node:tree source: codeView list asString) open
    "

    (tree notNil and:[tree types notEmptyOrNil]) ifTrue:[
        classTree := tree types detect:[:t | (position - 1) between: t declarationSourceStart and: t declarationSourceEnd ] ifNone:[nil].
        (classTree notNil and: [ classTree methods notEmptyOrNil ]) ifTrue:[
            methodTree := classTree methods detect:[:m | (position - 1) between: m declarationSourceStart and: m declarationSourceEnd ] ifNone:[nil].
            methodTree notNil ifTrue:[ 
                methodTree parseStatements: parser in: tree.
            ].
        ].
        finder := JAVA stx libjava tools ast ASTNodeFinder new.
        finder setPosition: position - 1.
        tree traverse: finder scope: tree scope.
        node := finder node.
        scope := finder scope.
    ].


    context node: node position: position.

    node isNil ifTrue:[
        result := JavaCompletionEngineSimple new complete: context.
    ] ifFalse:[
        self completeNode: node.
    ].

    ^ result

    "Created: / 02-10-2013 / 13:55:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 07-08-2014 / 10:05:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !