SmallSense__SmalltalkEditSupport.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 18 Sep 2013 00:58:49 +0100
changeset 89 8ff5fb2b27bf
parent 84 1d05306a49f8
child 100 6d2fb43e661b
permissions -rw-r--r--
Improvement in (Smalltalk)EditSupport. After some electric text is inserted, BackSpace will delete the inserted text instead of just last character.

"{ Package: 'jv:smallsense' }"

"{ NameSpace: SmallSense }"

EditSupport subclass:#SmalltalkEditSupport
	instanceVariableNames:'lastTypedKey0 lastTypedKey1 lastTypedKey2 lastTypedKey3'
	classVariableNames:''
	poolDictionaries:''
	category:'SmallSense-Smalltalk'
!


!SmalltalkEditSupport methodsFor:'accessing'!

language
    "superclass SmallSenseEditorSupport says that I am responsible to implement this method"

    ^SmalltalkLanguage instance

    "Modified: / 24-07-2013 / 23:46:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkEditSupport methodsFor:'editing'!

insertElectricBlockOpenedBy: openText closedBy: closeText
    | line col |

    textView undoableDo:[
        textView insertStringAtCursor: (openText ? '') , Character cr , Character cr, closeText , Character cr.
        line := textView cursorLine - 1.
        col := textView cursorCol  + 3.
        textView cursorLine: line col: col.
    ].

    "Created: / 25-07-2013 / 10:41:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkEditSupport methodsFor:'event handling'!

doKeyPressKeyComplete
    |cls 
"/     crsrPos interval node checkedNode
"/     char start stop selectorSoFar matchingSelectors
    codeView |

    codeView := service codeView.

    cls := codeView classHolder value.
    cls isNil ifTrue:[
        codeView showInfo:'No class'.
        ^ true.
    ].
    UserInformation handle:[:ex |
        codeView showInfo:(ex messageText).
        ex proceed.
    ] do:[
        codeView withWaitCursorDo:[
            | p node text items |

            codeView textView keyRelease: #Control_L x:0 y:0.
            p := codeView characterPositionOfCursor.
            items := Recognizer
                                    resultSetFor: codeView mode
                                    source: codeView contents
                                    class: cls
                                    line: codeView cursorLine
                                    column: codeView cursorCol.

            items notEmptyOrNil ifTrue:[
                (p := items position) notNil ifTrue:[
                    node := p node.                    
                    node notNil ifTrue:[
                        (node isVariable and:[p isInNode]) ifTrue:[
                            text := node name
                        ] ifFalse:[node isMessage ifTrue:[
                            text := node selector
                        ]].
                    ].            
                ].
            ].    

            CompletionWindow openForView: codeView text: text items: items.
        ]
    ].
    ^ true.

"/
"/    interval := self selectedInterval.
"/    interval isEmpty ifTrue:[
"/        crsrPos := codeView characterPositionOfCursor - 1.
"/        char := codeView characterUnderCursor.
"/        [crsrPos > 1 and:[char isSeparator or:['.' includes:char]]] whileTrue:[
"/            crsrPos := crsrPos - 1.
"/            char := codeView characterAtCharacterPosition:crsrPos.
"/        ].
"/        interval := crsrPos to:crsrPos.
"/    ].
"/
"/    node := self findNodeForInterval:interval allowErrors:true.
"/    [node isNil] whileTrue:[
"/        "/ expand to the left ...
"/        interval start > 1 ifFalse:[
"/            self showInfo:'No parseNode found'.
"/            ^ self.
"/        ].
"/        interval start:(interval start - 1).
"/        node := self findNodeForInterval:interval allowErrors:true.
"/    ].
"/
"/    node isVariable ifTrue:[
"/        self codeCompletionForVariable:node inClass:cls.
"/        ^ self.
"/    ].
"/
"/    checkedNode := node.
"/    [checkedNode notNil] whileTrue:[
"/        checkedNode isMessage ifTrue:[
"/            self codeCompletionForMessage:checkedNode inClass:cls.
"/            ^ self
"/        ].
"/        checkedNode isMethod ifTrue:[
"/            self codeCompletionForMethod:checkedNode inClass:cls.
"/            ^ self.
"/        ].
"/        checkedNode := checkedNode parent.
"/    ].
"/
"/    self showInfo:'Node is neither variable nor message.'.

    "Created: / 04-08-2013 / 02:33:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 19-08-2013 / 15:14:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

