Skip to content

Commit 7ced70a

Browse files
committed
Make a temp document method in workspaces
1 parent 7f496bb commit 7ced70a

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

Diff for: pylsp/python_lsp.py

+1-17
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@ def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs
602602
if notebookDocument is None:
603603
raise ValueError("Invalid notebook document")
604604

605-
random_uri = str(uuid.uuid4())
606605
# cell_list helps us map the diagnostics back to the correct cell later.
607606
cell_list: List[Dict[str, Any]] = []
608607

@@ -632,21 +631,8 @@ def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs
632631

633632
offset += num_lines
634633

635-
# TODO: make a workspace temp document context manager that yields the random uri and cleans up afterwards
636-
workspace.put_document(random_uri, total_source)
637-
log.info(f'Making new document {random_uri}')
638-
try:
634+
with workspace.temp_document(total_source) as random_uri:
639635
definitions = self.definitions(random_uri, position)
640-
log.info(f'Got definitions: {definitions}')
641-
642-
# {
643-
# 'uri': uris.uri_with(document.uri, path=str(d.module_path)),
644-
# 'range': {
645-
# 'start': {'line': d.line - 1, 'character': d.column},
646-
# 'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
647-
# }
648-
# }
649-
print(definitions)
650636
for definition in definitions:
651637
# TODO: a better test for if a definition is the random_uri
652638
if random_uri in definition['uri']:
@@ -659,8 +645,6 @@ def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs
659645
definition['range']['start']['line'] -= cell['line_start']
660646
definition['range']['end']['line'] -= cell['line_start']
661647
return definitions
662-
finally:
663-
workspace.rm_document(random_uri)
664648

665649
def m_text_document__definition(self, textDocument=None, position=None, **_kwargs):
666650
# textDocument here is just a dict with a uri

Diff for: pylsp/workspace.py

+11
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ def put_document(self, doc_uri, source, version=None):
119119
def put_notebook_document(self, doc_uri, notebook_type, cells, version=None, metadata=None):
120120
self._docs[doc_uri] = self._create_notebook_document(doc_uri, notebook_type, cells, version, metadata)
121121

122+
@contextmanager
123+
def temp_document(self, source):
124+
random_uri = str(uuid.uuid4())
125+
try:
126+
self.put_document(random_uri, source)
127+
yield random_uri
128+
finally:
129+
self.rm_document(random_uri)
130+
131+
132+
122133
def add_notebook_cells(self, doc_uri, cells, start):
123134
self._docs[doc_uri].add_cells(cells, start)
124135

0 commit comments

Comments
 (0)