SmallSense__SmalltalkEditSupport.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 02 Oct 2013 13:37:01 +0100
changeset 117 441529422c2f
parent 110 5ee9a90dbed5
child 120 4fefce92f5bb
permissions -rw-r--r--
Completion support refactored to make it more pluggable/extendable.

"{ 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:'accessing-classes'!

completionEngineClass
    ^ SmalltalkCompletionEngine

    "Created: / 02-10-2013 / 13:30:50 / 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  + 2.
        textView cursorLine: line col: col.
    ].

    "Created: / 25-07-2013 / 10:41:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 30-09-2013 / 11:42:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkEditSupport methodsFor:'event handling'!

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: / 24-09-2013 / 22:55:14 / 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:'initialization'!

initializeForService: anEditService    
    super initializeForService: anEditService.
    service textView  autoIndent:true.
    self initializeCompletion.

    "Created: / 27-09-2013 / 13:22:48 / 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> $'
! !