Tools__CodeCompletionMenu.st
author Claus Gittinger <cg@exept.de>
Fri, 01 Jul 2011 15:21:34 +0200
changeset 9986 234457682730
parent 9959 9ed584296d79
child 10078 3bd0f45a96d2
permissions -rw-r--r--
initial checkin

"
 COPYRIGHT (c) 2006 by eXept Software AG
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libtool' }"

"{ NameSpace: Tools }"

SelectionInListModelView subclass:#CodeCompletionMenu
	instanceVariableNames:'wholeList typedString codeView selectedString cursorCol column
		position'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-CodeView'
!

!CodeCompletionMenu class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
! !

!CodeCompletionMenu class methodsFor:'interface opening'!

openFor: aCodeView at: anInteger with:aCollection
    |m|

    m := (self new)
        initializeForView:aCodeView at: anInteger with: aCollection.

    ^ m openModalXX.

    "Created: / 16-02-2010 / 10:18:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 04-09-2010 / 08:41:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 01-07-2011 / 00:05:01 / cg"
! !

!CodeCompletionMenu methodsFor:'accessing'!

contentHeight

    | size |

    size := self list size max: 10.
    ^(size * (wholeList anyOne heightOn:self)) + 15

    "Created: / 22-03-2006 / 22:36:49 / janfrog"
    "Modified: / 26-03-2006 / 19:39:49 / janfrog"
    "Modified: / 16-02-2010 / 10:58:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

contentWidth

    | w |

    w := wholeList
        inject:0
        into:[:max :each|max max:(each widthOn:self)].

    ^(w + 20) min: 600

    "Created: / 22-03-2006 / 22:36:49 / janfrog"
    "Modified: / 06-03-2010 / 08:17:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CodeCompletionMenu methodsFor:'event handling'!

backspaceKeyPress

    typedString isEmpty ifTrue:[^self topView close].

    typedString := typedString allButLast.
    self updateList

    "Created: / 25-05-2005 / 20:03:22 / janfrog"
    "Modified: / 22-03-2006 / 21:04:09 / janfrog"
!

characterKeyPress:aCharacter

    | newString |
    newString := typedString , aCharacter.
    (wholeList anySatisfy:[:s|s startsWith:newString]) ifTrue:[
        typedString := newString.
        self updateList.
    ]

    "Created: / 25-05-2005 / 20:02:12 / janfrog"
    "Modified: / 27-05-2005 / 22:29:10 / janfrog"
!

doubleClicked
    self returnKeyPress

    "Created: / 30-06-2011 / 20:01:03 / cg"
!

keyPress:key x:x y:y

    key isCharacter ifTrue:[
        self characterKeyPress:key.
        codeView keyPress:key x:x y:y.
        ^self
    ].
    key == #Return ifTrue:[
        self returnKeyPress.
        ^self
    ].
    key == #BackSpace ifTrue:[
        codeView keyPress:key x:x y:y.
        self backspaceKeyPress.
        ^self
    ].
    key == #Tab ifTrue:[
           "/self tabKeyPressX:x y:y.
           Transcript showCR:'Tab-completion broken. Disabled'.
           ^self.
    ].

    ^super keyPress:key x:x y:y

    "Created: / 25-05-2005 / 19:57:27 / janfrog"
    "Modified: / 22-03-2006 / 21:12:35 / janfrog"
!

mapped
    super mapped.
    self forceUngrabPointer.
    self grabPointer.

"/    self requestFocus.
"/    self grabPointer.
"/    self grabKeyboard.

    "Created: / 30-06-2011 / 19:58:59 / cg"
!

returnKeyPress

    self list size = 1 ifTrue:[
        selectedString := self list first.
        codeView setCursorCol:cursorCol.
        self topView close
    ] ifFalse:[        
        self selection ifNotNil:[
            selectedString := self at:self selection.
            codeView setCursorCol:cursorCol.
            self topView close
        ]
    ]

    "Created: / 25-05-2005 / 20:34:16 / janfrog"
    "Modified: / 29-05-2005 / 09:40:15 / janfrog"
!

tabKeyPressX:x y:y

    | currentList idx char completionString|
    currentList := self list.
    idx := typedString size + 1.
    [
        (currentList allSatisfy:[:s|s size >= idx]) and:[
            char := currentList anyOne at:idx.
            currentList allSatisfy:[:s|(s at:idx) == char]
        ]
    ] whileTrue:[
        idx := idx + 1.
    ].
    completionString := currentList anyOne copyFrom:(typedString size + 1) to:idx - 1.
    completionString do:[:c|codeView keyPress:c x:x y:y].
    typedString := typedString , completionString.
    Transcript showCR:typedString

    "Created: / 29-05-2005 / 09:50:03 / janfrog"
! !

!CodeCompletionMenu methodsFor:'initialize'!

initialize

    super initialize.

    typedString := ''.
    highlightMode := #line

    "Created: / 28-05-2005 / 19:49:09 / janfrog"
!

initializeForView: aTextView at: anInteger with: aCollection 

    self initialize.

    codeView := aTextView.
    wholeList := aCollection.
    "/ self font:codeView font.
    position := anInteger.
    cursorCol := codeView cursorCol.

    "Modified: / 29-05-2005 / 09:39:35 / janfrog"
    "Created: / 16-02-2010 / 10:10:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 01-07-2011 / 00:08:31 / cg"
! !

!CodeCompletionMenu methodsFor:'interface opening'!

openModalXX
    | modalBox p |

    self updateList.

    self list size == 1 ifTrue:[^self list anyOne string copyFrom:(typedString size + 1)].

    modalBox := PopUpView new.

    ScrollableView   
        forView:self 
        hasHorizontalScrollBar:false 
        hasVerticalScrollBar:true 
        miniScrollerH:true 
        miniScrollerV:true 
        origin:0.0@0.0 
        corner:1.0@1.0 
        in:modalBox.

    modalBox extent:(self contentWidth) @ (self contentHeight).

    p := codeView originRelativeTo: nil.
    p x: (p x + (codeView xOfPosition:position) - textStartLeft - 3).
    p y: (p y + ("(codeView yOfPosition:position) max:"
                    (codeView yOfCursor)) + font maxHeight).
    "/x := codeView absoluteXOfCursor - (typedString widthOn:codeView) - 10 - 12"/width of icon
    " 10 - Magic number, but looks good on my computer"
    "/x := (codeView absoluteXOfPosition: position) - textStartLeft - 3.
    "/y := codeView absoluteYOfCursor + font maxHeight.
    "/y := (codeView absoluteYOfCursor max: (codeView absoluteYOfPosition: position))  + font maxHeight.

    modalBox origin:p.
    modalBox makeFullyVisible.
    modalBox exclusivePointer:false.
    modalBox open.
    ^ selectedString ifNotNil:[selectedString string copyFrom:(typedString size + 1)]

    "Modified: / 01-07-2011 / 09:30:29 / cg"
! !

!CodeCompletionMenu methodsFor:'updating'!

updateList

    self list:(wholeList select:[:s| s startsWith:typedString])

    "Created: / 25-05-2005 / 20:06:07 / janfrog"
    "Modified: / 27-05-2005 / 22:23:37 / janfrog"
! !

!CodeCompletionMenu class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libtool/Tools__CodeCompletionMenu.st,v 1.2 2011-07-01 13:21:34 cg Exp $'
!

version_SVN
    ^ '§Id: Tools__CodeCompletionMenu.st 7690 2011-03-19 02:23:25Z vranyj1 §'
! !