Skip to content

Commit bfc1232

Browse files
Improve saving in texteditor (#2571)
* Improve code in TextEditor * Fix modification indication after saving on Text Editor
1 parent c166db2 commit bfc1232

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/robotide/editor/texteditor.py

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ def focused(func):
7979
def f(event):
8080
if self.is_focused() and self._editor.is_focused():
8181
func(event)
82+
8283
return f
84+
8385
self.register_shortcut('CtrlCmd-X', focused(lambda e: self._editor.cut()))
8486
self.register_shortcut('CtrlCmd-C', focused(lambda e: self._editor.copy()))
8587
if IS_MAC: # Mac needs this key binding
@@ -89,6 +91,7 @@ def f(event):
8991
self.register_shortcut('CtrlCmd-Z', focused(lambda e: self._editor.undo()))
9092
self.register_shortcut('CtrlCmd-Y', focused(lambda e: self._editor.redo()))
9193
# self.register_shortcut('Del', focused(lambda e: self._editor.delete()))
94+
self.register_shortcut('CtrlCmd-S', focused(lambda e: self.OnSaving(e)))
9295
self.register_shortcut('CtrlCmd-Shift-I', focused(lambda e: self._editor.insert_cell(e)))
9396
# self.register_shortcut('CtrlCmd-Shift-D', focused(lambda e: self._editor.delete_cell(e)))
9497
self.register_shortcut('Alt-Up', focused(lambda e: self._editor.move_row_up(e)))
@@ -127,8 +130,10 @@ def OnSaving(self, message):
127130
_ = message
128131
if self.is_focused():
129132
self._editor.save()
130-
self._editor.GetFocus(None)
131-
else:
133+
# print(f"DEBUG: textedit OnSaving ENTER {message=} isfocused={self.is_focused()}")
134+
# self._editor.GetFocus(None)
135+
elif isinstance(message, RideSaving):
136+
# print(f"DEBUG: textedit OnSaving Open Saved from other {message=} isfocused={self.is_focused()}")
132137
self._open() # Was saved from other Editor
133138

134139
def OnDataChanged(self, message):
@@ -151,7 +156,7 @@ def _on_timer(self, event):
151156
@staticmethod
152157
def _should_process_data_changed_message(message):
153158
return isinstance(message, RideDataChanged) and \
154-
not isinstance(message, RideDataChangedToDirty)
159+
not isinstance(message, RideDataChangedToDirty)
155160

156161
def OnTreeSelection(self, message):
157162
self._editor.store_position()
@@ -224,7 +229,6 @@ def is_focused(self):
224229

225230

226231
class DummyController(_WithStepsController):
227-
228232
_populator = robotapi.UserKeywordPopulator
229233
filename = ""
230234

@@ -304,7 +308,7 @@ def _handle_sanity_check_failure(self):
304308
# DEBUG: use widgets.Dialog
305309
dlg = wx.MessageDialog(self._editor, 'ERROR: Data sanity check failed!\n'
306310
'Reset changes?',
307-
'Can not apply changes from Txt Editor', style=wx.YES | wx.NO)
311+
'Can not apply changes from Txt Editor', style=wx.YES | wx.NO)
308312
dlg.InheritAttributes()
309313
"""
310314
dlg.SetBackgroundColour(Colour(200, 222, 40))
@@ -525,7 +529,7 @@ def datafile_controller(self):
525529
def OnFind(self, event):
526530
if self._editor:
527531
text = self._editor.GetSelectedText()
528-
if len(text) > 0 and text.lower() != self._search_field.GetValue().lower() and\
532+
if len(text) > 0 and text.lower() != self._search_field.GetValue().lower() and \
529533
event.GetEventType() == wx.wxEVT_TOOL:
530534
# if a search string selected in text and CTRL+G is pressed
531535
# put the string into the _search_field
@@ -631,7 +635,7 @@ def auto_ident(self):
631635
or line.strip().startswith("ELSE")):
632636
tsize += 1
633637
elif linenum > 0 and tsize == 0: # Advance if first task/test case or keyword
634-
prevline = self._editor.GetLine(linenum-1).lower()
638+
prevline = self._editor.GetLine(linenum - 1).lower()
635639
if prevline.startswith("**") and not ("variables" in prevline or "settings" in prevline):
636640
tsize = 1
637641
# DEBUG: elif prevline.startswith("\n"):
@@ -695,7 +699,7 @@ def deindent_block(self):
695699
def indent_line(self, line):
696700
if line > 0:
697701
pos = self._editor.PositionFromLine(line)
698-
text = self._editor.GetLine(line-1)
702+
text = self._editor.GetLine(line - 1)
699703
lenline = len(text)
700704
if lenline > 0:
701705
idx = 0
@@ -972,7 +976,7 @@ def _enclose_text(open_symbol, value=''):
972976
close_symbol = ')'
973977
else:
974978
close_symbol = open_symbol
975-
return open_symbol+value+close_symbol
979+
return open_symbol + value + close_symbol
976980

977981
def move_row_up(self, event):
978982
_ = event
@@ -982,7 +986,7 @@ def move_row_up(self, event):
982986
if ini_line > 0:
983987
end_line = self._editor.LineFromPosition(end)
984988
# get the previous row content and length
985-
rowabove = self._editor.GetLine(ini_line-1)
989+
rowabove = self._editor.GetLine(ini_line - 1)
986990
lenabove = len(rowabove.encode('utf-8'))
987991
# get the content of the block rows
988992
rowselblock = ''
@@ -992,10 +996,10 @@ def move_row_up(self, event):
992996
rowcnt += 1
993997
# add the content of previous row
994998
rowselblock += rowabove
995-
begpos = self._editor.PositionFromLine(ini_line-1)
996-
endpos = self._editor.PositionFromLine(end_line+1)
999+
begpos = self._editor.PositionFromLine(ini_line - 1)
1000+
endpos = self._editor.PositionFromLine(end_line + 1)
9971001
self._editor.Replace(begpos, endpos, rowselblock)
998-
self._editor.SetSelection(begpos, endpos-lenabove-1)
1002+
self._editor.SetSelection(begpos, endpos - lenabove - 1)
9991003
# DEBUG: recalculate line identation for new position and old
10001004

10011005
def move_row_down(self, event):
@@ -1004,7 +1008,7 @@ def move_row_down(self, event):
10041008
ini_line = self._editor.LineFromPosition(start)
10051009
end_line = self._editor.LineFromPosition(end)
10061010
# get the next row content and length
1007-
rowbelow = self._editor.GetLine(end_line+1)
1011+
rowbelow = self._editor.GetLine(end_line + 1)
10081012
lenbelow = len(rowbelow.encode('utf-8'))
10091013
# get the content of the block rows after adding the content below first
10101014
# no new rows anymore?
@@ -1018,9 +1022,9 @@ def move_row_down(self, event):
10181022
rowselblock += self._editor.GetLine(rowcnt)
10191023
rowcnt += 1
10201024
begpos = self._editor.PositionFromLine(ini_line)
1021-
endpos = self._editor.PositionFromLine(end_line+2)
1025+
endpos = self._editor.PositionFromLine(end_line + 2)
10221026
self._editor.Replace(begpos, endpos, rowselblock)
1023-
self._editor.SetSelection(begpos+lenbelow, endpos-1)
1027+
self._editor.SetSelection(begpos + lenbelow, endpos - 1)
10241028
# DEBUG: recalculate line identation for new position and old
10251029

10261030
def delete_row(self, event):
@@ -1148,23 +1152,23 @@ def insert_cell(self, event):
11481152
ini_line = self._editor.LineFromPosition(start)
11491153
end_line = self._editor.LineFromPosition(end)
11501154
begpos = self._editor.PositionFromLine(ini_line)
1151-
endpos = self._editor.PositionFromLine(end_line+1)
1155+
endpos = self._editor.PositionFromLine(end_line + 1)
11521156
cell_no_beg = self._get_cell_no(begpos, endpos, start)
11531157
cell_pos_beg = self._get_position_of_cell(begpos, endpos, cell_no_beg)
11541158
# if there is a selection subtract 1 from endpos to circumvent cursor being on end of cell
11551159
# --> otherwise no will be next cell no
11561160
if start != end:
1157-
cell_no_end = self._get_cell_no(begpos, endpos, end-1)
1161+
cell_no_end = self._get_cell_no(begpos, endpos, end - 1)
11581162
else:
11591163
cell_no_end = cell_no_beg
11601164
# print(f"DEBUG: cell range to handle beg={cell_no_beg} end={cell_no_end}")
11611165
celltab = ' ' * self._tab_size
11621166
# If the selection spans more than one line:
1163-
if ini_line < end_line: # DEBUG: do inserts in such a way that they can be undone in 1 undo
1167+
if ini_line < end_line: # DEBUG: do inserts in such a way that they can be undone in 1 undo
11641168
new_start = cell_pos_beg
1165-
for line in range(ini_line, end_line+1):
1169+
for line in range(ini_line, end_line + 1):
11661170
begthis = self._editor.PositionFromLine(line)
1167-
endthis = self._editor.PositionFromLine(line+1)
1171+
endthis = self._editor.PositionFromLine(line + 1)
11681172
cell_pos_beg = self._get_position_of_cell(begthis, endthis, cell_no_beg)
11691173
self._editor.InsertText(cell_pos_beg, celltab)
11701174
new_end = cell_pos_beg + (len(celltab.encode('utf-8')))
@@ -1190,16 +1194,16 @@ def delete_cell(self, event):
11901194
ini_line = self._editor.LineFromPosition(start)
11911195
end_line = self._editor.LineFromPosition(end)
11921196
begpos = self._editor.PositionFromLine(ini_line)
1193-
endpos = self._editor.PositionFromLine(end_line+1)
1197+
endpos = self._editor.PositionFromLine(end_line + 1)
11941198
cell_no_beg = self._get_cell_no(begpos, endpos, start)
11951199
cell_pos_beg = self._get_position_of_cell(begpos, endpos, cell_no_beg)
11961200
# if there is a selection subtract 1 from endpos to circumvent cursor being on end of cell
11971201
# --> otherwise no will be next cell no
11981202
if start != end:
1199-
cell_no_end = self._get_cell_no(begpos, endpos, end-1)
1203+
cell_no_end = self._get_cell_no(begpos, endpos, end - 1)
12001204
else:
12011205
cell_no_end = cell_no_beg
1202-
cell_pos_end = self._get_position_of_cell(begpos, endpos, cell_no_end+1)
1206+
cell_pos_end = self._get_position_of_cell(begpos, endpos, cell_no_end + 1)
12031207
self._editor.Remove(cell_pos_beg, cell_pos_end)
12041208
new_start = cell_pos_beg
12051209
new_end = new_start + (end - start)
@@ -1247,15 +1251,15 @@ def _get_position_of_cell(self, begpos, endpos, cell_no):
12471251
while fndidx != -1:
12481252
fndidx = textencode.find(cellencode, fndidx)
12491253
if fndidx != -1:
1250-
if fndcnt == 1 and fndidx == 0: # check if begpos is at the beginning of a cell
1254+
if fndcnt == 1 and fndidx == 0: # check if begpos is at the beginning of a cell
12511255
fndcnt -= 1
12521256
fndcnt += 1
12531257
if cell_no == fndcnt:
12541258
cellpos = begpos + fndidx
12551259
break
1256-
fndidx += 1 # for next search
1260+
fndidx += 1 # for next search
12571261
else: # cell_no does not exist -- return endpos-1
1258-
cellpos = endpos-1
1262+
cellpos = endpos - 1
12591263
return cellpos
12601264

12611265
def execute_sharp_comment(self, event):
@@ -1269,7 +1273,7 @@ def execute_sharp_comment(self, event):
12691273
maxsize = self._editor.GetLineCount()
12701274
# If the selection spans on more than one line:
12711275
if ini_line < end_line:
1272-
for line in range(ini_line, end_line+1):
1276+
for line in range(ini_line, end_line + 1):
12731277
count += 1
12741278
if line < maxsize:
12751279
self._editor.GotoLine(line)
@@ -1303,7 +1307,7 @@ def execute_sharp_comment(self, event):
13031307
if cursor > pos:
13041308
idx = cursor - pos
13051309
while idx >= len(spaces):
1306-
if row[idx-len(spaces):idx] != spaces:
1310+
if row[idx - len(spaces):idx] != spaces:
13071311
idx -= 1
13081312
else:
13091313
break
@@ -1336,7 +1340,7 @@ def execute_sharp_uncomment(self, event):
13361340
# maxsize = self._editor.GetLineCount()
13371341
# If the selection spans on more than one line:
13381342
if ini_line < end_line:
1339-
for line in range(ini_line, end_line+1):
1343+
for line in range(ini_line, end_line + 1):
13401344
pos = self._editor.PositionFromLine(line)
13411345
row = self._editor.GetLine(line)
13421346
lenline = len(row)
@@ -1345,8 +1349,8 @@ def execute_sharp_uncomment(self, event):
13451349
while idx < lenline and row[idx] == ' ':
13461350
idx += 1
13471351
size = 1
1348-
if idx + 1 < lenline and row[idx:idx+1] == '#':
1349-
if idx + 2 < lenline and row[idx+1:idx+2] == ' ':
1352+
if idx + 1 < lenline and row[idx:idx + 1] == '#':
1353+
if idx + 2 < lenline and row[idx + 1:idx + 2] == ' ':
13501354
size = 2
13511355
# Here we clean up escaped spaces from Apply
13521356
if idx + size < lenline:
@@ -1386,7 +1390,7 @@ def execute_sharp_uncomment(self, event):
13861390
if cursor > pos:
13871391
idx = cursor - pos
13881392
while idx >= len(spaces):
1389-
if row[idx-len(spaces):idx] != spaces:
1393+
if row[idx - len(spaces):idx] != spaces:
13901394
idx -= 1
13911395
else:
13921396
break
@@ -1555,11 +1559,11 @@ def _set_styles(self, readonly=False):
15551559
else:
15561560
bkg = tuple(int(h[i:i + 2], 16) for i in (0, 2, 4))
15571561
if bkg >= (180, 180, 180):
1558-
bkg = (max(160, bkg[0]-80), max(160, bkg[1]-80),
1559-
max(160, bkg[2]-80))
1562+
bkg = (max(160, bkg[0] - 80), max(160, bkg[1] - 80),
1563+
max(160, bkg[2] - 80))
15601564
else:
1561-
bkg = (min(255, bkg[0]+180), min(255, bkg[1]+180),
1562-
min(255, bkg[2]+180))
1565+
bkg = (min(255, bkg[0] + 180), min(255, bkg[1] + 180),
1566+
min(255, bkg[2] + 180))
15631567
background = '#%02X%02X%02X' % bkg
15641568
if robotframeworklexer:
15651569
styles = {
@@ -1645,12 +1649,12 @@ def stylize(self):
16451649
shift = 0
16461650
for position, token, value in self.lexer.get_tokens_unprocessed(self.editor.GetText()):
16471651
if wx.VERSION < (4, 1, 0):
1648-
self.editor.StartStyling(position+shift, 31)
1652+
self.editor.StartStyling(position + shift, 31)
16491653
else:
16501654
self.editor.StartStyling(position + shift)
16511655
try:
16521656
self.editor.SetStyling(len(value.encode('utf-8')), self.tokens[token])
1653-
shift += len(value.encode('utf-8'))-len(value)
1657+
shift += len(value.encode('utf-8')) - len(value)
16541658
except UnicodeEncodeError:
16551659
self.editor.SetStyling(len(value), self.tokens[token])
16561660
shift += len(value) - len(value)

utest/resources/fake.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

22
auto imports = []
33
pythonpath = []
4+
excludes = None
5+
txt number of spaces = 2
46
[Plugins]

0 commit comments

Comments
 (0)