Tools__BreakpointService.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 26 Mar 2012 16:34:31 +0100
branchjv
changeset 12207 17656fc80e6c
parent 12130 764a3c16d308
child 12229 5c129972b1fd
permissions -rw-r--r--
Improvements in method rewriter

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

breakpoints
    ^ breakpoints
! !

!BreakpointService methodsFor:'change & update'!

update: aspect with: param from: sender
    |method|

    method := codeView methodHolder value.
    method ~~ currentMethod ifTrue:[
        self updateBreakPointsFor:method.
    ].

    super update: aspect with: param from: sender

    "Created: / 06-07-2011 / 15:21:08 / cg"
!

updateBreakPointsFor:aMethod
    |methodsBreakPoints|

    methodsBreakPoints := OrderedCollection new.
    aMethod notNil ifTrue:[
        aMethod literalsDo:[:eachLiteral |
            eachLiteral class == Breakpoint ifTrue:[
                methodsBreakPoints add:eachLiteral copy.
            ].
        ].
    ].
    breakpoints := methodsBreakPoints.
    currentMethod := aMethod.

    "Created: / 06-07-2011 / 15:24:09 / cg"
    "Modified: / 06-07-2011 / 17:32:54 / jv"
! !

!BreakpointService methodsFor:'event handling'!

buttonPress:button x:x y:y in:view 
    |lineNr|

    codeView methodHolder value isNil ifTrue:[
        ^ false
    ].
    view == gutterView ifTrue:[
        button == 1 ifTrue:[
            lineNr := textView yVisibleToLineNr:y.
            lineNr notNil ifTrue:[ self setOrToggleBreakpointAtLine:lineNr ].
            ^ 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: / 19-09-2011 / 14:41:00 / cg"
!

linesDeletedFrom: start to: end

    breakpoints isEmptyOrNil ifTrue:[^self].
    self moveBreakpointsAfterLine: start - 1 by: (end - start + 1) negated

    "Created: / 06-07-2011 / 17:16:27 / jv"
!

linesInsertedFrom: start to: end

    breakpoints isEmptyOrNil ifTrue:[^self].
    self moveBreakpointsAfterLine: start - 1 by: (end - start + 1)

    "Created: / 06-07-2011 / 17:16:36 / jv"
! !

!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"
!

moveBreakpointsAfterLine:line by: delta

    |pos |


    breakpoints do:[:bpnt|
        bpnt line >= line ifTrue:[
            pos := textView characterPositionOfLine:bpnt line + delta col:1.
            bpnt line: bpnt line + delta; position: pos
        ]
    ].

    "/gutterView redrawLinesFrom: line.

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

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].

            Class withoutUpdatingChangesDo:[
                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 ].
            ].
"/ self halt.
            breakpoints := breakpoints 
                            select:[:bp | 
                                bp isReached ifFalse:[ Transcript show:'remove unreached:'; showCR: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:[
        (codeView topView class ~~ DebugView) ifTrue:[
            breakpoints add:((bpnt := Breakpoint new)
                                position:pos;
                                line:line).
            Display shiftDown ifTrue:[
                "/ trace
                bpnt beTracepoint
            ].
            self recompile.
        ].
    ] ifFalse:[
        Display shiftDown ifTrue:[
            bpnt toggleTracing
        ] ifFalse:[
            bpnt toggle.
        ]
    ].
    gutterView redrawLine:line.

    "Created: / 17-06-2011 / 13:45:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 27-07-2011 / 13:27:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 27-01-2012 / 13:55:12 / 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|

    codeView methodHolder value isNil ifTrue:[
        ^ self
    ].
    view == gutterView ifTrue:[
        bpnt := self breakpointAtLine:lineNo.
        bpnt isNil ifTrue:[
            ^ self
        ].
        icon := bpnt icon.
        icon isNil ifTrue:[
            ^ self
        ].
        self 
            drawAnnotationIcon:icon
            atX:x y:y width:w height:h
    ].

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

!BreakpointService class methodsFor:'documentation'!

version
    ^ '$Id: Tools__BreakpointService.st 7856 2012-01-30 20:09:58Z vranyj1 $'
!

version_CVS
    ^ '§Header: /cvs/stx/stx/libtool/Tools__BreakpointService.st,v 1.11 2012/01/27 13:08:07 cg Exp §'
!

version_SVN
    ^ '$Id: Tools__BreakpointService.st 7856 2012-01-30 20:09:58Z vranyj1 $'
! !