author | Laurent Bessard |
Tue, 26 Mar 2013 00:03:57 +0100 | |
changeset 999 | cbab4c1635bd |
parent 980 | c7ba67d01d65 |
child 1057 | 3837e165b3f9 |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import re |
|
26 |
from types import * |
|
27 |
||
28 |
import wx |
|
29 |
import wx.stc |
|
30 |
||
31 |
from graphics.GraphicCommons import ERROR_HIGHLIGHT, SEARCH_RESULT_HIGHLIGHT, REFRESH_HIGHLIGHT_PERIOD |
|
32 |
from plcopen.structures import ST_BLOCK_START_KEYWORDS, ST_BLOCK_END_KEYWORDS, IEC_BLOCK_START_KEYWORDS, IEC_BLOCK_END_KEYWORDS, LOCATIONDATATYPES |
|
33 |
from EditorPanel import EditorPanel |
|
34 |
||
35 |
#------------------------------------------------------------------------------- |
|
36 |
# Textual programs Viewer class |
|
37 |
#------------------------------------------------------------------------------- |
|
38 |
||
39 |
||
40 |
NEWLINE = "\n" |
|
41 |
NUMBERS = [str(i) for i in xrange(10)] |
|
42 |
LETTERS = ['_'] |
|
43 |
for i in xrange(26): |
|
44 |
LETTERS.append(chr(ord('a') + i)) |
|
45 |
LETTERS.append(chr(ord('A') + i)) |
|
46 |
||
47 |
[STC_PLC_WORD, STC_PLC_COMMENT, STC_PLC_NUMBER, STC_PLC_STRING, |
|
48 |
STC_PLC_VARIABLE, STC_PLC_PARAMETER, STC_PLC_FUNCTION, STC_PLC_JUMP, |
|
49 |
STC_PLC_ERROR, STC_PLC_SEARCH_RESULT] = range(10) |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
50 |
[SPACE, WORD, NUMBER, STRING, WSTRING, COMMENT, PRAGMA, DPRAGMA] = range(8) |
814 | 51 |
|
52 |
[ID_TEXTVIEWER, ID_TEXTVIEWERTEXTCTRL, |
|
53 |
] = [wx.NewId() for _init_ctrls in range(2)] |
|
54 |
||
55 |
if wx.Platform == '__WXMSW__': |
|
56 |
faces = { 'times': 'Times New Roman', |
|
57 |
'mono' : 'Courier New', |
|
58 |
'helv' : 'Arial', |
|
59 |
'other': 'Comic Sans MS', |
|
60 |
'size' : 10, |
|
61 |
} |
|
62 |
else: |
|
63 |
faces = { 'times': 'Times', |
|
64 |
'mono' : 'Courier', |
|
65 |
'helv' : 'Helvetica', |
|
66 |
'other': 'new century schoolbook', |
|
67 |
'size' : 12, |
|
68 |
} |
|
69 |
re_texts = {} |
|
70 |
re_texts["letter"] = "[A-Za-z]" |
|
71 |
re_texts["digit"] = "[0-9]" |
|
72 |
re_texts["identifier"] = "((?:%(letter)s|(?:_(?:%(letter)s|%(digit)s)))(?:_?(?:%(letter)s|%(digit)s))*)"%re_texts |
|
73 |
IDENTIFIER_MODEL = re.compile(re_texts["identifier"]) |
|
74 |
LABEL_MODEL = re.compile("[ \t\n]%(identifier)s:[ \t\n]"%re_texts) |
|
75 |
EXTENSIBLE_PARAMETER = re.compile("IN[1-9][0-9]*$") |
|
76 |
||
77 |
HIGHLIGHT_TYPES = { |
|
78 |
ERROR_HIGHLIGHT: STC_PLC_ERROR, |
|
79 |
SEARCH_RESULT_HIGHLIGHT: STC_PLC_SEARCH_RESULT, |
|
80 |
} |
|
81 |
||
82 |
def GetCursorPos(old, new): |
|
83 |
old_length = len(old) |
|
84 |
new_length = len(new) |
|
85 |
common_length = min(old_length, new_length) |
|
86 |
i = 0 |
|
87 |
for i in xrange(common_length): |
|
88 |
if old[i] != new[i]: |
|
89 |
break |
|
90 |
if old_length < new_length: |
|
91 |
if common_length > 0 and old[i] != new[i]: |
|
92 |
return i + new_length - old_length |
|
93 |
else: |
|
94 |
return i + new_length - old_length + 1 |
|
95 |
elif old_length > new_length or i < min(old_length, new_length) - 1: |
|
96 |
if common_length > 0 and old[i] != new[i]: |
|
97 |
return i |
|
98 |
else: |
|
99 |
return i + 1 |
|
100 |
else: |
|
101 |
return None |
|
102 |
||
103 |
def LineStartswith(line, symbols): |
|
104 |
return reduce(lambda x, y: x or y, map(lambda x: line.startswith(x), symbols), False) |
|
105 |
||
106 |
class TextViewer(EditorPanel): |
|
107 |
||
108 |
ID = ID_TEXTVIEWER |
|
109 |
||
110 |
if wx.VERSION < (2, 6, 0): |
|
111 |
def Bind(self, event, function, id = None): |
|
112 |
if id is not None: |
|
113 |
event(self, id, function) |
|
114 |
else: |
|
115 |
event(self, function) |
|
116 |
||
117 |
def _init_Editor(self, prnt): |
|
118 |
self.Editor = wx.stc.StyledTextCtrl(id=ID_TEXTVIEWERTEXTCTRL, |
|
119 |
parent=prnt, name="TextViewer", size=wx.Size(0, 0), style=0) |
|
120 |
self.Editor.ParentWindow = self |
|
121 |
||
122 |
self.Editor.CmdKeyAssign(ord('+'), wx.stc.STC_SCMOD_CTRL, wx.stc.STC_CMD_ZOOMIN) |
|
123 |
self.Editor.CmdKeyAssign(ord('-'), wx.stc.STC_SCMOD_CTRL, wx.stc.STC_CMD_ZOOMOUT) |
|
124 |
||
125 |
self.Editor.SetViewWhiteSpace(False) |
|
126 |
||
127 |
self.Editor.SetLexer(wx.stc.STC_LEX_CONTAINER) |
|
128 |
||
129 |
# Global default styles for all languages |
|
130 |
self.Editor.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces) |
|
131 |
self.Editor.StyleClearAll() # Reset all to be like the default |
|
132 |
||
133 |
self.Editor.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,size:%(size)d" % faces) |
|
134 |
self.Editor.SetSelBackground(1, "#E0E0E0") |
|
135 |
||
136 |
# Highlighting styles |
|
137 |
self.Editor.StyleSetSpec(STC_PLC_WORD, "fore:#00007F,bold,size:%(size)d" % faces) |
|
138 |
self.Editor.StyleSetSpec(STC_PLC_VARIABLE, "fore:#7F0000,size:%(size)d" % faces) |
|
139 |
self.Editor.StyleSetSpec(STC_PLC_PARAMETER, "fore:#7F007F,size:%(size)d" % faces) |
|
140 |
self.Editor.StyleSetSpec(STC_PLC_FUNCTION, "fore:#7F7F00,size:%(size)d" % faces) |
|
141 |
self.Editor.StyleSetSpec(STC_PLC_COMMENT, "fore:#7F7F7F,size:%(size)d" % faces) |
|
142 |
self.Editor.StyleSetSpec(STC_PLC_NUMBER, "fore:#007F7F,size:%(size)d" % faces) |
|
143 |
self.Editor.StyleSetSpec(STC_PLC_STRING, "fore:#007F00,size:%(size)d" % faces) |
|
144 |
self.Editor.StyleSetSpec(STC_PLC_JUMP, "fore:#FF7FFF,size:%(size)d" % faces) |
|
145 |
self.Editor.StyleSetSpec(STC_PLC_ERROR, "fore:#FF0000,back:#FFFF00,size:%(size)d" % faces) |
|
146 |
self.Editor.StyleSetSpec(STC_PLC_SEARCH_RESULT, "fore:#FFFFFF,back:#FFA500,size:%(size)d" % faces) |
|
147 |
||
148 |
# Indicators styles |
|
149 |
self.Editor.IndicatorSetStyle(0, wx.stc.STC_INDIC_SQUIGGLE) |
|
150 |
if self.ParentWindow is not None and self.Controler is not None: |
|
151 |
self.Editor.IndicatorSetForeground(0, wx.RED) |
|
152 |
else: |
|
153 |
self.Editor.IndicatorSetForeground(0, wx.WHITE) |
|
154 |
||
155 |
# Line numbers in the margin |
|
156 |
self.Editor.SetMarginType(1, wx.stc.STC_MARGIN_NUMBER) |
|
157 |
self.Editor.SetMarginWidth(1, 50) |
|
158 |
||
159 |
# Folding |
|
160 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEROPEN, wx.stc.STC_MARK_BOXMINUS, "white", "#808080") |
|
161 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDER, wx.stc.STC_MARK_BOXPLUS, "white", "#808080") |
|
162 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERSUB, wx.stc.STC_MARK_VLINE, "white", "#808080") |
|
163 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERTAIL, wx.stc.STC_MARK_LCORNER, "white", "#808080") |
|
164 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEREND, wx.stc.STC_MARK_BOXPLUSCONNECTED, "white", "#808080") |
|
165 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.stc.STC_MARK_BOXMINUSCONNECTED, "white", "#808080") |
|
166 |
self.Editor.MarkerDefine(wx.stc.STC_MARKNUM_FOLDERMIDTAIL, wx.stc.STC_MARK_TCORNER, "white", "#808080") |
|
167 |
||
168 |
# Indentation size |
|
169 |
self.Editor.SetTabWidth(2) |
|
170 |
self.Editor.SetUseTabs(0) |
|
171 |
||
172 |
self.Editor.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT| |
|
173 |
wx.stc.STC_MOD_BEFOREDELETE| |
|
174 |
wx.stc.STC_PERFORMED_USER) |
|
175 |
||
176 |
self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.OnStyleNeeded, id=ID_TEXTVIEWERTEXTCTRL) |
|
177 |
self.Editor.Bind(wx.stc.EVT_STC_MARGINCLICK, self.OnMarginClick) |
|
178 |
self.Editor.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown) |
|
179 |
if self.Controler is not None: |
|
180 |
self.Editor.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus) |
|
181 |
self.Bind(wx.stc.EVT_STC_DO_DROP, self.OnDoDrop, id=ID_TEXTVIEWERTEXTCTRL) |
|
182 |
self.Bind(wx.stc.EVT_STC_MODIFIED, self.OnModification, id=ID_TEXTVIEWERTEXTCTRL) |
|
183 |
||
184 |
def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""): |
|
185 |
if tagname != "" and controler is not None: |
|
186 |
self.VARIABLE_PANEL_TYPE = controler.GetPouType(tagname.split("::")[1]) |
|
187 |
||
188 |
EditorPanel.__init__(self, parent, tagname, window, controler, debug) |
|
189 |
||
190 |
self.Keywords = [] |
|
191 |
self.Variables = {} |
|
192 |
self.Functions = {} |
|
193 |
self.TypeNames = [] |
|
194 |
self.Jumps = [] |
|
195 |
self.EnumeratedValues = [] |
|
196 |
self.DisableEvents = True |
|
197 |
self.TextSyntax = None |
|
198 |
self.CurrentAction = None |
|
199 |
self.Highlights = [] |
|
200 |
self.SearchParams = None |
|
201 |
self.SearchResults = None |
|
202 |
self.CurrentFindHighlight = None |
|
203 |
self.InstancePath = instancepath |
|
204 |
self.ContextStack = [] |
|
205 |
self.CallStack = [] |
|
206 |
||
207 |
self.RefreshHighlightsTimer = wx.Timer(self, -1) |
|
208 |
self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer) |
|
209 |
||
210 |
def __del__(self): |
|
211 |
self.RefreshHighlightsTimer.Stop() |
|
212 |
||
213 |
def GetTitle(self): |
|
214 |
if self.Debug or self.TagName == "": |
|
215 |
if len(self.InstancePath) > 15: |
|
216 |
return "..." + self.InstancePath[-12:] |
|
217 |
return self.InstancePath |
|
218 |
return EditorPanel.GetTitle(self) |
|
219 |
||
220 |
def GetInstancePath(self): |
|
221 |
return self.InstancePath |
|
222 |
||
223 |
def IsViewing(self, tagname): |
|
224 |
if self.Debug or self.TagName == "": |
|
225 |
return self.InstancePath == tagname |
|
226 |
else: |
|
227 |
return self.TagName == tagname |
|
228 |
||
229 |
def GetText(self): |
|
230 |
return self.Editor.GetText() |
|
231 |
||
232 |
def SetText(self, text): |
|
233 |
self.Editor.SetText(text) |
|
234 |
||
235 |
def SelectAll(self): |
|
236 |
self.Editor.SelectAll() |
|
237 |
||
238 |
def Colourise(self, start, end): |
|
239 |
self.Editor.Colourise(start, end) |
|
240 |
||
241 |
def StartStyling(self, pos, mask): |
|
242 |
self.Editor.StartStyling(pos, mask) |
|
243 |
||
244 |
def SetStyling(self, length, style): |
|
245 |
self.Editor.SetStyling(length, style) |
|
246 |
||
247 |
def GetCurrentPos(self): |
|
248 |
return self.Editor.GetCurrentPos() |
|
249 |
||
250 |
def OnModification(self, event): |
|
251 |
if not self.DisableEvents: |
|
252 |
mod_type = event.GetModificationType() |
|
253 |
if mod_type&wx.stc.STC_MOD_BEFOREINSERT: |
|
254 |
if self.CurrentAction == None: |
|
255 |
self.StartBuffering() |
|
256 |
elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1: |
|
257 |
self.Controler.EndBuffering() |
|
258 |
self.StartBuffering() |
|
259 |
self.CurrentAction = ("Add", event.GetPosition()) |
|
260 |
wx.CallAfter(self.RefreshModel) |
|
261 |
elif mod_type&wx.stc.STC_MOD_BEFOREDELETE: |
|
262 |
if self.CurrentAction == None: |
|
263 |
self.StartBuffering() |
|
264 |
elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1: |
|
265 |
self.Controler.EndBuffering() |
|
266 |
self.StartBuffering() |
|
267 |
self.CurrentAction = ("Delete", event.GetPosition()) |
|
268 |
wx.CallAfter(self.RefreshModel) |
|
269 |
event.Skip() |
|
270 |
||
271 |
def OnDoDrop(self, event): |
|
272 |
try: |
|
273 |
values = eval(event.GetDragText()) |
|
274 |
except: |
|
275 |
values = event.GetDragText() |
|
276 |
if isinstance(values, tuple): |
|
277 |
message = None |
|
278 |
if values[1] in ["program", "debug"]: |
|
279 |
event.SetDragText("") |
|
280 |
elif values[1] in ["functionBlock", "function"]: |
|
281 |
blocktype = values[0] |
|
282 |
blockname = values[2] |
|
283 |
if len(values) > 3: |
|
284 |
blockinputs = values[3] |
|
285 |
else: |
|
286 |
blockinputs = None |
|
287 |
if values[1] != "function": |
|
288 |
if blockname == "": |
|
289 |
dialog = wx.TextEntryDialog(self.ParentWindow, "Block name", "Please enter a block name", "", wx.OK|wx.CANCEL|wx.CENTRE) |
|
290 |
if dialog.ShowModal() == wx.ID_OK: |
|
291 |
blockname = dialog.GetValue() |
|
292 |
else: |
|
293 |
return |
|
294 |
dialog.Destroy() |
|
295 |
if blockname.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
296 |
message = _("\"%s\" pou already exists!")%blockname |
|
297 |
elif blockname.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
298 |
message = _("\"%s\" element for this pou already exists!")%blockname |
|
299 |
else: |
|
300 |
self.Controler.AddEditedElementPouVar(self.TagName, values[0], blockname) |
|
301 |
self.RefreshVariablePanel() |
|
302 |
self.RefreshVariableTree() |
|
303 |
blockinfo = self.Controler.GetBlockType(blocktype, blockinputs, self.Debug) |
|
304 |
hint = ',\n '.join( |
|
305 |
[ " " + fctdecl[0]+" := (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["inputs"]] + |
|
306 |
[ " " + fctdecl[0]+" => (*"+fctdecl[1]+"*)" for fctdecl in blockinfo["outputs"]]) |
|
307 |
if values[1] == "function": |
|
308 |
event.SetDragText(blocktype+"(\n "+hint+")") |
|
309 |
else: |
|
310 |
event.SetDragText(blockname+"(\n "+hint+")") |
|
311 |
elif values[1] == "location": |
|
312 |
pou_name, pou_type = self.Controler.GetEditedElementType(self.TagName, self.Debug) |
|
313 |
if len(values) > 2 and pou_type == "program": |
|
314 |
var_name = values[3] |
|
315 |
if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
316 |
message = _("\"%s\" pou already exists!")%var_name |
|
317 |
elif var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
318 |
message = _("\"%s\" element for this pou already exists!")%var_name |
|
319 |
else: |
|
320 |
location = values[0] |
|
321 |
if not location.startswith("%"): |
|
322 |
dialog = wx.SingleChoiceDialog(self.ParentWindow, |
|
323 |
_("Select a variable class:"), _("Variable class"), |
|
324 |
["Input", "Output", "Memory"], |
|
325 |
wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL) |
|
326 |
if dialog.ShowModal() == wx.ID_OK: |
|
327 |
selected = dialog.GetSelection() |
|
328 |
else: |
|
329 |
selected = None |
|
330 |
dialog.Destroy() |
|
331 |
if selected is None: |
|
332 |
event.SetDragText("") |
|
333 |
return |
|
334 |
if selected == 0: |
|
335 |
location = "%I" + location |
|
336 |
elif selected == 1: |
|
337 |
location = "%Q" + location |
|
338 |
else: |
|
339 |
location = "%M" + location |
|
340 |
if values[2] is not None: |
|
341 |
var_type = values[2] |
|
342 |
else: |
|
343 |
var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0] |
|
344 |
self.Controler.AddEditedElementPouVar(self.TagName, var_type, var_name, location, values[4]) |
|
345 |
self.RefreshVariablePanel() |
|
346 |
self.RefreshVariableTree() |
|
347 |
event.SetDragText(var_name) |
|
348 |
else: |
|
349 |
event.SetDragText("") |
|
350 |
elif values[1] == "Global": |
|
351 |
var_name = values[0] |
|
352 |
if var_name.upper() in [name.upper() for name in self.Controler.GetProjectPouNames(self.Debug)]: |
|
353 |
message = _("\"%s\" pou already exists!")%var_name |
|
354 |
else: |
|
355 |
if not var_name.upper() in [name.upper() for name in self.Controler.GetEditedElementVariables(self.TagName, self.Debug)]: |
|
356 |
self.Controler.AddEditedElementPouExternalVar(self.TagName, values[2], var_name) |
|
357 |
self.RefreshVariablePanel() |
|
358 |
self.RefreshVariableTree() |
|
359 |
event.SetDragText(var_name) |
|
360 |
elif values[1] == "Constant": |
|
361 |
event.SetDragText(values[0]) |
|
362 |
elif values[3] == self.TagName: |
|
363 |
self.ResetBuffer() |
|
364 |
event.SetDragText(values[0]) |
|
365 |
wx.CallAfter(self.RefreshModel) |
|
366 |
else: |
|
367 |
message = _("Variable don't belong to this POU!") |
|
368 |
if message is not None: |
|
369 |
dialog = wx.MessageDialog(self, message, _("Error"), wx.OK|wx.ICON_ERROR) |
|
370 |
dialog.ShowModal() |
|
371 |
dialog.Destroy() |
|
372 |
event.SetDragText("") |
|
373 |
event.Skip() |
|
374 |
||
375 |
def SetTextSyntax(self, syntax): |
|
376 |
self.TextSyntax = syntax |
|
377 |
if syntax in ["ST", "ALL"]: |
|
378 |
self.Editor.SetMarginType(2, wx.stc.STC_MARGIN_SYMBOL) |
|
379 |
self.Editor.SetMarginMask(2, wx.stc.STC_MASK_FOLDERS) |
|
380 |
self.Editor.SetMarginSensitive(2, 1) |
|
381 |
self.Editor.SetMarginWidth(2, 12) |
|
382 |
if syntax == "ST": |
|
383 |
self.BlockStartKeywords = ST_BLOCK_START_KEYWORDS |
|
384 |
self.BlockEndKeywords = ST_BLOCK_START_KEYWORDS |
|
385 |
else: |
|
386 |
self.BlockStartKeywords = IEC_BLOCK_START_KEYWORDS |
|
387 |
self.BlockEndKeywords = IEC_BLOCK_START_KEYWORDS |
|
388 |
else: |
|
389 |
self.BlockStartKeywords = [] |
|
390 |
self.BlockEndKeywords = [] |
|
391 |
||
392 |
def SetKeywords(self, keywords): |
|
393 |
self.Keywords = [keyword.upper() for keyword in keywords] |
|
394 |
self.Colourise(0, -1) |
|
395 |
||
396 |
def RefreshJumpList(self): |
|
397 |
if self.TextSyntax != "IL": |
|
398 |
self.Jumps = [jump.upper() for jump in LABEL_MODEL.findall(self.GetText())] |
|
399 |
self.Colourise(0, -1) |
|
400 |
||
401 |
# Buffer the last model state |
|
402 |
def RefreshBuffer(self): |
|
403 |
self.Controler.BufferProject() |
|
404 |
if self.ParentWindow: |
|
405 |
self.ParentWindow.RefreshTitle() |
|
406 |
self.ParentWindow.RefreshFileMenu() |
|
407 |
self.ParentWindow.RefreshEditMenu() |
|
408 |
||
409 |
def StartBuffering(self): |
|
410 |
self.Controler.StartBuffering() |
|
411 |
if self.ParentWindow: |
|
412 |
self.ParentWindow.RefreshTitle() |
|
413 |
self.ParentWindow.RefreshFileMenu() |
|
414 |
self.ParentWindow.RefreshEditMenu() |
|
415 |
||
416 |
def ResetBuffer(self): |
|
417 |
if self.CurrentAction != None: |
|
418 |
self.Controler.EndBuffering() |
|
419 |
self.CurrentAction = None |
|
420 |
||
421 |
def GetBufferState(self): |
|
422 |
if not self.Debug and self.TextSyntax != "ALL": |
|
423 |
return self.Controler.GetBufferState() |
|
424 |
return False, False |
|
425 |
||
426 |
def Undo(self): |
|
427 |
if not self.Debug and self.TextSyntax != "ALL": |
|
428 |
self.Controler.LoadPrevious() |
|
429 |
self.ParentWindow.CloseTabsWithoutModel() |
|
430 |
||
431 |
def Redo(self): |
|
432 |
if not self.Debug and self.TextSyntax != "ALL": |
|
433 |
self.Controler.LoadNext() |
|
434 |
self.ParentWindow.CloseTabsWithoutModel() |
|
435 |
||
436 |
def HasNoModel(self): |
|
437 |
if not self.Debug and self.TextSyntax != "ALL": |
|
438 |
return self.Controler.GetEditedElement(self.TagName) is None |
|
439 |
return False |
|
440 |
||
441 |
def RefreshView(self, variablepanel=True): |
|
442 |
EditorPanel.RefreshView(self, variablepanel) |
|
443 |
||
444 |
if self.Controler is not None: |
|
445 |
self.ResetBuffer() |
|
446 |
self.DisableEvents = True |
|
447 |
old_cursor_pos = self.GetCurrentPos() |
|
448 |
old_text = self.GetText() |
|
449 |
new_text = self.Controler.GetEditedElementText(self.TagName, self.Debug) |
|
450 |
self.SetText(new_text) |
|
451 |
new_cursor_pos = GetCursorPos(old_text, new_text) |
|
452 |
if new_cursor_pos != None: |
|
453 |
self.Editor.GotoPos(new_cursor_pos) |
|
454 |
else: |
|
455 |
self.Editor.GotoPos(old_cursor_pos) |
|
456 |
self.Editor.ScrollToColumn(0) |
|
457 |
self.RefreshJumpList() |
|
458 |
self.Editor.EmptyUndoBuffer() |
|
459 |
self.DisableEvents = False |
|
460 |
||
461 |
self.RefreshVariableTree() |
|
462 |
||
463 |
self.TypeNames = [typename.upper() for typename in self.Controler.GetDataTypes(self.TagName, True, self.Debug)] |
|
464 |
self.EnumeratedValues = [value.upper() for value in self.Controler.GetEnumeratedDataValues()] |
|
465 |
||
466 |
self.Functions = {} |
|
467 |
for category in self.Controler.GetBlockTypes(self.TagName, self.Debug): |
|
468 |
for blocktype in category["list"]: |
|
469 |
blockname = blocktype["name"].upper() |
|
470 |
if blocktype["type"] == "function" and blockname not in self.Keywords and blockname not in self.Variables.keys(): |
|
471 |
interface = dict([(name, {}) for name, type, modifier in blocktype["inputs"] + blocktype["outputs"] if name != '']) |
|
472 |
for param in ["EN", "ENO"]: |
|
473 |
if not interface.has_key(param): |
|
474 |
interface[param] = {} |
|
475 |
if self.Functions.has_key(blockname): |
|
476 |
self.Functions[blockname]["interface"].update(interface) |
|
477 |
self.Functions[blockname]["extensible"] |= blocktype["extensible"] |
|
478 |
else: |
|
479 |
self.Functions[blockname] = {"interface": interface, |
|
480 |
"extensible": blocktype["extensible"]} |
|
481 |
||
482 |
self.Colourise(0, -1) |
|
483 |
||
484 |
def RefreshVariableTree(self): |
|
485 |
words = self.TagName.split("::") |
|
486 |
self.Variables = self.GenerateVariableTree([(variable["Name"], variable["Type"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)]) |
|
487 |
if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL": |
|
488 |
return_type = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug) |
|
489 |
if return_type is not None: |
|
490 |
var_tree, var_dimension = self.Controler.GenerateVarTree(return_type, self.Debug) |
|
491 |
self.Variables[words[-1].upper()] = self.GenerateVariableTree(var_tree) |
|
492 |
else: |
|
493 |
self.Variables[words[-1].upper()] = {} |
|
494 |
||
495 |
def GenerateVariableTree(self, list): |
|
496 |
tree = {} |
|
497 |
for var_name, var_type, (var_tree, var_dimension) in list: |
|
498 |
tree[var_name.upper()] = self.GenerateVariableTree(var_tree) |
|
499 |
return tree |
|
500 |
||
501 |
def IsValidVariable(self, name, context): |
|
502 |
return context is not None and context.get(name, None) is not None |
|
503 |
||
504 |
def IsCallParameter(self, name, call): |
|
505 |
if call is not None: |
|
506 |
return (call["interface"].get(name.upper(), None) is not None or |
|
507 |
call["extensible"] and EXTENSIBLE_PARAMETER.match(name.upper()) is not None) |
|
508 |
return False |
|
509 |
||
510 |
def RefreshLineFolding(self, line_number): |
|
511 |
if self.TextSyntax in ["ST", "ALL"]: |
|
512 |
level = wx.stc.STC_FOLDLEVELBASE + self.Editor.GetLineIndentation(line_number) |
|
513 |
line = self.Editor.GetLine(line_number).strip() |
|
514 |
if line == "": |
|
515 |
if line_number > 0: |
|
516 |
if LineStartswith(self.Editor.GetLine(line_number - 1).strip(), self.BlockEndKeywords): |
|
517 |
level = self.Editor.GetFoldLevel(self.Editor.GetFoldParent(line_number - 1)) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
518 |
else: |
|
519 |
level = self.Editor.GetFoldLevel(line_number - 1) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
520 |
if level != wx.stc.STC_FOLDLEVELBASE: |
|
521 |
level |= wx.stc.STC_FOLDLEVELWHITEFLAG |
|
522 |
elif LineStartswith(line, self.BlockStartKeywords): |
|
523 |
level |= wx.stc.STC_FOLDLEVELHEADERFLAG |
|
524 |
elif LineStartswith(line, self.BlockEndKeywords): |
|
525 |
if LineStartswith(self.Editor.GetLine(line_number - 1).strip(), self.BlockEndKeywords): |
|
526 |
level = self.Editor.GetFoldLevel(self.Editor.GetFoldParent(line_number - 1)) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
527 |
else: |
|
528 |
level = self.Editor.GetFoldLevel(line_number - 1) & wx.stc.STC_FOLDLEVELNUMBERMASK |
|
529 |
self.Editor.SetFoldLevel(line_number, level) |
|
530 |
||
531 |
def OnStyleNeeded(self, event): |
|
532 |
self.TextChanged = True |
|
533 |
line_number = self.Editor.LineFromPosition(self.Editor.GetEndStyled()) |
|
534 |
if line_number == 0: |
|
535 |
start_pos = last_styled_pos = 0 |
|
536 |
else: |
|
537 |
start_pos = last_styled_pos = self.Editor.GetLineEndPosition(line_number - 1) + 1 |
|
538 |
self.RefreshLineFolding(line_number) |
|
539 |
end_pos = event.GetPosition() |
|
540 |
self.StartStyling(start_pos, 0xff) |
|
541 |
||
542 |
current_context = self.Variables |
|
543 |
current_call = None |
|
544 |
||
545 |
current_pos = last_styled_pos |
|
546 |
state = SPACE |
|
547 |
line = "" |
|
548 |
word = "" |
|
549 |
while current_pos < end_pos: |
|
550 |
char = chr(self.Editor.GetCharAt(current_pos)).upper() |
|
551 |
line += char |
|
552 |
if char == NEWLINE: |
|
553 |
self.ContextStack = [] |
|
554 |
current_context = self.Variables |
|
555 |
if state == COMMENT: |
|
556 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_COMMENT) |
|
557 |
elif state == NUMBER: |
|
558 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
559 |
elif state == WORD: |
|
560 |
if word in self.Keywords or word in self.TypeNames: |
|
561 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
562 |
elif self.IsValidVariable(word, current_context): |
|
563 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
564 |
elif self.IsCallParameter(word, current_call): |
|
565 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
566 |
elif word in self.Functions: |
|
567 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
568 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
|
569 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
570 |
elif word in self.EnumeratedValues: |
|
571 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
572 |
else: |
|
573 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
574 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
|
575 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
|
576 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
|
577 |
self.StartStyling(current_pos, 0xff) |
|
578 |
else: |
|
579 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
580 |
last_styled_pos = current_pos |
|
581 |
state = SPACE |
|
582 |
line = "" |
|
583 |
line_number += 1 |
|
584 |
self.RefreshLineFolding(line_number) |
|
585 |
elif line.endswith("(*") and state != COMMENT: |
|
586 |
self.SetStyling(current_pos - last_styled_pos - 1, 31) |
|
587 |
last_styled_pos = current_pos |
|
588 |
if state == WORD: |
|
589 |
current_context = self.Variables |
|
590 |
state = COMMENT |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
591 |
elif line.endswith("{") and state not in [PRAGMA, DPRAGMA]: |
858
daafaa8a28fd
Fix bug in TextViewer when starting text with '{' character
Laurent Bessard
parents:
814
diff
changeset
|
592 |
self.SetStyling(current_pos - last_styled_pos, 31) |
814 | 593 |
last_styled_pos = current_pos |
594 |
if state == WORD: |
|
595 |
current_context = self.Variables |
|
596 |
state = PRAGMA |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
597 |
elif line.endswith("{{") and state == PRAGMA: |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
598 |
self.SetStyling(current_pos - last_styled_pos, 31) |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
599 |
last_styled_pos = current_pos |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
600 |
state = DPRAGMA |
814 | 601 |
elif state == COMMENT: |
602 |
if line.endswith("*)"): |
|
603 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
|
604 |
last_styled_pos = current_pos + 1 |
|
605 |
state = SPACE |
|
606 |
elif state == PRAGMA: |
|
607 |
if line.endswith("}"): |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
608 |
self.SetStyling(current_pos - last_styled_pos, 31) |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
609 |
last_styled_pos = current_pos |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
610 |
state = SPACE |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
611 |
elif state == DPRAGMA: |
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
612 |
if line.endswith("}}"): |
814 | 613 |
self.SetStyling(current_pos - last_styled_pos + 2, 31) |
614 |
last_styled_pos = current_pos + 1 |
|
615 |
state = SPACE |
|
616 |
elif (line.endswith("'") or line.endswith('"')) and state not in [STRING, WSTRING]: |
|
617 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
618 |
last_styled_pos = current_pos |
|
619 |
if state == WORD: |
|
620 |
current_context = self.Variables |
|
621 |
if line.endswith("'"): |
|
622 |
state = STRING |
|
623 |
else: |
|
624 |
state = WSTRING |
|
625 |
elif state == STRING: |
|
626 |
if line.endswith("'") and not line.endswith("$'"): |
|
627 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
628 |
last_styled_pos = current_pos + 1 |
|
629 |
state = SPACE |
|
630 |
elif state == WSTRING: |
|
631 |
if line.endswith('"') and not line.endswith('$"'): |
|
632 |
self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_STRING) |
|
633 |
last_styled_pos = current_pos + 1 |
|
634 |
state = SPACE |
|
635 |
elif char in LETTERS: |
|
636 |
if state == NUMBER: |
|
637 |
word = "#" |
|
638 |
state = WORD |
|
639 |
elif state == SPACE: |
|
640 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
641 |
word = char |
|
642 |
last_styled_pos = current_pos |
|
643 |
state = WORD |
|
644 |
else: |
|
645 |
word += char |
|
646 |
elif char in NUMBERS or char == '.' and state != WORD: |
|
647 |
if state == SPACE: |
|
648 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
649 |
last_styled_pos = current_pos |
|
650 |
state = NUMBER |
|
651 |
elif state == WORD and char != '.': |
|
652 |
word += char |
|
653 |
elif char == '(' and state == SPACE: |
|
654 |
self.CallStack.append(current_call) |
|
655 |
current_call = None |
|
656 |
else: |
|
657 |
if state == WORD: |
|
658 |
if word in self.Keywords or word in self.TypeNames: |
|
659 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
660 |
elif self.IsValidVariable(word, current_context): |
|
661 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
662 |
elif self.IsCallParameter(word, current_call): |
|
663 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
664 |
elif word in self.Functions: |
|
665 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
666 |
elif self.TextSyntax == "IL" and word in self.Jumps: |
|
667 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
668 |
elif word in self.EnumeratedValues: |
|
669 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
670 |
else: |
|
671 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
672 |
if word not in ["]", ")"] and (self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos): |
|
673 |
self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK) |
|
674 |
self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK) |
|
675 |
self.StartStyling(current_pos, 0xff) |
|
676 |
if char == '.': |
|
677 |
if word != "]": |
|
678 |
if current_context is not None: |
|
679 |
current_context = current_context.get(word, None) |
|
680 |
else: |
|
681 |
current_context = None |
|
682 |
elif char == '(': |
|
683 |
self.CallStack.append(current_call) |
|
684 |
current_call = self.Functions.get(word, None) |
|
685 |
if current_call is None and self.IsValidVariable(word, current_context): |
|
686 |
current_call = {"interface": current_context.get(word, {}), |
|
687 |
"extensible": False} |
|
688 |
current_context = self.Variables |
|
689 |
else: |
|
894
a4919f228924
Fixed bug when indexing table in Pragma within ST/IL code
Laurent Bessard
parents:
871
diff
changeset
|
690 |
if char == '[' and current_context is not None: |
814 | 691 |
self.ContextStack.append(current_context.get(word, None)) |
692 |
current_context = self.Variables |
|
693 |
||
694 |
word = "" |
|
695 |
last_styled_pos = current_pos |
|
696 |
state = SPACE |
|
697 |
elif state == NUMBER: |
|
698 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
699 |
last_styled_pos = current_pos |
|
700 |
state = SPACE |
|
701 |
if char == ']': |
|
702 |
if len(self.ContextStack) > 0: |
|
703 |
current_context = self.ContextStack.pop() |
|
704 |
else: |
|
705 |
current_context = self.Variables |
|
706 |
word = char |
|
707 |
state = WORD |
|
708 |
elif char == ')': |
|
709 |
current_context = self.Variables |
|
710 |
if len(self.CallStack) > 0: |
|
711 |
current_call = self.CallStack.pop() |
|
712 |
else: |
|
713 |
current_call = None |
|
714 |
word = char |
|
715 |
state = WORD |
|
716 |
current_pos += 1 |
|
717 |
if state == COMMENT: |
|
718 |
self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT) |
|
719 |
elif state == NUMBER: |
|
720 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
721 |
elif state == WORD: |
|
722 |
if word in self.Keywords or word in self.TypeNames: |
|
723 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD) |
|
724 |
elif self.IsValidVariable(word, current_context): |
|
725 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE) |
|
726 |
elif self.IsCallParameter(word, current_call): |
|
727 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_PARAMETER) |
|
728 |
elif self.TextSyntax == "IL" and word in self.Functions: |
|
729 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION) |
|
730 |
elif word in self.Jumps: |
|
731 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP) |
|
732 |
elif word in self.EnumeratedValues: |
|
733 |
self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER) |
|
734 |
else: |
|
735 |
self.SetStyling(current_pos - last_styled_pos, 31) |
|
736 |
else: |
|
737 |
self.SetStyling(current_pos - start_pos, 31) |
|
738 |
self.ShowHighlights(start_pos, end_pos) |
|
739 |
event.Skip() |
|
740 |
||
741 |
def OnMarginClick(self, event): |
|
742 |
if event.GetMargin() == 2: |
|
743 |
line = self.Editor.LineFromPosition(event.GetPosition()) |
|
744 |
if self.Editor.GetFoldLevel(line) & wx.stc.STC_FOLDLEVELHEADERFLAG: |
|
745 |
self.Editor.ToggleFold(line) |
|
746 |
event.Skip() |
|
747 |
||
748 |
def Cut(self): |
|
749 |
self.ResetBuffer() |
|
750 |
self.DisableEvents = True |
|
751 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_CUT) |
|
752 |
self.DisableEvents = False |
|
753 |
self.RefreshModel() |
|
754 |
self.RefreshBuffer() |
|
755 |
||
756 |
def Copy(self): |
|
757 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_COPY) |
|
758 |
||
759 |
def Paste(self): |
|
760 |
self.ResetBuffer() |
|
761 |
self.DisableEvents = True |
|
762 |
self.Editor.CmdKeyExecute(wx.stc.STC_CMD_PASTE) |
|
763 |
self.DisableEvents = False |
|
764 |
self.RefreshModel() |
|
765 |
self.RefreshBuffer() |
|
766 |
||
767 |
def Find(self, direction, search_params): |
|
768 |
if self.SearchParams != search_params: |
|
769 |
self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT) |
|
770 |
||
771 |
self.SearchParams = search_params |
|
772 |
criteria = { |
|
773 |
"raw_pattern": search_params["find_pattern"], |
|
774 |
"pattern": re.compile(search_params["find_pattern"]), |
|
775 |
"case_sensitive": search_params["case_sensitive"], |
|
776 |
"regular_expression": search_params["regular_expression"], |
|
777 |
"filter": "all"} |
|
778 |
||
779 |
self.SearchResults = [ |
|
780 |
(infos[1:], start, end, SEARCH_RESULT_HIGHLIGHT) |
|
781 |
for infos, start, end, text in |
|
782 |
self.Controler.SearchInPou(self.TagName, criteria, self.Debug)] |
|
783 |
||
784 |
if len(self.SearchResults) > 0: |
|
785 |
if self.CurrentFindHighlight is not None: |
|
786 |
old_idx = self.SearchResults.index(self.CurrentFindHighlight) |
|
787 |
if self.SearchParams["wrap"]: |
|
788 |
idx = (old_idx + direction) % len(self.SearchResults) |
|
789 |
else: |
|
790 |
idx = max(0, min(old_idx + direction, len(self.SearchResults) - 1)) |
|
791 |
if idx != old_idx: |
|
792 |
self.RemoveHighlight(*self.CurrentFindHighlight) |
|
793 |
self.CurrentFindHighlight = self.SearchResults[idx] |
|
794 |
self.AddHighlight(*self.CurrentFindHighlight) |
|
795 |
else: |
|
796 |
self.CurrentFindHighlight = self.SearchResults[0] |
|
797 |
self.AddHighlight(*self.CurrentFindHighlight) |
|
798 |
||
799 |
else: |
|
800 |
if self.CurrentFindHighlight is not None: |
|
801 |
self.RemoveHighlight(*self.CurrentFindHighlight) |
|
802 |
self.CurrentFindHighlight = None |
|
803 |
||
804 |
def RefreshModel(self): |
|
805 |
self.RefreshJumpList() |
|
806 |
self.Controler.SetEditedElementText(self.TagName, self.GetText()) |
|
807 |
||
808 |
def OnKeyDown(self, event): |
|
809 |
if self.Controler is not None: |
|
810 |
||
811 |
if self.Editor.CallTipActive(): |
|
812 |
self.Editor.CallTipCancel() |
|
813 |
key = event.GetKeyCode() |
|
814 |
key_handled = False |
|
815 |
||
816 |
line = self.Editor.GetCurrentLine() |
|
817 |
if line == 0: |
|
818 |
start_pos = 0 |
|
819 |
else: |
|
820 |
start_pos = self.Editor.GetLineEndPosition(line - 1) + 1 |
|
821 |
end_pos = self.GetCurrentPos() |
|
822 |
lineText = self.Editor.GetTextRange(start_pos, end_pos).replace("\t", " ") |
|
823 |
||
824 |
# Code completion |
|
825 |
if key == wx.WXK_SPACE and event.ControlDown(): |
|
826 |
||
827 |
words = lineText.split(" ") |
|
828 |
words = [word for i, word in enumerate(words) if word != '' or i == len(words) - 1] |
|
829 |
||
830 |
kw = [] |
|
831 |
||
832 |
if self.TextSyntax == "IL": |
|
833 |
if len(words) == 1: |
|
834 |
kw = self.Keywords |
|
835 |
elif len(words) == 2: |
|
836 |
if words[0].upper() in ["CAL", "CALC", "CALNC"]: |
|
837 |
kw = self.Functions |
|
838 |
elif words[0].upper() in ["JMP", "JMPC", "JMPNC"]: |
|
839 |
kw = self.Jumps |
|
840 |
else: |
|
841 |
kw = self.Variables.keys() |
|
842 |
else: |
|
871
1af078aa0cf8
Add support for double bracket pragma in syntax highlighting of TextViewer
Laurent Bessard
parents:
858
diff
changeset
|
843 |
kw = self.Keywords + self.Variables.keys() + self.Functions.keys() |
814 | 844 |
if len(kw) > 0: |
845 |
if len(words[-1]) > 0: |
|
846 |
kw = [keyword for keyword in kw if keyword.startswith(words[-1])] |
|
847 |
kw.sort() |
|
848 |
self.Editor.AutoCompSetIgnoreCase(True) |
|
849 |
self.Editor.AutoCompShow(len(words[-1]), " ".join(kw)) |
|
850 |
key_handled = True |
|
851 |
elif key == wx.WXK_RETURN or key == wx.WXK_NUMPAD_ENTER: |
|
852 |
if self.TextSyntax in ["ST", "ALL"]: |
|
853 |
indent = self.Editor.GetLineIndentation(line) |
|
854 |
if LineStartswith(lineText.strip(), self.BlockStartKeywords): |
|
855 |
indent = (indent / 2 + 1) * 2 |
|
856 |
self.Editor.AddText("\n" + " " * indent) |
|
857 |
key_handled = True |
|
858 |
elif key == wx.WXK_BACK: |
|
859 |
if self.TextSyntax in ["ST", "ALL"]: |
|
860 |
indent = self.Editor.GetLineIndentation(line) |
|
861 |
if lineText.strip() == "" and indent > 0: |
|
862 |
self.Editor.DelLineLeft() |
|
863 |
self.Editor.AddText(" " * ((max(0, indent - 1) / 2) * 2)) |
|
864 |
key_handled = True |
|
865 |
if not key_handled: |
|
866 |
event.Skip() |
|
867 |
||
868 |
def OnKillFocus(self, event): |
|
869 |
self.Editor.AutoCompCancel() |
|
870 |
event.Skip() |
|
871 |
||
872 |
#------------------------------------------------------------------------------- |
|
873 |
# Highlights showing functions |
|
874 |
#------------------------------------------------------------------------------- |
|
875 |
||
876 |
def OnRefreshHighlightsTimer(self, event): |
|
877 |
self.RefreshView() |
|
878 |
event.Skip() |
|
879 |
||
880 |
def ClearHighlights(self, highlight_type=None): |
|
881 |
EditorPanel.ClearHighlights(self, highlight_type) |
|
882 |
||
883 |
if highlight_type is None: |
|
884 |
self.Highlights = [] |
|
885 |
else: |
|
886 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
887 |
if highlight_type is not None: |
|
888 |
self.Highlights = [(infos, start, end, highlight) for (infos, start, end, highlight) in self.Highlights if highlight != highlight_type] |
|
889 |
self.RefreshView() |
|
890 |
||
891 |
def AddHighlight(self, infos, start, end, highlight_type): |
|
892 |
EditorPanel.AddHighlight(self, infos, start, end, highlight_type) |
|
893 |
||
894 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
895 |
if infos[0] == "body" and highlight_type is not None: |
|
896 |
self.Highlights.append((infos[1], start, end, highlight_type)) |
|
897 |
self.Editor.GotoPos(self.Editor.PositionFromLine(start[0]) + start[1]) |
|
898 |
self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) |
|
899 |
||
900 |
def RemoveHighlight(self, infos, start, end, highlight_type): |
|
901 |
EditorPanel.RemoveHighlight(self, infos, start, end, highlight_type) |
|
902 |
||
903 |
highlight_type = HIGHLIGHT_TYPES.get(highlight_type, None) |
|
904 |
if (infos[0] == "body" and highlight_type is not None and |
|
905 |
(infos[1], start, end, highlight_type) in self.Highlights): |
|
906 |
self.Highlights.remove((infos[1], start, end, highlight_type)) |
|
907 |
self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True) |
|
908 |
||
909 |
def ShowHighlights(self, start_pos, end_pos): |
|
910 |
for indent, start, end, highlight_type in self.Highlights: |
|
911 |
if start[0] == 0: |
|
912 |
highlight_start_pos = start[1] - indent |
|
913 |
else: |
|
914 |
highlight_start_pos = self.Editor.GetLineEndPosition(start[0] - 1) + start[1] - indent + 1 |
|
915 |
if end[0] == 0: |
|
916 |
highlight_end_pos = end[1] - indent + 1 |
|
917 |
else: |
|
918 |
highlight_end_pos = self.Editor.GetLineEndPosition(end[0] - 1) + end[1] - indent + 2 |
|
919 |
if highlight_start_pos < end_pos and highlight_end_pos > start_pos: |
|
920 |
self.StartStyling(highlight_start_pos, 0xff) |
|
921 |
self.SetStyling(highlight_end_pos - highlight_start_pos, highlight_type) |
|
922 |
self.StartStyling(highlight_start_pos, 0x00) |
|
923 |
self.SetStyling(len(self.Editor.GetText()) - highlight_end_pos, wx.stc.STC_STYLE_DEFAULT) |
|
924 |