keyPress: key x:x y:y in: view

    "Handles an event in given view (a subview of codeView).
     If the method returns true, the event will not be processed
     by the view."

    view ~~ textView ifTrue:[ ^ false ].

    lastTypedKey3 := lastTypedKey2.
    lastTypedKey2 := lastTypedKey1.
    lastTypedKey1 := lastTypedKey0.
    lastTypedKey0 := key.

    key == #BackSpace ifTrue:[
        backspaceIsUndo ifTrue:[
             textView undo.
             backspaceIsUndo := false.
             ^ true.
        ].
    ].
    backspaceIsUndo := false.


    key == $^ ifTrue:[
        ^ self keyPressReturnToken
    ].
    key == #Return ifTrue:[
        ^ self keyPressReturn
    ]. 

    ^ super keyPress: key x:x y:y in: view

    "Created: / 07-03-2010 / 09:36:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-09-2013 / 23:19:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

keyPressReturn
    | line tokens i t |

    line := textView listAt: textView cursorLine.
    line isNil ifTrue:[ ^ false ].
    line := line string.
    line size > textView cursorCol ifTrue: [ ^ false ].
    (line indexOfAny:'[|/') == 0 ifTrue:[ ^ false ].

    "/ Insert "/ at the beggining of the line if current line starts with "/
    i := line indexOfNonSeparator.
    (i ~~ 0 and:[ i < line size and:[(line at:i) == $" and:[(line at:i + 1) == $/]]]) ifTrue:[
        "/ OK, current line contains eol-comment
        self insertDo:[
             textView insertCharAtCursor: Character cr. 
        ].
        self insertDo:[
            textView insertStringAtCursor: '"/ '
        ].
        ^ true   
    ].

    ('[|' includes: lastTypedKey1) ifFalse:[ ^ false ].
    i := line size.
    [ (line at: i) isSeparator and:[i > 0] ] whileTrue:[ i := i - 1 ].
    i == 0 ifTrue:[ ^ false ].
    (line at: i) == $[ ifTrue:[
        self insertElectricBlockOpenedBy: nil closedBy: '].'.
        ^ true
    ].
    tokens := self tokensAtCursorLine.
    i := tokens size.
    t := tokens at: i.
    t == $[ ifTrue:[
        self insertElectricBlockOpenedBy: nil closedBy: '].'.
        ^ true
    ].
    t == $| ifTrue:[
        i := i - 1.
        [ i > 1 and:[ (tokens at: i) == #Identifier and:[ (tokens at: i - 1) == $: ]] ] whileTrue:[ i := i - 2 ].

        (i ~~ 0 and: [(tokens at: i) == $[]) ifTrue:[
            self insertElectricBlockOpenedBy: nil closedBy: '].'.
            ^ true
        ].
        i := tokens size  - 1.
        [ i > 0 and:[ (tokens at: i) == #Identifier ] ] whileTrue:[ i := i - 1 ].
        (i ~~ 0 and: [(tokens at: i) == $|]) ifTrue:[
            RBFormatter emptyLineAfterTemporaries ifTrue:[
                textView undoableDo:[
                    textView insertStringAtCursor: (Character cr asString , Character cr , Character cr)
                ].
                ^ true
            ]
        ]
    ].
    ^ false.

    "Created: / 25-07-2013 / 00:02:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-09-2013 / 23:21:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

keyPressReturnToken
    RBFormatter spaceAfterReturnToken ifTrue:[
        self insertDo:[ 
            textView insertStringAtCursor:'^ ' 
        ].
        ^ true
    ].
    ^ false

    "Created: / 24-07-2013 / 23:59:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-09-2013 / 23:20:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkEditSupport methodsFor:'private'!

tokensAtCursorLine
    | scanner token |

    scanner := Scanner for: (service textView listAt: service textView cursorLine) string.
    ^ OrderedCollection streamContents:[:tokens |
        [ token := scanner nextToken.token ~~ #EOF ] whileTrue:[
            tokens nextPut: token.
        ].
    ].

    "Created: / 25-07-2013 / 00:07:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkEditSupport class methodsFor:'documentation'!

version_HG

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