-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeEditor.py
213 lines (163 loc) · 7.21 KB
/
CodeEditor.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import iop, screen
import buffer, Editors
import reader, eval, misc
from reader import Symbol
class CodeEditor(Editors.TreeEditor):
def __init__(self, aBuffer=None, zippedNodes={}):
super(CodeEditor, self).__init__(aBuffer, zippedNodes)
self.statusDescription = reader.Symbol('CodeEditor')
self.env = eval.global_env
self.vars = None
self.context = None
self.parent = None
self.printingMode = 'code'
self.printingModeOptions = ['code', 'horizontal', 'vertical']
self.evalCursorMode = 'active'
self.evalCursorModeOptions = ['active', 'disabled']
self.nodeValues = {}
@classmethod
def fromBufferParts(cls, root, viewAdd=[0], cursorAdd=[0], zippedNodes={}):
newBuffer = buffer.BufferSexp(root, viewAdd, cursorAdd)
return cls(newBuffer, zippedNodes)
def storeNodeValue(self, node, val, env=None):
self.nodeValues[node] = (val, env)
def getNodeValue(self, node):
if node in self.nodeValues:
val, env = self.nodeValues[node]
return val
def hasNodeValue(self, node):
return True if node in self.nodeValues else False
def getNodeEnv(self, node):
if node in self.nodeValues:
val, env = self.nodeValues[node]
return env
def evalBuffer(self):
eval.eval(buffer.BufferSexp(self.buffer.root), self.env, self.storeNodeValue)
def syncWithImage(self, newImageRoot):
if newImageRoot != self.buffer.root:
# special case when we have a newly created buffer
if self.buffer.emptyBuffer():
newBuffer = self.buffer.indexNewRoot(newImageRoot)
else:
newBuffer = self.buffer.syncToNewRoot(newImageRoot)
newSelf = self.updateBuffer(newBuffer)
newSelf.evalBuffer()
return newSelf
else:
return self
def status(self):
return [self.statusDescription, self.buffer.viewAdd, self.buffer.cursorAdd,
[Symbol('nodeID'), self.buffer.cursor.nodeID],
[Symbol('=>'), self.getNodeValue(self.buffer.cursor)]]
# a bit of a hack, necessary because everything is handled in handleKeys. We need to make sure that
# the codeEditor returns with a newly evaluated buffer if there were any significant changes.
def handleKeys(self, key):
result = self.handleKeysInitial(key)
if result != 'UNDO' and result.updateUndo:
result.evalBuffer() # updating imperatively
return result
def handleKeysMain(self, key):
# evaluate the current context
if key.code == iop.KEY_ENTER and not self.editing:
nodeValue = self.getNodeValue(self.buffer.cursor)
if key.shift():
result = self.updateList(
('buffer', self.buffer.appendAtCursor(nodeValue).curNext()),
('yankBuffer', nodeValue),
('updateUndo', True))
else:
result = self.updateList(
('yankBuffer', nodeValue),
('_message', "Result to buffer"))
elif key.code == iop.KEY_F2:
newCursorMode = misc.cycleThroughList(self.evalCursorMode, self.evalCursorModeOptions)
result = self.update('evalCursorMode', newCursorMode)
# Create a repl like environment to evaluate code in
elif key.code == iop.KEY_F8:
newBuff = self.buffer.curChild().curLast()
newBuff = newBuff.viewToCursor().curChild().curLast()
self.printingMode = 'vertical'
self.topLine = 0
return self.update('buffer', newBuff)
# elif key.char == '>':
# curNode = self.buffer.cursor
# if curNode.isSubNode():
# args = []
# if curNode.child.next:
# for i in curNode.child.next:
# args.append(self.nodeValues[i])
#
# (newTree, env) = self.nodeValues[curNode.child]('inspect', *args)
#
# newEd = InspectionEditor(newTree.root, newTree.rootToCursorAdd(),
# zippedNodes=self.zippedNodes)
#
# newEd.context = self.buffer
# newEd.contextParent = self.id # not really needed?
# newEd.env = env
# newEd.evalBuffer()
# return newEd
# else:
# return self
else:
result = super(CodeEditor, self).handleKeysMain(key)
return result
class InspectionEditor(CodeEditor):
def __init__(self, *args, **kwargs):
super(InspectionEditor, self).__init__(*args, **kwargs)
self.statusDescription = reader.Symbol('InspectionEditor')
def evalBuffer(self):
eval.eval(buffer.BufferSexp(self.buffer.root, self.buffer.viewAdd), self.env, self.storeNodeValue)
def status(self):
return [self.statusDescription,
[[Symbol(key), value] for key, value in self.env.items()],
[Symbol('='), self.getNodeValue(self.buffer.cursor)]]
class ProgInspectionEditor(InspectionEditor):
def __init__(self, *args, **kwargs):
super(InspectionEditor, self).__init__(*args, **kwargs)
self.statusDescription = reader.Symbol('InspectionEditor')
self.progID = None
#def syncArgsToProgInput(self, input):
# self.env = eval.Env(self.env.vars, )
class evalIOHandler(CodeEditor):
def __init__(self, aBuffer=None):
super(evalIOHandler, self).__init__(aBuffer)
self.keyHistory = []
self.lastKey = 0
self.output = ''
self.evalBuffer()
self.mode = 'prog'
self.function = self.getNodeValue(self.buffer.cursor)
def handleKeysProg(self, key):
if key.isPrintable():
self.keyHistory.append(key.char)
self.lastKey = key.char
handleKeysMethod = self.function.call('handleKeys')
self.function = handleKeysMethod.call(self.lastKey)
return self
def handleKeys(self, key):
if key.code == iop.KEY_TAB:
self.mode = 'inspect' if self.mode == 'prog' else 'prog'
elif self.mode == 'prog':
return self.handleKeysProg(key)
else:
return super(evalIOHandler, self).handleKeys(key)
def handleMouse(self, mouse):
return self
def drawProg(self, maxx, maxy, isActive=False):
if self.lastKey != 0:
if hasattr(self.function, 'call'):
drawFunc = self.function.call('draw')
self.output = drawFunc.call(0, 0)
else:
self.output = "Not a function" #ProgException(self.function, "Not a Function")
return screen.stringToImage(self.output, maxx, maxy, self.colourScheme.bgCol, self.colourScheme.identifierCol)
def draw(self, maxx, maxy, isActive=False):
if self.mode == 'prog':
return self.drawProg(maxx, maxy)
else:
return super(evalIOHandler, self).draw(maxx, maxy, isActive)
def status(self):
return [self.statusDescription,
[[Symbol(key), value] for key, value in self.env.items()],
[Symbol('='), self.getNodeValue(self.buffer.cursor)]]