-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathapi.py
1418 lines (1188 loc) · 45.6 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""
General Spyder completion API constants and enumerations.
The constants and enums presented on this file correspond to a superset of
those used by the Language Server Protocol (LSP), available at:
https://microsoft.github.io/language-server-protocol/specifications/specification-current/
"""
# Standard library imports
from typing import Any, Optional, Tuple, Union
# Third party imports
from qtpy.QtCore import Signal, QObject, Slot, Qt
# Local imports
from spyder.api.config.mixins import SpyderConfigurationObserver
# Supported LSP programming languages
SUPPORTED_LANGUAGES = [
'Bash', 'C#', 'Cpp', 'CSS/LESS/SASS', 'Go', 'GraphQL', 'Groovy', 'Elixir',
'Erlang', 'Fortran', 'Haxe', 'HTML', 'Java', 'JavaScript', 'JSON',
'Julia', 'Kotlin', 'OCaml', 'PHP', 'R', 'Rust', 'Scala', 'Swift',
'TypeScript'
]
# ------------------ WORKSPACE SYMBOLS CONSTANTS --------------------
class SymbolKind:
"""LSP workspace symbol constants."""
FILE = 1
MODULE = 2
NAMESPACE = 3
PACKAGE = 4
CLASS = 5
METHOD = 6
PROPERTY = 7
FIELD = 8
CONSTRUCTOR = 9
ENUM = 10
INTERFACE = 11
FUNCTION = 12
VARIABLE = 13
CONSTANT = 14
STRING = 15
NUMBER = 16
BOOLEAN = 17
ARRAY = 18
OBJECT = 19
KEY = 20
NULL = 21
ENUM_MEMBER = 22
STRUCT = 23
EVENT = 24
OPERATOR = 25
TYPE_PARAMETER = 26
# Additional symbol constants (non-standard)
BLOCK_COMMENT = 224
CELL = 225
# Mapping between symbol enum and icons
SYMBOL_KIND_ICON = {
SymbolKind.FILE: 'file',
SymbolKind.MODULE: 'module',
SymbolKind.NAMESPACE: 'namespace',
SymbolKind.PACKAGE: 'package',
SymbolKind.CLASS: 'class',
SymbolKind.METHOD: 'method',
SymbolKind.PROPERTY: 'property',
SymbolKind.FIELD: 'field',
SymbolKind.CONSTRUCTOR: 'constructor',
SymbolKind.ENUM: 'enum',
SymbolKind.INTERFACE: 'interface',
SymbolKind.FUNCTION: 'function',
SymbolKind.VARIABLE: 'variable',
SymbolKind.CONSTANT: 'constant',
SymbolKind.STRING: 'string',
SymbolKind.NUMBER: 'number',
SymbolKind.BOOLEAN: 'boolean',
SymbolKind.ARRAY: 'array',
SymbolKind.OBJECT: 'object',
SymbolKind.KEY: 'key',
SymbolKind.NULL: 'null',
SymbolKind.ENUM_MEMBER: 'enum_member',
SymbolKind.STRUCT: 'struct',
SymbolKind.EVENT: 'event',
SymbolKind.OPERATOR: 'operator',
SymbolKind.TYPE_PARAMETER: 'type_parameter',
SymbolKind.BLOCK_COMMENT: 'blockcomment',
SymbolKind.CELL: 'cell'
}
# -------------------- WORKSPACE CONFIGURATION CONSTANTS ----------------------
class ResourceOperationKind:
"""LSP workspace resource operations."""
# The client is able to create workspace files and folders
CREATE = 'create'
# The client is able to rename workspace files and folders
RENAME = 'rename'
# The client is able to delete workspace files and folders
DELETE = 'delete'
class FileChangeType:
CREATED = 1
CHANGED = 2
DELETED = 3
class FailureHandlingKind:
"""LSP workspace modification error codes."""
# Applying the workspace change is simply aborted if one
# of the changes provided fails. All operations executed before, stay.
ABORT = 'abort'
# All the operations succeed or no changes at all.
TRANSACTIONAL = 'transactional'
# The client tries to undo the applied operations, best effort strategy.
UNDO = 'undo'
# The textual changes are applied transactionally, whereas
# creation/deletion/renaming operations are aborted.
TEXT_ONLY_TRANSACTIONAL = 'textOnlyTransactional'
# -------------------- CLIENT CONFIGURATION SETTINGS --------------------------
# WorkspaceClientCapabilities define capabilities the
# editor / tool provides on the workspace
WORKSPACE_CAPABILITIES = {
# The client supports applying batch edits to the workspace.
# Request: An array of `TextDocumentEdit`s to express changes
# to n different text documents
"applyEdit": True,
# Workspace edition settings
"workspaceEdit": {
# The client supports versioned document changes.
"documentChanges": True,
# The resource operations that the client supports
"resourceOperations": [ResourceOperationKind.CREATE,
ResourceOperationKind.RENAME,
ResourceOperationKind.DELETE],
# Failure handling strategy applied by the client.
"failureHandling": FailureHandlingKind.TRANSACTIONAL
},
# Did change configuration notification supports dynamic registration.
"didChangeConfiguration": {
# Reload server settings dynamically
"dynamicRegistration": True
},
# The watched files notification is sent from the client to the server
# when the client detects changes to files watched by
# the language client.
"didChangeWatchedFiles": {
# Can be turned on/off dynamically
"dynamicRegistration": True
},
# The workspace symbol request is sent from the client to the server to
# list project-wide symbols matching the query string.
"symbol": {
# Can be turned on/off dynamically
"dynamicRegistration": True
},
# The workspace/executeCommand request is sent from the client to the
# server to trigger command execution on the server. In most cases the
# server creates a WorkspaceEdit structure and applies the changes to
# the workspace using the request workspace/applyEdit which is sent from
# the server to the client.
"executeCommand": {
# Can be turned on/off dynamically
"dynamicRegistration": True,
# Specific capabilities for the `SymbolKind` in the `workspace/symbol`
# request.
"symbolKind": {
# The symbol kind values the client supports.
"valueSet": [value for value in SymbolKind.__dict__.values()
if isinstance(value, int)]
}
},
# The client has support for workspace folders.
"workspaceFolders": True,
# The client supports `workspace/configuration` requests.
"configuration": True
}
# TextDocumentClientCapabilities define capabilities the editor / tool
# provides on text documents.
TEXT_EDITOR_CAPABILITES = {
# Editor supports file watching and synchronization (Required)
"synchronization": {
# File synchronization can be turned on/off.
"dynamicRegistration": True,
# The client (Spyder) will send a willSave notification
# to the server when a file is about to be saved.
"willSave": True,
# The client (Spyder) supports sending a will save request and
# waits for a response providing text edits which will
# be applied to the document before it is saved.
"willSaveWaitUntil": True,
# The client (Spyder) supports did save notifications.
# The document save notification is sent from the client to
# the server when the document was saved in the client.
"didSave": True
},
# Editor supports code completion operations.
# The Completion request is sent from the client to the server to
# compute completion items at a given cursor position.
"completion": {
# Code completion can be turned on/off dynamically.
"dynamicRegistration": True,
"completionItem": {
# Client (Spyder) supports snippets as insert text.
# A snippet can define tab stops and placeholders with `$1`, `$2`
# and `${3:foo}`. `$0` defines the final tab stop, it defaults to
# the end of the snippet. Placeholders with equal identifiers are
# linked, that is typing in one will update others too.
"snippetSupport": True,
# Completion item docs can only be handled in plain text
"documentationFormat": ['plaintext'],
}
},
# The hover request is sent from the client to the server to request
# hover information at a given text document position.
"hover": {
# Hover introspection can be turned on/off dynamically.
"dynamicRegistration": True,
# Hover contents can only be handled in plain text by Spyder
"contentFormat": ['plaintext'],
},
# The signature help request is sent from the client to the server to
# request signature information at a given cursor position.
"signatureHelp": {
# Function/Class/Method signature hinting can be turned on/off
# dynamically.
"dynamicRegistration": True,
# Signature docs can only be handled in plain text by Spyder
"signatureInformation": {
"documentationFormat": ['plaintext'],
}
},
# Editor allows to find references.
# The references request is sent from the client to the server to resolve
# project-wide references for the symbol denoted by the given text
# document position.
"references": {
# Find references can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor allows to highlight different text sections at the same time.
# The document highlight request is sent from the client to the server to
# resolve a document highlights for a given text document position
"documentHighlight": {
# Code highlighting can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor supports finding symbols on a document.
# The document symbol request is sent from the client to the server to list
# all symbols found in a given text document.
"documentSymbol": {
# Find symbols on document can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor allows to autoformat all the document.
# The document formatting request is sent from the server to the client to
# format a whole document.
"formatting": {
# Document formatting can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor can autoformat only a selected region on a document.
# The document range formatting request is sent from the client to the
# server to format a given range in a document.
"rangeFormatting": {
# Partial document formatting can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor allows to format a document while an edit is taking place.
# The document on type formatting request is sent from the client to the
# server to format parts of the document during typing.
"onTypeFormatting": {
# On-Type formatting can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor has an option to go to a function/class/method definition.
# The goto definition request is sent from the client to the server to
# resolve the definition location of a symbol at a given text document
# position.
"definition": {
# Go-to-definition can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor can give/highlight refactor tips/solutions.
# The code action request is sent from the client to the server to compute
# commands for a given text document and range. These commands are
# typically code fixes to either fix problems or to beautify/refactor code.
"codeAction": {
# Code hints can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor can display additional commands/statistics per each line.
# The code lens request is sent from the client to the server to compute
# code lenses for a given text document.
# A code lens represents a command that should be shown along with
# source text, like the number of references, a way to run tests, etc.
"codeLens": {
# Code lens can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor allows to find cross-document link references.
# The document links request is sent from the client to the server to
# request the location of links in a document.
# A document link is a range in a text document that links to an internal
# or external resource, like another text document or a web site.
"documentLink": {
# Finding document cross-references can be turned on/off dynamically.
"dynamicRegistration": True
},
# Editor allows to rename a variable/function/reference globally
# on a document.
# The rename request is sent from the client to the server to perform
# a workspace-wide rename of a symbol.
"rename": {
"dynamicRegistration": True
}
}
# Spyder editor and workspace capabilities
CLIENT_CAPABILITES = {
"workspace": WORKSPACE_CAPABILITIES,
"textDocument": TEXT_EDITOR_CAPABILITES
}
# -------------------- SERVER CONFIGURATION SETTINGS --------------------------
# Text document synchronization mode constants
class TextDocumentSyncKind:
"""Text document synchronization modes supported by a lsp-server"""
NONE = 0 # Text synchronization is not supported
FULL = 1 # Text synchronization requires all document contents
INCREMENTAL = 2 # Partial text synchronization is supported
# Save options.
SAVE_OPTIONS = {
# The client is supposed to include the content on save.
'includeText': True
}
# Text synchronization capabilities
TEXT_DOCUMENT_SYNC_OPTIONS = {
# Open and close notifications are sent to the server.
'openClose': True,
# Change notifications are sent to the server.
# See TextDocumentSyncKind.NONE, TextDocumentSyncKind.FULL
# and TextDocumentSyncKind.INCREMENTAL.
'change': TextDocumentSyncKind.NONE,
# Will save notifications are sent to the server.
'willSave': False,
# Will save wait until requests are sent to the server.
'willSaveWaitUntil': False,
# Save notifications are sent to the server.
'save': SAVE_OPTIONS
}
# Code completion options
COMPLETION_OPTIONS = {
# The server provides support to resolve additional
# information for a completion item.
'resolveProvider': False,
# The characters that trigger completion automatically.
'triggerCharacters': []
}
# Signature help options
SIGNATURE_HELP_OPTIONS = {
# The characters that trigger signature help automatically.
'triggerCharacters': []
}
# Code lens options
CODE_LENS_OPTIONS = {
# Code lens has a resolve provider as well.
'resolveProvider': False
}
# Format document on type options
DOCUMENT_ON_TYPE_FORMATTING_OPTIONS = {
# A character on which formatting should be triggered, like `}`.
'firstTriggerCharacter': None,
# More trigger characters.
'moreTriggerCharacter': [],
}
# Document link options
DOCUMENT_LINK_OPTIONS = {
# Document links have a resolve provider as well.
'resolveProvider': False
}
# Execute command options.
EXECUTE_COMMAND_OPTIONS = {
# The commands to be executed on the server
'commands': []
}
# Workspace options.
WORKSPACE_OPTIONS = {
# The server has support for workspace folders
'workspaceFolders': {
'supported': False,
'changeNotifications': False
}
}
# Server available capabilites options as defined by the protocol.
SERVER_CAPABILITES = {
# Defines how text documents are synced.
# Is either a detailed structure defining each notification or
# for backwards compatibility the TextDocumentSyncKind number.
'textDocumentSync': TEXT_DOCUMENT_SYNC_OPTIONS,
# The server provides hover support.
'hoverProvider': False,
# The server provides completion support.
'completionProvider': COMPLETION_OPTIONS,
# The server provides signature help support.
'signatureHelpProvider': SIGNATURE_HELP_OPTIONS,
# The server provides goto definition support.
'definitionProvider': False,
# The server provides find references support.
'referencesProvider': False,
# The server provides document highlight support.
'documentHighlightProvider': False,
# The server provides document symbol support.
'documentSymbolProvider': False,
# The server provides workspace symbol support.
'workspaceSymbolProvider': False,
# The server provides code actions.
'codeActionProvider': False,
# The server provides code lens.
'codeLensProvider': CODE_LENS_OPTIONS,
# The server provides document formatting.
'documentFormattingProvider': False,
# The server provides document range formatting.
'documentRangeFormattingProvider': False,
# The server provides document formatting on typing.
'documentOnTypeFormattingProvider': DOCUMENT_ON_TYPE_FORMATTING_OPTIONS,
# The server provides rename support.
'renameProvider': False,
# The server provides document link support.
'documentLinkProvider': DOCUMENT_LINK_OPTIONS,
# The server provides execute command support.
'executeCommandProvider': EXECUTE_COMMAND_OPTIONS,
# Workspace specific server capabillities.
'workspace': WORKSPACE_OPTIONS,
# Experimental server capabilities.
'experimental': None
}
class LSPEventTypes:
"""Language Server Protocol event types."""
DOCUMENT = 'textDocument'
WORKSPACE = 'workspace'
WINDOW = 'window'
CODE_LENS = 'codeLens'
class CompletionRequestTypes:
"""Language Server Protocol request/response types."""
# General requests
INITIALIZE = 'initialize'
INITIALIZED = 'initialized'
SHUTDOWN = 'shutdown'
EXIT = 'exit'
CANCEL_REQUEST = '$/cancelRequest'
# Window requests
WINDOW_SHOW_MESSAGE = 'window/showMessage'
WINDOW_SHOW_MESSAGE_REQUEST = 'window/showMessageRequest'
WINDOW_LOG_MESSAGE = 'window/logMessage'
TELEMETRY_EVENT = 'telemetry/event'
# Client capabilities requests
CLIENT_REGISTER_CAPABILITY = 'client/registerCapability'
CLIENT_UNREGISTER_CAPABILITY = 'client/unregisterCapability'
# Workspace requests
WORKSPACE_FOLDERS = 'workspace/workspaceFolders'
WORKSPACE_FOLDERS_CHANGE = 'workspace/didChangeWorkspaceFolders'
WORKSPACE_CONFIGURATION = 'workspace/configuration'
WORKSPACE_CONFIGURATION_CHANGE = 'workspace/didChangeConfiguration'
WORKSPACE_WATCHED_FILES_UPDATE = 'workspace/didChangeWatchedFiles'
WORKSPACE_SYMBOL = 'workspace/symbol'
WORKSPACE_EXECUTE_COMMAND = 'workspace/executeCommand'
WORKSPACE_APPLY_EDIT = 'workspace/applyEdit'
# Document requests
DOCUMENT_PUBLISH_DIAGNOSTICS = 'textDocument/publishDiagnostics'
DOCUMENT_DID_OPEN = 'textDocument/didOpen'
DOCUMENT_DID_CHANGE = 'textDocument/didChange'
DOCUMENT_WILL_SAVE = 'textDocument/willSave'
DOCUMENT_WILL_SAVE_UNTIL = 'textDocument/willSaveWaitUntil'
DOCUMENT_DID_SAVE = 'textDocument/didSave'
DOCUMENT_DID_CLOSE = 'textDocument/didClose'
DOCUMENT_COMPLETION = 'textDocument/completion'
COMPLETION_RESOLVE = 'completionItem/resolve'
DOCUMENT_HOVER = 'textDocument/hover'
DOCUMENT_SIGNATURE = 'textDocument/signatureHelp'
DOCUMENT_REFERENCES = ' textDocument/references'
DOCUMENT_HIGHLIGHT = 'textDocument/documentHighlight'
DOCUMENT_SYMBOL = 'textDocument/documentSymbol'
DOCUMENT_FORMATTING = 'textDocument/formatting'
DOCUMENT_FOLDING_RANGE = 'textDocument/foldingRange'
DOCUMENT_RANGE_FORMATTING = 'textDocument/rangeFormatting'
DOCUMENT_ON_TYPE_FORMATTING = 'textDocument/onTypeFormatting'
DOCUMENT_DEFINITION = 'textDocument/definition'
DOCUMENT_CODE_ACTION = 'textDocument/codeAction'
DOCUMENT_CODE_LENS = 'textDocument/codeLens'
CODE_LENS_RESOLVE = 'codeLens/resolve'
DOCUMENT_LINKS = 'textDocument/documentLink'
DOCUMENT_LINK_RESOLVE = 'documentLink/resolve'
DOCUMENT_RENAME = 'textDocument/rename'
# Spyder extensions to LSP
DOCUMENT_CURSOR_EVENT = 'textDocument/cursorEvent'
# -------------------- LINTING RESPONSE RELATED VALUES ------------------------
class DiagnosticSeverity:
"""LSP diagnostic severity levels."""
ERROR = 1
WARNING = 2
INFORMATION = 3
HINT = 4
# ----------------- AUTO-COMPLETION RESPONSE RELATED VALUES -------------------
class CompletionItemKind:
"""LSP completion element categories."""
TEXT = 1
METHOD = 2
FUNCTION = 3
CONSTRUCTOR = 4
FIELD = 5
VARIABLE = 6
CLASS = 7
INTERFACE = 8
MODULE = 9
PROPERTY = 10
UNIT = 11
VALUE = 12
ENUM = 13
KEYWORD = 14
SNIPPET = 15
COLOR = 16
FILE = 17
REFERENCE = 18
class InsertTextFormat:
"""LSP completion text interpretations."""
PLAIN_TEXT = 1
SNIPPET = 2
# ----------------- SAVING REQUEST RELATED VALUES -------------------
class TextDocumentSaveReason:
"""LSP text document saving action causes."""
MANUAL = 1
AFTER_DELAY = 2
FOCUS_OUT = 3
# ----------------------- INTERNAL CONSTANTS ------------------------
class ClientConstants:
"""Internal LSP Client constants."""
CANCEL = 'lsp-cancel'
# ----------------------- WORKSPACE UPDATE CONSTANTS ----------------
class WorkspaceUpdateKind:
ADDITION = 'addition'
DELETION = 'deletion'
# ---------------- OTHER GENERAL PURPOSE CONSTANTS ------------------
COMPLETION_ENTRYPOINT = 'spyder.completions'
# -------------- SPYDER COMPLETION PROVIDER INTERFACE ---------------
class CompletionConfigurationObserver(SpyderConfigurationObserver):
"""
Extension to the :class:`spyder.api.config.mixins.SpyderConfigurationObserver`
mixin implementation to consider a nested provider configuration.
"""
def _gather_observers(self):
"""Gather all the methods decorated with `on_conf_change`."""
for method_name in dir(self):
method = getattr(self, method_name, None)
if hasattr(method, '_conf_listen'):
info = method._conf_listen
if len(info) > 1:
self._multi_option_listeners |= {method_name}
for section, option in info:
if section is None:
section = 'completions'
if option == '__section':
option = (
'provider_configuration',
self.COMPLETION_PROVIDER_NAME,
'values'
)
else:
option = self._wrap_provider_option(option)
section_listeners = self._configuration_listeners.get(
section, {})
option_listeners = section_listeners.get(option, [])
option_listeners.append(method_name)
section_listeners[option] = option_listeners
self._configuration_listeners[section] = section_listeners
def _wrap_provider_option(self, option):
if isinstance(option, tuple):
option = (
'provider_configuration',
self.COMPLETION_PROVIDER_NAME,
'values',
*option
)
else:
option = (
'provider_configuration',
self.COMPLETION_PROVIDER_NAME,
'values',
option
)
return option
class SpyderCompletionProvider(QObject, CompletionConfigurationObserver):
"""
Spyder provider API for completion providers.
All completion providers must implement this interface in order to interact
with Spyder CodeEditor and Projects manager.
"""
sig_response_ready = Signal(str, int, dict)
"""
This signal is used to send a response back to the completion manager.
Parameters
----------
completion_provider_name: str
Name of the completion provider that produced this response.
request_seq: int
Sequence number for the request.
response: dict
Actual request corpus response.
"""
sig_provider_ready = Signal(str)
"""
This signal is used to indicate that the completion provider is ready
to handle requests.
Parameters
----------
completion_provider_name: str
Name of the completion provider.
"""
sig_language_completions_available = Signal(dict, str)
"""
This signal is used to indicate that completion capabilities are supported
for a given programming language.
Parameters
----------
completion_capabilites: dict
Available configurations supported by the provider, it should conform
to `spyder.plugins.completion.api.SERVER_CAPABILITES`.
language: str
Name of the programming language whose completion capabilites are
available.
"""
sig_disable_provider = Signal(str)
"""
This signal is used to indicate that a completion provider should be
disabled.
Parameters
----------
completion_provider_name: str
Name of the completion provider to disable.
"""
sig_show_widget = Signal(object)
"""
This signal is used to display a graphical widget such as a QMessageBox.
Parameters
----------
widget: Union[QWidget, Callable[[QWidget], QWidget]]
Widget to display, its constructor should receive parent as its first
and only argument.
"""
sig_call_statusbar = Signal(str, str, tuple, dict)
"""
This signal is used to call a remote method on a statusbar widget
registered via the `STATUS_BAR_CLASSES` attribute.
Parameters
----------
statusbar_key: str
Status bar key identifier that was registered on the
`STATUS_BAR_CLASSES` attribute.
method_name: str
Name of the remote method defined on the statusbar.
args: tuple
Tuple with positional arguments to invoke the method.
kwargs: dict
Dictionary containing optional arguments to invoke the method.
"""
sig_open_file = Signal(str)
"""
This signal is used to open a file in the editor.
Parameters
----------
path: str
Path to a file to open with the editor.
"""
sig_stop_completions = Signal(str)
"""
This signal is used to stop the completion services on Spyder plugins
that depend on them.
Parameters
----------
language: str
Name of the programming language whose completion services are going
to be stopped.
"""
sig_exception_occurred = Signal(dict)
"""
This signal can be emitted to report an exception from any provider.
Parameters
----------
error_data: dict
The dictionary containing error data. The expected keys are:
>>> error_data= {
"text": str,
"is_traceback": bool,
"repo": str,
"title": str,
"label": str,
"steps": str,
}
Notes
-----
The `is_traceback` key indicates if `text` contains plain text or a
Python error traceback.
The `title` and `repo` keys indicate how the error data should
customize the report dialog and Github error submission.
The `label` and `steps` keys allow customizing the content of the
error dialog.
This signal is automatically connected to the main Spyder interface.
"""
# ---------------------------- ATTRIBUTES ---------------------------------
# Name of the completion provider
# Status: Required
COMPLETION_PROVIDER_NAME = None
# Define the priority of this provider, with 1 being the highest one
# Status: Required
DEFAULT_ORDER = -1
# Define if the provider response time is not constant and may take
# a long time for some requests.
SLOW = False
# Define configuration options for the provider.
# List of tuples with the first item being the option name and the second
# one its default value.
#
# CONF_DEFAULTS_EXAMPLE = [
# ('option-1', 'some-value'),
# ('option-2': True)
# ]
CONF_DEFAULTS = []
# IMPORTANT NOTES:
# 1. If you want to *change* the default value of a current option, you
# need to do a MINOR update in config version, e.g. from 0.1.0 to 0.2.0
# 2. If you want to *remove* options that are no longer needed or if you
# want to *rename* options, then you need to do a MAJOR update in
# version, e.g. from 0.1.0 to 1.0.0
# 3. You don't need to touch this value if you're just adding a new option
CONF_VERSION = "0.1.0"
# Widget to be added as a tab in the "Completion and linting" entry of
# Spyder Preferences dialog. This will allow users to graphically configure
# the options declared by the provider.
CONF_TABS = []
# A list of status bars classes that the provider declares to
# display on Spyder.
#
# Each status bar should correspond to a
# :class:`spyder.api.widgets.status.StatusBarWidget` or
# a callable that returns a StatusBarWidget.
#
# type: Union[StatusBarWidget, Callable[[QWidget], StatusBarWidget]]
#
# STATUS_BAR_CLASSES = [
# StatusBarClass1,
# StatusBarClass2,
# FunctionThatReturnsAStatusBar
# ...
# ]
STATUS_BAR_CLASSES = []
def __init__(self, parent, config):
"""
Main completion provider constructor.
Parameters
----------
parent: spyder.plugins.completion.plugin.CompletionPlugin
Instance of the completion plugin that manages this provider
config: dict
Current provider configuration values, whose keys correspond to
the ones defined on `CONF_DEFAULTS` and the values correspond to
the current values according to the Spyder configuration system.
"""
self.CONF_SECTION = (parent.CONF_SECTION
if parent is not None else 'completions')
QObject.__init__(self, parent)
CompletionConfigurationObserver.__init__(self)
self.main = parent
self.config = config
def get_name(self) -> str:
"""Return a human readable name of the completion provider."""
return ''
def register_file(self, language: str, filename: str, codeeditor):
"""
Register file to perform completions.
If a language provider is not available for a given file, then this
method should keep a queue, such that files can be initialized once
a server is available.
Parameters
----------
language: str
Programming language of the given file
filename: str
Filename to register
codeeditor: spyder.plugins.editor.widgets.codeeditor.CodeEditor
Codeeditor to send the provider configurations
"""
pass
def send_request(
self, language: str, req_type: str, req: dict, req_id: int):
"""
Send completion/introspection request from Spyder.
The completion request `req_type` needs to have a response.
Parameters
----------
language: str
Programming language for the incoming request
req_type: str
Type of request, one of
:class:`spyder.plugins.completion.api.CompletionRequestTypes`
req: dict
Request body
{
'filename': str,
**kwargs: request-specific parameters
}
req_id: int
Request identifier for response
Notes
-----
A completion client should always reply to the
`textDocument/completion` request, even if the answer is empty.
"""
pass
def send_notification(
self, language: str, notification_type: str, notification: dict):
"""
Send notification to completion server based on Spyder changes.
All notifications sent won't return a response by the provider.
Parameters
----------
language: str
Programming language for the incoming request
notification_type: str
Type of request, one of