Tools__BreakpointService.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 30 Nov 2012 17:23:39 +0000
branchjv
changeset 12308 5d9291c0fc27
parent 12287 400a99059170
child 12401 4714b9640528
permissions -rw-r--r--
Merged with /trunk

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

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 position:pos line:(bpnt line + delta). 
        ]
    ].

    "/gutterView redrawLinesFrom: line.

    "Created: / 17-06-2011 / 13:45:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Created: / 06-07-2011 / 17:26:30 / jv"
    "Modified: / 02-08-2012 / 09:27:10 / cg"
!

recompile
    "recompile the current method for changed breakpoints"

    |oldMethod newMethod compiler class selector|

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

            class := oldMethod mclass.
            class isNil ifTrue:[
                class := codeView classHolder value.
            ].
            selector := oldMethod selector.

            Class withoutUpdatingChangesDo:[
                compiler := ByteCodeCompilerWithBreakpointSupport new.
                compiler breakpoints:breakpoints.
                compiler methodClass:MethodWithBreakpoints.
                newMethod := compiler
                            compile:oldMethod source
                            forClass:class
                            inCategory:oldMethod category
                            notifying:nil
                            install:false
                            skipIfSame:false
                            silent:true
                            foldConstants:true
                            ifFail:[ Transcript showCR:'BreakpointService: failed to recompile for breakpoint' ].
                newMethod originalMethod: oldMethod.

                selector isNil ifTrue:[
                    "/ May happen as the selector is not stored in the method but
                    "/ searches through method's mclass methodDictionary.
                    "/ Following should be save as breakpoint is not installed when
                    "/ the code is modified...
                    selector := compiler selector.
                ].

                (class primAddSelector: selector withMethod:newMethod) ifFalse:[
                    self breakPoint: #cg.
                    self breakPoint: #jv.
                    ^ self
                ].
                codeView methodHolder value:newMethod.
                oldMethod mclass isNil ifTrue:[
                    oldMethod mclass:class.
                ].
            ].
"/ 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"
    "Modified: / 18-07-2012 / 10:53:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 02-08-2012 / 09:35:41 / 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: / 02-08-2012 / 09:26:38 / 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.17 2012/11/01 14:18:23 cg Exp §'

!

version_SVN
    ^ '$Id: Tools__BreakpointService.st 8074 2012-11-30 17:23:39Z vranyj1 $'
! !