Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Revert "ExtractTempDriver"" #17461

Open
wants to merge 1 commit into
base: Pharo13
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions src/Refactoring-Core/RBExtractToTemporaryRefactoring.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ Class {
#tag : 'Refactorings-Unused'
}

{ #category : 'instance creation' }
RBExtractToTemporaryRefactoring class >> extract: anInterval from: aSelector in: aClass [
^ self new
extract: anInterval
from: aSelector
in: aClass
]

{ #category : 'instance creation' }
RBExtractToTemporaryRefactoring class >> extract: anInterval to: aString from: aSelector in: aClass [
^ self new
Expand All @@ -29,26 +37,22 @@ RBExtractToTemporaryRefactoring class >> extract: anInterval to: aString from: a
]

{ #category : 'instance creation' }
RBExtractToTemporaryRefactoring class >> model: aRBSmalltalk extract: anInterval to: aString from: aSelector in: aClass [
RBExtractToTemporaryRefactoring class >> model: aRBSmalltalk extract: anInterval from: aSelector in: aClass [
^ self new
model: aRBSmalltalk;
extract: anInterval
to: aString
from: aSelector
in: aClass;
from: aSelector
in: aClass;
yourself
]

{ #category : 'preconditions' }
RBExtractToTemporaryRefactoring >> applicabilityPreconditions [

^ {
(ReDefinesSelectorsCondition new definesSelectors: (Array with: selector) in: class).
(ReIsValidInstanceVariableName new name: newVariableName).
(ReCheckVariableNameCondition
class: class
variableName: newVariableName
parseTree: parseTree ).
(ReDefinesSelectorsCondition new definesSelectors: { selector } in: class).
self preconditionValidInstVarName.
self preconditionCheckVariableName
}
]

Expand All @@ -67,14 +71,26 @@ RBExtractToTemporaryRefactoring >> constructAssignmentFrom: aNode [
^ASTAssignmentNode variable: valueNode value: aNode copy.
]

{ #category : 'initialization' }
RBExtractToTemporaryRefactoring >> extract: anInterval to: aString from: aSelector in: aClass [
{ #category : 'instance creation' }
RBExtractToTemporaryRefactoring >> extract: anInterval from: aSelector in: aClass [
class := self classObjectFor: aClass.
selector := aSelector.
sourceInterval := anInterval.
]

{ #category : 'instance creation' }
RBExtractToTemporaryRefactoring >> extract: anInterval to: aString from: aSelector in: aClass [
class := self classObjectFor: aClass.
selector := aSelector.
sourceInterval := anInterval.
newVariableName := aString
]

{ #category : 'initialization' }
RBExtractToTemporaryRefactoring >> extractToVariable: aString [
newVariableName := aString
]

{ #category : 'transforming' }
RBExtractToTemporaryRefactoring >> insertTemporary [

Expand All @@ -95,6 +111,26 @@ RBExtractToTemporaryRefactoring >> insertTemporary [
each replaceWith: (ASTVariableNode named: newVariableName) ]
]

{ #category : 'accessing' }
RBExtractToTemporaryRefactoring >> parseTree [
^ parseTree
]

{ #category : 'preconditions' }
RBExtractToTemporaryRefactoring >> preconditionCheckVariableName [

^ ReCheckVariableNameCondition
class: class
variableName: newVariableName
parseTree: parseTree
]

{ #category : 'preconditions' }
RBExtractToTemporaryRefactoring >> preconditionValidInstVarName [

^ ReIsValidInstanceVariableName new name: newVariableName
]

{ #category : 'transforming' }
RBExtractToTemporaryRefactoring >> prepareForExecution [

Expand All @@ -107,11 +143,8 @@ RBExtractToTemporaryRefactoring >> prepareForExecution [
ifTrue: [ self refactoringError: 'Cannot assign temp to multiple statements' ].
parseTree ifNil: [
parseTree := class parseTreeForSelector: selector.
parseTree ifNil: [ self refactoringError: 'Could not parse method' ]
].
parseTree doSemanticAnalysis


parseTree ifNil: [ self refactoringError: 'Could not parse method' ]].
parseTree doSemanticAnalysis
]

{ #category : 'transforming' }
Expand Down
2 changes: 1 addition & 1 deletion src/Refactoring-Core/RBInlineTemporaryRefactoring.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ RBInlineTemporaryRefactoring class >> model: aRBSmalltalk inline: anInterval fro
RBInlineTemporaryRefactoring >> applicabilityPreconditions [

^ {
(ReDefinesSelectorsCondition new definesSelectors: (Array with: selector) in: class).
(ReDefinesSelectorsCondition new definesSelectors: { selector } in: class).
(ReIsValidInstanceVariableName new name: assignmentNode variable name).
(ReSingleAssignmentCondition new
variableName: assignmentNode variable name; definingNode: sourceTree).
Expand Down
102 changes: 102 additions & 0 deletions src/Refactoring-UI-Tests/ReExtractToTemporaryDriverTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
Class {
#name : 'ReExtractToTemporaryDriverTest',
#superclass : 'ReDriverTest',
#category : 'Refactoring-UI-Tests-Driver',
#package : 'Refactoring-UI-Tests',
#tag : 'Driver'
}

{ #category : 'initialization' }
ReExtractToTemporaryDriverTest >> setUpMocksOn: driver [

| requestClass dialog |
dialog := MockObject new.

dialog
on: #variableName
respond: ('temp').

driver requestDialog: dialog.

requestClass := MockObject new.
requestClass
on: #openOn:
respond: dialog.

driver variableNameEditorPresenterClass: requestClass.
]

{ #category : 'tests' }
ReExtractToTemporaryDriverTest >> testExtractToTemporaryExpectChanges [

| driver driverChanges rbclasses|
rbclasses := RBClassEnvironment classes: { RBClassDataForRefactoringTest }.
driver := ReExtractToTemporaryDriver new
scopes: { rbclasses };
extract: (52 to: 73)
from: #caller2
in: RBClassDataForRefactoringTest.

self setUpDriver: driver.

self setUpMocksOn: driver.

driver runRefactoring.
driverChanges := driver refactoring changes.

self assert: driverChanges changes size equals: 1.
]

{ #category : 'tests' }
ReExtractToTemporaryDriverTest >> testFailureInvalidVariableNameWhenVariableIsAlreadyTemporary [

| driver rbclasses dialog requestClass|
rbclasses := RBClassEnvironment classes: { RBClassDataForRefactoringTest }.
driver := ReExtractToTemporaryDriver new
scopes: { rbclasses };
extract: (51 to: 60)
from: #modifiesCollection
in: RBClassDataForRefactoringTest.

self setUpDriver: driver.

dialog := MockObject new.
dialog
on: #variableName
respond: ('collection').
driver requestDialog: dialog.
requestClass := MockObject new.
requestClass
on: #openOn:
respond: dialog.
driver variableNameEditorPresenterClass: requestClass.

self should: [ driver runRefactoring ] raise: RBRefactoringError.
]

{ #category : 'tests' }
ReExtractToTemporaryDriverTest >> testFailureNonExistantSelector [

| driver rbclasses dialog requestClass |
rbclasses := RBClassEnvironment classes: { RBClassDataForRefactoringTest }.
driver := ReExtractToTemporaryDriver new
scopes: { rbclasses };
extract: (14 to: 23)
from: #checkClass1:
in: RBClassDataForRefactoringTest.

self setUpDriver: driver.

dialog := MockObject new.
dialog
on: #variableName
respond: ('asdf').
driver requestDialog: dialog.
requestClass := MockObject new.
requestClass
on: #openOn:
respond: dialog.
driver variableNameEditorPresenterClass: requestClass.

self should: [ driver runRefactoring ] raise: RBRefactoringError.
]
114 changes: 114 additions & 0 deletions src/Refactoring-UI/ReExtractToTemporaryDriver.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
Class {
#name : 'ReExtractToTemporaryDriver',
#superclass : 'RBInteractionDriver',
#instVars : [
'sourceInterval',
'newVariableName',
'selector',
'class',
'shouldEscape',
'variableNameEditorPresenterClass'
],
#category : 'Refactoring-UI-Drivers',
#package : 'Refactoring-UI',
#tag : 'Drivers'
}

{ #category : 'execution' }
ReExtractToTemporaryDriver >> changes [

^ refactoring privateTransform; changes

]

{ #category : 'resources' }
ReExtractToTemporaryDriver >> configureRefactoring [

refactoring := RBExtractToTemporaryRefactoring
model: model
extract: sourceInterval
from: selector
in: class
]

{ #category : 'instance creation' }
ReExtractToTemporaryDriver >> extract: anInterval from: aSelector in: aClass [

class := RBNamespace new classObjectFor: aClass.
selector := aSelector.
sourceInterval := anInterval.
]

{ #category : 'initialization' }
ReExtractToTemporaryDriver >> initialize [

super initialize.
shouldEscape := false.
]

{ #category : 'accessing' }
ReExtractToTemporaryDriver >> requestDialogWith: variableName [

^ requestDialog := (self variableNameEditorPresenterClass
openOn: variableName ) presenter.
]

{ #category : 'execution' }
ReExtractToTemporaryDriver >> requestVariableName [

| dialog variableName|
variableName := 'enter a variable name'.
dialog := self requestDialogWith: variableName.
dialog ifNil: [ shouldEscape := true. ^ self].
^ dialog
]

{ #category : 'execution' }
ReExtractToTemporaryDriver >> runRefactoring [

| checkVariableName checkVariableDefinition messageStream dialog |

self configureRefactoring.
refactoring prepareForExecution.

[
dialog := self requestVariableName.
newVariableName := dialog variableName.
shouldEscape ifTrue: [ ^ self ].
refactoring extractToVariable: newVariableName.

checkVariableName := refactoring preconditionValidInstVarName.
checkVariableDefinition := refactoring preconditionCheckVariableName.
messageStream := WriteStream on: String new.

checkVariableName := refactoring preconditionValidInstVarName.
checkVariableName check ifFalse: [
checkVariableName violationMessageOn: messageStream.
dialog showError: messageStream contents. false ]
ifTrue: [
checkVariableDefinition check ifFalse: [
checkVariableDefinition violationMessageOn: messageStream.
dialog showError: messageStream contents. false ]
ifTrue: [ true ]].
] whileFalse.

refactoring checkPreconditions.
self applyChanges.
]

{ #category : 'accessing' }
ReExtractToTemporaryDriver >> scopes: aCollection [
scopes := aCollection.
model := self refactoringScopeOn: scopes first
]

{ #category : 'accessing' }
ReExtractToTemporaryDriver >> variableNameEditorPresenterClass [
^ variableNameEditorPresenterClass ifNil: [ variableNameEditorPresenterClass := StVariableNameEditorPresenter ]

]

{ #category : 'accessing' }
ReExtractToTemporaryDriver >> variableNameEditorPresenterClass: aClass [
variableNameEditorPresenterClass := aClass
]
Loading