Tools__BreakpointService.st
author Claus Gittinger <cg@exept.de>
Tue, 05 Jul 2011 23:27:49 +0200
changeset 10182 9ce79271722f
parent 10071 1bf516aac0b8
child 10208 5f0f44bd199a
permissions -rw-r--r--
added: #recompile

"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
              All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
"{ Package: 'stx:libtool' }"

"{ NameSpace: Tools }"

CodeViewService subclass:#BreakpointService
	instanceVariableNames:'breakpoints'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-CodeView'
!

!BreakpointService class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
              All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
! !

!BreakpointService methodsFor:'event handling'!

buttonPress:button x:x y:y in:view 
    codeView methodHolder value isNil ifTrue:[
        ^ false
    ].
    view == gutterView ifTrue:[
        button == 1 ifTrue:[
            self setOrToggleBreakpointAtLine:(textView yVisibleToLineNr:y).
            ^ true.
        ].
        button == 3 ifTrue:[
            ^ true.
        ]
    ].
    ^ false

    "Created: / 17-06-2011 / 13:05:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-06-2011 / 08:31:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 05-07-2011 / 21:33:29 / cg"
! !

!BreakpointService methodsFor:'initialization'!

initialize

    super initialize.
    breakpoints := OrderedCollection new.

    "Created: / 17-06-2011 / 13:49:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BreakpointService methodsFor:'private'!

breakpointAtLine:line 
    |pos|

    pos := textView characterPositionOfLine:line col:1.
    ^ breakpoints ? #() detect:[:each | each position = pos ] ifNone:[ nil ]

    "Modified: / 17-06-2011 / 13:59:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 05-07-2011 / 21:33:23 / cg"
!

recompile
    "recompile the current method for changed breakpoints"

    |method|

    method := codeView methodHolder value.
    (method notNil and:[method hasPrimitiveCode not]) ifTrue:[
        "/ be careful: if the text has been edited/modified, do not compile
        textView modified ifTrue:[
            self halt
        ] ifFalse:[
            "/ prepare to get reachable bpts
            breakpoints do:[:bp | bp isReached:false].

            ByteCodeCompilerWithBreakpointSupport new
                breakpoints:breakpoints;

                compile:method source
                forClass:method mclass 
                inCategory:method category 
                notifying:nil
                install:true 
                skipIfSame:false 
                silent:true 
                foldConstants:true
                ifFail:[ self halt ].

            breakpoints := breakpoints select:[:bp | bp isReached]
        ]
    ]

    "Created: / 05-07-2011 / 21:33:13 / cg"
!

setOrToggleBreakpointAtLine:line 
    |pos bpnt|

    pos := textView characterPositionOfLine:line col:1.
    bpnt := self breakpointAtLine:line.
    bpnt isNil ifTrue:[
        breakpoints add:((Breakpoint new)
                    position:pos;
                    line:line).
        self recompile.
    ] ifFalse:[
        bpnt toggle.
    ].
    gutterView redrawLine:line.

    "Created: / 17-06-2011 / 13:45:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 05-07-2011 / 21:32:49 / cg"
! !

!BreakpointService methodsFor:'redrawing'!

drawLine:lineNo in:view atX:x y:y width:w height:h from:startCol to:endColOrNil with:fg and:bg 
    "Called by both gutterView and textView (well, not yet) to
     allow services to draw custom things on text view.
     Ask JV what the args means if unsure (I'm lazy to document
     them, now it is just an experiment...)"
    
    |bpnt icon dx dy|

    codeView methodHolder value isNil ifTrue:[
        ^ self
    ].
    view == gutterView ifTrue:[
        bpnt := self breakpointAtLine:lineNo.
        bpnt isNil ifTrue:[
            ^ self
        ].
        icon := bpnt icon.
        icon isNil ifTrue:[
            ^ self
        ].
        dx := ((w - icon width) / 2) rounded.
        dy := ((h - icon height) / 2) rounded.
        icon 
            displayOn:view
            x:x + dx
            y:y - h + dy + 4. "TODO: Magic constant"
    ].

    "Created: / 17-06-2011 / 13:52:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 05-07-2011 / 22:14:33 / cg"
! !

!BreakpointService class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libtool/Tools__BreakpointService.st,v 1.3 2011-07-05 21:27:49 cg Exp $'
!

version_SVN
    ^ '§Id§'
! !