SmallSense__SmalltalkQuickFixer.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 18 Sep 2013 01:30:51 +0100
changeset 92 67592095147e
parent 67 020b7461b15e
child 97 062cd1535107
permissions -rw-r--r--
Improvement in QuickFixer - if the fix is local to the method, only update codeview's contents.

"{ Package: 'jv:smallsense' }"

"{ NameSpace: SmallSense }"

Object subclass:#SmalltalkQuickFixer
	instanceVariableNames:'view rule fixes'
	classVariableNames:''
	poolDictionaries:''
	category:'SmallSense-Smalltalk-Lint'
!

Object subclass:#QuickFix
	instanceVariableNames:'rule label action'
	classVariableNames:''
	poolDictionaries:''
	privateIn:SmalltalkQuickFixer
!


!SmalltalkQuickFixer class methodsFor:'instance creation'!

for: rule

    ^self new initializeForRule: rule.

    "Created: / 01-02-2012 / 12:06:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

for: rule view: view

    ^self new initializeForRule: rule view: view

    "Created: / 17-02-2012 / 00:21:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkQuickFixer methodsFor:'adding'!

fix
    "Create and return new quick fix. Caller MUST set its 
     label and action"

    | fix |

    fix := QuickFix new.
    fix rule: rule.
    fixes add: fix.
    ^fix

    "Created: / 01-02-2012 / 10:51:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkQuickFixer methodsFor:'initialization'!

initializeForRule: anRBLintRule view: aCodeView2
    rule := anRBLintRule.
    view := aCodeView2.
    fixes := OrderedCollection new: 3.
    rule fixes: self.

    "Created: / 17-02-2012 / 00:20:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkQuickFixer methodsFor:'performing fixes'!

performFix: index

    index <= fixes size ifTrue:[
        (fixes at: index) performFix.
    ] ifFalse:[
        ^ Dialog warn: ('No such fix (%1)' bindWith: index).
    ]

    "Created: / 16-02-2012 / 14:48:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkQuickFixer methodsFor:'printing & storing'!

printHtmlOn: html

    fixes isEmptyOrNil ifTrue:[ ^ self ].
    fixes size > 1 ifTrue:[
        html nextPutAll: '<br>'.
        html nextPutLine: 'Possible fixes'.
    ].
    html nextPutLine:'<ul>'.
    fixes withIndexDo:[:fix :index|
        html 
            nextPutAll:'<li><a action="doit: self doQuickFix:';
            nextPutAll: index printString;
            nextPutAll:'">';
            nextPutAll: fix label;
            nextPutAll:'</a></li>'.
    ].
    html nextPutLine:'</ul>'.

    "Created: / 01-02-2012 / 12:13:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkQuickFixer methodsFor:'utilities'!

apply: allChanges

    allChanges isEmptyOrNil ifTrue:[ ^ self ].
    (allChanges size == 1 and:[allChanges anElement isMethodCodeChange]) ifTrue:[
        | oldSource newSource |

        oldSource := view contents asString string.
        newSource := allChanges anElement source asString.
        oldSource ~= newSource ifTrue:[
            view list: newSource asStringCollection.
            view modified: true.
            view isCodeView2 ifTrue:[
                view reallyModifiedChannel value: true.
            ]
        ].
    ] ifFalse:[
        | browser |

       browser := view application topApplication.
       (browser respondsTo: #performRefactoring:) ifTrue:[
           browser performRefactoring: allChanges.
       ] ifFalse:[
           self breakPoint: #jv.
           Dialog warn: 'Sorry, this application does not support quick fixes'
       ]
    ]

    "Created: / 16-02-2012 / 14:46:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 07-09-2013 / 00:36:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SmalltalkQuickFixer::QuickFix methodsFor:'accessing'!

action
    ^ action
!

action:aBlock
    action := aBlock.
!

label
    ^ label
!

label:aString
    label := aString.
!

performFix

    ^action value

    "Created: / 16-02-2012 / 14:48:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

rule
    ^ rule
!

rule:anRBLintRule
    rule := anRBLintRule.
! !

!SmalltalkQuickFixer class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '$Id: SmallSenseQuickFixer.st 7911 2012-02-22 09:55:48Z vranyj1 $'
! !