Skip to content

Commit

Permalink
Refaactoring the appliers. Move the appliying logic to the appliers. …
Browse files Browse the repository at this point in the history
…Cleaning the api
  • Loading branch information
jordanmontt committed Nov 6, 2022
1 parent ab026b9 commit 1affa2c
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 92 deletions.
10 changes: 5 additions & 5 deletions src/RewriteRuleTools-Tests/MatchToolPresenter.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ Extension { #name : #MatchToolPresenter }
{ #category : #'*RewriteRuleTools-Tests' }
MatchToolPresenter >> astCode [

"This accessor should ONLY be called in the tests. This is for not break encapsulation."
"This accessor should ONLY be called in the tests."

^ astCode
]

{ #category : #'*RewriteRuleTools-Tests' }
MatchToolPresenter >> astPattern [

"This accessor should ONLY be called in the tests. This is for not break encapsulation."
"This accessor should ONLY be called in the tests."

^ astPattern
]

{ #category : #'*RewriteRuleTools-Tests' }
MatchToolPresenter >> bindingsTable [

"This accessor should ONLY be called in the tests. This is for not break encapsulation."
"This accessor should ONLY be called in the tests."

^ bindingsTable
]

{ #category : #'*RewriteRuleTools-Tests' }
MatchToolPresenter >> codeEditor [

"This accessor should ONLY be called in the tests. This is for not break encapsulation."
"This accessor should ONLY be called in the tests."

^ codeEditor
]
Expand All @@ -51,7 +51,7 @@ MatchToolPresenter >> methodCheckbox [
{ #category : #'*RewriteRuleTools-Tests' }
MatchToolPresenter >> ruleEditor [

"This accessor should ONLY be called in the tests. This is for not break encapsulation."
"This accessor should ONLY be called in the tests."

^ ruleEditor
]
4 changes: 2 additions & 2 deletions src/RewriteRuleTools-Tests/RTChangesBrowserTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ RTChangesBrowserTest >> getChangesArray: aClassName [
`@codeBlock'.

changes := RTAbstractApplier defaultEngineApplier
changesOf: ((self class environment classNamed: aClassName) methods)
forRules: { rule }.
calculateChangesForClasses: ((self class environment classNamed: aClassName) methods)
transformationRules: { rule }.

^ changes
]
Expand Down
8 changes: 5 additions & 3 deletions src/RewriteRuleTools/ApplyRuleOnAllClassesCommand.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ ApplyRuleOnAllClassesCommand class >> iconName [
{ #category : #executing }
ApplyRuleOnAllClassesCommand >> execute [

| changes |
| changes ruleHolder |
(UIManager default
confirm: 'This process can take between 1 and 2 minutes.'
label: 'Proceed?')
ifFalse: [ ^ self ].
ruleHolder := RTAbstractApplier
createRuleHolder: self context lhs -> self context rhs
isforMethod: self context isForMethod.

changes := RTAbstractApplier defaultEngineApplier
changesToAllClassesAssociation: self context lhs -> self context rhs
isForMethod: self context isRuleForMethod.
calculateAllChangesForRules: ruleHolder asOrderedCollection.
^ (RTChangesBrowser changes: changes) open
]
11 changes: 8 additions & 3 deletions src/RewriteRuleTools/ApplyRuleOnSelectedClassesCommand.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ ApplyRuleOnSelectedClassesCommand class >> iconName [
ApplyRuleOnSelectedClassesCommand >> execute [

| dialogWindow changes |
self flag: 'Need to refactor this. the message scopeSelectorPresenter est specifique à RTRuleEditorPresenter.'.
dialogWindow := self context scopeSelectorPresenter openAsDialog.
dialogWindow
okAction: [
| ruleHolder |
ruleHolder := RTAbstractApplier
createRuleHolder: self context lhs -> self context rhs
isforMethod: self context isForMethod.

changes := RTAbstractApplier defaultEngineApplier
changesToClasses: self context scopeSelectorPresenter selectedClasses
association: self context lhs -> self context rhs
isForMethod: self context isRuleForMethod.
calculateChangesForClasses: self context scopeSelectorPresenter selectedClasses
transformationRules: ruleHolder asOrderedCollection.
(RTChangesBrowser changes: changes) open ]


Expand Down
3 changes: 1 addition & 2 deletions src/RewriteRuleTools/MatchToolPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ MatchToolPresenter >> selectedMatchChanged: selection [
bindingsTable items: #( ).
codeEditor clearSelection ]
ifNotNil: [
bindingsTable items:
(self getBindingsItemsForMatch: selectedMatch value).
bindingsTable items: (self getBindingsItemsForMatch: selectedMatch value).
codeEditor selectionInterval: selectedMatch key sourceInterval ]
]
10 changes: 3 additions & 7 deletions src/RewriteRuleTools/RTChangesBrowser.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ RTChangesBrowser class >> changes: aCollection [
{ #category : #actions }
RTChangesBrowser >> accept [

| refactoringJob |
(self okToChange not or: [ selectedChanges isEmptyOrNil ]) ifTrue: [
UIManager default inform: 'No changes to apply.'.
^ self ].

refactoringJob := [ selectedChanges do: [ :change |
RBRefactoryChangeManager instance performChange: change ] ] asJob.
refactoringJob
title: 'Refactoring';
run.
RTAbstractApplier applyTransformationChanges: selectedChanges.

self closeWindow
]

Expand Down Expand Up @@ -154,10 +150,10 @@ RTChangesBrowser >> selectedChanges [
RTChangesBrowser >> updateChanges [

| aCompositeChange |
self flag: 'This is weird. Think about refactoring'.
aCompositeChange := RBRefactoryChangeManager changeFactory compositeRefactoryChange.
changes do: [ :each | aCompositeChange addChange: each ].

"Later we could filter the shown changes depending on the selected scope"

changesTree roots: (aCompositeChange whatToDisplayIn: self)
]
6 changes: 3 additions & 3 deletions src/RewriteRuleTools/RTCodeTransformerPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ RTCodeTransformerPresenter >> runReplace [
| changes selectedRules |
selectedRules := rulesTablePresenter selectedRules.
changes := RTAbstractApplier defaultEngineApplier
changesOf: methodsSelectorPresenter selectedClasses
forRules: selectedRules.
calculateChangesForClasses: methodsSelectorPresenter selectedClasses
transformationRules: selectedRules.

^ (RTChangesBrowser changes: changes) open
]
Expand All @@ -167,7 +167,7 @@ RTCodeTransformerPresenter >> runReplaceOnAllClasses [

| changes |
changes := RTAbstractApplier defaultEngineApplier
changesOfAllMethodsFor: rulesTablePresenter selectedRules.
calculateAllChangesForRules: rulesTablePresenter selectedRules.

(RTChangesBrowser changes: changes) open
]
3 changes: 2 additions & 1 deletion src/RewriteRuleTools/RTExpressionFinderPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ RTExpressionFinderPresenter >> patternCode: aString [
RTExpressionFinderPresenter >> searchExpression [

| methods ruleHolder critiques |
self flag: 'Need to refactor this we should get the methods from a class not calculate them our selves.'.
methods := (RPackage organizer packages flatCollect: #classes)
flatCollect: #methods.
ruleHolder := RTRuleHolder
Expand All @@ -140,7 +141,7 @@ RTExpressionFinderPresenter >> searchExpressionOnAllClasses [

| dialogWindow methods ruleHolder critiques |
dialogWindow := scopeSelectorPresenter openAsDialog.

self flag: 'This logic should be in the applier class'.
dialogWindow okAction: [
methods := scopeSelectorPresenter selectedClasses
flatCollectAsSet: [ :each | each methods ].
Expand Down
21 changes: 10 additions & 11 deletions src/RewriteRulesRewriter/RTAbstractApplier.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ Class {
}

{ #category : #api }
RTAbstractApplier class >> changesOf: classes forRules: ruleHolderCollection [

^ self subclassResponsibility
]

{ #category : #api }
RTAbstractApplier class >> changesOfAllMethodsFor: ruleHolderCollection [

^ self subclassResponsibility
RTAbstractApplier class >> applyTransformationChanges: changes [

| refactoringJob |
refactoringJob := [
changes do: [ :change | RBRefactoryChangeManager instance performChange: change ] ] asJob.
refactoringJob
title: 'Refactoring';
run
]

{ #category : #api }
RTAbstractApplier class >> changesToAllClassesAssociation: ruleAsAssociation isForMethod: aBoolean [
RTAbstractApplier class >> calculateAllChangesForRules: ruleHolderCollection [

^ self subclassResponsibility
]

{ #category : #api }
RTAbstractApplier class >> changesToClasses: classes association: ruleAsAssociation isForMethod: aBoolean [
RTAbstractApplier class >> calculateChangesForClasses: classes transformationRules: ruleHolderCollection [

^ self subclassResponsibility
]
Expand Down
31 changes: 4 additions & 27 deletions src/RewriteRulesRewriter/RTRBApplier.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@ Class {
}

{ #category : #api }
RTRBApplier class >> changesOf: classes forRules: ruleHolderCollection [

| env rewriter |
env := RTEnvironmentCreator scopeEnvironmentForClasses: classes.
rewriter := self rewriter: ruleHolderCollection.

^ self obtainChanges: env rewriter: rewriter
]

{ #category : #api }
RTRBApplier class >> changesOfAllMethodsFor: ruleHolderCollection [
RTRBApplier class >> calculateAllChangesForRules: ruleHolderCollection [

| env rewriter |
env := RTEnvironmentCreator scopeEnvironmentForAllPackages.
Expand All @@ -28,23 +18,11 @@ RTRBApplier class >> changesOfAllMethodsFor: ruleHolderCollection [
]

{ #category : #api }
RTRBApplier class >> changesToAllClassesAssociation: ruleAsAssociation isForMethod: aBoolean [
RTRBApplier class >> calculateChangesForClasses: classes transformationRules: ruleHolderCollection [

| ruleHolder env rewriter |
env := RTEnvironmentCreator scopeEnvironmentForAllPackages.
ruleHolder := self createRuleHolder: ruleAsAssociation isforMethod: aBoolean.
rewriter := self rewriter: { ruleHolder }.

^ self obtainChanges: env rewriter: rewriter
]

{ #category : #api }
RTRBApplier class >> changesToClasses: classes association: ruleAsAssociation isForMethod: aBoolean [

| ruleHolder env rewriter |
| env rewriter |
env := RTEnvironmentCreator scopeEnvironmentForClasses: classes.
ruleHolder := self createRuleHolder: ruleAsAssociation isforMethod: aBoolean.
rewriter := self rewriter: { ruleHolder }.
rewriter := self rewriter: ruleHolderCollection.

^ self obtainChanges: env rewriter: rewriter
]
Expand All @@ -61,7 +39,6 @@ RTRBApplier class >> obtainChanges: env rewriter: rewriter [
rule: transformationRule;
environment: env.
checker run.

^ transformationRule changes
]

Expand Down
37 changes: 9 additions & 28 deletions src/RewriteRulesRewriter/RTRenrakuApplier.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,24 @@ Class {
}

{ #category : #api }
RTRenrakuApplier class >> changesOf: methods forRules: rules [

| critiques changes |
critiques := self obtainCritiquesOf: methods forRules: rules.
"At this point you have a collection of critiques. Each critique can tell you which rule created it, and which target it criticizes.
As the critiques are 'smart', the type you have here (node replace critiques) can give you change compatible with the ChangesBrowser or RewriteChangesBrowser."
changes := critiques collect: [: each | each change].
^ changes
]

{ #category : #api }
RTRenrakuApplier class >> changesOfAllMethodsFor: aRulesCollection [
RTRenrakuApplier class >> calculateAllChangesForRules: aRulesCollection [

| allSystemMethods |
allSystemMethods := (RPackage organizer packages flatCollect: [:each | each classes])
flatCollect: [ :each | each methods].

^ self changesOf: allSystemMethods asSet forRules: aRulesCollection
]

{ #category : #api }
RTRenrakuApplier class >> changesToAllClassesAssociation: ruleAsAssociation isForMethod: aBoolean [

| ruleHolder |
ruleHolder := self createRuleHolder: ruleAsAssociation isforMethod: aBoolean.

^ self changesOfAllMethodsFor: { ruleHolder }
^ self calculateChangesForClasses: allSystemMethods asSet transformationRules: aRulesCollection
]

{ #category : #api }
RTRenrakuApplier class >> changesToClasses: classes association: ruleAsAssociation isForMethod: aBoolean [

| ruleHolder methodsInTheClasses |
ruleHolder := self createRuleHolder: ruleAsAssociation isforMethod: aBoolean.
methodsInTheClasses := (classes collect: [ :each | each methods ]) asSet.
RTRenrakuApplier class >> calculateChangesForClasses: methods transformationRules: rules [

^ self changesOf: methodsInTheClasses forRules: { ruleHolder }
| critiques changes |
critiques := self obtainCritiquesOf: methods forRules: rules.
"At this point you have a collection of critiques. Each critique can tell you which rule created it, and which target it criticizes.
As the critiques are 'smart', the type you have here (node replace critiques) can give you change compatible with the ChangesBrowser or RewriteChangesBrowser."
changes := critiques collect: [: each | each change].
^ changes
]

{ #category : #private }
Expand Down

0 comments on commit 1affa2c

Please sign in to comment.