-
Notifications
You must be signed in to change notification settings - Fork 1
/
cs-script.py
1548 lines (1260 loc) · 60.7 KB
/
cs-script.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
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
import os
import io
import codecs
import sys
import html
import time
import sublime
import sublime_plugin
import subprocess
import shutil
import signal
import threading
from subprocess import Popen, PIPE, STDOUT
from os import path
# https://www.sublimetext.com/docs/1/api-reference
version = '1.3.4' # build 0
os.environ["PACKAGE_VERSION"] = version
from .imports.utils import * # should be imported after environ["PACKAGE_VERSION"] is set
new_deployment = (not os.path.isdir(path.join(bin_dest+'cs-script_v'+version)))
# -----------------
bash_string = r'''#!/bin/bash
dotnet /home/user/.config/sublime-text/Packages/User/cs-script/cs-script_v1.2.3/cscs.dll -help > /home/user/.config/sublime-text/Packages/User/cs-script/help.txt
'''
# output = subprocess.check_output(bash_string, shell=True, executable='/bin/bash')
# -----------------
if sys.version_info < (3, 3): raise RuntimeError('CS-Script.ST3 works with Sublime Text 3 only')
# -------------------------
def is_script_file(file):
return file.endswith(".cs") or file.endswith(".csx")
def settings():
return sublime.load_settings("cs-script.sublime-settings")
def save_settings():
return sublime.save_settings("cs-script.sublime-settings")
def on_plugin_loaded():
Runtime.init(version, new_deployment)
print_config()
sublime.set_timeout_async(lambda: check_environment(False))
# -------------------------
class CodeViewTextCommand(sublime_plugin.TextCommand):
# -----------------
def is_enabled(self):
return is_csharp(self.view)
# -----------------
def is_visible(self):
panel_name = self.view.window().active_panel()
if panel_name and panel_name == 'output.CS-Script':
panel = self.view.window().find_output_panel(panel_name[len('output.'):])
return not(panel is not None and panel.id() == self.view.id())
else:
return True
# -------------------------
class csscript_check_deployment(sublime_plugin.TextCommand):
def run(self, edit):
check_environment(True)
# -------------------------
def clear_old_versions_but(version):
if new_deployment:
try:
if os.name == 'nt':
os.system('taskkill /f /im VBCSCompiler.exe') # stop roslyn server if it is runningif os.name == 'nt':
os.system('taskkill /f /im syntaxer.exe') # stop syntaxer
except:
pass
def delete_file(file):
if os.path.isfile(file):
os.remove(file)
# old cs-script engine, prior .NET 6 migration
delete_file(path.join(bin_dest, 'cscs.exe'))
delete_file(path.join(bin_dest, 'css_config.xml'))
delete_file(path.join(bin_dest, 'CSSRoslynProvider.dll'))
sub_dirs = [name for name in os.listdir(bin_dest)
if os.path.isdir(os.path.join(bin_dest, name))]
for dir in sub_dirs:
if dir.startswith('syntaxer') and not dir.endswith(version):
try:
shutil.rmtree(path.join(bin_dest, dir))
except:
pass
for dir in sub_dirs:
if dir.startswith('cs-script') and not dir.endswith(version):
try:
shutil.rmtree(path.join(bin_dest, dir))
except:
pass
# -------------------------
def ensure_default_config(csscriptApp):
config_file = path.join(path.dirname(Runtime.cscs_path), 'css_config.xml')
if not path.exists(config_file):
subprocess.Popen(to_args([Runtime.cscs_path, '-config:create']),
stdout=subprocess.PIPE,
cwd=path.dirname(Runtime.cscs_path),
shell=True).wait()
updated_config = ''
with open(config_file, "r") as f:
updated_config = f.read()
if os.name == 'nt':
updated_config = updated_config.replace("<useAlternativeCompiler></useAlternativeCompiler>",
"<useAlternativeCompiler>CSSRoslynProvider.dll</useAlternativeCompiler>")
need_explicit_tuple_ref = False
if need_explicit_tuple_ref:
updated_config = updated_config.replace("</defaultRefAssemblies>",
" %syntaxer_dir%"+os.sep+"System.ValueTuple.dll</defaultRefAssemblies>")
else:
updated_config = updated_config.replace(" %syntaxer_dir%"+os.sep+"System.ValueTuple.dll", "")
updated_config = updated_config.replace("<roslynDir></roslynDir>",
"<roslynDir>%syntaxer_dir%</roslynDir>")
with open(config_file, "w") as file:
file.write(updated_config)
else:
# update existing config to be compatible with the current cscs.exe
if os.getenv("new_deployment") == 'true':
with open(config_file, "r") as f:
updated_config = f.read()
updated_config = updated_config.replace("%syntaxer_dir%"+os.sep+"System.ValueTuple.dll", "")
with open(config_file, "w") as file:
file.write(updated_config)
# -------------------------
def ensure_default_roslyn_config(csscriptApp):
if os.getenv("new_deployment") == 'true':
if os.name == 'nt':
subprocess.Popen(to_args([Runtime.cscs_path, '-config:set:RoslynDir="'+current_bin_dest+'"']),
stdout=subprocess.PIPE,
cwd=path.dirname(Runtime.cscs_path),
shell=True).wait()
subprocess.Popen(to_args([Runtime.cscs_path, '-config:set:useAlternativeCompiler=CSSRoslynProvider.dll']),
stdout=subprocess.PIPE,
cwd=path.dirname(Runtime.cscs_path),
shell=True).wait()
# -------------------------
def deploy_shadow_dir(src_subdir, dest_subdir):
def copy_and_overwrite(from_path, to_path):
if os.path.exists(to_path):
shutil.rmtree(to_path)
shutil.copytree(from_path, to_path)
if not path.exists(bin_dest):
os.makedirs(bin_dest)
dest_dir = bin_dest
if dest_subdir:
dest_dir = path.join(dest_dir, dest_subdir)
if path.exists(dest_dir):
return
else:
os.makedirs(dest_dir)
src = path.join(bin_src, src_subdir)
dest = path.join(bin_dest, dest_subdir)
try:
# print('deploying', dest)
copy_and_overwrite(src, dest)
except Exception as ex :
print('deploy_shadow_bin', ex)
pass
return dest
shutil.copytree(dir_name, dest)
deploy_shadow_dir('cs-script',"cs-script_v"+version)
deploy_shadow_dir('cs-syntaxer', "syntaxer_v"+version)
clear_old_versions_but(version)
showTooltipOverGutter = settings().get('show_tooltip_over_gutter', True)
# -------------------------
def print_config():
print('----------------')
print('cscs: ', Runtime.cscs_path)
syntaxer_print_config()
print('syntaxcheck_on_save: ', settings().get('syntaxcheck_on_save', True))
print('server_autostart: ', settings().get('server_autostart', True))
print('----------------')
# start_syntax_server()
sublime.status_message('Starting syntaxer server...')
args = ['dotnet']
args.append(Runtime.syntaxer_path)
args.append('-listen')
args.append('-port:'+str(Runtime.syntaxer_port))
args.append('-timeout:3000')
args.append('-client:{0}'.format(os.getpid()))
args.append('-cscs_path:{0}'.format(Runtime.cscs_path))
args = to_args(args)
start = time.time()
subprocess.Popen(args, shell=True)
print('>', args)
print('> Syntaxer server started:', time.time()-start, 'seconds')
# -------------------------
from .imports.syntaxer import *
from .imports.setup import *
csscript_setup.version = version
# =================================================================================
# TODO
# Detect upgrade and fresh install for showing readme/help
# csscript_execute_and_wait should only be available on windows
# =================================================================================
formatted_views = {}
def is_formatted(view):
if view.id() in formatted_views.keys():
last_format_time = formatted_views[view.id()]
return time.time() - last_format_time < 2
return False
def mark_as_formatted(view):
formatted_views[view.id()] = time.time()
def is_sdk_available():
try:
def onOutput(line):
global result
result = line.strip()
execute(['dotnet', "--list-sdks"], onOutput)
# 6.0.101 [C:\Program Files\dotnet\sdk]
# 6.0.201 [/snap/dotnet-sdk/158/sdk]
print(result)
return len(result) > 0
except Exception as e:
print(e)
return False
# =================================================================================
# C#/CS-Script plugin "new script" service
# =================================================================================
class csscript_new(sublime_plugin.TextCommand):
# -----------------
def run(self, edit):
backup_file = None
if os.path.exists(new_file_path):
backup_file = new_file_path+'.bak'
if os.path.exists(backup_file):
os.remove(backup_file)
os.rename(new_file_path, backup_file)
content = ''
if backup_file:
content = '// The previous content of this file has been saved into: '+backup_file+' \n\n'
sample_type = ''
if is_sdk_available():
content += "//css_engine csc\n"
sample_type = 'toplevel'
else:
sample_type = 'console'
subprocess\
.Popen(to_args(['dotnet', Runtime.cscs_path, '-new:'+sample_type, new_file_path]), stdout=subprocess.PIPE, shell=True)\
.wait()
with open(new_file_path, "r") as f:
content = content + f.read()
with open(new_file_path, "w") as file:
file.write(content)
if os.path.exists(new_file_path):
sublime.active_window().open_file(new_file_path)
# =================================================================================
# C#/CS-Script plugin help service
# =================================================================================
class csscript_help(sublime_plugin.TextCommand):
# -----------------
def run(self, edit):
file = csscript_setup.prepare_readme()
if os.path.exists(file):
sublime.active_window().open_file(file)
else:
sublime.error_message('Cannot find '+file)
# =================================================================================
# C#/CS-Script CS-Script help service
# =================================================================================
class csscript_css_help(sublime_plugin.TextCommand):
# -----------------
def run(self, edit):
if is_linux() and settings().get('allow_large_stdout_on_linux', False) != True:
execute_in_terminal(["dotnet", Runtime.cscs_path, "-help"])
output_view_show(out_panel)
output_view_clear(out_panel)
output_view_write_line(out_panel, "It has been observed that Sublime Text Python interpreter (on some specs of Linux in virtual environments) exhibits difficulties "\
"capturing subprocess output of a significant size. Thus the automatic generation of the documentation is ST is disabled.")
output_view_write_line(out_panel, "You can access CS-Script help by executing the following manual command in the terminal:")
output_view_write_line(out_panel, "")
output_view_write_line(out_panel, "dotnet "+Runtime.cscs_path+" -help")
output_view_write_line(out_panel, "")
output_view_write_line(out_panel, "You can stil enable the automatic documentation generation by setting the plugin setting `allow_large_stdout_on_linux` to `true`.")
# The next line works perfectly in terminal and in ST
# retval = os.system('ls -l > /home/user/.config/sublime-text/Packages/User/cs-script/help.txt ')
# The next 3 lines work perfectly in terminal but none in ST
# retval = os.system('dotnet /home/user/.config/sublime-text/Packages/User/cs-script/cs-script_v1.2.3/cscs.dll -help -out:/home/user/.config/sublime-text/Packages/User/cs-script/help.txt ')
# retval = os.system('dotnet /home/user/.config/sublime-text/Packages/User/cs-script/cs-script_v1.2.3/cscs.dll -help > /home/user/.config/sublime-text/Packages/User/cs-script/help.txt ')
# retval = os.system('dotnet /home/user/.config/sublime-text/Packages/User/cs-script/cs-script_v1.2.3/cscs.dll -help ')
# The next line aslso works perfectly in terminal and in ST. The only difference between `--version` and `-help` is the larger amout of the output for `-help`
# retval = os.system('dotnet /home/user/.config/sublime-text/Packages/User/cs-script/cs-script_v1.2.3/cscs.dll --version > /home/user/.config/sublime-text/Packages/User/cs-script/help.txt')
# output_view_write_line(out_panel, str(retval))
return
file = os.path.join(plugin_dir, 'cs-script.help.txt')
with open(file, "w") as out:
def onOutput(line): out.write(line)
execute(['dotnet', Runtime.cscs_path, '-help'], onOutput)
if os.path.exists(file):
sublime.active_window().open_file(file)
else:
sublime.error_message('Cannot find '+file)
# =================================================================================
# C#/CS-Script syntaxer restart service
# =================================================================================
class settings_listener(sublime_plugin.EventListener):
hooked = False
# -----------------
def on_activated(self, view):
if not settings_listener.hooked:
settings_listener.hooked = True
on_plugin_loaded()
self.callback()
os.environ['CSSCRIPT_SYNTAXER_PORT'] = str(Runtime.syntaxer_port)
settings().add_on_change("cscs_path", self.callback)
settings().add_on_change("server_port", self.on_port_changed)
def on_port_changed(self):
# may be fired when setting are not available yet
try:
port = settings().get('server_port', 18000)
if Runtime.syntaxer_port != port:
Runtime.syntaxer_port = port
os.environ['CSSCRIPT_SYNTAXER_PORT'] = str(Runtime.syntaxer_port)
except :
pass
def callback(self):
sublime.set_timeout_async(self.process_settings_change, 100)
def process_settings_change(self):
# may be fired when setting are not available yet
try:
if Runtime.cscs_path != settings().get('cscs_path', '<none>'):
read_engine_config()
# sublime.error_message('About to send '+Runtime.cscs_path)
set_engine_path(Runtime.cscs_path)
if settings().get('suppress_embedded_nuget_execution', False):
# the default nuget app on Linux (e.g. Mint 18) is incompatible with std.out redirection.
# This is valid for both both Python and .NET apps hosted by ST3. So suppress execution of 'nuget'
# by cscs.exe internally for resolving packages.
if os.name != 'nt':
os.environ["NUGET_INCOMPATIBLE_HOST"] = 'true'
else:
try:
os.unsetenv('NUGET_INCOMPATIBLE_HOST')
except Exception as e:
pass
ensure_default_config(Runtime.cscs_path)
ensure_default_roslyn_config(Runtime.cscs_path)
if os.getenv("new_deployment") != 'true' and os.getenv("engine_preloaded") != 'true':
os.environ["engine_preloaded"] = 'true'
# Preloading only improves the initial overhead for compiling but not for the intellisense.
# Important: must to wait a bit to allow Roslyn binaries to be done copied (if they ar being moved)
sublime.set_timeout(preload_engine, 5000)
except:
pass
# =================================================================================
# C#/CS-Script completion service
# =================================================================================
class csscript_listener(sublime_plugin.EventListener):
test_count = 0
suppress_post_save_checking = False
post_save_jobs = []
# -----------------
def __init__(self, *args, **kwargs):
sublime_plugin.EventListener.__init__(self, *args, **kwargs)
# -----------------
def on_activated(self, view):
pass
# -----------------
def on_window_command(self, vindow, command_name, args):
pass
# -----------------
def on_hover(self, view, point, hover_zone):
if is_output_panel(view) and view == sublime.active_window().find_output_panel(out_panel):
csscript_show_tooltip(view, point).do_output_panel()
elif is_csharp(view):
if hover_zone == sublime.HOVER_TEXT:
csscript_show_tooltip(view, point).do()
if showTooltipOverGutter and hover_zone == sublime.HOVER_GUTTER:
csscript_show_tooltip(view, point).do_gutter()
# -----------------
def on_post_text_command(self, view, command_name, args):
if command_name == 'drag_select':
if is_output_panel(view):
if 'by' in args.keys() and args['by'] == 'words':
try:
point = view.sel()[0].begin()
line_region = view.line(point)
line_text = view.substr(line_region)
view.sel().clear()
view.sel().add(line_region)
sublime.status_message('Navigating to clicked item...')
navigate_to_file_ref(line_text)
except:
pass
# -----------------
def on_load_async(self, view):
csscript_syntax_check.show_errors()
# view.assign_syntax('Packages/C#/C#.tmLanguage')
# view.assign_syntax('Packages/Text/Plain text.tmLanguage')
# -----------------
def on_modified(self, view):
if is_csharp(view):
# >>> view.scope_name(view.sel()[0].begin())
# string scope
# 'source.cs meta.class.source.cs meta.class.body.source.cs meta.method.source.cs meta.method.body.source.cs meta.method-call.source.cs string.quoted.double.source.cs '
# comment scope
# 'source.cs meta.class.source.cs meta.class.body.source.cs meta.method.source.cs meta.method.body.source.cs comment.line.double-slash.source.cs '
# comment.line.double-slash.source.cs
# string.quoted.double.source.cs
point = view.sel()[0].begin()
scope = view.scope_name(point)
inside_of_string = "string.quoted" in scope
inside_of_comment = "comment.line" in scope or "comment.block" in scope
if not inside_of_string and not inside_of_comment:
typed_char = view.substr(point-1)
if '.' == typed_char:
if settings().get('auto_trigger_autocomplete', True):
view.window().run_command("auto_complete")
elif ('(' == typed_char or ',' == typed_char) :
if settings().get('auto_trigger_tooltip', True):
view.window().run_command("csscript_pop_tooltip")
# -----------------
def on_post_save(self, view):
# if not is_csharp(view):m
# print('---------------')
# clear_console()
# sublime.log_commands(True)
if is_csharp(view):
# print('> on_post_save')
# view may be engaged in 'focus changing' activity (e.g. CodeMap)
# for i in range(5):
# if not is_csharp(sublime.active_window().active_view()):
# time.sleep(1)
active_view = sublime.active_window().active_view()
if active_view == view:
if not is_formatted(view) and settings().get('format_on_save', True):
# print('> formatting')
mark_as_formatted(view)
view.run_command("csscript_format_code")
view.run_command("save")
elif settings().get('syntaxcheck_on_save', True):
# print('>>>> syntax check')
view.window().run_command("csscript_syntax_check", {'skip_saving':True})
# -----------------
def is_enabled(self):
return is_csharp(sublime.active_window().active_view())
# -----------------
def on_query_completions(self, view, prefix, locations):
curr_doc = view.file_name()
if is_script_file(curr_doc):
completions = []
if not is_valid_selection(view):
sublime.status_message('Incompatible selection')
return completions
(curr_doc, location, as_temp_file) = get_saved_doc(view)
response = send_completion_request(curr_doc, location)
if as_temp_file:
os.remove(curr_doc)
completions = self.parse_response(response)
if completions:
return (completions, sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS)
# -----------------
def parse_response(self, response):
if not response:
return None
completions = []
error = None
for line in response.split('\n'):
if line.startswith('<error>'):
error = "Error: cannot get C# completion from the syntax server\n"
if not error:
parts = line.strip().split('|')
if len(parts) == 2:
completions.append((parts[0], parts[1]))
else:
error += line.replace('<error>', '')
if error:
print(error)
return completions
# =================================================================================
# CS-Script plugin settings
# =================================================================================
class csscript_show_settings(sublime_plugin.TextCommand):
# -----------------
def run(self, edit):
self.view.window().open_file(plugin_settings_file)
# =================================================================================
# CS-Script code show cs-script config
# =================================================================================
class csscript_show_config(sublime_plugin.TextCommand):
# -----------------
def run(self, edit):
run_cscs(\
['-config'], \
handle_line = lambda line: output_view_write_line(out_panel, line), \
on_done=lambda: sublime.show(1, 1), \
header= 'CS-Script configuration (see `https://github.com/oleg-shilo/cs-script/wiki/CS-Script---Command-Line-Interface#-configoption`)')
# =================================================================================
# CS-Script code formatter service
# =================================================================================
class csscript_format_code(CodeViewTextCommand):
# -----------------
def run(self, edit):
mark_as_formatted(self.view)
sublime.status_message('Formatting script "'+self.view.file_name()+'"')
# position in text and in file mey not be the same depending on line endings type
text_location = -1
if len(self.view.sel()) > 0:
text_location = self.view.sel()[0].begin()
(curr_doc, file_location, as_temp_file) = get_saved_doc(self.view)
response = send_formatting_request(curr_doc, file_location)
if as_temp_file:
os.remove(curr_doc)
if response:
if response.startswith('<error>'):
print('Formatting error:', response.replace('<error>', ''))
else:
parts = response.split('\n', 1)
new_file_location = int(parts[0])
formatted_code = parts[1]
new_text_location = to_text_pos(formatted_code, new_file_location)
new_text = formatted_code.replace('\r', '')
self.view.replace(edit, sublime.Region(0, self.view.size()), new_text)
# with open(self.view.file_name(), "w") as file:
# file.write(formatted_code)
# surprisingly mapping of selection is not required. ST3 does it by itself
# self.view.sel().clear()
# self.view.sel().add(sublime.Region(new_text_location, new_text_location))
# print('formatting done')
# sublime.active_window().run_command("save")
# =================================================================================
# CS-Script async replecement service
# =================================================================================
# DONE
class csscript_resolve_using_async(CodeViewTextCommand):
# -----------------
def run(self, edit, **args):
parts = args['region'].split(',')
region = sublime.Region(int(parts[0]), int(parts[1]))
replacement = args['replacement']
self.view.replace(edit, region, replacement)
# =================================================================================
# CS-Script resolve missing usings service
# =================================================================================
# DONE
class csscript_resolve_using(CodeViewTextCommand):
inprogress= False
# -----------------
def run(self, edit, **args):
view = self.view
self.edit = edit
self.point = None
self.first_suffestion = False
if 'point' in args.keys():
self.point = int(args['point'])
self.first_suffestion = True
if self.point == None:
if len(view.sel()) == 0:
return
else:
self.point = view.sel()[0].begin()
busy_indicator.show('Analyzing')
sublime.set_timeout(self.do, 10)
# -----------------
def do(self):
try:
csscript_resolve_using.inprogress = True
view = self.view
word_region = view.word(self.point)
word_to_resolve = view.substr(word_region)
# print('view', view.file_name())
(saved_doc, location, as_temp_file) = get_saved_doc(view)
response = send_resolve_using_request(saved_doc, word_to_resolve)
busy_indicator.hide()
if as_temp_file:
os.remove(saved_doc)
if response == '<null>':
pass
elif response.startswith('<error>'):
print(response.replace('<error>', 'CS-Script error: '))
else:
items = response.split('\n')
def on_done(index):
if index != -1:
for region in self.view.lines(sublime.Region(0,self.view.size())):
line = self.view.substr(region)
if not line.startswith("//"):
# cannot use 'self.view.replace(self.edit...' as edit is already invalid
# so need to start a new command that ctreats 'edit'; Remember, 'edit' cannot be created from code
# self.view.replace(self.edit, sublime.Region(start, start), 'using '+items[index]+';'+'\n')
region = str(region.begin())+','+str(region.begin())
replacement = 'using '+items[index]+';'+'\n'
sublime.active_window().run_command("csscript_resolve_using_async", {'replacement':replacement, 'region':region})
sublime.active_window().run_command('save')
break
if self.first_suffestion:
on_done(0)
else:
self.view.show_popup_menu(items, on_done, 1)
except Exception as err:
print(err)
busy_indicator.hide()
csscript_resolve_using.inprogress = False
# =================================================================================
# CS-Script tooltip service (for invoking via hotkeys)
# =================================================================================
class csscript_pop_tooltip(CodeViewTextCommand):
# -----------------
def run(self, edit):
self.view.hide_popup()
point = self.view.sel()[0].begin()
left_char = self.view.substr(point-1)
right_char = self.view.substr(point)
line = self.view.line(point)
line_str = self.view.substr(line)
new_point = -1
if line_str.startswith('//css'):
csscript_show_tooltip(self.view, line.begin()).do()
return
if left_char == ' ' or left_char == '(' or left_char == ',' or right_char == ')':
new_point = point - 1
while self.view.substr(new_point) != '(':
new_point = new_point - 1
if new_point < line.begin():
new_point = -1
break
if new_point != -1:
hint = self.view.substr(sublime.Region(point, new_point))
csscript_show_tooltip(self.view, new_point+1, hint).do()
else:
csscript_show_tooltip(self.view, point).do()
# =================================================================================
# CS-Script references search service
# =================================================================================
class csscript_find_references(CodeViewTextCommand):
# -----------------
def run(self, edit):
clear_and_print_result_header(self.view.file_name())
sublime.set_timeout(self.do, 100)
def do(self):
(saved_doc, location, as_temp_file) = get_saved_doc(self.view)
response = send_resolve_references(saved_doc, location)
if as_temp_file:
os.remove(saved_doc)
response = response.replace(saved_doc, self.view.file_name())
if response == '<null>':
pass
elif response.startswith('<error>'):
print(response.replace('<error>', 'CS-Script error: '))
else:
output_view_write_line(out_panel, response)
# =================================================================================
# CS-Script project UI marshaling service (not in use yet)
# =================================================================================
class dispatcher(CodeViewTextCommand):
queue = []
# -----------------
def invoke(edit, action):
sublime.active_window().run_command('dispatcher', {'action': action})
pass
# -----------------
def run(self, edit, **args):
if 'action' in args.keys():
action = args['action']
try:
action(self, edit)
except Exception as ex:
print("dispatcher:", ex)
pass
# =================================================================================
# CS-Script project output panel service
# =================================================================================
class csscript_show_output(sublime_plugin.TextCommand):
# -----------------
def run(self, edit):
output_view_show(out_panel)
# =================================================================================
# CS-Script project resolver service
# =================================================================================
class csscript_about(sublime_plugin.TextCommand):
# -----------------
def run(self, edit):
run_cscs(\
[], \
handle_line = lambda line: output_view_write_line(out_panel, line), \
on_done=lambda: sublime.show(1, 1), \
header= 'CS-Script.ST3 - C# intellisense and execution plugin (v'+version+')')
# =================================================================================
# CS-Script project resolver service
# =================================================================================
# class csscript_load_proj(CodeViewTextCommand):
class csscript_list_proj_files(CodeViewTextCommand):
# -----------------
def handle_line(self, line):
curr_prefix = line.split(':', 1)[0]
if curr_prefix != self.prefix:
self.prefix = curr_prefix
# output_view_write_line(out_panel, '-------')
output_view_write_line(out_panel, line.replace(curr_prefix+':', curr_prefix+": "))
# -----------------
def run(self, edit):
view = self.view
self.prefix = 'file'
sublime.status_message('Checking script deficiencies for "'+self.view.file_name()+'"')
if self.view.is_dirty():
sublime.active_window().run_command("save")
sublime.set_timeout(self.do, 100)
else:
self.do()
# -----------------
def do(self):
def on_done():
output_view_write_line(out_panel, "---------------------\n[Script dependencies]")
run_doc_in_cscs(['-l', "-proj:dbg"], self.view, self.handle_line, on_done)
# =================================================================================
# CS-Script project (sources only) resolver service
# =================================================================================
class csscript_list_proj_sources(CodeViewTextCommand):
# -----------------
def handle_line(self, line):
curr_prefix = line.split(':', 1)[0]
if curr_prefix != self.prefix:
self.prefix = curr_prefix
# don't separate for now
# output_view_write_line(out_panel, '-------')
if not line.endswith('dbg.cs'):
if curr_prefix.startswith('file'):
text = line.replace("file:", '')
if self.prev_line:
output_view_write_line(out_panel, item_boxed_prefix + self.prev_line)
else:
output_view_write_line(out_panel, 'Sources')
self.prev_line = text
# -----------------
def run(self, edit):
view = self.view
self.prefix = 'file'
self.prev_line = None
sublime.status_message('Checking script dependencies for "'+self.view.file_name()+'"')
if self.view.is_dirty():
sublime.active_window().run_command("save")
sublime.set_timeout(self.do, 100)
else:
self.do()
# -----------------
def do(self):
def on_done():
if self.prev_line:
output_view_write_line(out_panel, last_item_boxed_prefix + self.prev_line)
self.prev_line = None
output_view_write_line(out_panel, "---------------------\n[Script sources]")
run_doc_in_cscs(['-l', "-proj:dbg"], self.view, self.handle_line, on_done)
# =================================================================================
# CS-Script syntax check service
# =================================================================================
# DONE
class csscript_syntax_check(CodeViewTextCommand):
errors = {}
instance = None
# -----------------
def run(self, edit, **args):
view = self.view
sublime.status_message('Checking syntax of "'+view.file_name()+'"')
if view.is_dirty() and not 'skip_saving' in args.keys():
sublime.active_window().run_command("save")
curr_doc = view.file_name()
clear_and_print_result_header(curr_doc)
if not path.exists(Runtime.cscs_path):
print('Error: cannot find CS-Script launcher - ', Runtime.cscs_path)
elif not curr_doc:
print('Error: cannot find out the document path')
else:
clear_and_print_result_header(curr_doc)
if '//css_nuget' in view.substr(sublime.Region(0, view.size())):
output_view_write_line(out_panel, "Resolving NuGet packages may take time...")
csscript_syntax_check.clear_errors()
proc = popen_redirect(['dotnet', Runtime.cscs_path, '-l', "-check", curr_doc])
first_result = True
for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"):
line = line.strip()
if first_result:
first_result = False
clear_and_print_result_header(curr_doc)
output_view_write_line(out_panel, line)
error_info = extract_location(line.strip())
if error_info:
file, line, column, context = error_info
file = os.path.abspath(file)
if file not in csscript_syntax_check.errors.keys():
csscript_syntax_check.errors[file] = []
csscript_syntax_check.errors[file].append((line, column, context))
output_view_write_line(out_panel, "[Syntax check]")
csscript_syntax_check.show_errors()
# -----------------
def has_errors(file):
for key in csscript_syntax_check.errors.keys():
if key.lower() == file.lower():
return True
return False
# -----------------
def get_errors(file, line): # line is 1-based
errors = []
for key in csscript_syntax_check.errors.keys():
if key.lower() == file.lower():
for error_info in csscript_syntax_check.errors[key]:
ln, col, cxt = error_info
if ln == line:
errors.append(error_info)
if len(errors) > 0:
break
return errors
# -----------------
def clear_errors():
for view in sublime.active_window().views():
if view.file_name():
view.erase_regions("cs-script.errors")
csscript_syntax_check.errors.clear()
# -----------------
def show_errors():
error_strong_appearence = settings().get('error_strong_appearence', False)
error_strong_appearence = True
for file in csscript_syntax_check.errors.keys():
view = find_file_view(file)
if view:
view.erase_regions("cs-script.errors")
regions = []
for line, column, context in csscript_syntax_check.errors[file]:
pt = view.text_point(line-1, column-1)
regions.append(view.word(pt))