/
sessions.py
164 lines (148 loc) · 5.73 KB
/
sessions.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
from .types import ClientConfig, ClientStates, Settings
from .protocol import Request
from .transports import start_tcp_transport
from .rpc import Client, attach_stdio_client
from .process import start_server
from .url import filename_to_uri
from .logging import debug
import os
from .protocol import completion_item_kinds, symbol_kinds
try:
from typing import Callable, Dict, Any, Optional
assert Callable and Dict and Any and Optional
except ImportError:
pass
def create_session(config: ClientConfig, project_path: str, env: dict, settings: Settings,
on_created=None, on_ended: 'Optional[Callable[[str], None]]' = None,
bootstrap_client=None) -> 'Optional[Session]':
session = None
if config.binary_args:
process = start_server(config.binary_args, project_path, env, settings.log_stderr)
if process:
if config.tcp_port:
transport = start_tcp_transport(config.tcp_port, config.tcp_host)
if transport:
session = Session(config, project_path, Client(transport, settings), on_created, on_ended)
else:
# try to terminate the process
try:
process.terminate()
except Exception:
pass
else:
client = attach_stdio_client(process, settings)
session = Session(config, project_path, client, on_created, on_ended)
else:
if config.tcp_port:
transport = start_tcp_transport(config.tcp_port)
session = Session(config, project_path, Client(transport, settings),
on_created, on_ended)
elif bootstrap_client:
session = Session(config, project_path, bootstrap_client,
on_created, on_ended)
else:
debug("No way to start session")
return session
def get_initialize_params(project_path: str, config: ClientConfig):
initializeParams = {
"processId": os.getpid(),
"rootUri": filename_to_uri(project_path),
"rootPath": project_path,
"capabilities": {
"textDocument": {
"synchronization": {
"didSave": True
},
"hover": {
"contentFormat": ["markdown", "plaintext"]
},
"completion": {
"completionItem": {
"snippetSupport": True
},
"completionItemKind": {
"valueSet": completion_item_kinds
}
},
"signatureHelp": {
"signatureInformation": {
"documentationFormat": ["markdown", "plaintext"],
"parameterInformation": {
"labelOffsetSupport": True
}
}
},
"references": {},
"documentHighlight": {},
"documentSymbol": {
"symbolKind": {
"valueSet": symbol_kinds
}
},
"formatting": {},
"rangeFormatting": {},
"declaration": {},
"definition": {},
"typeDefinition": {},
"implementation": {},
"codeAction": {
"codeActionLiteralSupport": {
"codeActionKind": {
"valueSet": []
}
}
},
"rename": {}
},
"workspace": {
"applyEdit": True,
"didChangeConfiguration": {},
"executeCommand": {},
"symbol": {
"symbolKind": {
"valueSet": symbol_kinds
}
}
}
}
}
if config.init_options:
initializeParams['initializationOptions'] = config.init_options
return initializeParams
class Session(object):
def __init__(self, config: ClientConfig, project_path, client: Client,
on_created=None, on_ended: 'Optional[Callable[[str], None]]' = None) -> None:
self.config = config
self.project_path = project_path
self.state = ClientStates.STARTING
self._on_created = on_created
self._on_ended = on_ended
self.capabilities = dict() # type: Dict[str, Any]
self.client = client
self.initialize()
def has_capability(self, capability):
return capability in self.capabilities and self.capabilities[capability] is not False
def get_capability(self, capability):
return self.capabilities.get(capability)
def initialize(self):
params = get_initialize_params(self.project_path, self.config)
self.client.send_request(
Request.initialize(params),
lambda result: self._handle_initialize_result(result))
def _handle_initialize_result(self, result):
self.state = ClientStates.READY
self.capabilities = result.get('capabilities', dict())
if self._on_created:
self._on_created(self)
def end(self):
self.state = ClientStates.STOPPING
self.client.send_request(
Request.shutdown(),
lambda result: self._handle_shutdown_result(),
lambda: self._handle_shutdown_result())
def _handle_shutdown_result(self):
self.client.exit()
self.client = None
self.capabilities = dict()
if self._on_ended:
self._on_ended(self.config.name)