Skip to content

Commit 551f72d

Browse files
authored
Allow requests for documents not managed by the server (#296)
1 parent 1c5062f commit 551f72d

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

pyls/workspace.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ def is_local(self):
107107
return (self._root_uri_scheme == '' or self._root_uri_scheme == 'file') and os.path.exists(self._root_path)
108108

109109
def get_document(self, doc_uri):
110-
return self._docs[doc_uri]
110+
"""Return a managed document if-present, else create one pointing at disk.
111111
112-
def put_document(self, doc_uri, content, version=None):
113-
path = uris.to_fs_path(doc_uri)
114-
self._docs[doc_uri] = Document(
115-
doc_uri, content,
116-
extra_sys_path=self.source_roots(path), version=version, rope=self._rope
117-
)
112+
See https://github.com/Microsoft/language-server-protocol/issues/177
113+
"""
114+
return self._docs.get(doc_uri) or self._create_document(doc_uri)
115+
116+
def put_document(self, doc_uri, source, version=None):
117+
self._docs[doc_uri] = self._create_document(doc_uri, source=source, version=version)
118118

119119
def rm_document(self, doc_uri):
120120
self._docs.pop(doc_uri)
@@ -140,6 +140,13 @@ def source_roots(self, document_path):
140140
files = _utils.find_parents(self._root_path, document_path, ['setup.py']) or []
141141
return [os.path.dirname(setup_py) for setup_py in files]
142142

143+
def _create_document(self, doc_uri, source=None, version=None):
144+
path = uris.to_fs_path(doc_uri)
145+
return Document(
146+
doc_uri, source=source,
147+
extra_sys_path=self.source_roots(path), rope=self._rope, version=version
148+
)
149+
143150

144151
class Document(object):
145152

test/test_workspace.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import os
3-
import pytest
43
from pyls import uris
54

65
DOC_URI = uris.from_fs_path(__file__)
@@ -21,17 +20,19 @@ def test_get_document(pyls):
2120
assert pyls.workspace.get_document(DOC_URI).source == 'TEXT'
2221

2322

23+
def test_get_missing_document(tmpdir, pyls):
24+
source = 'TEXT'
25+
doc_path = tmpdir.join("test_document.py")
26+
doc_path.write(source)
27+
doc_uri = uris.from_fs_path(str(doc_path))
28+
assert pyls.workspace.get_document(doc_uri).source == 'TEXT'
29+
30+
2431
def test_rm_document(pyls):
2532
pyls.workspace.put_document(DOC_URI, 'TEXT')
2633
assert pyls.workspace.get_document(DOC_URI).source == 'TEXT'
2734
pyls.workspace.rm_document(DOC_URI)
28-
with pytest.raises(KeyError):
29-
pyls.workspace.get_document(DOC_URI)
30-
31-
32-
def test_bad_get_document(pyls):
33-
with pytest.raises(KeyError):
34-
pyls.workspace.get_document("BAD_URI")
35+
assert pyls.workspace.get_document(DOC_URI)._source is None
3536

3637

3738
def test_non_root_project(pyls):

0 commit comments

Comments
 (0)