-
Notifications
You must be signed in to change notification settings - Fork 46
/
ycmd.el
2478 lines (2172 loc) · 93.4 KB
/
ycmd.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
;;; ycmd.el --- emacs bindings to the ycmd completion server -*- lexical-binding: t -*-
;;
;; Copyright (c) 2014-2017 Austin Bingham, Peter Vasil
;;
;; Authors: Austin Bingham <[email protected]>
;; Peter Vasil <[email protected]>
;; Version: 1.3-cvs
;; URL: https://github.com/abingham/emacs-ycmd
;; Package-Requires: ((emacs "24.4") (dash "2.13.0") (s "1.11.0") (deferred "0.5.1") (cl-lib "0.6.1") (let-alist "1.0.5") (request "0.3.0") (request-deferred "0.3.0") (pkg-info "0.6"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Description:
;;
;; ycmd is a modular server that provides completion for C/C++/ObjC
;; and Python, among other languages. This module provides an emacs
;; client for that server.
;;
;; ycmd is a bit peculiar in a few ways. First, communication with the
;; server uses HMAC to authenticate HTTP messages. The server is
;; started with an HMAC secret that the client uses to generate hashes
;; of the content it sends. Second, the server gets this HMAC
;; information (as well as other configuration information) from a
;; file that the server deletes after reading. So when the code in
;; this module starts a server, it has to create a file containing the
;; secret code. Since the server deletes this file, this code has to
;; create a new one for each server it starts. Hopefully by knowing
;; this, you'll be able to make more sense of some of what you see
;; below.
;;
;; For more details, see the project page at
;; https://github.com/abingham/emacs-ycmd.
;;
;; Installation:
;;
;; Copy this file to to some location in your emacs load path. Then add
;; "(require 'ycmd)" to your emacs initialization (.emacs,
;; init.el, or something).
;;
;; Example config:
;;
;; (require 'ycmd)
;; (ycmd-setup)
;;
;; Basic usage:
;;
;; First you'll want to configure a few things. If you've got a global
;; ycmd config file, you can specify that with `ycmd-global-config':
;;
;; (set-variable 'ycmd-global-config "/path/to/global_conf.py")
;;
;; Then you'll want to configure your "extra-config whitelist"
;; patterns. These patterns determine which extra-conf files will get
;; loaded automatically by ycmd. So, for example, if you want to make
;; sure that ycmd will automatically load all of the extra-conf files
;; underneath your "~/projects" directory, do this:
;;
;; (set-variable 'ycmd-extra-conf-whitelist '("~/projects/*"))
;;
;; Now, the first time you open a file for which ycmd can perform
;; completions, a ycmd server will be automatically started.
;;
;; When ycmd encounters an extra-config that's not on the white list,
;; it checks `ycmd-extra-conf-handler' to determine what to do. By
;; default this is set to `ask', in which case the user is asked
;; whether to load the file or ignore it. You can also set it to
;; `load', in which case all extra-confs are loaded (and you don't
;; really need to worry about `ycmd-extra-conf-whitelist'.) Or you can
;; set this to `ignore', in which case all extra-confs are
;; automatically ignored.
;;
;; Use `ycmd-get-completions' to get completions at some point in a
;; file. For example:
;;
;; (ycmd-get-completions buffer position)
;;
;; You can use `ycmd-display-completions' to toy around with completion
;; interactively and see the shape of the structures in use.
;;
;;; License:
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
;; files (the "Software"), to deal in the Software without
;; restriction, including without limitation the rights to use, copy,
;; modify, merge, publish, distribute, sublicense, and/or sell copies
;; of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Code:
(eval-when-compile
(require 'cl-lib)
(require 'let-alist))
(require 'dash)
(require 's)
(require 'deferred)
(require 'hmac-def)
(require 'json)
(require 'request)
(require 'request-deferred)
(require 'etags)
(require 'easymenu)
(require 'diff)
(require 'diff-mode)
(declare-function pkg-info-version-info "pkg-info" (package))
(defgroup ycmd nil
"a ycmd emacs client"
:link '(url-link :tag "Github" "https://github.com/abingham/emacs-ycmd")
:group 'tools
:group 'programming)
(defcustom ycmd-global-config nil
"Path to global extra conf file."
:type '(string))
(defcustom ycmd-extra-conf-whitelist nil
"List of glob expressions which match extra configs.
Whitelisted configs are loaded without confirmation."
:type '(repeat string))
(defcustom ycmd-extra-conf-handler 'ask
"What to do when an un-whitelisted extra config is encountered.
Options are:
`load'
Automatically load unknown extra confs.
`ignore'
Ignore unknown extra confs and do not load them.
`ask'
Ask the user for each unknown extra conf."
:type '(choice (const :tag "Load unknown extra confs" load)
(const :tag "Ignore unknown extra confs" ignore)
(const :tag "Ask the user" ask))
:risky t)
(defcustom ycmd-host "127.0.0.1"
"The host on which the ycmd server is running."
:type '(string))
(defcustom ycmd-server-command nil
"The ycmd server program command.
The value is a list of arguments to run the ycmd server.
Example value:
\(set-variable 'ycmd-server-command (\"python\" \"/path/to/ycmd/package/\"))"
:type '(repeat string))
(defcustom ycmd-server-args '("--log=debug"
"--keep_logfile"
"--idle_suicide_seconds=10800")
"Extra arguments to pass to the ycmd server."
:type '(repeat string))
(defcustom ycmd-server-port nil
"The ycmd server port. If nil, use random port."
:type '(number))
(defcustom ycmd-file-parse-result-hook nil
"Functions to run with file-parse results.
Each function will be called with with the results returned from
ycmd when it parses a file in response to
/event_notification."
:type 'hook
:risky t)
(defcustom ycmd-idle-change-delay 0.5
"Number of seconds to wait after buffer modification before
re-parsing the contents."
:type '(number)
:safe #'numberp)
(defcustom ycmd-keepalive-period 600
"Number of seconds between keepalive messages."
:type '(number))
(defcustom ycmd-startup-timeout 3
"Number of seconds to wait for the server to start."
:type '(number))
(defcustom ycmd-delete-process-delay 3
"Seconds to wait for the server to finish before killing the process."
:type '(number))
(defcustom ycmd-parse-conditions '(save new-line mode-enabled)
"When ycmd should reparse the buffer.
The variable is a list of events that may trigger parsing the
buffer for new completion:
`save'
Set buffer-needs-parse flag after the buffer was saved.
`new-line'
Set buffer-needs-parse flag immediately after a new
line was inserted into the buffer.
`idle-change'
Set buffer-needs-parse flag a short time after a
buffer has changed. (See `ycmd-idle-change-delay')
`mode-enabled'
Set buffer-needs-parse flag after `ycmd-mode' has been
enabled.
`buffer-focus'
Set buffer-needs-parse flag when an unparsed buffer gets
focus.
If nil, never set buffer-needs-parse flag. For a manual reparse,
use `ycmd-parse-buffer'."
:type '(set (const :tag "After the buffer was saved" save)
(const :tag "After a new line was inserted" new-line)
(const :tag "After a buffer was changed and idle" idle-change)
(const :tag "After a `ycmd-mode' was enabled" mode-enabled)
(const :tag "After an unparsed buffer gets focus" buffer-focus))
:safe #'listp)
(defcustom ycmd-default-tags-file-name "tags"
"The default tags file name."
:type 'string)
(defcustom ycmd-force-semantic-completion nil
"Whether to use always semantic completion."
:type 'boolean)
(defcustom ycmd-auto-trigger-semantic-completion t
"If non-nil, semantic completion is turned off.
Semantic completion is still available if
`ycmd-force-semantic-completion' is non-nil."
:type 'boolean)
(defcustom ycmd-hide-url-status t
"Whether to quash url status messages for ycmd requests."
:type 'boolean)
(defcustom ycmd-bypass-url-proxy-services t
"Bypass proxies for local traffic with the ycmd server.
If non-nil, bypass the variable `url-proxy-services' in
`ycmd--request' by setting it to nil and add `no_proxy' to
`process-environment' to bypass proxies when using `curl' as
`request-backend' and for the ycmd process."
:type 'boolean)
(defcustom ycmd-tag-files nil
"Whether to collect identifiers from tags file.
nil
Do not collect identifiers from tag files.
`auto'
Look up directory hierarchy for first found tags file with
`ycmd-default-tags-file-name'.
string
A tags file name.
list
A list of tag file names."
:type '(choice (const :tag "Don't use tag file." nil)
(const :tag "Locate tags file automatically" auto)
(string :tag "Tag file name")
(repeat :tag "List of tag files"
(string :tag "Tag file name")))
:safe (lambda (obj)
(or (symbolp obj)
(stringp obj)
(ycmd--string-list-p obj))))
(defcustom ycmd-file-type-map
'((c++-mode . ("cpp"))
(c-mode . ("c"))
(caml-mode . ("ocaml"))
(csharp-mode . ("cs"))
(d-mode . ("d"))
(erlang-mode . ("erlang"))
(emacs-lisp-mode . ("elisp"))
(go-mode . ("go"))
(java-mode . ("java"))
(js-mode . ("javascript"))
(js2-mode . ("javascript"))
(lua-mode . ("lua"))
(objc-mode . ("objc"))
(perl-mode . ("perl"))
(cperl-mode . ("perl"))
(php-mode . ("php"))
(python-mode . ("python"))
(ruby-mode . ("ruby"))
(rust-mode . ("rust"))
(swift-mode . ("swift"))
(scala-mode . ("scala"))
(tuareg-mode . ("ocaml"))
(typescript-mode . ("typescript")))
"Mapping from major modes to ycmd file-type strings.
Used to determine a) which major modes we support and b) how to
describe them to ycmd."
:type '(alist :key-type symbol :value-type (repeat string)))
(defcustom ycmd-min-num-chars-for-completion 2
"The minimum number of characters for identifier completion.
It controls the number of characters the user needs to type
before identifier-based completion suggestions are triggered.
This option is NOT used for semantic completion.
Setting this it to a high number like 99 effectively turns off
the identifier completion engine and just leaves the semantic
engine."
:type 'integer)
(defcustom ycmd-max-num-identifier-candidates 10
"The maximum number of identifier completion results."
:type 'integer)
(defcustom ycmd-seed-identifiers-with-keywords nil
"Whether to seed identifier database with keywords."
:type 'boolean)
(defcustom ycmd-get-keywords-function 'ycmd--get-keywords-from-alist
"Function to get keywords for current mode."
:type 'symbol)
(defcustom ycmd-gocode-binary-path nil
"Gocode binary path."
:type 'string)
(defcustom ycmd-godef-binary-path nil
"Godef binary path."
:type 'string)
(defcustom ycmd-rust-src-path nil
"Rust source path."
:type 'string)
(defcustom ycmd-swift-src-path nil
"Swift source path."
:type 'string)
(defcustom ycmd-racerd-binary-path nil
"Racerd binary path."
:type 'string)
(defcustom ycmd-python-binary-path nil
"Python binary path."
:type 'string)
(defcustom ycmd-global-modes t
"Modes for which `ycmd-mode' is turned on by `global-ycmd-mode'.
If t, ycmd mode is turned on for all major modes in
`ycmd-file-type-map'. If set to all, ycmd mode is turned on
for all major-modes. If a list, ycmd mode is turned on for all
`major-mode' symbols in that list. If the `car' of the list is
`not', ycmd mode is turned on for all `major-mode' symbols _not_
in that list. If nil, ycmd mode is never turned on by
`global-ycmd-mode'."
:type '(choice (const :tag "none" nil)
(const :tag "member in `ycmd-file-type-map'" t)
(const :tag "all" all)
(set :menu-tag "mode specific" :tag "modes"
:value (not)
(const :tag "Except" not)
(repeat :inline t (symbol :tag "mode")))))
(defcustom ycmd-confirm-fixit t
"Whether to confirm when applying fixit on line."
:type 'boolean)
(defcustom ycmd-after-exception-hook nil
"Function to run if server request resulted in exception.
This hook is run whenever an exception is thrown after a ycmd
server request. Four arguments are passed to the function, a
string with the type of request that triggerd the exception, the
buffer and the point at the time of the request and the server
response structure which looks like this:
((exception
(TYPE . \"RuntimeError\"))
(traceback . \"long traceback string\")
(message . \"Can't jump to definition.\"))
This variable is a normal hook. See Info node `(elisp)Hooks'."
:type 'hook
:risky t)
(defcustom ycmd-after-teardown-hook nil
"Functions to run after execution of `ycmd--teardown'.
This variable is a normal hook. See Info node `(elisp)Hooks'."
:type 'hook
:risky t)
(defcustom ycmd-mode-line-prefix "ycmd"
"Base mode line lighter for ycmd."
:type 'string
:package-version '(ycmd . "1.3"))
(defcustom ycmd-completing-read-function #'completing-read
"Function to read from minibuffer with completion.
The function must be compatible to the built-in `completing-read'
function."
:type '(choice (const :tag "Default" completing-read)
(const :tag "IDO" ido-completing-read)
(function :tag "Custom function"))
:risky t
:package-version '(ycmd . "1.2"))
(defconst ycmd--file-types-with-diagnostics
'("c"
"cpp"
"objc"
"objcpp"
"cs"
"typescript")
"A list of ycmd file type strings which support semantic diagnostics.")
(defvar ycmd-keywords-alist
'((c++-mode
"alignas" "alignof" "and" "and_eq" "asm" "auto" "bitand" "bitor" "bool"
"break" "case" "catch" "char" "char16_t" "char32_t" "class" "compl"
"concept" "const" "const_cast" "constexpr" "continue" "decltype" "default"
"define" "defined" "delete" "do" "double" "dynamic_cast" "elif" "else"
"endif" "enum" "error" "explicit" "export" "extern" "false" "final" "float"
"for" "friend" "goto" "if" "ifdef" "ifndef" "include" "inline" "int" "line"
"long" "mutable" "namespace" "new" "noexcept" "not" "not_eq" "nullptr"
"operator" "or" "or_eq" "override" "pragma" "_Pragma" "private" "protected"
"public" "register" "reinterpret_cast" "requires" "return" "short" "signed"
"sizeof" "static" "static_assert" "static_cast" "struct" "switch"
"template" "this" "thread_local" "throw" "true" "try" "typedef" "typeid"
"typename" "union" "unsigned" "using" "virtual" "void" "volatile" "wchar_t"
"while" "xor" "xor_eq")
(c-mode
"auto" "_Alignas" "_Alignof" "_Atomic" "_Bool" "break" "case" "char"
"_Complex" "const" "continue" "default" "define" "defined" "do" "double"
"elif" "else" "endif" "enum" "error" "extern" "float" "for" "goto"
"_Generic" "if" "ifdef" "ifndef" "_Imaginary" "include" "inline" "int"
"line" "long" "_Noreturn" "pragma" "register" "restrict" "return" "short"
"signed" "sizeof" "static" "struct" "switch" "_Static_assert" "typedef"
"_Thread_local" "undef" "union" "unsigned" "void" "volatile" "while")
(go-mode
"break" "case" "chan" "const" "continue" "default" "defer" "else"
"fallthrough" "for" "func" "go" "goto" "if" "import" "interface" "map"
"package" "range" "return" "select" "struct" "switch" "type" "var")
(lua-mode
"and" "break" "do" "else" "elseif" "end" "false" "for" "function" "if" "in"
"local" "nil" "not" "or" "repeat" "return" "then" "true" "until" "while")
(python-mode
"ArithmeticError" "AssertionError" "AttributeError" "BaseException"
"BufferError" "BytesWarning" "DeprecationWarning" "EOFError" "Ellipsis"
"EnvironmentError" "Exception" "False" "FloatingPointError" "FutureWarning"
"GeneratorExit" "IOError" "ImportError" "ImportWarning" "IndentationError"
"IndexError" "KeyError" "KeyboardInterrupt" "LookupError" "MemoryError"
"NameError" "None" "NotImplemented" "NotImplementedError" "OSError"
"OverflowError" "PendingDeprecationWarning" "ReferenceError" "RuntimeError"
"RuntimeWarning" "StandardError" "StopIteration" "SyntaxError"
"SyntaxWarning" "SystemError" "SystemExit" "TabError" "True" "TypeError"
"UnboundLocalError" "UnicodeDecodeError" "UnicodeEncodeError"
"UnicodeError" "UnicodeTranslateError" "UnicodeWarning" "UserWarning"
"ValueError" "Warning" "ZeroDivisionError" "__builtins__" "__debug__"
"__doc__" "__file__" "__future__" "__import__" "__init__" "__main__"
"__name__" "__package__" "_dummy_thread" "_thread" "abc" "abs" "aifc" "all"
"and" "any" "apply" "argparse" "array" "as" "assert" "ast" "asynchat"
"asyncio" "asyncore" "atexit" "audioop" "base64" "basestring" "bdb" "bin"
"binascii" "binhex" "bisect" "bool" "break" "buffer" "builtins" "bytearray"
"bytes" "bz2" "calendar" "callable" "cgi" "cgitb" "chr" "chuck" "class"
"classmethod" "cmath" "cmd" "cmp" "code" "codecs" "codeop" "coerce"
"collections" "colorsys" "compile" "compileall" "complex" "concurrent"
"configparser" "contextlib" "continue" "copy" "copyreg" "copyright"
"credits" "crypt" "csv" "ctypes" "curses" "datetime" "dbm" "decimal" "def"
"del" "delattr" "dict" "difflib" "dir" "dis" "distutils" "divmod" "doctest"
"dummy_threading" "elif" "else" "email" "enumerate" "ensurepip" "enum"
"errno" "eval" "except" "exec" "execfile" "exit" "faulthandler" "fcntl"
"file" "filecmp" "fileinput" "filter" "finally" "float" "fnmatch" "for"
"format" "formatter" "fpectl" "fractions" "from" "frozenset" "ftplib"
"functools" "gc" "getattr" "getopt" "getpass" "gettext" "glob" "global"
"globals" "grp" "gzip" "hasattr" "hash" "hashlib" "heapq" "help" "hex"
"hmac" "html" "http" "id" "if" "imghdr" "imp" "impalib" "import"
"importlib" "in" "input" "inspect" "int" "intern" "io" "ipaddress" "is"
"isinstance" "issubclass" "iter" "itertools" "json" "keyword" "lambda"
"len" "license" "linecache" "list" "locale" "locals" "logging" "long"
"lzma" "macpath" "mailbox" "mailcap" "map" "marshal" "math" "max"
"memoryview" "mimetypes" "min" "mmap" "modulefinder" "msilib" "msvcrt"
"multiprocessing" "netrc" "next" "nis" "nntplib" "not" "numbers" "object"
"oct" "open" "operator" "optparse" "or" "ord" "os" "ossaudiodev" "parser"
"pass" "pathlib" "pdb" "pickle" "pickletools" "pipes" "pkgutil" "platform"
"plistlib" "poplib" "posix" "pow" "pprint" "print" "profile" "property"
"pty" "pwd" "py_compiler" "pyclbr" "pydoc" "queue" "quit" "quopri" "raise"
"random" "range" "raw_input" "re" "readline" "reduce" "reload" "repr"
"reprlib" "resource" "return" "reversed" "rlcompleter" "round" "runpy"
"sched" "select" "selectors" "self" "set" "setattr" "shelve" "shlex"
"shutil" "signal" "site" "slice" "smtpd" "smtplib" "sndhdr" "socket"
"socketserver" "sorted" "spwd" "sqlite3" "ssl" "stat" "staticmethod"
"statistics" "str" "string" "stringprep" "struct" "subprocess" "sum"
"sunau" "super" "symbol" "symtable" "sys" "sysconfig" "syslog" "tabnanny"
"tarfile" "telnetlib" "tempfile" "termios" "test" "textwrap" "threading"
"time" "timeit" "tkinter" "token" "tokenize" "trace" "traceback"
"tracemalloc" "try" "tty" "tuple" "turtle" "type" "types" "unichr"
"unicode" "unicodedata" "unittest" "urllib" "uu" "uuid" "vars" "venv"
"warnings" "wave" "weakref" "webbrowser" "while" "winsound" "winreg" "with"
"wsgiref" "xdrlib" "xml" "xmlrpc" "xrange" "yield" "zip" "zipfile" "zipimport"
"zlib")
(rust-mode
"Self"
"as" "box" "break" "const" "continue" "crate" "else" "enum" "extern"
"false" "fn" "for" "if" "impl" "in" "let" "loop" "macro" "match" "mod"
"move" "mut" "pub" "ref" "return" "self" "static" "struct" "super"
"trait" "true" "type" "unsafe" "use" "where" "while")
(swift-mode
"true" "false" "nil" "#available" "#colorLiteral" "#column" "#else"
"#elseif" "#endif" "#fileLiteral" "#file" "#function" "#if" "#imageLiteral"
"#keypath" "#line" "#selector" "#sourceLocation" "associatedtype" "class"
"deinit" "enum" "extension" "fileprivate" "func" "import" "init" "inout"
"internal" "let" "open" "operator" "private" "protocol" "public" "static"
"struct" "subscript" "typealias" "var" "break" "case" "continue" "default"
"defer" "do" "else" "fallthrough" "for" "guard" "if" "in" "repeat" "return"
"switch" "where" "while" "as" "catch" "dynamicType" "is" "rethrows" "super"
"self" "Self" "throws" "throw" "try" "Protocol" "Type" "and" "assignment"
"associativity" "convenience" "didSet" "dynamic" "final" "get" "higherThan"
"indirect" "infix" "lazy" "left" "lowerThan" "mutating" "none"
"nonmutating" "optional" "override" "postfix" "precedence"
"precedencegroup" "prefix" "required" "right" "set" "unowned" "weak"
"willSet"))
"Alist mapping major-modes to keywords for.
Keywords source: https://github.com/auto-complete/auto-complete/tree/master/dict
and `company-keywords'.")
(defvar ycmd--server-actual-port nil
"The actual port being used by the ycmd server.
This is set based on the value of `ycmd-server-port' if set, or
the value from the output of the server itself.")
(defvar ycmd--hmac-secret nil
"This is populated with the hmac secret of the current connection.
Users should never need to modify this, hence the defconst. It is
not, however, treated as a constant by this code. This value gets
set in ycmd-open.")
(defconst ycmd--server-process-name "ycmd-server"
"The Emacs name of the server process.
This is used by functions like `start-process', `get-process'
and `delete-process'.")
(defvar-local ycmd--notification-timer nil
"Timer for notifying ycmd server to do work, e.g. parsing files.")
(defvar ycmd--keepalive-timer nil
"Timer for sending keepalive messages to the server.")
(defvar ycmd--on-focus-timer nil
"Timer for deferring ycmd server notification to parse a buffer.")
(defvar ycmd--server-timeout-timer nil)
(defconst ycmd--server-buffer-name "*ycmd-server*"
"Name of the ycmd server buffer.")
(defvar-local ycmd--last-status-change 'unparsed
"The last status of the current buffer.")
(defvar-local ycmd--last-modified-tick nil
"The BUFFER's last FileReadyToParse tick counter.")
(defvar-local ycmd--buffer-visit-flag nil)
(defvar ycmd--available-completers (make-hash-table :test 'eq))
(defvar ycmd--process-environment nil)
(defvar ycmd--mode-keywords-loaded nil
"List of modes for which keywords have been loaded.")
(defconst ycmd-hooks-alist
'((after-save-hook . ycmd--on-save)
(after-change-functions . ycmd--on-change)
(window-configuration-change-hook . ycmd--on-window-configuration-change)
(kill-buffer-hook . ycmd--on-close-buffer)
(before-revert-hook . ycmd--teardown)
(post-command-hook . ycmd--perform-deferred-parse))
"Hooks which ycmd hooks in.")
(add-hook 'kill-emacs-hook 'ycmd-close)
(defvar ycmd-command-map
(let ((map (make-sparse-keymap)))
(define-key map "p" 'ycmd-parse-buffer)
(define-key map "o" 'ycmd-open)
(define-key map "c" 'ycmd-close)
(define-key map "." 'ycmd-goto)
(define-key map "gi" 'ycmd-goto-include)
(define-key map "gd" 'ycmd-goto-definition)
(define-key map "gD" 'ycmd-goto-declaration)
(define-key map "gm" 'ycmd-goto-implementation)
(define-key map "gp" 'ycmd-goto-imprecise)
(define-key map "gr" 'ycmd-goto-references)
(define-key map "gt" 'ycmd-goto-type)
(define-key map "s" 'ycmd-toggle-force-semantic-completion)
(define-key map "v" 'ycmd-show-debug-info)
(define-key map "V" 'ycmd-version)
(define-key map "d" 'ycmd-show-documentation)
(define-key map "C" 'ycmd-clear-compilation-flag-cache)
(define-key map "O" 'ycmd-restart-semantic-server)
(define-key map "t" 'ycmd-get-type)
(define-key map "T" 'ycmd-get-parent)
(define-key map "f" 'ycmd-fixit)
(define-key map "r" 'ycmd-refactor-rename)
(define-key map "x" 'ycmd-completer)
map)
"Keymap for `ycmd-mode' interactive commands.")
(defcustom ycmd-keymap-prefix (kbd "C-c Y")
"Prefix for key bindings of `ycmd-mode'.
Changing this variable outside Customize does not have any
effect. To change the keymap prefix from Lisp, you need to
explicitly re-define the prefix key:
(define-key ycmd-mode-map ycmd-keymap-prefix nil)
(setq ycmd-keymap-prefix (kbd \"C-c ,\"))
(define-key ycmd-mode-map ycmd-keymap-prefix
ycmd-command-map)"
:type 'string
:risky t
:set
(lambda (variable key)
(when (and (boundp variable) (boundp 'ycmd-mode-map))
(define-key ycmd-mode-map (symbol-value variable) nil)
(define-key ycmd-mode-map key ycmd-command-map))
(set-default variable key)))
(defvar ycmd-mode-map
(let ((map (make-sparse-keymap)))
(define-key map ycmd-keymap-prefix ycmd-command-map)
map)
"Keymap for `ycmd-mode'.")
(easy-menu-define ycmd-mode-menu ycmd-mode-map
"Menu used when `ycmd-mode' is active."
'("YCMd"
["Start server" ycmd-open]
["Stop server" ycmd-close]
"---"
["Parse buffer" ycmd-parse-buffer]
"---"
["GoTo" ycmd-goto]
["GoToDefinition" ycmd-goto-definition]
["GoToDeclaration" ycmd-goto-declaration]
["GoToInclude" ycmd-goto-include]
["GoToImplementation" ycmd-goto-implementation]
["GoToReferences" ycmd-goto-references]
["GoToType" ycmd-goto-type]
["GoToImprecise" ycmd-goto-imprecise]
"---"
["Show documentation" ycmd-show-documentation]
["Show type" ycmd-get-type]
["Show parent" ycmd-get-parent]
"---"
["FixIt" ycmd-fixit]
["RefactorRename" ycmd-refactor-rename]
"---"
["Load extra config" ycmd-load-conf-file]
["Restart semantic server" ycmd-restart-semantic-server]
["Clear compilation flag cache" ycmd-clear-compilation-flag-cache]
["Force semantic completion" ycmd-toggle-force-semantic-completion
:style toggle :selected ycmd-force-semantic-completion]
"---"
["Show debug info" ycmd-show-debug-info]
["Show version" ycmd-version t]
["Log enabled" ycmd-toggle-log-enabled
:style toggle :selected ycmd--log-enabled]))
(defmacro ycmd--kill-timer (timer)
"Cancel TIMER."
`(when ,timer
(cancel-timer ,timer)
(setq ,timer nil)))
(defun ycmd-parsing-in-progress-p ()
"Return t if parsing is in progress."
(eq ycmd--last-status-change 'parsing))
(defun ycmd--report-status (status)
"Report ycmd STATUS."
(setq ycmd--last-status-change status)
(force-mode-line-update))
(defun ycmd--mode-line-status-text ()
"Get text for the mode line."
(let ((force-semantic
(when ycmd-force-semantic-completion "/s"))
(text (pcase ycmd--last-status-change
(`parsed "")
(`parsing "*")
(`unparsed "?")
(`stopped "-")
(`starting ">")
(`errored "!"))))
(concat " " ycmd-mode-line-prefix force-semantic text)))
;;;###autoload
(define-minor-mode ycmd-mode
"Minor mode for interaction with the ycmd completion server.
When called interactively, toggle `ycmd-mode'. With prefix ARG,
enable `ycmd-mode' if ARG is positive, otherwise disable it.
When called from Lisp, enable `ycmd-mode' if ARG is omitted,
nil or positive. If ARG is `toggle', toggle `ycmd-mode'.
Otherwise behave as if called interactively.
\\{ycmd-mode-map}"
:init-value nil
:keymap ycmd-mode-map
:lighter (:eval (ycmd--mode-line-status-text))
:group 'ycmd
:require 'ycmd
:after-hook
(progn (unless (ycmd-running-p) (ycmd-open))
(ycmd--conditional-parse 'mode-enabled 'deferred))
(cond
(ycmd-mode
(dolist (hook ycmd-hooks-alist)
(add-hook (car hook) (cdr hook) nil 'local)))
(t
(dolist (hook ycmd-hooks-alist)
(remove-hook (car hook) (cdr hook) 'local))
(ycmd--teardown))))
;;;###autoload
(defun ycmd-setup ()
"Setup `ycmd-mode'.
Hook `ycmd-mode' into modes in `ycmd-file-type-map'."
(interactive)
(dolist (it ycmd-file-type-map)
(add-hook (intern (format "%s-hook" (symbol-name (car it)))) 'ycmd-mode)))
(make-obsolete 'ycmd-setup 'global-ycmd-mode "1.0")
(defun ycmd-version (&optional show-version)
"Get the `emacs-ycmd' version as string.
If called interactively or if SHOW-VERSION is non-nil, show the
version in the echo area and the messages buffer.
The returned string includes both, the version from package.el
and the library version, if both a present and different.
If the version number could not be determined, signal an error,
if called interactively, or if SHOW-VERSION is non-nil, otherwise
just return nil."
(interactive (list t))
(if (require 'pkg-info nil :no-error)
(let ((version (pkg-info-version-info 'ycmd)))
(when show-version
(message "emacs-ycmd version: %s" version))
version)
(error "Cannot determine version without package pkg-info")))
(defun ycmd--maybe-enable-mode ()
"Enable `ycmd-mode' according `ycmd-global-modes'."
(when (pcase ycmd-global-modes
(`t (ycmd-major-mode-to-file-types major-mode))
(`all t)
(`(not . ,modes) (not (memq major-mode modes)))
(modes (memq major-mode modes)))
(ycmd-mode)))
;;;###autoload
(define-globalized-minor-mode global-ycmd-mode ycmd-mode
ycmd--maybe-enable-mode
:init-value nil)
(defun ycmd-unload-function ()
"Unload function for ycmd."
(global-ycmd-mode -1)
(remove-hook 'kill-emacs-hook #'ycmd-close))
(defvar-local ycmd--deferred-parse nil
"If non-nil, a deferred file parse notification is pending.")
(defun ycmd--must-defer-parse ()
"Determine whether parsing the file has to be deferred.
Return t if parsing is to be deferred, or nil otherwise."
(or (not (ycmd--server-alive-p))
(not (get-buffer-window))
(ycmd-parsing-in-progress-p)
revert-buffer-in-progress-p))
(defun ycmd--deferred-parse-p ()
"Return non-nil if current buffer has a deferred parse."
ycmd--deferred-parse)
(defun ycmd--parse-deferred ()
"Defer parse notification for current buffer."
(setq ycmd--deferred-parse t))
(defun ycmd--perform-deferred-parse ()
"Perform the deferred parse."
(when (ycmd--deferred-parse-p)
(setq ycmd--deferred-parse nil)
(ycmd--conditional-parse)))
(defun ycmd--conditional-parse (&optional condition force-deferred)
"Reparse the buffer under CONDITION.
If CONDITION is non-nil, determine whether a ready to parse
notification should be sent according `ycmd-parse-conditions'.
If FORCE-DEFERRED is non-nil perform parse notification later."
(when (and ycmd-mode
(or (not condition)
(memq condition ycmd-parse-conditions)))
(if (or force-deferred (ycmd--must-defer-parse))
(ycmd--parse-deferred)
(let ((buffer (current-buffer)))
(deferred:$
(deferred:next
(lambda ()
(with-current-buffer buffer
(ycmd--on-visit-buffer))))
(deferred:nextc it
(lambda ()
(with-current-buffer buffer
(let ((tick (buffer-chars-modified-tick)))
(unless (equal tick ycmd--last-modified-tick)
(setq ycmd--last-modified-tick tick)
(ycmd-notify-file-ready-to-parse)))))))))))
(defun ycmd--on-save ()
"Function to run when the buffer has been saved."
(ycmd--conditional-parse 'save))
(defun ycmd--on-idle-change ()
"Function to run on idle-change."
(ycmd--kill-timer ycmd--notification-timer)
(ycmd--conditional-parse 'idle-change))
(defun ycmd--on-change (beg end _len)
"Function to run when a buffer change between BEG and END.
_LEN is ununsed."
(save-match-data
(when ycmd-mode
(ycmd--kill-timer ycmd--notification-timer)
(if (string-match-p "\n" (buffer-substring beg end))
(ycmd--conditional-parse 'new-line)
(setq ycmd--notification-timer
(run-at-time ycmd-idle-change-delay nil
#'ycmd--on-idle-change))))))
(defun ycmd--on-unparsed-buffer-focus (buffer)
"Function to run when an unparsed BUFFER gets focus."
(ycmd--kill-timer ycmd--on-focus-timer)
(with-current-buffer buffer
(ycmd--conditional-parse 'buffer-focus)))
(defun ycmd--on-window-configuration-change ()
"Function to run by `window-configuration-change-hook'."
(if (ycmd--deferred-parse-p)
(ycmd--perform-deferred-parse)
(when (and ycmd-mode
(pcase ycmd--last-status-change
((or `unparsed `starting) t))
(memq 'buffer-focus ycmd-parse-conditions))
(ycmd--kill-timer ycmd--on-focus-timer)
(let ((on-buffer-focus-fn
(apply-partially 'ycmd--on-unparsed-buffer-focus
(current-buffer))))
(setq ycmd--on-focus-timer
(run-at-time 1.0 nil on-buffer-focus-fn))))))
(defmacro ycmd--with-all-ycmd-buffers (&rest body)
"Execute BODY with each `ycmd-mode' enabled buffer."
(declare (indent 0) (debug t))
`(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when ycmd-mode
,@body))))
(defun ycmd--exception-p (response)
"Check whether RESPONSE is an exception."
(and (listp response) (assq 'exception response)))
(cl-defmacro ycmd-with-handled-server-exceptions (request
&rest body
&key
dont-show-exception-msg
on-exception-form
return-form
bind-current-buffer
&allow-other-keys)
"Run a deferred REQUEST and exectute BODY on success. Catch all
exceptions raised through server communication. If it is raised
because of a unknown .ycm_extra_conf.py file, load the file or
ignore it after asking the user. Otherwise print exception to
minibuffer if NO-EXCEPTION-MESSAGE is nil. ON-EXCEPTION-FORM is
run if an exception occurs. The value of RETURN-FORM is returned
on exception. If BIND-CURRENT-BUFFER is non-nil, bind
`current-buffer' to `request-buffer' var.
\(fn REQUEST &key NO-DISPLAY ERROR-FORM RETURN-FORM
BIND-CURRENT-BUFFER &rest BODY)"
(declare (indent 1) (debug t))
(while (keywordp (car body))
(setq body (cdr (cdr body))))
`(let ((request-buffer (and ,bind-current-buffer (current-buffer))))
(deferred:$
,request
(deferred:nextc it
(lambda (response)
(cl-macrolet ((with-optional-current-buffer
(buffer-or-name &rest body-2)
`(if ,buffer-or-name
(with-current-buffer ,buffer-or-name
,@body-2)
,@body-2)))
(with-optional-current-buffer
request-buffer
(if (ycmd--exception-p response)
(let-alist response
(if (string= .exception.TYPE "UnknownExtraConf")
(ycmd--handle-extra-conf-exception
.exception.extra_conf_file)
(unless ,dont-show-exception-msg
(message "%s: %s" .exception.TYPE .message))
,on-exception-form
,return-form))
(if (null ',body) response ,@body)))))))))
(defun ycmd--on-visit-buffer ()
"If `ycmd--buffer-visit-flag' is nil send BufferVisit event."
(when (and (not ycmd--buffer-visit-flag)
(ycmd--server-alive-p))
(ycmd-with-handled-server-exceptions
(ycmd--event-notification "BufferVisit")
:bind-current-buffer t
(setq ycmd--buffer-visit-flag t))))
(defun ycmd--on-close-buffer ()
"Notify server that the current buffer is no longer open.
Cleanup emacs-ycmd variables."
(when (ycmd--server-alive-p)
(ycmd-with-handled-server-exceptions
(ycmd--event-notification "BufferUnload")))
(ycmd--teardown))
(defun ycmd--reset-parse-status ()
(ycmd--report-status 'unparsed)
(setq ycmd--last-modified-tick nil))
(defun ycmd--teardown ()
"Teardown ycmd in current buffer."
(ycmd--kill-timer ycmd--notification-timer)
(ycmd--reset-parse-status)
(setq ycmd--deferred-parse nil)
(run-hooks 'ycmd-after-teardown-hook))
(defun ycmd--global-teardown ()
"Teardown ycmd in all buffers."
(ycmd--kill-timer ycmd--on-focus-timer)
(setq ycmd--mode-keywords-loaded nil)
(clrhash ycmd--available-completers)
(ycmd--with-all-ycmd-buffers
(ycmd--teardown)
(setq ycmd--buffer-visit-flag nil)))
(defun ycmd-file-types-with-diagnostics (mode)
"Find the ycmd file types for MODE which support semantic diagnostics.
Returns a possibly empty list of ycmd file type strings. If this
is empty, then ycmd doesn't support semantic completion (or
diagnostics) for MODE."
(-intersection
ycmd--file-types-with-diagnostics