SmallSenseSyntaxHighlightingService.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 02 Aug 2013 16:40:58 +0100
changeset 47 547d48a49dbd
parent 42 48307f46ff8e
child 48 1ff5ddc8dc9c
permissions -rw-r--r--
Added simple tab completion.

"{ Package: 'jv:smallsense' }"

Tools::CodeHighlightingService subclass:#SmallSenseSyntaxHighlightingService
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'SmallSense-Services'
!

!SmallSenseSyntaxHighlightingService class methodsFor:'accessing'!

label
    "Answers a short label - for UI"

    ^'SmallSense - Syntax Highlighting'

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

!SmallSenseSyntaxHighlightingService methodsFor:'change & update'!

sourceChanged:force
    ^self sourceChanged: force from: nil to: nil.

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

sourceChanged:force from:start to:end    
    "Called when codeview's text changes"

    (force or:[codeView reallyModified]) ifTrue:[
        self process
    ].

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

!SmallSenseSyntaxHighlightingService methodsFor:'event handling'!

linesDeletedFrom:start to:end 
    self sourceChanged:true from:start to:end

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

linesInsertedFrom:start to:end 
    self sourceChanged:true from:start to:end

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

linesModifiedFrom:start to:end 
    self sourceChanged:true from:start to:end

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

!SmallSenseSyntaxHighlightingService methodsFor:'private'!

process: delayed

    |oldCode oldCodeList newCode elements cls mthd highlighterClasses|

    done := false.
    modified := false.
    codeView syntaxElements: nil.
    codeView syntaxElementSelection: nil.

    highlighterClasses := self syntaxHighlighters.

    cls := codeView klass.
    (cls notNil and:[cls isObsolete]) ifTrue:[
        cls isMeta ifTrue:[
            cls := (Smalltalk at:cls theNonMetaclass name) class
        ] ifFalse:[
            cls := Smalltalk at:cls name
        ].
    ].
    mthd := codeView methodHolder value.

    "textView" modified ifFalse:[
        oldCodeList := textView list copy.
        "textView" modified ifFalse:[
            oldCodeList isNil ifFalse:[
                oldCode := oldCodeList asStringWithoutEmphasis.
                "textView" modified ifFalse:[
                    Screen currentScreenQuerySignal answer:codeView device
                    do:[
                        Parser parseErrorSignal handle:[:ex |
                            |errMsg|

                            errMsg := ex description asStringCollection first asString.

                            "/ Transcript topView raiseDeiconified.
                            "/ Transcript showCR:'ParseError: ', ex description.
"/ self halt.
                            "/ self showInfo:(errMsg colorizeAllWith:Color red).
                            newCode := nil.
                        ] do:[
                            | codeAspect |
                            
                            elements := ParseTreeIndex new.
                            newCode := oldCode asText.
                            codeAspect := codeView codeAspect.
                            codeAspect == SyntaxHighlighter codeAspectMethod ifTrue:[
                                highlighterClasses do:[:e|newCode := e formatMethod:mthd source:newCode in:cls using:syntaxPreferences elementsInto: elements].
                            ] ifFalse:[
                                codeAspect == (SyntaxHighlighter codeAspectExpression) ifTrue:[
                                    highlighterClasses do:[:e|newCode := e formatExpression:newCode in:cls elementsInto: elements].
                                ] ifFalse:[
                                    codeView codeAspect == #classDefinition ifTrue:[
                                        highlighterClasses do:[:e|newCode := e formatClassDefinition:newCode in:cls elementsInto: elements].
                                    ]
                                ].
                            ].
                        ]
                    ].
                    newCode notNil ifTrue:[
                        "textView" modified ifFalse:[
                            newCode ~= oldCodeList ifTrue:[
                                newCode := newCode asStringCollection.
                                "textView" modified ifFalse:[
                                    done := true.
                                    textView notNil ifTrue:[
                                        "/ must add this event - and not been interrupted
                                        "/ by any arriving key-event.
                                        "/ self showInfo:nil.
                                        delayed ifTrue:[
                                            codeView sensor
                                                pushUserEvent:#setHighlightedCode:elements:
                                                for:self
                                                withArguments:(Array with:newCode with: elements).
                                                "/self delayedUpdateBufferLabelWithCheckIfModified
                                        ] ifFalse:[
                                            textView contents: newCode.
                                            codeView syntaxElements: elements.
                                            gutterView invalidate.
                                        ]
                                    ]
                                ]
                            ].
                        ]
                    ]
                ]
            ]
        ]
    ]

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