forked from thonny/thonny
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditors.py
More file actions
1548 lines (1282 loc) · 54 KB
/
Copy patheditors.py
File metadata and controls
1548 lines (1282 loc) · 54 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import os.path
import re
import time
import tkinter as tk
import warnings
from logging import exception, getLogger
from tkinter import messagebox, simpledialog, ttk
from typing import List, Literal, Optional, Union, cast
from _tkinter import TclError
from pystart import get_runner, get_workbench
from pystart.base_file_browser import ask_backend_path, choose_node_for_file_operations
from pystart.codeview import BinaryFileException, CodeView, CodeViewText
from pystart.common import (
InlineCommand,
TextRange,
ToplevelResponse,
UserError,
is_same_path,
normpath_with_actual_case,
universal_dirname,
)
from pystart.custom_notebook import CustomNotebook, CustomNotebookPage, CustomNotebookTab
from pystart.languages import tr
from pystart.lsp_proxy import LanguageServerProxy
from pystart.lsp_types import (
DidChangeTextDocumentParams,
DidCloseTextDocumentParams,
DidOpenTextDocumentParams,
Position,
Range,
RangedTextDocumentContentChangeEvent,
TextDocumentIdentifier,
TextDocumentItem,
VersionedTextDocumentIdentifier,
)
from pystart.misc_utils import (
PLACEHOLDER_URI,
UNTITLED_URI_SCHEME,
ensure_uri,
format_untitled_uri,
is_local_uri,
is_remote_uri,
is_untitled_uri,
local_path_to_uri,
make_legacy_remote_path,
remote_path_to_uri,
running_on_mac_os,
running_on_windows,
uri_to_legacy_filename,
uri_to_long_title,
uri_to_target_path,
)
from pystart.tktextext import rebind_control_a
from pystart.ui_utils import (
askopenfilename,
asksaveasfilename,
get_beam_cursor,
parse_text_index,
select_sequence,
)
PYTHON_FILES_STR = tr("Python files")
_dialog_filetypes = [(PYTHON_FILES_STR, ".py .pyw .pyi .pyde .pyx"), (tr("all files"), ".*")]
PYTHON_EXTENSIONS = {"py", "pyw", "pyi", "pyde"}
PYTHONLIKE_EXTENSIONS = {"pyx", "pyde", "toml"}
DEBOUNCE_SECONDS = 0.5
logger = getLogger(__name__)
class EditorCodeViewText(CodeViewText):
"""Allows separate class binding for CodeViewTexts which are inside editors"""
def __init__(self, master=None, cnf={}, **kw):
super().__init__(
master=master,
cnf=cnf,
**kw,
)
self.bindtags(self.bindtags() + ("EditorCodeViewText",))
class BaseEditor(ttk.Frame):
def __init__(
self, master, uri: str, propose_remove_line_numbers: bool, suppress_events: bool = False
):
self._uri: str = uri
ttk.Frame.__init__(self, master)
self._code_view = CodeView(
self,
propose_remove_line_numbers=propose_remove_line_numbers,
font="EditorFont",
text_class=EditorCodeViewText,
cursor=get_beam_cursor(),
suppress_events=suppress_events,
)
self._code_view.grid(row=0, column=0, sticky=tk.NSEW, in_=self)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self._file_source = None
self.update_file_type()
def is_untitled(self) -> bool:
return is_untitled_uri(self.get_uri())
def is_local(self) -> bool:
return is_local_uri(self.get_uri())
def is_remote(self) -> bool:
return is_remote_uri(self.get_uri())
def get_uri(self) -> str:
return self._uri
def set_uri(self, uri: str) -> None:
self._uri = uri
self.update_file_type()
self.update_appearance()
def get_target_path(self) -> Optional[str]:
if self.is_untitled():
return None
return uri_to_target_path(self.get_uri())
def update_appearance(self):
self._code_view.set_gutter_visibility(
get_workbench().get_option("view.show_line_numbers") or get_workbench().in_simple_mode()
)
self._code_view.set_line_length_margin(
get_workbench().get_option("view.recommended_line_length")
)
self._code_view.text.update_tab_stops()
self._code_view.text.indent_width = get_workbench().get_option("edit.indent_width")
self._code_view.text.tab_width = get_workbench().get_option("edit.tab_width")
self._code_view.text.event_generate("<<UpdateAppearance>>")
self._code_view.grid_main_widgets()
def update_file_type(self):
if self.is_untitled():
self._code_view.set_file_type("python")
else:
ext = self.get_target_path().split(".")[-1].lower()
if ext in PYTHON_EXTENSIONS:
file_type = "python"
elif ext in PYTHONLIKE_EXTENSIONS:
file_type = "pythonlike"
else:
file_type = None
self._code_view.set_file_type(file_type)
def is_modified(self):
return bool(self._code_view.text.edit_modified())
def get_title(self):
if self.is_untitled():
result = format_untitled_uri(self.get_uri())
elif self.is_remote():
name = self.get_target_path().split("/")[-1]
result = "[ " + name + " ]"
else:
assert self.is_local()
result = self.shorten_filename_for_title(self.get_target_path())
if self.is_modified():
result += " *"
return result
def shorten_filename_for_title(self, path: str) -> str:
return os.path.basename(path)
def get_text_widget(self) -> CodeViewText:
return self._code_view.text
def get_code_view(self):
# TODO: try to get rid of this
return self._code_view
class Editor(BaseEditor):
def __init__(self, master: "EditorNotebook", uri: str):
self._initialized_ls_proxies: List[LanguageServerProxy] = (
get_workbench().get_initialized_ls_proxies()
)
self._primed_ls_proxies: List[LanguageServerProxy] = []
self.containing_notebook: EditorNotebook = master
super().__init__(master, uri, propose_remove_line_numbers=True)
get_workbench().event_generate(
"EditorTextCreated", editor=self, text_widget=self.get_text_widget()
)
self._last_change_time: float = 0
self._unpublished_incremental_changes = []
self._content_at_server: Optional[str] = None # for validating incremental updates
self._last_fully_published_version: Optional[int] = None
self._last_known_mtime = None
self._code_view.text.bind("<<Modified>>", self._on_text_modified, True)
self._code_view.text.bind("<<TextChange>>", self._on_text_change, True)
self._code_view.text.bind("<Control-Tab>", self._control_tab, True)
get_workbench().bind("TextInsert", self._register_text_change, True)
get_workbench().bind("TextDelete", self._register_text_change, True)
get_workbench().bind("DebuggerResponse", self._listen_debugger_progress, True)
get_workbench().bind("ToplevelResponse", self._listen_for_toplevel_response, True)
get_workbench().bind("LanguageServerInitialized", self._language_server_initialized, True)
get_workbench().bind("LanguageServerInvalidated", self._language_server_invalidated, True)
self.update_appearance()
if is_local_uri(self._uri) or is_remote_uri(self._uri):
successful_load = self._load_file(self._uri)
if not successful_load:
# Encoding errors were communicated to the user already during _load_file
raise UserError(f"Could not load {self._uri}")
else:
assert is_untitled_uri(self._uri)
self._update_language_servers()
def get_content(self, up_to_end=False) -> str:
return self._code_view.get_content(up_to_end=up_to_end)
def get_filename(self, try_hard=False):
warnings.warn(
"Editor.get_filename is deprecated. Use get_target_path instead", DeprecationWarning
)
if self.is_untitled() and try_hard:
self.save_file()
path = self.get_target_path()
if path is None:
return None
if self.is_remote():
return make_legacy_remote_path(path)
else:
return path
def get_file_source(self):
return self._file_source
def set_uri(self, uri: str) -> None:
old_uri = self._uri
if old_uri != uri:
self._disconnect_from_language_servers()
super().set_uri(uri)
self.update_title()
if old_uri != uri:
self._update_language_servers()
def check_for_external_changes(self):
if self.is_untitled() or self.is_remote():
return
if self._last_known_mtime is None:
return
elif not os.path.exists(self.get_target_path()):
self.containing_notebook.select(self)
if messagebox.askyesno(
tr("File is gone"),
tr("Looks like '%s' was deleted or moved.") % self.get_target_path()
+ "\n\n"
+ tr("Do you want to also close the editor?"),
master=self,
):
self.containing_notebook.close_editor(self)
else:
self.get_text_widget().edit_modified(True)
self._last_known_mtime = None
elif os.path.getmtime(self.get_target_path()) != self._last_known_mtime:
skip_confirmation = not self.is_modified() and get_workbench().get_option(
"edit.auto_refresh_saved_files"
)
if not skip_confirmation:
self.containing_notebook.select(self)
if skip_confirmation or messagebox.askyesno(
tr("External modification"),
tr("Looks like '%s' was modified outside of the editor.") % self.get_target_path()
+ "\n\n"
+ tr(
"Do you want to discard current editor content and reload the file from disk?"
),
master=self,
):
prev_location = self.get_text_widget().index("insert")
self._load_file(self.get_uri(), keep_undo=True)
try:
self.get_text_widget().mark_set("insert", prev_location)
self.see_line(int(prev_location.split(".")[0]))
except Exception:
logger.exception("Could not restore previous location")
self._last_known_mtime = os.path.getmtime(self.get_target_path())
def get_long_description(self):
result = uri_to_long_title(self._uri)
try:
index = self._code_view.text.index("insert")
if index and "." in index:
line, col = index.split(".")
result += " @ {} : {}".format(line, int(col) + 1)
except Exception:
exception("Finding cursor location")
return result
def _load_file(self, uri: str, keep_undo=False):
try:
if is_remote_uri(uri):
result = self._load_remote_file(uri_to_target_path(uri))
else:
result = self._load_local_file(uri_to_target_path(uri), keep_undo)
if not result:
return False
self.set_uri(uri)
except BinaryFileException:
messagebox.showerror(
tr("Problem"), tr("%s doesn't look like a text file") % (uri,), master=self
)
return False
except SyntaxError as e:
assert "encoding" in str(e).lower()
messagebox.showerror(
tr("Problem loading file"),
tr(
"This file seems to have problems with encoding.\n\n"
"Make sure it is in UTF-8 or contains proper encoding hint."
),
master=self,
)
return False
self._update_language_servers()
self.update_appearance()
self._update_file_source()
return True
def _load_local_file(self, path, keep_undo=False):
if os.path.exists(path):
with open(path, "rb") as fp:
source = fp.read()
exists = True
else:
source = b""
exists = False
# Make sure Windows filenames have proper format
path = normpath_with_actual_case(path)
if exists:
self._last_known_mtime = os.path.getmtime(path)
get_workbench().event_generate(
"Open", editor=self, uri=local_path_to_uri(path), filename=path
)
if not self._code_view.set_content_as_bytes(source, keep_undo):
return False
self.get_text_widget().edit_modified(not exists)
self._code_view.focus_set()
get_workbench().get_editor_notebook().remember_recent_file(path)
get_workbench().event_generate(
"Opened", editor=self, uri=local_path_to_uri(path), filename=path
)
return True
def _load_remote_file(self, remote_path):
self._code_view.set_content("")
self._code_view.text.set_read_only(True)
response = get_runner().send_command_and_wait(
InlineCommand(
"read_file", path=remote_path, description=tr("Loading %s") % remote_path
),
dialog_title=tr("Loading"),
)
if response.get("error"):
# TODO: make it softer
raise RuntimeError(response["error"])
content = response["content_bytes"]
self._code_view.text.set_read_only(False)
if not self._code_view.set_content_as_bytes(content):
return False
self.get_text_widget().edit_modified(False)
return True
def save_file_enabled(self):
return self.is_modified() or self.is_untitled()
def save_file(self, ask_target=False, save_copy=False, node=None) -> Optional[str]:
if not self.is_untitled() and not ask_target:
save_uri = self._uri
get_workbench().event_generate(
"Save",
editor=self,
uri=save_uri,
filename=uri_to_legacy_filename(save_uri),
)
else:
save_uri = self.ask_new_uri(node)
if not save_uri:
return None
if self.containing_notebook.get_editor(save_uri) is not None:
messagebox.showerror(
tr("File is open"),
tr(
"This file is already open in PyStart.\n\n"
"If you want to save with this name,\n"
"close the existing editor first!"
),
master=get_workbench(),
)
return None
get_workbench().event_generate(
"SaveAs",
editor=self,
uri=save_uri,
filename=uri_to_legacy_filename(save_uri),
save_copy=save_copy,
)
content_bytes = self._code_view.get_content_as_bytes()
if is_remote_uri(save_uri):
result = self.write_remote_file(uri_to_target_path(save_uri), content_bytes, save_copy)
else:
result = self.write_local_file(uri_to_target_path(save_uri), content_bytes, save_copy)
if not result:
return None
if not save_copy:
self.set_uri(save_uri)
if not save_copy or self._uri == save_uri:
self.update_title()
get_workbench().event_generate(
"Saved", editor=self, uri=save_uri, filename=uri_to_legacy_filename(save_uri)
)
self._update_file_source()
return save_uri
def write_local_file(self, target_path, content_bytes, save_copy):
process_shebang = content_bytes.startswith(b"#!/") and get_workbench().get_option(
"file.make_saved_shebang_scripts_executable"
)
if process_shebang:
content_bytes = content_bytes.replace(b"\r\n", b"\n")
try:
f = open(target_path, mode="wb")
f.write(content_bytes)
f.flush()
# Force writes on disk, see https://learn.adafruit.com/adafruit-circuit-playground-express/creating-and-editing-code#1-use-an-editor-that-writes-out-the-file-completely-when-you-save-it
os.fsync(f)
f.close()
if process_shebang:
os.chmod(target_path, 0o755)
if not save_copy or target_path == self.get_target_path():
self._last_known_mtime = os.path.getmtime(target_path)
get_workbench().event_generate("LocalFileOperation", path=target_path, operation="save")
except PermissionError:
messagebox.showerror(
tr("Permission Error"),
tr("Looks like this file or folder is not writable."),
master=self,
)
return False
if not save_copy or target_path == self.get_target_path():
self.containing_notebook.remember_recent_file(target_path)
if not save_copy or target_path == self.get_target_path():
self._code_view.text.edit_modified(False)
return True
def write_remote_file(self, target_path, content_bytes, save_copy):
if get_runner().ready_for_remote_file_operations(show_message=True):
result = get_runner().send_command_and_wait(
InlineCommand(
"write_file",
path=target_path,
content_bytes=content_bytes,
editor_id=id(self),
blocking=True,
description=tr("Saving to %s") % target_path,
make_shebang_scripts_executable=get_workbench().get_option(
"file.make_saved_shebang_scripts_executable"
),
),
dialog_title=tr("Saving"),
)
if result is None:
result = {"error": "Unknown error"}
if "error" in result:
messagebox.showerror(tr("Could not save"), str(result["error"]))
return False
if not save_copy:
self._code_view.text.edit_modified(False)
self.update_title()
# NB! edit_modified is not falsed yet!
get_workbench().event_generate(
"RemoteFileOperation", path=target_path, operation="save"
)
get_workbench().event_generate("RemoteFilesChanged")
return True
else:
messagebox.showerror(tr("Could not save"), tr("Back-end is not ready"))
return False
def ask_new_uri(self, node=None) -> Optional[str]:
if node is None:
node = choose_node_for_file_operations(self.winfo_toplevel(), tr("Where to save to?"))
if not node:
return None
if node == "local":
path = self.ask_new_local_path()
if path is not None:
return local_path_to_uri(path)
else:
assert node == "remote"
path = self.ask_new_remote_path()
if path is not None:
return remote_path_to_uri(path)
return None
def ask_new_remote_path(self) -> Optional[str]:
target_path = ask_backend_path(self.winfo_toplevel(), "save", filetypes=_dialog_filetypes)
if target_path:
target_path = self._check_add_py_extension(target_path)
return make_legacy_remote_path(target_path)
else:
return None
def ask_new_local_path(self) -> Optional[str]:
if self.is_untitled():
initialdir = get_workbench().get_local_cwd()
initialfile = None
else:
initialdir = os.path.dirname(self.get_target_path())
initialfile = os.path.basename(self.get_target_path())
# https://tcl.tk/man/tcl8.6/TkCmd/getOpenFile.htm
type_var = tk.StringVar(value="")
new_filename = asksaveasfilename(
filetypes=_dialog_filetypes,
defaultextension=None,
initialdir=initialdir,
initialfile=initialfile,
parent=get_workbench(),
typevariable=type_var,
)
logger.info("Save dialog returned %r with typevariable %r", new_filename, type_var.get())
# Different tkinter versions may return different values
if new_filename in ["", (), None]:
return None
if running_on_windows():
# may have /-s instead of \-s and wrong case
new_filename = os.path.join(
normpath_with_actual_case(os.path.dirname(new_filename)),
os.path.basename(new_filename),
)
if type_var.get() == PYTHON_FILES_STR or type_var.get() == "":
new_filename = self._check_add_py_extension(
new_filename, without_asking=type_var.get() == PYTHON_FILES_STR
)
if new_filename.endswith(".py"):
base = os.path.basename(new_filename)
mod_name = base[:-3].lower()
if running_on_windows():
mod_name = mod_name.lower()
if mod_name in [
"math",
"turtle",
"random",
"statistics",
"pygame",
"matplotlib",
"numpy",
]:
# More proper name analysis will be performed by ProgramNamingAnalyzer
if not tk.messagebox.askyesno(
tr("Potential problem"),
tr(
"If you name your script '%s', "
"you won't be able to import the library module named '%s'"
)
% (base, mod_name)
+ ".\n\n"
+ tr("Do you still want to use this name for your script?"),
master=self,
):
return self.ask_new_local_path()
return new_filename
def show(self):
self.containing_notebook.select(self)
def close(self):
self._disconnect_from_language_servers()
self.destroy()
def _disconnect_from_language_servers(self) -> None:
for ls_proxy in self._primed_ls_proxies:
logger.info("Disconnecting from %s", ls_proxy)
ls_proxy.notify_did_close_text_document(
DidCloseTextDocumentParams(TextDocumentIdentifier(uri=self.get_uri()))
)
self._primed_ls_proxies = []
self._unpublished_incremental_changes = []
self._last_fully_published_version = None
self._content_at_server = None
def _listen_debugger_progress(self, event):
# Go read-only
# TODO: check whether this module is active?
self._code_view.text.set_read_only(True)
def _listen_for_toplevel_response(self, event: ToplevelResponse) -> None:
self._code_view.text.set_read_only(False)
def _control_tab(self, event):
if event.state & 1: # shift was pressed
direction = -1
else:
direction = 1
self.containing_notebook.select_next_prev_editor(direction)
return "break"
def _shift_control_tab(self, event):
self.containing_notebook.select_next_prev_editor(-1)
return "break"
def select_range(self, text_range):
self._code_view.select_range(text_range)
def select_line(self, lineno, col_offset=None):
self._code_view.select_range(TextRange(lineno, 0, lineno + 1, 0))
self.see_line(lineno)
if col_offset is None:
col_offset = 0
self.get_text_widget().mark_set("insert", "%d.%d" % (lineno, col_offset))
def see_line(self, lineno):
# first see an earlier line in order to push target line downwards
self._code_view.text.see("%d.0" % max(lineno - 4, 1))
self._code_view.text.see("%d.0" % lineno)
def focus_set(self):
self._code_view.focus_set()
def is_focused(self):
return self.focus_displayof() == self._code_view.text
def _on_text_modified(self, event):
self.update_title()
def update_title(self):
if self.containing_notebook.has_content(self):
self.containing_notebook.update_editor_title(self)
def _on_text_change(self, event):
# may not be added to the Notebook yet
if self.containing_notebook.has_content(self):
self.update_title()
def _register_text_change(self, event):
if self._code_view.text is not event["text_widget"]:
return
self._last_change_time = time.time()
if self._last_fully_published_version is not None:
# meaning the changes should be collected
self._unpublished_incremental_changes.append(event)
self.after(int(DEBOUNCE_SECONDS * 1000), self._consider_sending_changes_to_server)
def destroy(self):
get_workbench().unbind("DebuggerResponse", self._listen_debugger_progress)
get_workbench().unbind("ToplevelResponse", self._listen_for_toplevel_response)
ttk.Frame.destroy(self)
get_workbench().event_generate(
"EditorTextDestroyed", editor=self, text_widget=self.get_text_widget()
)
def _check_add_py_extension(self, path: str, without_asking: bool = False) -> str:
assert path
parts = re.split(r"[/\\]", path)
name = parts[-1]
if "." not in name:
if without_asking or messagebox.askyesno(
title=tr("Confirmation"),
message=tr("Python files usually have .py extension.")
+ "\n\n"
+ tr("Did you mean '%s'?" % (name + ".py")),
parent=self.winfo_toplevel(),
):
return path + ".py"
else:
return path
return path
def _update_file_source(self):
if self.is_remote():
proxy = get_runner().get_backend_proxy()
if proxy is not None:
self._file_source = get_runner().get_backend_proxy().get_machine_id()
else:
logger.warning("update_file_source: no proxy, leaving as is")
else:
self._file_source = "-" # should not match any machine id
def _language_server_initialized(self, ls_proxy: LanguageServerProxy) -> None:
logger.info("Registering initialized language server %s", ls_proxy)
self._initialized_ls_proxies.append(ls_proxy)
self._update_language_servers()
def _language_server_invalidated(self, ls_proxy: LanguageServerProxy) -> None:
if ls_proxy in self._initialized_ls_proxies:
self._initialized_ls_proxies.remove(ls_proxy)
if ls_proxy in self._primed_ls_proxies:
self._primed_ls_proxies.remove(ls_proxy)
def _get_version_to_be_published(self) -> int:
return (
1
if self._last_fully_published_version is None
else self._last_fully_published_version + 1
)
def _update_language_servers(self) -> None:
self.send_changes_to_primed_servers()
for ls_proxy in self._initialized_ls_proxies:
if (
ls_proxy not in self._primed_ls_proxies
and self.get_language_id() in ls_proxy.get_supported_language_ids()
):
self._prime_language_server(ls_proxy)
self._unpublished_incremental_changes = []
self._content_at_server = self.get_content()
get_workbench().event_generate("AfterSendingDocumentUpdates", uri=self.get_uri())
self._last_fully_published_version = self._get_version_to_be_published()
def _prime_language_server(self, ls_proxy: LanguageServerProxy) -> None:
logger.info("Connecting %r to language server %s", self.get_uri(), ls_proxy)
assert ls_proxy not in self._primed_ls_proxies
current_content = self.get_content(up_to_end=True)
ls_proxy.notify_did_open_text_document(
DidOpenTextDocumentParams(
textDocument=TextDocumentItem(
version=self._get_version_to_be_published(),
uri=self.get_uri(),
text=current_content,
languageId=self.get_language_id(),
)
)
)
self._primed_ls_proxies.append(ls_proxy)
def _consider_sending_changes_to_server(self, event=None):
time_since_last_change = time.time() - self._last_change_time
if time_since_last_change >= DEBOUNCE_SECONDS:
self.send_changes_to_primed_servers()
else:
wait_time = DEBOUNCE_SECONDS - time_since_last_change
self.after(int(wait_time * 1000), self._consider_sending_changes_to_server)
def send_changes_to_primed_servers(self) -> None:
if not self._unpublished_incremental_changes:
logger.debug("No unpublished changes")
return
if not self._primed_ls_proxies:
logger.debug("No primed proxies, not sending changes")
return
logger.debug("Publishing %s events", len(self._unpublished_incremental_changes))
ls_changes = []
for change in self._unpublished_incremental_changes:
logger.info("processing change %r", change)
if change["sequence"] == "TextInsert":
line, col = parse_text_index(change["index"])
pos = Position(line=line - 1, character=col) # TODO: Utf-16
ls_change = RangedTextDocumentContentChangeEvent(
range=Range(start=pos, end=pos), text=change["text"]
)
else:
assert change["sequence"] == "TextDelete"
start_line, start_col = parse_text_index(change["index1"])
end_line, end_col = parse_text_index(change["index2"])
ls_change = RangedTextDocumentContentChangeEvent(
range=Range(
start=Position(line=start_line - 1, character=start_col),
end=Position(line=end_line - 1, character=end_col),
),
text="",
)
ls_changes.append(ls_change)
version = self._get_version_to_be_published()
for ls_proxy in self._primed_ls_proxies:
ls_proxy.notify_did_change_text_document(
DidChangeTextDocumentParams(
textDocument=VersionedTextDocumentIdentifier(
version=version, uri=self.get_uri()
),
contentChanges=ls_changes,
)
)
self._unpublished_incremental_changes = []
def get_language_id(self) -> str:
return self.get_text_widget().file_type
class EditorNotebook(CustomNotebook):
"""
Manages opened files / modules
"""
def __init__(self, master):
super().__init__(master)
self._untitled_name_counter: int = 0
get_workbench().set_default("file.reopen_files", True)
get_workbench().set_default("file.open_files", [])
get_workbench().set_default("file.current_file", None)
get_workbench().set_default("file.recent_files", [])
get_workbench().set_default("view.highlight_current_line", False)
get_workbench().set_default("view.show_line_numbers", True)
get_workbench().set_default("view.recommended_line_length", 0)
get_workbench().set_default("edit.indent_with_tabs", False)
get_workbench().set_default("edit.auto_refresh_saved_files", True)
get_workbench().set_default("edit.indent_width", 4)
get_workbench().set_default("edit.tab_width", 4)
get_workbench().set_default("file.make_saved_shebang_scripts_executable", True)
self._recent_menu = tk.Menu(
get_workbench().get_menu("file"), postcommand=self._update_recent_menu
)
self._init_commands()
self.enable_traversal()
# open files from last session
""" TODO: they should go only to recent files
for filename in prefs["open_files"].split(";"):
if os.path.exists(filename):
self._open_file(filename)
"""
# should be in the end, so that it can be detected when
# constructor hasn't completed yet
self._checking_external_changes = False
get_workbench().bind("WindowFocusIn", self.check_for_external_changes, True)
get_workbench().bind("ToplevelResponse", self.check_for_external_changes, True)
self.bind("<<NotebookTabChanged>>", self.on_tab_changed, True)
def on_tab_changed(self, *args):
# Required to avoid incorrect sizing of parent panes
self.update_idletasks()
def _init_commands(self):
# TODO: do these commands have to be in EditorNotebook ??
# Create a module level function install_editor_notebook ??
# Maybe add them separately, when notebook has been installed ??
get_workbench().add_command(
"new_file",
"file",
tr("New"),
self._cmd_new_file,
caption=tr("New"),
default_sequence=select_sequence("<Control-n>", "<Command-n>"),
extra_sequences=["<Control-Greek_nu>"],
group=10,
image="new-file",
include_in_toolbar=True,
)
get_workbench().add_command(
"open_file",
"file",
tr("Open..."),
self._cmd_open_file,
caption=tr("Load"),
default_sequence=select_sequence("<Control-o>", "<Command-o>"),
extra_sequences=["<Control-Greek_omicron>"],
group=10,
image="open-file",
include_in_toolbar=True,
)
get_workbench().add_command(
"recents", "file", tr("Recent files"), group=10, submenu=self._recent_menu
)
# http://stackoverflow.com/questions/22907200/remap-default-keybinding-in-tkinter
get_workbench().bind_class("Text", "<Control-o>", self._control_o)
get_workbench().bind_class("Text", "<Control-Greek_omicron>", self._control_o)
rebind_control_a(get_workbench())
get_workbench().add_command(
"close_file",
"file",
tr("Close"),
self._cmd_close_file,
default_sequence=select_sequence("<Control-w>", "<Command-w>"),
extra_sequences=["<Control-Greek_finalsmallsigma>"],
tester=lambda: self.get_current_editor() is not None,
group=10,
)
get_workbench().add_command(
"close_files",
"file",
tr("Close all"),
self.close_tabs,
tester=lambda: self.get_current_editor() is not None,
default_sequence=select_sequence("<Control-W>", "<Command-Alt-w>"),
group=10,
)
get_workbench().add_command(
"save_file",
"file",
tr("Save"),
self._cmd_save_file,
caption=tr("Save"),
default_sequence=select_sequence("<Control-s>", "<Command-s>"),
extra_sequences=["<Control-Greek_sigma>"],
tester=self._cmd_save_file_enabled,
group=10,
image="save-file",
include_in_toolbar=True,
)
get_workbench().add_command(
"save_all_files",
"file",
tr("Save All files"),
self._cmd_save_all_files,
caption=tr("Save All files"),
default_sequence=select_sequence("<Control-Alt-s>", "<Command-Alt-s>"),
tester=self._cmd_save_all_files_enabled,
group=10,
)