-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGtPythonCoderModel.class.st
More file actions
194 lines (163 loc) · 6.33 KB
/
Copy pathGtPythonCoderModel.class.st
File metadata and controls
194 lines (163 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Class {
#name : #GtPythonCoderModel,
#superclass : #GtSourceCoder,
#instVars : [
'pythonApplicationStrategy',
'pharoBindings'
],
#category : #'Lepiter-Python-Coder'
}
{ #category : #converting }
GtPythonCoderModel >> asCoderViewModel [
^ GtPythonCoderViewModel new coder: self
]
{ #category : #'private - execution' }
GtPythonCoderModel >> bindAndExecute: sourceString inContext: aGtSourceCoderEvaluationContext [
"Answer the source code with all declared variables returned in an immediate dictionary"
| trimmedSource ast commandFactory lastStatement pythonBindings application ws startPosition varNodeVisitor returnBindings |
trimmedSource := SmaCCString on: sourceString.
ast := PythonParser parseWithErrors: sourceString.
varNodeVisitor := GtPythonVarNodeVisitor new.
varNodeVisitor accept: ast.
startPosition := ast statements first startPosition.
ws := trimmedSource startOfLineWhitespaceBeforeIndex: startPosition.
trimmedSource insert: 'self = ' at: startPosition.
trimmedSource
insert: (aGtSourceCoderEvaluationContext requesterObject selfObject
ifNil: [ 'None' ]
ifNotNil: [ :obj |
(obj isKindOf: PBProxyObject)
ifTrue: [ obj resolveIdPyExpression generate ]
ifFalse: [ 'None' ] ])
at: startPosition.
trimmedSource insert: String cr , ws at: startPosition.
"Assign the final statement to snippetResult if it isn't already an assignment"
lastStatement := ast statements last.
lastStatement isValueStatement
ifTrue: [ trimmedSource insert: 'snippetResult = ' at: lastStatement startPosition ]
ifFalse: [ trimmedSource append: String cr; append: 'snippetResult = None' ].
pythonBindings := self pythonBindings.
application := pythonApplicationStrategy applicationServer.
application isRunning ifFalse: [ application start ].
commandFactory := application newCommandStringFactory.
"Only bind when read"
pythonBindings do: [ :binding |
(varNodeVisitor accessedFirstVariableNames includes: binding key)
ifTrue: [ commandFactory addBinding: binding ] ].
"Only bind when written"
returnBindings := pythonBindings select: [ :binding |
varNodeVisitor assignedVariableNames includes: binding key ].
commandFactory
script: trimmedSource asString;
resultExpression: (self returnVariablesExpressionFrom: returnBindings) string.
^ commandFactory sendAndWait
]
{ #category : #'api - ast' }
GtPythonCoderModel >> computeAst: theSourceString [
^ PythonParser
parseWithErrors: theSourceString
startingAt: PythonParser startingStateForfile_input
]
{ #category : #'api - ast' }
GtPythonCoderModel >> implicitVariableReferencesTo: aString do: aBlock [
self astSync withAllNodesOfType: PyVariableExpressionNode do: [ :node |
node nameToken source = aString ifTrue: [ aBlock value: node ] ]
]
{ #category : #initialize }
GtPythonCoderModel >> initializeAddOns: addOns [
super initializeAddOns: addOns.
addOns addStyler: (GtCoderAstSmaCCParserStyler new smaccStyler: PythonParser gtStyler).
addOns
addMainAction: 'Evaluate' translated
icon: BrGlamorousVectorIcons play
action: [ :aCoderUIModel :anElement |
GtCoderCodeExecutor doIt
coderViewModel: aCoderUIModel;
element: anElement;
execute ]
id: GtSourceCoderDoItActionId.
addOns
addMainAction: 'Inspect' translated
icon: BrGlamorousVectorIcons playinspect
action: [ :aCoderUIModel :anElement |
GtCoderCodeExecutor doItAndGo
coderViewModel: aCoderUIModel;
element: anElement;
execute ]
id: GtSourceCoderDoItAndGoActionId.
]
{ #category : #initialize }
GtPythonCoderModel >> initializeShortcuts: addOns [
super initializeShortcuts: addOns.
addOns
addShortcut: GtSourceCoderDoItShortcut new;
addShortcut: GtSourceCoderDoItAndInspectShortcut new
]
{ #category : #'instance creation' }
GtPythonCoderModel >> newCompletionStrategy [
^ GtCompletionStrategy new
]
{ #category : #accessing }
GtPythonCoderModel >> pharoBindings [
^ pharoBindings
]
{ #category : #accessing }
GtPythonCoderModel >> pharoBindings: anObject [
pharoBindings := anObject
]
{ #category : #'private - actions' }
GtPythonCoderModel >> primitiveDebug: aSourceString inContext: aGtSourceCoderEvaluationContext onFailDo: anEvaluationFailBlock [
self flag: #TODO.
]
{ #category : #'private - actions' }
GtPythonCoderModel >> primitiveEvaluate: aSourceString inContext: aGtSourceCoderEvaluationContext onFailDo: anEvaluationFailBlock [
| result remoteValue |
result := self
bindAndExecute: aSourceString
inContext: aGtSourceCoderEvaluationContext.
result associationsDo: [ :binding |
remoteValue := binding value.
"try to unwrap all but snippetResult to primitive types"
binding key = 'snippetResult'
ifFalse: [ remoteValue := remoteValue unwrapPrimitiveTypes ].
(pharoBindings bindingOf: binding key asSymbol) value: remoteValue ].
^ result at: 'snippetResult' ifAbsent: anEvaluationFailBlock
]
{ #category : #accessing }
GtPythonCoderModel >> pythonApplicationStrategy [
^ pythonApplicationStrategy
]
{ #category : #accessing }
GtPythonCoderModel >> pythonApplicationStrategy: anObject [
pythonApplicationStrategy := anObject
]
{ #category : #'private - execution' }
GtPythonCoderModel >> pythonBindings [
"Answer a collection of python bindings.
This is basically any snippet binding that can be passed in,
but always excluding thisSnippet and thisSnippetElement
while snippetResult is set to nil (None)"
| pythonBindings canBind |
pythonBindings := OrderedCollection new.
(pharoBindings bindingOf: #snippetResult) value: nil.
pharoBindings asDictionary keysAndValuesDo: [ :key :value |
(#(thisSnippet thisSnippetElement) includes: key) ifFalse: [
canBind := [ MpEncoder encode: value value. true ]
on: Error do: [ false ].
canBind ifTrue: [ pythonBindings add: (PBBinding key: key wrap: value value) ] ] ].
^ pythonBindings
]
{ #category : #private }
GtPythonCoderModel >> returnVariablesExpressionFrom: pythonBindings [
"Answer the LeUninterpretedPythonStatement that returns all the bindings"
| bindingNames expressionString |
bindingNames := pythonBindings collect: [ :binding | binding key ] as: Set.
bindingNames add: #snippetResult.
expressionString := String streamContents: [ :stream |
stream << '{ '.
bindingNames
do: [ :name | stream << '"'; << name; << '": registry().proxy('; << name; << ')' ]
separatedBy: [ stream << ', ' ].
stream << ' }' ].
^ LeUninterpretedPythonStatement new string: expressionString
]