Skip to content

Commit

Permalink
Introduction of a draft driver + exposing command in menu
Browse files Browse the repository at this point in the history
- logic is just straight path 
- needs a lot of love
  • Loading branch information
Ducasse authored and balsa-sarenac committed Apr 26, 2024
1 parent bb79e77 commit 4227120
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 15 deletions.
150 changes: 150 additions & 0 deletions src/Refactoring-UI/ReExtractMethodDriver.class.st
@@ -0,0 +1,150 @@
Class {
#name : 'ReExtractMethodDriver',
#superclass : 'RBInteractionDriver',
#instVars : [
'class',
'originalMessage',
'newMessage',
'shouldEscape',
'methodNameEditorPresenterClass',
'body',
'sourceMethod',
'newMethod'
],
#category : 'Refactoring-UI-Drivers',
#package : 'Refactoring-UI',
#tag : 'Drivers'
}

{ #category : 'testing' }
ReExtractMethodDriver >> canAddArgs [
^ false
]

{ #category : 'testing' }
ReExtractMethodDriver >> canEditName [
^ true
]

{ #category : 'testing' }
ReExtractMethodDriver >> canRemoveArgs [
^ false
]

{ #category : 'testing' }
ReExtractMethodDriver >> canRenameArgs [
"for now false"
^ false
]

{ #category : 'execution' }
ReExtractMethodDriver >> changes [
"Remember should not call generateChanges"
^ refactoring privateTransform; changes


]

{ #category : 'execution' }
ReExtractMethodDriver >> configureMessage [

refactoring newSelector: newMessage selector.
"refactoring permutation: newMessage permutation.
refactoring renameMap: newMessage renameMap."
]

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

"here we do not fully configure the refactoring because we are missing information such as the signature.
Since we want to let the refactoring validate input we need to create it upfront then configure later."

refactoring := RBExtractMethodTransformation new
extract: body
from: sourceMethod selector
in: class.
refactoring prepareForExecution
]

{ #category : 'instance creation' }
ReExtractMethodDriver >> extract: aString from: compiledMethod in: aClass [

class := aClass.
sourceMethod := compiledMethod.
body := aString
]

{ #category : 'execution' }
ReExtractMethodDriver >> handleBreakingChanges [

| select |
select := self selectDialog.
select ifNotNil: [ select action ]
]

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

super initialize.
shouldEscape := false.
]

{ #category : 'accessing' }
ReExtractMethodDriver >> methodNameEditorPresenterClass [

^ methodNameEditorPresenterClass ifNil: [ methodNameEditorPresenterClass := StMethodNameEditorPresenter]
]

{ #category : 'accessing' }
ReExtractMethodDriver >> methodNameEditorPresenterClass: aClass [

methodNameEditorPresenterClass := aClass
]

{ #category : 'accessing' }
ReExtractMethodDriver >> requestDialogWith: methodName [
"This is lazy loaded and tests expect lazy loading, because they set `requestDialog`
and thus side-step methodNameEditorPresenter"

^ requestDialog ifNil: [
requestDialog := (self methodNameEditorPresenterClass
openOn: methodName
withInvalidArgs: (RBCondition
invalidArgumentNamesForSelector:
sourceMethod selector
in: model)
canRenameArgs: self canRenameArgs
canRemoveArgs: self canRemoveArgs
canAddArgs: self canAddArgs
canEditName: self canEditName) presenter ]
]

{ #category : 'interaction' }
ReExtractMethodDriver >> requestNewMessage [

| methodName dialog |

methodName := RBMethodName
selector: (refactoring selectorStartingFrom: 'f' argumentsSize: refactoring arguments size)
arguments: refactoring arguments.
dialog := self requestDialogWith: methodName.

dialog ifNil: [ shouldEscape := true. ^ self ].

^ dialog methodName
]

{ #category : 'interaction' }
ReExtractMethodDriver >> runRefactoring [

self configureRefactoring.
newMessage := self requestNewMessage.
refactoring newSelector: newMessage selector.
"self configureMessage."
self applyChanges
]

{ #category : 'accessing' }
ReExtractMethodDriver >> scopes: aCollection [
scopes := aCollection
]
Expand Up @@ -7,7 +7,8 @@ Class {
#traits : 'TRefactoringCommandSupport',
#classTraits : 'TRefactoringCommandSupport classTrait',
#instVars : [
'selectedTextInterval'
'selectedTextInterval',
'refactoringScopes'
],
#category : 'SystemCommands-SourceCodeCommands',
#package : 'SystemCommands-SourceCodeCommands'
Expand All @@ -16,7 +17,7 @@ Class {
{ #category : 'testing' }
SycExtractMethodCommand class >> canBeExecutedInContext: aSourceCodeContext [

^ false. "(super canBeExecutedInContext: aSourceCodeContext) and: [
^ true. "(super canBeExecutedInContext: aSourceCodeContext) and: [
aSourceCodeContext isMethodSelected not ]"
]

Expand All @@ -30,19 +31,13 @@ SycExtractMethodCommand class >> methodEditorShortcutActivation [
{ #category : 'converting' }
SycExtractMethodCommand >> asRefactorings [

| selectedInterval refactoring |
selectedInterval := selectedTextInterval ifEmpty: [
sourceNode sourceInterval ].
^{self createRefactoring}
]

refactoring := RBExtractMethodTransformation
extract: (method sourceCode
copyFrom: selectedInterval first
to: selectedInterval last)
from: method selector
to: self getNewSelector selector
in: method origin name.
self setUpOptionsOf: refactoring.
^ { refactoring }
{ #category : 'converting' }
SycExtractMethodCommand >> createRefactoring [
"not used"
^ self
]

{ #category : 'accessing' }
Expand All @@ -53,7 +48,25 @@ SycExtractMethodCommand >> defaultMenuIconName [
{ #category : 'accessing' }
SycExtractMethodCommand >> defaultMenuItemName [

^ 'Extract method'
^ '(T) Extract method'
]

{ #category : 'converting' }
SycExtractMethodCommand >> execute [

| selectedInterval driver |
selectedInterval := selectedTextInterval ifEmpty: [
sourceNode sourceInterval ].
driver := ReExtractMethodDriver new
scopes: refactoringScopes;
model: model;
extract: (method sourceCode
copyFrom: selectedInterval first
to: selectedInterval last)
from: method
in: method origin.
driver runRefactoring.

]

{ #category : 'converting' }
Expand All @@ -74,13 +87,29 @@ SycExtractMethodCommand >> getNewSelector [
^ methodName
]

{ #category : 'testing' }
SycExtractMethodCommand >> isComplexRefactoring [
^ false
]

{ #category : 'execution' }
SycExtractMethodCommand >> readParametersFromContext: aSourceCodeContext [
super readParametersFromContext: aSourceCodeContext.
self setUpModelFromContext: aSourceCodeContext.
selectedTextInterval := aSourceCodeContext selectedTextInterval
]

{ #category : 'initialization' }
SycExtractMethodCommand >> setUpModelFromContext: aContext [
| browser scope |

browser := aContext browser.
scope := browser defaultNavigationScope.
scope isCurrentImage ifTrue: [ scope := ScopesManager defaultScope ].
model := RBNamespace onEnvironment: scope asRBEnvironment.
refactoringScopes := aContext refactoringScopes.
]

{ #category : 'execution' }
SycExtractMethodCommand >> setUpOptionToChangeExtractionClass: refactoring [

Expand Down

0 comments on commit 4227120

Please sign in to comment.