-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathworkspace.py
232 lines (177 loc) · 7.79 KB
/
workspace.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
# Copyright 2017 Palantir Technologies, Inc.
import io
import logging
import os
import re
import jedi
from . import lsp, uris, _utils
log = logging.getLogger(__name__)
# TODO: this is not the best e.g. we capture numbers
RE_START_WORD = re.compile('[A-Za-z_0-9]*$')
RE_END_WORD = re.compile('^[A-Za-z_0-9]*')
class Workspace(object):
M_PUBLISH_DIAGNOSTICS = 'textDocument/publishDiagnostics'
M_APPLY_EDIT = 'workspace/applyEdit'
M_SHOW_MESSAGE = 'window/showMessage'
def __init__(self, root_uri, endpoint):
self._root_uri = root_uri
self._endpoint = endpoint
self._root_uri_scheme = uris.urlparse(self._root_uri)[0]
self._root_path = uris.to_fs_path(self._root_uri)
self._docs = {}
# Whilst incubating, keep rope private
self.__rope = None
self.__rope_config = None
def _rope_project_builder(self, rope_config):
from rope.base.project import Project
# TODO: we could keep track of dirty files and validate only those
if self.__rope is None or self.__rope_config != rope_config:
rope_folder = rope_config.get('ropeFolder')
self.__rope = Project(self._root_path, ropefolder=rope_folder)
self.__rope.prefs.set('extension_modules', rope_config.get('extensionModules', []))
self.__rope.prefs.set('ignore_syntax_errors', True)
self.__rope.prefs.set('ignore_bad_imports', True)
self.__rope.validate()
return self.__rope
@property
def documents(self):
return self._docs
@property
def root_path(self):
return self._root_path
@property
def root_uri(self):
return self._root_uri
def is_local(self):
return (self._root_uri_scheme == '' or self._root_uri_scheme == 'file') and os.path.exists(self._root_path)
def get_document(self, doc_uri):
"""Return a managed document if-present, else create one pointing at disk.
See https://github.com/Microsoft/language-server-protocol/issues/177
"""
return self._docs.get(doc_uri) or self._create_document(doc_uri)
def put_document(self, doc_uri, source, version=None):
self._docs[doc_uri] = self._create_document(doc_uri, source=source, version=version)
def rm_document(self, doc_uri):
self._docs.pop(doc_uri)
def update_document(self, doc_uri, change, version=None):
self._docs[doc_uri].apply_change(change)
self._docs[doc_uri].version = version
def apply_edit(self, edit):
return self._endpoint.request(self.M_APPLY_EDIT, {'edit': edit})
def publish_diagnostics(self, doc_uri, diagnostics):
self._endpoint.notify(self.M_PUBLISH_DIAGNOSTICS, params={'uri': doc_uri, 'diagnostics': diagnostics})
def show_message(self, message, msg_type=lsp.MessageType.Info):
self._endpoint.notify(self.M_SHOW_MESSAGE, params={'type': msg_type, 'message': message})
def source_roots(self, document_path):
"""Return the source roots for the given document."""
files = _utils.find_parents(self._root_path, document_path, ['setup.py', 'pyproject.toml']) or []
return list(set((os.path.dirname(project_file) for project_file in files))) or [self._root_path]
def _create_document(self, doc_uri, source=None, version=None):
path = uris.to_fs_path(doc_uri)
return Document(
doc_uri, source=source, version=version,
extra_sys_path=self.source_roots(path),
rope_project_builder=self._rope_project_builder,
)
class Document(object):
def __init__(self, uri, source=None, version=None, local=True, extra_sys_path=None, rope_project_builder=None):
self.uri = uri
self.version = version
self.path = uris.to_fs_path(uri)
self.filename = os.path.basename(self.path)
self._local = local
self._source = source
self._extra_sys_path = extra_sys_path or []
self._rope_project_builder = rope_project_builder
def __str__(self):
return str(self.uri)
def _rope_resource(self, rope_config):
from rope.base import libutils
return libutils.path_to_resource(self._rope_project_builder(rope_config), self.path)
@property
def lines(self):
return self.source.splitlines(True)
@property
def source(self):
if self._source is None:
with io.open(self.path, 'r', encoding='utf-8') as f:
return f.read()
return self._source
def apply_change(self, change):
"""Apply a change to the document."""
text = change['text']
change_range = change.get('range')
if not change_range:
# The whole file has changed
self._source = text
return
start_line = change_range['start']['line']
start_col = change_range['start']['character']
end_line = change_range['end']['line']
end_col = change_range['end']['character']
# Check for an edit occuring at the very end of the file
if start_line == len(self.lines):
self._source = self.source + text
return
new = io.StringIO()
# Iterate over the existing document until we hit the edit range,
# at which point we write the new text, then loop until we hit
# the end of the range and continue writing.
for i, line in enumerate(self.lines):
if i < start_line:
new.write(line)
continue
if i > end_line:
new.write(line)
continue
if i == start_line:
new.write(line[:start_col])
new.write(text)
if i == end_line:
new.write(line[end_col:])
self._source = new.getvalue()
def position_at_offset(self, offset):
lines = self.source[:offset].splitlines(True)
last_line = lines[-1]
if last_line.rstrip('\r\n') == last_line:
return {'line': len(lines) - 1, 'character': len(last_line)}
return {'line': len(lines), 'character': 0}
def offset_at_position(self, position):
"""Return the byte-offset pointed at by the given position."""
return position['character'] + len(''.join(self.lines[:position['line']]))
def word_at_position(self, position):
"""Get the word under the cursor returning the start and end positions."""
if position['line'] >= len(self.lines):
return ''
line = self.lines[position['line']]
i = position['character']
# Split word in two
start = line[:i]
end = line[i:]
# Take end of start and start of end to find word
# These are guaranteed to match, even if they match the empty string
m_start = RE_START_WORD.findall(start)
m_end = RE_END_WORD.findall(end)
return m_start[0] + m_end[-1]
def jedi_names(self, all_scopes=False, definitions=True, references=False):
return jedi.api.names(
source=self.source, path=self.path, all_scopes=all_scopes,
definitions=definitions, references=references
)
def jedi_script(self, position=None):
kwargs = {
'source': self.source,
'path': self.path,
'sys_path': self.sys_path()
}
if position:
kwargs['line'] = position['line'] + 1
kwargs['column'] = _utils.clip_column(position['character'], self.lines, position['line'])
return jedi.Script(**kwargs)
def sys_path(self):
# Copy our extra sys path
path = list(self._extra_sys_path)
# TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
environment = jedi.api.environment.get_cached_default_environment()
path.extend(environment.get_sys_path())
return path