-
Notifications
You must be signed in to change notification settings - Fork 17
/
tss.el
1224 lines (1108 loc) · 50.2 KB
/
tss.el
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
;;; tss.el --- provide a interface for auto-complete.el/flymake.el on typescript-mode.
;; Copyright (C) 2013-2014 Hiroaki Otsu
;; Author: Hiroaki Otsu <[email protected]>
;; Keywords: typescript, completion
;; URL: https://github.com/aki2o/emacs-tss
;; Version: 0.6.0
;; Package-Requires: ((auto-complete "1.4.0") (json-mode "1.1.0") (log4e "0.2.0") (yaxception "0.1"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This extension is a interface for typescript-tools.
;; This extension provides the following on typescript-mode.
;; - Auto completion by using auto-complete.el
;; - Check syntax by using flymake.el
;;
;; For more infomation,
;; see <https://github.com/aki2o/emacs-tss/blob/master/README.md>
;;; Dependency:
;;
;; - typescript.el ( see <http://www.typescriptlang.org/> )
;; - typescript-tools ( see <https://github.com/clausreinke/typescript-tools> )
;; - auto-complete.el ( see <https://github.com/auto-complete/auto-complete> )
;; - json-mode.el ( see <https://github.com/joshwnj/json-mode> )
;; - yaxception.el ( see <https://github.com/aki2o/yaxception> )
;; - log4e.el ( see <https://github.com/aki2o/log4e> )
;;; Installation:
;;
;; Put this to your load-path.
;; And put the following lines in your .emacs or site-start.el file.
;;
;; (require 'tss)
;;; Configuration:
;;
;; ;; Key Binding
;; (setq tss-popup-help-key "C-:")
;; (setq tss-jump-to-definition-key "C->")
;;
;; Make config suit for you. About the config item, see Customization
;; or eval the following sexp.
;;
;; (customize-group "tss")
;;
;; (tss-config-default)
;;; Customization:
;;
;; [EVAL] (autodoc-document-lisp-buffer :type 'user-variable :prefix "tss-[^\-]" :docstring t)
;; `tss-popup-help-key'
;; Keystroke for `tss-popup-help' at point.
;; `tss-jump-to-definition-key'
;; Keystroke for `tss-jump-to-definition' at point.
;; `tss-implement-definition-key'
;; Keystroke for `tss-implement-definition' at point.
;; `tss-enable-modes'
;; Major modes TSS is enabled on.
;; `tss-ac-trigger-command-keys'
;; Keystrokes for doing `ac-start' with self insert.
;; `tss-inactive-code-faces'
;; Faces not considered a code part.
;; `tss-ac-summary-truncate-length'
;; Length for truncation of candidate summary of auto-complete.el.
;;
;; *** END auto-documentation
;;; API:
;;
;; [EVAL] (autodoc-document-lisp-buffer :type 'command :prefix "tss-[^\-]" :docstring t)
;; `tss-popup-help'
;; Popup help about anything at point.
;; `tss-jump-to-definition'
;; Jump to method definition at point.
;; `tss-implement-definition'
;; Implement inherited definitions of current component.
;; `tss-run-flymake'
;; Run check by flymake for current buffer.
;; `tss-reload-current-project'
;; Reload project data for current buffer.
;; `tss-restart-current-buffer'
;; Restart TSS for current buffer.
;; `tss-stop-current-buffer'
;; Stop TSS for current buffer.
;; `tss-setup-current-buffer'
;; Do setup for using TSS in current buffer.
;;
;; *** END auto-documentation
;;
;; [Note] Functions and variables other than listed above, Those
;; specifications may be changed without notice.
;;; Tested On:
;;
;; - Emacs ... GNU Emacs 23.3.1 (i386-mingw-nt5.1.2600) of 2011-08-15 on GNUPACK
;; - typescript-tools ... Version For Typescript v0.9
;; - auto-complete.el ... Version 1.4.0
;; - json-mode.el ... Version 1.1.0
;; - yaxception.el ... Version 0.1
;; - log4e.el ... Version 0.2.0
;; Enjoy!!!
;;; Code:
(eval-when-compile (require 'cl))
(require 'auto-complete)
(require 'json-mode)
(require 'json)
(require 'ring)
(require 'etags)
(require 'flymake)
(require 'eldoc)
(require 'pos-tip nil t)
(require 'anything nil t)
(require 'helm nil t)
(require 'log4e)
(require 'yaxception)
(defgroup tss nil
"Auto completion / Flymake for TypeScript."
:group 'completion
:prefix "tss-")
(defcustom tss-popup-help-key nil
"Keystroke for `tss-popup-help' at point."
:type 'string
:group 'tss)
(defcustom tss-jump-to-definition-key nil
"Keystroke for `tss-jump-to-definition' at point."
:type 'string
:group 'tss)
(defcustom tss-implement-definition-key nil
"Keystroke for `tss-implement-definition' at point."
:type 'string
:group 'tss)
(defcustom tss-enable-modes '(typescript-mode)
"Major modes TSS is enabled on."
:type '(repeat symbol)
:group 'tss)
(defcustom tss-ac-trigger-command-keys '("SPC" "." ":" "<")
"Keystrokes for doing `ac-start' with self insert."
:type '(repeat string)
:group 'tss)
(defcustom tss-inactive-code-faces '(font-lock-comment-face
font-lock-string-face)
"Faces not considered a code part."
:type '(repeat symbol)
:group 'tss)
(defcustom tss-ac-summary-truncate-length 22
"Length for truncation of candidate summary of auto-complete.el."
:type 'integer
:group 'tss)
(defface tss-eldoc-function-name-face
'((t (:inherit font-lock-function-name-face)))
"Face for the part of function in eldoc."
:group 'tss)
(defface tss-eldoc-type-face
'((t (:inherit font-lock-keyword-face)))
"Face for the part of type in eldoc."
:group 'tss)
(log4e:deflogger "tss" "%t [%l] %m" "%H:%M:%S" '((fatal . "fatal")
(error . "error")
(warn . "warn")
(info . "info")
(debug . "debug")
(trace . "trace")))
(tss--log-set-level 'trace)
(yaxception:deferror 'tss-command-not-found nil "Not found 'tss' command")
;;;;;;;;;;;;;
;; Utility
(defvar tss--builtin-keywords
'("any" "break" "case" "class" "constructor" "continue" "declare" "default"
"do" "enum" "export" "extends" "false" "function" "get" "implements"
"import" "in" "interface" "module" "new" "null" "number" "private" "public"
"return" "set" "static" "super" "switch" "this" "true" "undefined" "until"
"var" "void" "while" "with"))
(defvar tss--builtin-special-comments
'(("reference" . ("path" "no-default-lib"))
("summary" . nil)
("param" . ("name" "type"))
("value" . ("type"))))
(defvar tss--last-send-string-failed-p nil)
(make-variable-buffer-local 'tss--last-send-string-failed-p)
(defvar tss--current-active-p t)
(make-variable-buffer-local 'tss--current-active-p)
(defmacro tss--awhen (test &rest body)
(declare (indent 1))
`(let ((it ,test)) (when it ,@body)))
(defun* tss--show-message (msg &rest args)
(apply 'message (concat "[TSS] " msg) args)
nil)
(defsubst tss--active-p ()
(and tss--current-active-p
(memq major-mode tss-enable-modes)
t))
(defsubst tss--active-code-point-p (&optional pt)
(let ((fc (get-text-property (or pt (point)) 'face)))
(not (memq fc tss-inactive-code-faces))))
(defsubst tss--search-backward-in-code (str &optional limitpt)
(loop while (search-backward str limitpt t)
if (tss--active-code-point-p) return (point)))
(defsubst tss--search-forward-in-code (str &optional limitpt)
(loop while (search-forward str limitpt t)
if (tss--active-code-point-p) return (point)))
(defsubst tss--get-position-argument ()
(save-excursion
(widen)
(format "%d %d"
(line-number-at-pos)
(+ (- (point) (line-beginning-position)) 1))))
(defsubst tss--send-string (proc sendstr)
(yaxception:$
(yaxception:try
(when proc
(process-send-string proc (concat sendstr "\n"))
t))
(yaxception:catch 'error e
(tss--error "failed send string : %s" (yaxception:get-text e))
(tss--delete-process t)
(if (not tss--last-send-string-failed-p)
(setq tss--last-send-string-failed-p t)
(setq tss--current-active-p nil)
(tss--popup-tip
(concat
"Stopped TSS by errored on TypeScript Services Server.\n"
"Maybe it be caused by the incomplete source code in buffer.\n"
"At later, execute `tss-restart-current-buffer' for restart TSS."))
(sleep-for 2))
nil)))
(defsubst tss--stringify-response-element (e)
(or (and e (downcase (format "%s" e)))
"unknown"))
(defsubst tss--function-kind-p (kind)
(member kind '("function" "method" "constructor" "local function")))
(defun tss--popup-tip (text)
(if (and (functionp 'ac-quick-help-use-pos-tip-p)
(ac-quick-help-use-pos-tip-p))
(pos-tip-show text 'popup-tip-face nil nil 300 popup-tip-max-width)
(popup-tip text)))
(defun tss--get-current-statement-startpt ()
(save-excursion
(or (tss--awhen (tss--search-backward-in-code ";")
(+ it 1))
(point-min))))
(defun tss--get-current-param-startpt (&optional limitpt)
(save-excursion
(loop with limitpt = (or limitpt (point-min))
for pt = (loop with depth = 1
for bspt = (or (save-excursion
(tss--search-backward-in-code "(" limitpt))
(point-min))
for bept = (or (save-excursion (tss--search-backward-in-code ")" limitpt))
(point-min))
while (> (point) limitpt)
do (cond ((> bspt bept) (decf depth) (goto-char bspt))
(t (incf depth) (goto-char bept)))
if (= depth 0) return (point)
finally return limitpt)
while (> pt limitpt)
if (string-match "\\`[a-zA-Z0-9_$]\\'" (format "%c" (char-before pt)))
return pt)))
(defun tss--buffer-substring-in-code (startpt endpt)
(save-excursion
(loop initially (goto-char startpt)
with ret = ""
while (< (point) endpt)
for nextpt = (or (next-single-property-change (point) 'face nil endpt)
endpt)
if (tss--active-code-point-p)
do (setq ret (concat ret (buffer-substring-no-properties (point) nextpt)))
do (goto-char nextpt)
finally return ret)))
(defun tss--remove-brace-part (str)
(loop for nstr = (replace-regexp-in-string "([^(]*)" "" str)
while (not (string= str nstr))
do (setq str nstr)
finally return nstr))
(defun tss--make-temp-buffer (code)
(with-current-buffer (get-buffer-create " *tss temp*")
(delete-region (point-min) (point-max))
(insert code)
(current-buffer)))
(defun tss--build-method-definition (name type &optional not-propertize)
(concat
(if not-propertize
name
(propertize name 'face 'tss-eldoc-function-name-face))
(with-temp-buffer
(insert type)
(when (string= (format "%c" (char-before)) ")")
(backward-list 1)
(skip-syntax-backward " ")
(delete-region (point) (point-max)))
(goto-char (point-min))
(forward-char 1)
(while (search-forward ":" nil t)
(skip-syntax-forward " ")
(when (not not-propertize)
(put-text-property (point)
(cond ((string= (format "%c" (char-after)) "(")
(forward-list)
(re-search-forward " +=> +[^ ,)]+" nil t)
(point))
((re-search-forward "[ ,)]" nil t)
(- (point) 1))
(t
(goto-char (point-max))
(point)))
'face
'tss-eldoc-type-face)))
(buffer-string))))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;; For typescript-tools
(defvar tss--proc nil)
(make-variable-buffer-local 'tss--proc)
(defvar tss--server-response nil)
(make-variable-buffer-local 'tss--server-response)
(defvar tss--incomplete-server-response "")
(make-variable-buffer-local 'tss--incomplete-server-response)
(defvar tss--json-response-start-char "")
(make-variable-buffer-local 'tss--json-response-start-char)
(defvar tss--json-response-end-char "")
(make-variable-buffer-local 'tss--json-response-end-char)
(defun tss--get-process (&optional initializep)
(or (and (processp tss--proc)
(eq (process-status (process-name tss--proc)) 'run)
tss--proc)
(tss--start-process initializep)))
(defun tss--exist-process ()
(and (processp tss--proc)
(process-status (process-name tss--proc))
t))
(defun tss--delete-process (&optional killp waitp)
(when (tss--exist-process)
(yaxception:$
(yaxception:try
(cond (killp
(kill-process tss--proc)
(when waitp (sleep-for 1)))
(t
(process-send-string tss--proc "quit\n")
(delete-process tss--proc)))
t)
(yaxception:catch 'error e
(tss--error "failed delete process : %s" (yaxception:get-text e))))))
(defun tss--start-process (&optional initializep)
(when (not (executable-find "tss"))
(yaxception:throw 'tss-command-not-found))
(tss--trace "Start tss process for %s" (buffer-name))
(tss--delete-process t t)
(let* ((fpath (expand-file-name (buffer-file-name)))
(procnm (format "typescript-service-%s" (buffer-name)))
(cmdstr (format "tss %s" (shell-quote-argument fpath)))
(process-connection-type nil)
(proc (when (file-exists-p fpath)
(tss--trace "Do %s" cmdstr)
(cond (initializep (tss--show-message "Load '%s' ..." (buffer-name)))
(t (tss--show-message "Reload '%s' ..." (buffer-name))))
(start-process-shell-command procnm nil cmdstr)))
(waiti 0))
(when proc
;; parsing output from a process can happen async, make sure
;; path property is set before any other process actions.
(process-put proc 'source-path fpath)
(set-process-filter proc 'tss--receive-server-response)
(process-query-on-exit-flag proc)
(setq tss--server-response nil)
(setq tss--incomplete-server-response "")
(setq tss--json-response-start-char "")
(setq tss--json-response-end-char "")
(while (and (< waiti 50)
(not tss--server-response))
(accept-process-output proc 0.2 nil t)
(sleep-for 0.2)
(incf waiti))
(tss--info "Finished start tss process.")
(setq tss--proc proc)
(when (eq tss--server-response 'succeed)
(cond (initializep (tss--show-message "Loaded '%s'." (buffer-name)))
(t (tss--show-message "Reloaded '%s'." (buffer-name))))
t))))
(defun tss--balance-json-brace-p (str startbrace endbrace)
(if (or (string= startbrace "")
(string= endbrace ""))
t
(and (string= (substring str 0 1) startbrace)
(string= (substring str -1) endbrace)
;; (let* ((str (replace-regexp-in-string "\".*?[^\\\\]\"" "" str)) ; delete part of some text
;; (str (replace-regexp-in-string "\"\"" "" str)) ; delete part of empty text
;; (str (replace-regexp-in-string "\"[^\"]*\\'" "" str)) ; delete part of imcomplete text
;; (delstartstr (replace-regexp-in-string (regexp-quote startbrace) "" str))
;; (delendstr (replace-regexp-in-string (regexp-quote endbrace) "" str))
;; (len (length str)))
;; (= (- len (length delstartstr))
;; (- len (length delendstr)))))))
;; Use json-mode as substitute for self parsing
(with-temp-buffer
(insert str)
(json-mode)
(goto-char (point-max))
(ignore-errors (backward-list))
(= (point) (point-min))))))
(defun tss--receive-server-response (proc res)
(tss--trace "Received server response.\n%s" res)
(yaxception:$
(yaxception:try
(let* ((fpath (process-get proc 'source-path))
(buff (and fpath
(get-file-buffer fpath))))
(if (not (buffer-live-p buff))
(progn (tss--warn "Source buffer is not alive : %s" fpath)
(ignore-errors (delete-process proc)))
(with-current-buffer buff
(loop with endre = (rx-to-string `(and bol "\"" (or "loaded" "updated" "added")
(+ space) ,fpath))
for line in (split-string (or res "") "[\r\n]+")
if (string= line "null")
return (progn (tss--debug "Got null response")
(setq tss--server-response 'null))
if (and (not (string= line ""))
(or (not (string= tss--incomplete-server-response ""))
(string= (substring line 0 1) tss--json-response-start-char)))
return (progn (tss--debug "Got json response : %s" line)
(setq tss--incomplete-server-response (concat tss--incomplete-server-response line))
(when (tss--balance-json-brace-p tss--incomplete-server-response
tss--json-response-start-char
tss--json-response-end-char)
(tss--trace "Finished getting json response")
(setq tss--server-response (json-read-from-string tss--incomplete-server-response))
(setq tss--incomplete-server-response "")))
if (string-match endre line)
return (progn (tss--debug "Got other response : %s" line)
(setq tss--server-response 'succeed))
if (string-match "\\`\"TSS +\\(.+\\)\"\\'" line)
do (tss--handle-err-response (match-string-no-properties 1 line)))))))
(yaxception:catch 'json-error e
(tss--warn "failed parse response : %s" (yaxception:get-text e))
(setq tss--server-response 'failed))
(yaxception:catch 'error e
(tss--error "failed receive server response : %s\n%s"
(yaxception:get-text e)
(yaxception:get-stack-trace-string e))
(setq tss--server-response 'failed)
(tss--show-message "Failed receive TSS response : %s" (yaxception:get-text e)))))
(defun tss--handle-err-response (res)
(tss--trace "Handle error response : %s" res)
(cond ((string= res "closing")
nil)
((string-match "\\`command syntax error:" res)
nil)
(t
(tss--debug "Got error response : %s" res)
(tss--show-message "%s" res))))
(defun* tss--get-server-response (cmdstr &key waitsec response-start-char response-end-char)
(when (tss--active-p)
(tss--debug "Start get server response. cmdstr[%s] waitsec[%s]" cmdstr waitsec)
(let ((proc (tss--get-process))
(waiti 0)
(maxwaiti (* (or waitsec 3) 5)))
(setq tss--server-response nil)
(setq tss--incomplete-server-response "")
(setq tss--json-response-start-char (or response-start-char "{"))
(setq tss--json-response-end-char (or response-end-char "}"))
(when (tss--send-string proc cmdstr)
(tss--trace "Start wait response from server.")
(while (and (< waiti maxwaiti)
(not tss--server-response))
(accept-process-output proc 0.2 nil t)
(incf waiti))
(cond ((not (< waiti maxwaiti))
(tss--warn "Timeout get response of %s" cmdstr)
nil)
(t
(tss--trace "Got response from server.")
tss--server-response))))))
(defun* tss--sync-server (&key waitsec buff path
source linecount)
"Sync current source with tss service. Default to 15 waiti,
current buffer, current buffer's file path, current buffer's
content, current buffer's line count.
Use SOURCE and LINECOUNT to pass custom content and linecount.
These are useful when you are doing completing/templating when
the proposed changes are not even in buffer yet but you still
want to get some info about these supposed changes (like
definition/quickInfo and etc.). *NOTE*: I think these are needed
because the ts service only support stateless queries, but
nevertheless these might prove to be useful for other potential
source manipulation."
(when (tss--active-p)
(save-restriction
(widen)
(let ((proc (tss--get-process))
(waiti 0)
(maxwaiti (* (or waitsec 3) 5))
(cmdstr (format "update %d %s"
(or linecount
(with-current-buffer (or buff (current-buffer))
(count-lines (point-min) (point-max))))
(expand-file-name (or path (buffer-file-name))))))
(tss--debug "Start sync server : %s" cmdstr)
(when (tss--send-string proc cmdstr)
(setq tss--server-response nil)
(setq tss--incomplete-server-response "")
(setq tss--json-response-start-char "")
(setq tss--json-response-end-char "")
(tss--send-string proc (or source
(with-current-buffer (or buff (current-buffer))
(buffer-string))))
(tss--trace "Start wait sync server.")
(while (and (< waiti maxwaiti)
(not tss--server-response))
(accept-process-output proc 0.2 nil t)
(incf waiti))
(cond ((not (< waiti maxwaiti))
(tss--warn "Timeout sync server.")
nil)
(t
(tss--trace "Finished sync server.")
(eq tss--server-response 'succeed))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;
;; For auto-complete.el
(defvar ac-source-tss-member
'((candidates . tss--get-ac-member-candidates)
(prefix . (tss--get-active-code-prefix "\\.\\([a-zA-Z0-9_]*\\)"))
(requires . 0)
(cache)))
(defvar ac-source-tss-type
'((candidates . tss--get-ac-non-member-candidates)
(prefix . (tss--get-active-code-prefix ": ?\\([a-zA-Z0-9_]*\\)"))
(requires . 0)
(cache)))
(defvar ac-source-tss-new
'((candidates . tss--get-ac-non-member-candidates)
(prefix . (tss--get-active-code-prefix "\\<new +\\([a-zA-Z0-9_]*\\)"))
(requires . 0)
(cache)))
(defvar ac-source-tss-extends
'((candidates . tss--get-ac-non-member-candidates)
(prefix . (tss--get-active-code-prefix " +extends +\\([a-zA-Z0-9_]*\\)"))
(requires . 0)
(cache)))
(defvar ac-source-tss-implements
'((candidates . tss--get-ac-non-member-candidates)
(prefix . (tss--get-active-code-prefix " +implements +\\([a-zA-Z0-9_]*\\)"))
(requires . 0)
(cache)))
(defvar ac-source-tss-tag
'((candidates . tss--get-ac-non-member-candidates)
(prefix . (tss--get-active-code-prefix "[^/] *<\\([a-zA-Z0-9_]*\\)"))
(requires . 0)
(cache)))
(defvar ac-source-tss-anything
'((candidates . tss--get-ac-non-member-candidates)
(prefix . (tss--get-active-code-prefix "\\(?:^\\|[^a-zA-Z0-9_.]\\) *\\([a-zA-Z0-9_]+\\)"))
(requires . 1)
(cache)))
(defvar ac-source-tss-keyword
'((candidates . tss--get-ac-keyword-candidates)
(prefix . (tss--get-active-code-prefix "\\(?:^\\|[^a-zA-Z0-9_.]\\) *\\([a-zA-Z0-9_]+\\)"))
(requires . 1)
(cache)))
(defvar ac-source-tss-special-comment
'((candidates . tss--get-ac-special-comment-candidates)
(prefix . "^[\t ]*///[^\n]*</?\\([a-zA-Z0-9:-]*\\)")
(requires . 0)))
(defvar ac-source-tss-special-comment-attr
'((candidates . tss--get-ac-special-comment-attribute-candidates)
(prefix . "^[\t ]*///[^\n]*\\(?:<[a-zA-Z0-9:-]+\\|[^=]\"\\|[^=]'\\) +\\([a-zA-Z0-9-]*\\)")
(requires . 0)
(action . (lambda ()
(insert "=\"")
(when (and (not (= (point) (point-max)))
(string= (format "%c" (char-after)) "\""))
(delete-char 1))
(insert "\"")
(backward-char)
(auto-complete)))))
(defvar ac-source-tss-referenc-path
'((candidates . tss--get-ac-reference-path-candidates)
(prefix . "^[\t ]*/// *<reference +path=\"\\(?:[^\"\n]*/\\)?\\([^/\"\n]*\\)")
(requires . 0)
(action . (lambda ()
(when (string= (format "%c" (char-before)) "/")
(auto-complete '(ac-source-tss-referenc-path)))))))
(defvar tss--last-ac-start-point 1)
(make-variable-buffer-local 'tss--last-ac-start-point)
(defvar tss--last-ac-candidates nil)
(make-variable-buffer-local 'tss--last-ac-candidates)
(defun tss--insert-with-ac-trigger-command (n)
(interactive "p")
(self-insert-command n)
(auto-complete-1 :triggered 'trigger-key))
(defun tss--get-active-code-prefix (re)
(when (and (tss--active-code-point-p)
(re-search-backward (concat re "\\=") nil t))
(or (match-beginning 1)
(match-beginning 0))))
(defun tss--get-ac-special-comment-candidates ()
(loop for e in tss--builtin-special-comments
collect (car e)))
(defun tss--get-ac-special-comment-attribute-candidates ()
(let ((com (save-excursion
(goto-char (point-at-bol))
(when (re-search-forward "\\=[\t ]*/// *<\\([a-zA-Z0-9:-]+\\)" nil t)
(match-string-no-properties 1)))))
(when com
(assoc-default com tss--builtin-special-comments))))
(defun tss--get-ac-reference-path-candidates ()
(save-excursion
(re-search-backward "[^/\"\n]+\\=" nil t)
(let* ((currpt (point))
(prevpath (or (when (search-backward "\"" nil t)
(forward-char 1)
(buffer-substring-no-properties (point) currpt))
""))
(dirpath (concat (file-name-directory (expand-file-name (buffer-file-name)))
prevpath)))
(loop for e in (directory-files dirpath)
for fullpath = (concat dirpath e)
if (and (file-regular-p fullpath)
(string-match "\\.ts\\'" e))
collect e
else if (and (file-directory-p fullpath)
(or (string= prevpath "")
(string-match "\\`[./]+/\\'" prevpath)
(not (member e '("." "..")))))
collect (concat e "/")
else if (file-symlink-p fullpath)
collect e))))
(defun tss--get-ac-member-candidates ()
(tss--trace "start get ac member candidates.")
(tss--get-ac-candidates t))
(defun tss--get-ac-non-member-candidates ()
(tss--trace "start get ac non member candidates.")
(tss--get-ac-candidates nil))
(defun tss--get-ac-candidates (memberp)
(yaxception:$
(yaxception:try
(when (> tss--last-ac-start-point (point-max))
(setq tss--last-ac-start-point (point-max)))
(let* ((currpt (point))
(code (buffer-substring-no-properties currpt tss--last-ac-start-point)))
(if (and (> currpt tss--last-ac-start-point)
(string-match "\\`[a-zA-Z0-9_]+\\'" code))
(progn (tss--trace "Use last ac candidates. code[%s]" code)
tss--last-ac-candidates)
(tss--trace "Start get ac candidates. code[%s]" code)
(setq tss--last-ac-start-point currpt)
(setq tss--last-ac-candidates
(when (tss--sync-server)
(let* ((posarg (tss--get-position-argument))
(memberarg (cond (memberp "true")
(t "false")))
(fpath (expand-file-name (buffer-file-name)))
(cmdnm (cond (memberp "completions")
(t "completions-brief")))
(cmdstr (format "%s %s %s %s" cmdnm memberarg posarg fpath))
(ret (tss--get-server-response cmdstr :waitsec 2))
(entries (when (listp ret)
(cdr (assoc 'entries ret)))))
(mapcar (lambda (e)
(let ((name (cdr (assoc 'name e)))
(kind (cdr (assoc 'kind e)))
(type (cdr (assoc 'type e)))
(doc (cdr (assoc 'docComment e))))
(tss--debug "Got candidate name[%s] kind[%s] type[%s]" name kind type)
(popup-make-item
name
:symbol (tss--get-ac-symbol kind)
:summary (tss--get-ac-summary type)
:document `(lambda (sel) (tss--get-ac-document ,name ,kind ,type ,doc)))))
entries)))))))
(yaxception:catch 'error e
(tss--show-message "%s" (yaxception:get-text e))
(tss--error "failed get ac candidates : %s\n%s"
(yaxception:get-text e)
(yaxception:get-stack-trace-string e))
(setq tss--last-ac-candidates nil))))
(defun tss--get-ac-keyword-candidates ()
(tss--trace "start get ac keyword candidates.")
(mapcar (lambda (s)
(tss--debug "Got candidate name[%s] kind[builtin-keyword] type[]" s)
(propertize s 'tss--ac-cand-kind "builtin-keyword"))
tss--builtin-keywords))
(defun tss--get-ac-symbol (kind)
(let ((kind (tss--stringify-response-element kind)))
(cond ((member kind '("keyword" "builtin-keyword")) "w")
((string= kind "primitive type") "p")
((string= kind "module") "m")
((string= kind "interface") "i")
((string= kind "class") "c")
((member kind '("var" "property" "parameter")) "v")
((tss--function-kind-p kind) "f")
((string= kind "unknown") "")
(t
(tss--warn "found unknown server response for kind : %s" kind)
""))))
(defun tss--get-ac-summary (sum)
(when (stringp sum)
(let ((str (replace-regexp-in-string "\r?\n" " " sum)))
(truncate-string-to-width
str tss-ac-summary-truncate-length 0 nil "..."))))
(defun tss--get-ac-document (name kind type doc)
(let* ((sym (intern (tss--get-ac-symbol kind)))
(kind (tss--stringify-response-element kind))
(type (tss--stringify-response-element type))
(doc (or doc ""))
(typedesc (case sym
(w "")
(f (concat "Signature: " type "\n\n"))
(t (concat "Type: " (capitalize type) "\n\n")))))
(concat name " is " (upcase kind) ".\n\n" typedesc doc "\n")))
;;;;;;;;;;;;;;;;;;
;; For eldoc.el
(defvar tss--last-method-eldoc "")
(make-variable-buffer-local 'tss--last-method-eldoc)
(defvar tss--last-param-startpt 0)
(make-variable-buffer-local 'tss--last-param-startpt)
(defvar tss--last-param-index -2)
(make-variable-buffer-local 'tss--last-param-index)
(defvar tss--last-method-definition nil)
(make-variable-buffer-local 'tss--last-method-definition)
(defun tss--echo-method-usage ()
(yaxception:$
(yaxception:try
(when (tss--active-p)
(let* ((currpt (point))
(parampt (tss--get-current-param-startpt))
(arg-text (when parampt
(tss--remove-brace-part
(tss--buffer-substring-in-code parampt currpt))))
(paramidx (when arg-text (length (split-string arg-text ",")))))
(princ (cond ((not parampt)
"")
((and (eq parampt tss--last-param-startpt)
(eq paramidx tss--last-param-index))
tss--last-method-eldoc)
((and (eq parampt tss--last-param-startpt)
tss--last-method-definition)
(setq tss--last-param-index paramidx)
(setq tss--last-method-eldoc
(tss--build-method-eldoc tss--last-method-definition paramidx)))
(t
(setq tss--last-param-startpt parampt)
(setq tss--last-param-index paramidx)
(setq tss--last-method-definition
(when (tss--sync-server)
(let* ((posarg (save-excursion
(goto-char parampt)
(tss--get-position-argument)))
(fpath (expand-file-name (buffer-file-name)))
(cmdstr (format "type %s %s" posarg fpath))
(ret (tss--get-server-response cmdstr :waitsec 1))
(name (when (listp ret) (cdr (assoc 'fullSymbolName ret))))
(kind (when (listp ret) (cdr (assoc 'kind ret))))
(type (when (listp ret) (cdr (assoc 'type ret)))))
(if (tss--function-kind-p (format "%s" kind))
(tss--build-method-definition name type)
""))))
(setq tss--last-method-eldoc
(tss--build-method-eldoc tss--last-method-definition paramidx))))))))
(yaxception:catch 'error e
(tss--error "failed echo method usage : %s\n%s"
(yaxception:get-text e) (yaxception:get-stack-trace-string e))
(setq tss--last-method-definition "")
(setq tss--last-method-eldoc "")
(princ ""))))
(defvar tss--regexp-method-signature (rx-to-string `(and bos
(group (+ (not (any "("))))
"(" (group (+ anything)) ")"
(group (+ (not (any ")"))))
eos)))
(defun tss--build-method-eldoc (def highlight-index)
(tss--trace "start build method eldoc. def[%s] highlight-index[%s]" def highlight-index)
(when (not (facep 'tss--eldoc-type-highlight-face))
(make-face 'tss--eldoc-type-highlight-face)
(copy-face 'tss-eldoc-type-face 'tss--eldoc-type-highlight-face)
(set-face-bold-p 'tss--eldoc-type-highlight-face t))
(if (not (string-match tss--regexp-method-signature def))
def
(let* ((name (match-string 1 def))
(param (match-string 2 def))
(ret (match-string 3 def)))
(loop with startidx = 0
with endidx = (length param)
with tstartidx = 0
with curridx = 1
with chidx = 0
with not-type-char-p = (lambda (ch chk)
(and (string= ch chk)
(not (eq (get-text-property chidx 'face param)
'tss-eldoc-type-face))))
with update-face = (lambda ()
(when (and (= curridx highlight-index)
(> tstartidx startidx)
(> chidx tstartidx))
(put-text-property startidx tstartidx 'face 'bold param)
(put-text-property tstartidx chidx 'face 'tss--eldoc-type-highlight-face param)
t))
while (< chidx endidx)
for ch = (substring param chidx (+ chidx 1))
if (funcall not-type-char-p ch ":")
do (setq tstartidx (+ chidx 1))
if (funcall not-type-char-p ch ",")
do (progn
(when (funcall update-face) (setq chidx endidx))
(setq startidx (+ chidx 1))
(incf curridx))
do (incf chidx)
finally do (funcall update-face))
(concat name "(" param ")" ret))))
;;;;;;;;;;;;;;;;;;;
;; For implement
(defvar tss--regexp-component-definition
(rx-to-string `(and bol (* space)
(? "export" (+ space))
(? (or "public" "private") (+ space))
(group (or "class" "interface")) (+ space)
(group (+ (any "a-zA-Z0-9_."))))))
(defun tss--get-implementable-constructor-definition (ident code)
(let* ((buff (tss--make-temp-buffer
(concat code "\nvar _xxx = new " ident "()")))
(posarg (with-current-buffer buff
(forward-char -3)
(tss--get-position-argument)))
(fpath (expand-file-name (buffer-file-name)))
(cmdstr (format "type %s %s" posarg fpath))
(ret (when (tss--sync-server :buff buff :path fpath)
(tss--get-server-response cmdstr)))
(type (when (and ret (listp ret))
(cdr (assoc 'type ret)))))
(when type
(list (replace-regexp-in-string
":[^:]+\\'" "" (tss--build-method-definition "constructor" type t))))))
(defun tss--get-implementable-method-definition (ident code)
(let* ((buff (tss--make-temp-buffer
(concat code "\nvar _xxx: " ident "; _xxx.")))
(posarg (with-current-buffer buff
(tss--get-position-argument)))
(fpath (expand-file-name (buffer-file-name)))
(cmdstr (format "completions true %s %s" posarg fpath))
(ret (when (tss--sync-server :buff buff :path fpath)
(tss--get-server-response cmdstr)))
(entries (when (and ret (listp ret))
(cdr (assoc 'entries ret)))))
(loop for e in (mapcar (lambda (e)
(let ((name (cdr (assoc 'name e)))
(kind (cdr (assoc 'kind e)))
(type (cdr (assoc 'type e))))
(when (tss--function-kind-p (tss--stringify-response-element kind))
(tss--build-method-definition name type t))))
entries)
if e collect e)))
(defun tss--select-implementable-definition (cands)
(let* ((cands (append (list "all") cands))
(src `((name . "Implementable Definition")
(candidates . ,cands)
(candidate-number-limit . 999)
(action . (lambda (cand)
(or (when (fboundp 'helm-marked-candidates) (helm-marked-candidates))
(when (fboundp 'anything-marked-candidates) (anything-marked-candidates))
(list cand)))))))
(cond ((fboundp 'helm)
(helm :sources src))
((fboundp 'anything)
(anything :sources src))
(t
(list (completing-read "Implementable Definition" cands nil t nil '() "all"))))))
(defun tss--write-implementable-definition (defs classp)
(let ((suffix (if classp " {}" ";")))
(dolist (d defs)
(newline-and-indent)
(insert d suffix))))
;;;;;;;;;;;;;;;;;;
;; User Command
;;;###autoload
(defun tss-popup-help ()
"Popup help about anything at point."
(interactive)
(yaxception:$
(yaxception:try
(when (tss--sync-server)
(let* ((posarg (tss--get-position-argument))
(fpath (expand-file-name (buffer-file-name)))
(cmdstr (format "type %s %s" posarg fpath))
(ret (tss--get-server-response cmdstr))
(name (when (listp ret) (cdr (assoc 'fullSymbolName ret))))
(kind (when (listp ret) (cdr (assoc 'kind ret))))
(type (when (listp ret) (cdr (assoc 'type ret))))
(doc (when (listp ret) (cdr (assoc 'docComment ret)))))
(tss--popup-tip (tss--get-ac-document name kind type doc)))))
(yaxception:catch 'error e
(tss--show-message "%s" (yaxception:get-text e))
(tss--error "failed popup help : %s\n%s"
(yaxception:get-text e)