forked from mopemope/meghanada-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meghanada.el
1920 lines (1679 loc) · 68.7 KB
/
meghanada.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
;;; meghanada.el --- A better java development mode -*- coding: utf-8; lexical-binding: t; -*-
;; Copyright (C) 2018 Yutaka Matsubara
;; License: http://www.gnu.org/licenses/gpl.html
;; Author: Yutaka Matsubara ([email protected])
;; Homepage: https://github.com/mopemope/meghanada-emacs
;; Keywords: languages java
;; Package-Version: 1.2.0
;; Package-Requires: ((emacs "24.3") (yasnippet "0.6.1") (company "0.9.0") (flycheck "0.23"))
;; 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 program 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:
;;
;; Meghanada has a server component which can read the AST of your
;; project and its dependencies, providing features.
;;
;;
;;; Code:
(require 'cl-lib)
(require 'pcase)
(require 'thingatpt)
(require 'compile)
(require 'imenu)
(require 'url)
(require 'which-func)
(autoload 'meghanada-company-enable "company-meghanada")
(autoload 'meghanada-flycheck-enable "flycheck-meghanada")
(autoload 'meghanada-eldoc-enable "eldoc-meghanada")
;;
;; Const
;;
(defconst meghanada-version "1.2.0")
(defconst meghanada-setup-version "0.0.2")
(defconst meghanada--eot "\n;;EOT\n")
(defconst meghanada--junit-buf-name "*meghanada-junit*")
(defconst meghanada--task-buf-name "*meghanada-task*")
(defconst meghanada--ref-buf-name "*meghanada-reference*")
(defconst meghanada--search-buf-name "*meghanada-search-everywhere*")
(defconst meghanada--show-project-buf-name "*meghanada-project*")
(defconst meghanada--typeinfo-buf-name "*meghanada-typeinfo*")
(defconst meghanada--install-err-buf-name "*meghanada-install-error*")
(defconst meghanada--err-buf-name "*meghanada-error*")
;;
;; Customizable variables
;;
(defgroup meghanada nil
"Java minor mode powered by meghanada."
:group 'java)
(defcustom meghanada-host "127.0.0.1"
"Meghanada server host address."
:group 'meghanada
:type 'string)
(defcustom meghanada-port 0
"Meghanada server port.
A port number of 0 means that the port number is automatically allocated, typically from an ephemeral port range.
default 0.
"
:group 'meghanada
:type 'integer)
(defcustom meghanada-debug nil
"If true, meghanada-server outputs debug log."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-use-company t
"If true, company-mode auto-comletion is enabled."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-use-flycheck t
"If true, diagnostics report with flyecheck is enabled."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-use-eldoc t
"If true, eldoc for meghanada enabled."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-auto-start t
"If true, meghanada-server start automatically."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-server-remote-debug nil
"If true, meghanda-server enabled remote debug."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-server-install-dir (locate-user-emacs-file "meghanada/")
"Install directory for meghanada-server.
The slash is expected at the end."
:group 'meghanada
:risky t
:type 'directory)
(defcustom meghanada-java-path "java"
"Path of the java executable.
If the path of maven is in the environment, we can use the short path.
Otherwise, we can always use the full path, in Windows, we can use this to get it:
`(expand-file-name \"bin/java.exe\" (getenv \"JAVA_HOME\"))'
In linux or macOS, it can be \"java\". In Windows, it can be \"java.exe\"."
:group 'meghanada
:type 'string)
(defcustom meghanada-maven-path "mvn"
"Path of the maven executable.
If the path of maven is in the environment, we can use the short path.
Otherwise, we can always use the full path.
For the short paht:
In linux or macOS, it can be \"mvn\"; In Windows, it can be \"mvn.cmd\". "
:group 'meghanada
:type 'string)
(defcustom meghanada-maven-local-repository nil
"Overriding maven repository path."
:group 'meghanada
:type 'string)
(defcustom meghanada-javac-xlint "-Xlint:all"
"Overriding javac's -Xlint."
:group 'meghanada
:type 'string)
(defcustom meghanada-gradle-version nil
"Overriding gradle version."
:group 'meghanada
:type 'string)
(defcustom meghanada-gradle-prepare-compile-task nil
"Set to gradle prepare compileJava task name."
:group 'meghanada
:type 'string)
(defcustom meghanada-gradle-prepare-test-compile-task nil
"Set to gradle prepare compileTestJava task name."
:group 'meghanada
:type 'string)
(defcustom meghanada-skip-build-subprojects t
"If true, skip gradle dependency subprojects build."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-jvm-option nil
"Set to all meghanada java process jvm option.
Example. (setq meghanada-jvm-option \"-Dhttp.proxyHost=test.proxy.com -Dhttp.proxyPort=8080\")
"
:group 'meghanada
:type 'string)
(defcustom meghanada-server-jvm-option "-Xms128m -XX:ReservedCodeCacheSize=240m -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Dsun.io.useCanonCaches=false"
"Set to meghanada server process jvm option."
:group 'meghanada
:type 'string)
(defcustom meghanada-mode-key-prefix [?\C-c]
"The prefix key for meghanada-mode commands."
:group 'meghanada
:type 'sexp)
(defcustom meghanada-reference-prepare #'meghanada--reference-prepare
"It is called before meghanada-reference."
:group 'meghanada
:type 'function)
(defcustom meghanada-reference-callback #'meghanada--reference-callback
"It will be called after receiving meghanada-reference result."
:group 'meghanada
:type 'function)
(defcustom meghanada-typeinfo-prepare #'meghanada--typeinfo-prepare
"It is called before meghanada-typeinfo."
:group 'meghanada
:type 'function)
(defcustom meghanada-typeinfo-callback #'meghanada--typeinfo-callback
"It will be called after receiving meghanada-typeinfo result."
:group 'meghanada
:type 'function)
(defcustom meghanada-search-prepare #'meghanada--search-prepare
"It is called before meghanada-search-everywhere."
:group 'meghanada
:type 'function)
(defcustom meghanada-search-callback #'meghanada--search-callback
"It will be called after receiving meghanada-search-everywhere result."
:group 'meghanada
:type 'function)
(defcustom meghanada-cache-in-project nil
"If true, create a cache in the project.otherwise, create in cache root directory (~/.cache/meghanada). default nil"
:group 'meghanada
:type 'boolean)
(defcustom meghanada-cache-root nil
"Set to meghanada cache root.default value is '~/.cache/meghanada'."
:group 'meghanada
:type 'string)
(defcustom meghanada-task-buffer-auto-scroll t
"If true, automatically move to the end of the task buffer after inserting new output."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-make-search-query #'meghanada--make-search-query
"Sets the function that returns the search query."
:group 'meghanada
:type 'function)
(defcustom meghanada-import-static-enable "java.util.Objects,org.junit.Assert"
"Sets import static completion classes."
:group 'meghanada
:type 'string)
(defcustom meghanada-full-text-search-enable nil
"If true, Enable full text search and meghanada-search-everywhere."
:group 'meghanada
:type 'boolean)
(defcustom meghanada-completion-matcher "prefix"
"Select completion matcher. You can choose from prefix, contains, fuzzy, came-case. default is prefix."
:group 'meghanada
:type 'string)
(defcustom meghanada-class-completion-matcher "prefix"
"Select class completion matcher. You can choose from prefix, contains, fuzzy, came-case. default is prefix."
:group 'meghanada
:type 'string)
(defcustom meghanada-mode-after-test-hook '()
"Hook that is called after a JUnit test execution is done."
:group 'meghanada)
(defcustom meghanada-telemetry-enable nil
"If true, Enables telemetry and allows you to collect and submit performance data."
:group 'meghanada
:type 'boolean)
;;
;; utility
;;
(defun meghanada--what-line ()
"Return the current buffer line number and narrowed line number of point."
(let ((start (point-min))
(n (line-number-at-pos)))
(if (= start 1)
n
(save-excursion
(save-restriction
(widen)
(+ n (line-number-at-pos start) -1) n)))))
(defun meghanada--real-current-column ()
"like `current-column', but skip invisible characters in pretty-symbol-mode."
(- (point) (line-beginning-position)))
(defun meghanada--what-column ()
"TODO: FIX DOC ."
(number-to-string (1+ (meghanada--real-current-column))))
(defun meghanada--what-symbol ()
"TODO: FIX DOC ."
(thing-at-point 'symbol))
(defun meghanada--what-word ()
"TODO: FIX DOC ."
(thing-at-point 'word))
(defmacro meghanada--without-narrowing (&rest body)
"TODO: FIX DOC BODY."
(declare (indent 0) (debug t))
`(save-restriction
(widen)
(progn ,@body)))
(defun meghanada--remove-eot (out)
"TODO: FIX DOC OUT ."
(replace-regexp-in-string meghanada--eot "" out))
(defun meghanada--goto-line (line)
"TODO: FIX LINE ."
(goto-char (point-min))
(forward-line (1- line)))
(defun meghanada--line-column-to-point (line column)
"TODO: FIX LINE COLUMN."
(save-excursion
(meghanada--goto-line line)
(forward-char (1- column))
(point)))
(defun meghanada--delete-whole-line (&optional arg)
"Delete the current line without putting it in the `kill-ring'.
Derived from function `kill-whole-line'. ARG is defined as for that
function."
(setq arg (or arg 1))
(if (and (> arg 0)
(eobp)
(save-excursion (forward-visible-line 0) (eobp)))
(signal 'end-of-buffer nil))
(if (and (< arg 0)
(bobp)
(save-excursion (end-of-visible-line) (bobp)))
(signal 'beginning-of-buffer nil))
(cond ((zerop arg)
(delete-region (progn (forward-visible-line 0) (point))
(progn (end-of-visible-line) (point))))
((< arg 0)
(delete-region (progn (end-of-visible-line) (point))
(progn (forward-visible-line (1+ arg))
(unless (bobp)
(backward-char))
(point))))
(t
(delete-region (progn (forward-visible-line 0) (point))
(progn (forward-visible-line arg) (point))))))
(defun meghanada-version ()
"Show meghanada-version ."
(interactive)
(message meghanada-version))
;;
;; meghanada-server process management.
;;
(defvar meghanada--server-process nil)
(defvar meghanada--server-buffer "*meghanada-server-log*")
(defvar meghanada--server-pending nil)
(defvar meghanada--server-jar nil)
;;
;; meghanada-client process management.
;;
(defvar meghanada--client-process nil)
(defvar meghanada--client-buffer "*meghanada-client*")
(defvar meghanada--connect-host meghanada-host)
(defvar meghanada--connect-port meghanada-port)
(defvar meghanada--client-pending nil)
(defvar meghanada--task-client-process nil)
(defvar meghanada--task-buffer nil)
;; TODO pop-to-buffer
(defun meghanada--download-from-url (url dest-jar)
"Download a jar file from URL to DEST-JAR Path."
(let ((dest meghanada-server-install-dir))
(unless (file-exists-p dest)
(make-directory dest t))
(message (format "Download module from %s. Please wait ..." url))
(url-handler-mode t)
(if (file-exists-p url)
(progn
(url-copy-file url dest-jar)
(message (format "Downloaded module from %s to %s." url dest-jar)))
(error "Not found %s" url))))
(defun meghanada--setup ()
"Setup meghanada-server-module."
(meghanada--download-setup-jar)
(meghanada--run-setup))
(defun meghanada--setup-options ()
(let ((options '()))
(when meghanada-jvm-option
(push meghanada-jvm-option options))
(mapconcat 'identity options " ")))
(defun meghanada--run-setup ()
"Setup meghanada server module."
(let ((jar (meghanada--locate-setup-jar))
(dest meghanada-server-install-dir))
(if (file-exists-p jar)
(let ((cmd (format "%s %s -jar %s --dest %s --server-version %s --simple"
(shell-quote-argument meghanada-java-path)
(meghanada--setup-options)
(shell-quote-argument jar)
(expand-file-name dest)
meghanada-version)))
(message "Download meghanada server module. Please wait ...")
(let ((proc (start-process-shell-command "*meghanada-install*" "*meghanada-install*" cmd))
(buf (current-buffer)))
(pop-to-buffer "*meghanada-install*")
(set-process-filter
proc
#'(lambda (process msg)
(when (process-live-p process)
(with-current-buffer (process-buffer process)
(insert msg)
(message "Download server module ...")))))
(set-process-sentinel
proc
#'(lambda (process msg)
(unless (process-live-p process)
(message (format "Success. It downloaded to %s." meghanada-server-install-dir))
(pop-to-buffer buf)
(with-current-buffer buf
(meghanada-mode t)
(meghanada-restart))))))))))
(defun meghanada--download-setup-jar ()
"Download setup-jar file from bintray."
(let ((url (format
"https://dl.bintray.com/mopemope/meghanada/meghanada-setup-%s.jar"
meghanada-setup-version))
(setup-jar (meghanada--locate-setup-jar)))
(unless (file-exists-p setup-jar)
(meghanada--download-from-url
url
setup-jar))))
(defun meghanada--download-server-jar ()
"Direct download server jar file."
(let ((dest meghanada-server-install-dir)
(dest-jar (meghanada--locate-server-jar))
(url (format "https://dl.bintray.com/mopemope/meghanada/meghanada-%s.jar" meghanada-version)))
(unless (file-exists-p dest)
(make-directory dest t))
(message (format "Download server module from %s. Please wait." url))
(url-handler-mode t)
(if (file-exists-p url)
(progn
(url-copy-file url dest-jar)
(message (format "Downloaded server module from %s to %s." url dest-jar)))
(error "Not found %s" url))))
;;;###autoload
(defun meghanada-install-server ()
"Install meghanada-server's jar file from bintray ."
(interactive)
(let ((dest-jar (meghanada--locate-server-jar)))
(if (file-exists-p dest-jar)
nil
(condition-case err
(progn
(meghanada--setup)
t)
(error
(let ((error-buf meghanada--install-err-buf-name))
(with-current-buffer (get-buffer-create error-buf)
(insert (format "Error: %s" (error-message-string err)))
(compilation-mode))))))))
;;;###autoload
(defun meghanada-update-server ()
"Update meghanada-server's jar file from bintray ."
(interactive)
(meghanada-install-server))
(defun meghanada--locate-server-jar ()
"TODO FIX DOC."
(expand-file-name
(format "meghanada-%s.jar" meghanada-version)
meghanada-server-install-dir))
(defun meghanada--locate-setup-jar ()
"Return the path of the meghanada-setup.jar file."
(expand-file-name
(format "meghanada-setup-%s.jar" meghanada-setup-version)
meghanada-server-install-dir))
(defun meghanada--server-options ()
(let ((options '()))
(when meghanada-maven-path
(push (format "-Dmeghanada.maven.path=%s" meghanada-maven-path) options))
(when meghanada-maven-local-repository
(push (format "-Dmeghanada.maven.local.repository=%s" meghanada-maven-local-repository) options))
(when meghanada-javac-xlint
(push (format "-Dmeghanada.javac.arg=%s" meghanada-javac-xlint) options))
(when meghanada-import-static-enable
(push (format "-Dmeghanada.search.static.method.classes=%s" meghanada-import-static-enable) options))
(when meghanada-full-text-search-enable
(push "-Dmeghanada.full.text.searchs=true" options))
(when meghanada-gradle-version
(push (format "-Dmeghanada.gradle.version=%s" meghanada-gradle-version) options))
(when meghanada-gradle-prepare-compile-task
(push (format "-Dmeghanada.gradle.prepare.compile.task=%s" meghanada-gradle-prepare-compile-task) options))
(when meghanada-gradle-prepare-test-compile-task
(push (format "-Dmeghanada.gradle.prepare.test.compile.task=%s" meghanada-gradle-prepare-test-compile-task) options))
(if meghanada-skip-build-subprojects
(push "-Dmeghanada.skip.build.subprojects=true" options)
(push "-Dmeghanada.skip.build.subprojects=false" options))
(if meghanada-cache-in-project
(push "-Dmeghanada.cache.in.project=true" options)
(push "-Dmeghanada.cache.in.project=false" options))
(when meghanada-cache-root
(push (format "-Dmeghanada.cache.root=%s" meghanada-cache-root) options))
(when meghanada-server-remote-debug
(push "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" options))
(push "-Dmeghanada.format=sexp" options)
(when meghanada-completion-matcher
(push (format "-Dmeghanada.completion.matcher=%s" meghanada-completion-matcher) options))
(when meghanada-class-completion-matcher
(push (format "-Dmeghanada.class.completion.matcher=%s" meghanada-class-completion-matcher) options))
(when meghanada-jvm-option
(push meghanada-jvm-option options))
(when meghanada-telemetry-enable
(push "-Dmeghanada.telemetry.enable=true" options))
(push "-Djava.net.preferIPv4Stack=true" options)
(mapconcat 'identity options " ")))
(defun meghanada--start-server-process ()
"TODO: FIX DOC ."
(let ((jar (meghanada--locate-server-jar)))
(if (file-exists-p jar)
(let ((process-connection-type nil)
(process-adaptive-read-buffering nil)
(cmd (format "%s %s %s -Dfile.encoding=UTF-8 -jar %s -p %d %s %s"
(shell-quote-argument meghanada-java-path)
(meghanada--server-options)
meghanada-server-jvm-option
(shell-quote-argument jar)
meghanada-port
(if meghanada-debug "-v" "")
(concat "-l " temporary-file-directory "meghanada_server_" (number-to-string (user-uid)) ".log")))
process)
(message (format "launch server cmd:%s" cmd))
(setq process
(start-process-shell-command
"meghanada-server"
meghanada--server-buffer
cmd))
(buffer-disable-undo meghanada--server-buffer)
(set-process-query-on-exit-flag process nil)
(set-process-sentinel process 'meghanada--server-process-sentinel)
(set-process-filter process 'meghanada--server-process-filter)
(message "Meghanada-Server Starting ...")
process)
(message "%s"
(substitute-command-keys
"Missing server module. Type `\\[meghanada-install-server]' to install meghanada-server")))))
(defun meghanada--get-server-process-create ()
"TODO: FIX DOC ."
(if (and meghanada--server-process (process-live-p meghanada--server-process))
(progn
(message "already started meghanada-server. see *meghanada-server-log* buffer.")
meghanada--server-process)
(setq meghanada--server-process (meghanada--start-server-process))))
(defun meghanada--server-process-sentinel (process msg)
"TODO: FIX DOC PROCESS MSG ."
(unless (process-live-p process)
(set-process-sentinel process 'ignore)
(set-process-filter process 'ignore)
(delete-process process)
(setq meghanada--server-process nil)
(error (format "Error:meghanada-server process stopped: %s. Please check *meghanada-server-log* buffer" msg))))
(defun meghanada--server-process-filter (process output)
"TODO: FIX DOC PROCESS OUTPUT ."
(let ((pbuf (process-buffer process)))
(when (buffer-live-p pbuf)
(with-current-buffer pbuf
(let ((moving (= (point) (process-mark process))))
(save-excursion
(goto-char (process-mark process))
(insert output)
(set-marker (process-mark process) (point)))
(if moving
(goto-char (process-mark process)))
(when (string-match "Start server" output)
(string-match "port:\\([0-9]+\\)+" output)
(let* ((p (substring output (match-beginning 1) (match-end 1))))
(setq meghanada--connect-port (string-to-number p)))
(message "Server waiting client connection ...")
(when meghanada--server-pending
(funcall meghanada--server-pending)
(setq meghanada--server-pending nil)))
(when (string-match "MEGHANADA_FAILED " output)
(string-match "FILE:\\(.*.log\\)" output)
(let* ((log-file (substring output (match-beginning 1) (match-end 1))))
(with-help-window (get-buffer-create meghanada--err-buf-name)
(pop-to-buffer meghanada--err-buf-name)
(save-excursion
(insert-file-contents-literally log-file)
(message
(format "ERROR: project initialize error. please check log %s" log-file)))))))))))
;;;###autoload
(defun meghanada-server-start ()
"TODO: FIX DOC ."
(interactive)
(meghanada--get-server-process-create))
;;;###autoload
(defun meghanada-server-kill ()
"TODO: FIX DOC ."
(interactive)
(when (and meghanada--server-process (process-live-p meghanada--server-process))
(set-process-sentinel meghanada--server-process 'ignore)
(set-process-filter meghanada--server-process 'ignore)
(kill-buffer (process-buffer meghanada--server-process))
;; (kill-process meghanada--server-process)
(delete-process meghanada--server-process)
(setq meghanada--server-process nil)))
(defun meghanada--start-client-process ()
"TODO: FIX DOC ."
(let (process)
(message "meghanada-client process start ...")
(setq process
(make-network-process
:name "meghanada-client"
:buffer meghanada--client-buffer
:family 'ipv4
:host meghanada--connect-host
:service meghanada--connect-port
:noquery t
:sentinel 'meghanada--client-process-sentinel
:filter 'meghanada--client-process-filter))
(buffer-disable-undo meghanada--client-buffer)
process))
(defun meghanada--start-task-client-process ()
"TODO: FIX DOC ."
(make-network-process
:name "meghanada-task-client"
:buffer "*meghanada-task-client*"
:family 'ipv4
:host meghanada--connect-host
:service meghanada--connect-port
:noquery t
:sentinel 'meghanada--task-client-process-sentinel
:filter 'meghanada--task-client-process-filter))
(defun meghanada--start-server-and-client ()
"TODO: FIX DOC ."
(if (and meghanada--client-process (process-live-p meghanada--client-process))
meghanada--client-process
(unless (and meghanada--server-process (process-live-p meghanada--server-process))
(progn
(meghanada--client-kill)
(setq meghanada--server-pending
#'(lambda ()
(setq meghanada--client-process (meghanada--start-client-process))
(message "Please wait. Meghanada indexing ... ")))
(meghanada--get-server-process-create)))))
(defun meghanada--get-client-process-create ()
"TODO: FIX DOC ."
(if (and meghanada--client-process (process-live-p meghanada--client-process))
meghanada--client-process
(setq meghanada--client-process (meghanada--start-client-process))))
(defun meghanada--client-process-sentinel (process msg)
"TODO: FIX DOC PROCESS MSG."
(unless (process-live-p process)
(set-process-sentinel process 'ignore)
(set-process-filter process 'ignore)
(kill-buffer (process-buffer process))
(delete-process process)
(setq meghanada--client-process nil)
(error (format "Disconnected:meghanada-client process stopped: %s. Please check *meghanada-server-log* buffer" msg))))
(defun meghanada--task-client-process-sentinel (process msg)
"TODO: FIX DOC PROCESS MSG."
(unless (process-live-p process)
(set-process-sentinel process 'ignore)
(set-process-filter process 'ignore)
(kill-buffer (process-buffer process))
(delete-process process)
(setq meghanada--task-client-process nil)
(error (format "Disconnected:meghanada-task-client process stopped: %s. Please check *meghanada-server-log* buffer" msg))))
(defun meghanada--process-client-response (process response)
"TODO: FIX DOC PROCESS RESPONSE ."
(let* ((output (read (meghanada--normalize-repsonse-file-path (meghanada--remove-eot response))))
(callback (meghanada--process-pop-callback process))
(status (car output))
(res (car (cdr output))))
(pcase status
(`success
(when callback
(with-demoted-errors "Warning: %S"
(apply (car callback) res (cdr callback)))))
(`error
(ignore-errors
(progn
(message (format "Error:%s . Please check *meghanada-server-log*" res))
(apply (car callback) nil (cdr callback))))))))
(defun meghanada--normalize-repsonse-file-path (response)
"TODO: FIX RESPONSE ."
(if (eq system-type 'windows-nt)
(let (diver-char downcase-char)
(setq response (replace-regexp-in-string "\\\\" "/" response))
(when (string-match "\\([A-Z]\\):/" response)
(setq diver-char (match-string 1 response))
(setq downcase-char (downcase diver-char))
(setq response (replace-regexp-in-string (concat diver-char ":/") (concat downcase-char ":/") response)))
response))
response)
(defun meghanada--normalize-request-file-path (request)
"TODO: FIX REQUEST ."
(if (eq system-type 'windows-nt)
(let (diver-char upcase-char)
(when (string-match "\\([a-z]\\):/" request)
(setq diver-char (match-string 1 request))
(setq upcase-char (upcase diver-char))
(setq request (replace-regexp-in-string (concat diver-char ":/") (concat upcase-char ":/") request)))
(setq request (replace-regexp-in-string "/" "\\\\" request))
request))
request)
(defun meghanada--client-process-filter (process output)
"TODO: FIX DOC PROCESS OUTPUT."
(let ((pbuf (process-buffer process))
responses)
(when meghanada--client-pending
(funcall meghanada--client-pending)
(setq meghanada--client-pending nil))
(when (buffer-live-p pbuf)
(with-current-buffer pbuf
(save-excursion
(goto-char (process-mark process))
(insert output)
(set-marker (process-mark process) (point))
(goto-char (point-min))
(while (search-forward meghanada--eot nil t)
(let ((response (buffer-substring-no-properties (point-min)
(point))))
(delete-region (point-min) (point))
(setq responses (cons (meghanada--remove-eot response) responses))))
(goto-char (process-mark process)))))
;; Handle all responses.
(mapc #'(lambda (r)
(meghanada--process-client-response process r))
(nreverse responses))))
(defun meghanada--task-client-process-filter (ignored output)
"TODO: FIX DOC IGNORED OUTPUT."
(let ((buf meghanada--task-buffer))
;; (pop-to-buffer buf)
(with-current-buffer (get-buffer-create buf)
(let ((win (get-buffer-window buf))
(eob (eq (point) (point-max)))
(eot nil))
;; Insert the new output at the end of the buffer
;; and restore the buffer point afterward
(save-excursion
(goto-char (point-max))
(setq buffer-read-only nil)
(insert output)
(if (and (string= buf meghanada--junit-buf-name)
(search-backward meghanada--eot nil t))
(progn
(while (re-search-forward meghanada--eot nil t)
(replace-match "")
(setq eot t))
(when eot
(compilation-mode)))
(progn
(while (re-search-backward meghanada--eot nil t)
(replace-match "")
(setq eot t))
(when eot
(compilation-mode))))
(setq buffer-read-only t)
;; Run all after test hooks now that the buffer is read-only
(when (string= buf meghanada--junit-buf-name)
(run-hooks 'meghanada-mode-after-test-hook)))
;; If the cursor is already at the end of the buffer or if
;; auto-scrolling is activated, move the cursor to the end of the buffer
;; (moves both buffer point and window point)
(when (or meghanada-task-buffer-auto-scroll eob)
(goto-char (point-max))
(when win (set-window-point win (point))))))))
(defun meghanada--process-push-callback (process cb)
"TODO: FIX DOC PROCESS CB."
(let ((callbacks (process-get process 'meghanada-callback-stack)))
(if callbacks
(nconc callbacks (list cb))
(process-put process 'meghanada-callback-stack (list cb)))))
(defun meghanada--process-pop-callback (process)
"TODO: FIX DOC PROCESS."
(let ((callbacks (process-get process 'meghanada-callback-stack)))
(process-put process 'meghanada-callback-stack (cdr callbacks))
(car callbacks)))
(defun meghanada--client-kill ()
"Disconnect and kill meghanada-client."
(when (and meghanada--client-process (process-live-p meghanada--client-process))
(set-process-sentinel meghanada--client-process 'ignore)
(set-process-filter meghanada--client-process 'ignore)
(kill-buffer (process-buffer meghanada--client-process))
(delete-process meghanada--client-process)
(setq meghanada--client-process nil))
(when (and meghanada--task-client-process (process-live-p meghanada--task-client-process))
(set-process-sentinel meghanada--task-client-process 'ignore)
(set-process-filter meghanada--task-client-process 'ignore)
(kill-buffer (process-buffer meghanada--task-client-process))
(delete-process meghanada--task-client-process)
(setq meghanada--task-client-process nil)))
(defun meghanada--send-request (request callback &rest args)
"TODO: FIX DOC REQUEST CALLBACK ARGS."
(let* ((process (meghanada--get-client-process-create))
(argv (cons request args))
(callback (if (listp callback) callback (list callback)))
(send-str (meghanada--normalize-request-file-path (format "%s" argv))))
(when (and process (process-live-p process))
(meghanada--process-push-callback process callback)
(meghanada--without-narrowing
(process-send-string process
(format "%s\n" send-str))))))
(defun meghanada--send-request-process (request process callback &rest args)
"TODO: FIX DOC REQUEST PROCESS CALLBACK ARGS."
(let* ((argv (cons request args))
(callback (if (listp callback) callback (list callback)))
(send-str (format "%s" argv)))
(when (and process (process-live-p process))
(meghanada--process-push-callback process callback)
(meghanada--without-narrowing
(process-send-string process
(format "%s\n" send-str))))))
(defvar meghanada--sync-id 0)
(defvar meghanada--sync-result '(-1 . nil))
(defun meghanada--sync-request-callback (response id)
"TODO: FIX DOC RESPONSE ID."
(setq meghanada--sync-result (cons id response)))
(defun meghanada--send-request-sync (request &rest args)
"TODO: FIX DOC REQUEST ARGS."
(let* ((id meghanada--sync-id)
(callback (list #'meghanada--sync-request-callback id)))
(setq meghanada--sync-id (1+ meghanada--sync-id))
(with-local-quit
(let ((process (meghanada--get-client-process-create)))
(apply 'meghanada--send-request request callback args)
(while (not (= id (car meghanada--sync-result)))
(accept-process-output process))
(cdr meghanada--sync-result)))))
;;
;; meghanada api
;;
(defun meghanada-alive-p ()
"TODO: FIX DOC ."
(and meghanada-mode
meghanada--client-process
(process-live-p meghanada--client-process)))
;;
;; import
;;
(defun meghanada--goto-imports-start ()
"TODO: FIX DOC ."
(goto-char (point-min))
(let ((package-point (re-search-forward "package .*;" nil t))
(import-point (re-search-forward "import .*;" nil t)))
(cond (import-point (goto-char import-point)
(beginning-of-line))
(package-point (goto-char package-point)
(forward-line)
(open-line 2)
(forward-line))
(t (goto-char (point-min))
(open-line 1)))))
(defun meghanada--import-name (imp)
"TODO: FIX DOC IMP ."
(let ((cs case-fold-search))
(when cs
(setq case-fold-search nil))
(let ((imp (when (string-match "\\([a-z0-9_]+\\.\\)+[A-Za-z0-9_]+" imp)
(match-string 0 imp))))
(prog1
imp
(when cs
(setq case-fold-search t))))))
(defun meghanada--is-java-lang-package-p (fqcn)
"Check if FQCN belongs to java.lang package (exclude subpackages,
e.g. java.lang.annotation)."
(and
(string-prefix-p "java.lang." fqcn)
(not (string-match-p (regexp-quote "#") fqcn))
(<= (length (split-string fqcn "\\.")) 3)))
(defun meghanada--import-exists-p (imp)
"TODO: FIX DOC IMP ."
(save-excursion
(goto-char (point-min))
(re-search-forward (concat "^import\\s-+" imp "\\s-*;") nil t)))
(defun meghanada--add-import-callback (result buf)
"TODO: FIX DOC OUT BUF."
(with-current-buffer buf
(let ((severity (car result)))
(pcase severity
(`success
(let* ((fqcn (car (cdr result)))
(is-static (string-match-p (regexp-quote "#") fqcn))
(imp (if is-static
(replace-regexp-in-string "#" "." fqcn)
fqcn)))
(unless (or (meghanada--is-java-lang-package-p imp)
(meghanada--import-exists-p imp))
(let ((start t))
(save-excursion
(meghanada--goto-imports-start)
(while (and start (re-search-forward "^import .+;" nil t))
(forward-line)
(setq start (/= (point-at-bol) (point-at-eol))))
(if is-static
(insert (format "import static %s;\n" imp))
(insert (format "import %s;\n" imp))))))))))))
(defun meghanada--add-import (imp buf)
"TODO: FIX DOC IMP BUF."
(unless
(or
(meghanada--is-java-lang-package-p imp)
(meghanada--import-exists-p imp))
(meghanada-add-import-async
imp
#'meghanada--add-import-callback
buf)))
(defun meghanada-import-all--callback (result buf optimize)
"TODO: FIX DOC OUT BUF OPTIMIZE."
(with-current-buffer buf
(when result
(mapc
(lambda (imps)
(if (= (length imps) 1)
(meghanada--add-import (car imps) buf)
(let ((res (completing-read "import:" imps nil t)))
(unless (string= res "")
(meghanada--add-import res buf))))) result))
(when optimize
(save-buffer)
(meghanada-optimize-import))))
(defun meghanada-import-at--callback (result buf optimize)
"TODO: FIX DOC RESULT BUF OPTIMIZE."
(with-current-buffer buf
(when result
(mapc
(lambda (imports)
(let ((type (car imports))
(imps (cdr imports)))
(pcase type
(`class
(if (= (length imps) 1)
(meghanada--add-import (car imps) buf)
(let ((res (completing-read "import:" imps nil t)))
(unless (string= res "")
(meghanada--add-import res buf)))))
(`method
(if (= (length imps) 1)
(meghanada--add-import (car imps) buf)