Skip to content

Commit c836fc1

Browse files
committed
Make a temp document method in workspaces
1 parent fab7d91 commit c836fc1

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
@@ -593,7 +593,6 @@ def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs
593593
if notebookDocument is None:
594594
raise ValueError("Invalid notebook document")
595595

596-
random_uri = str(uuid.uuid4())
597596
# cell_list helps us map the diagnostics back to the correct cell later.
598597
cell_list: List[Dict[str, Any]] = []
599598

@@ -623,21 +622,8 @@ def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs
623622

624623
offset += num_lines
625624

626-
# TODO: make a workspace temp document context manager that yields the random uri and cleans up afterwards
627-
workspace.put_document(random_uri, total_source)
628-
log.info(f'Making new document {random_uri}')
629-
try:
625+
with workspace.temp_document(total_source) as random_uri:
630626
definitions = self.definitions(random_uri, position)
631-
log.info(f'Got definitions: {definitions}')
632-
633-
# {
634-
# 'uri': uris.uri_with(document.uri, path=str(d.module_path)),
635-
# 'range': {
636-
# 'start': {'line': d.line - 1, 'character': d.column},
637-
# 'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
638-
# }
639-
# }
640-
print(definitions)
641627
for definition in definitions:
642628
# TODO: a better test for if a definition is the random_uri
643629
if random_uri in definition['uri']:
@@ -650,8 +636,6 @@ def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs
650636
definition['range']['start']['line'] -= cell['line_start']
651637
definition['range']['end']['line'] -= cell['line_start']
652638
return definitions
653-
finally:
654-
workspace.rm_document(random_uri)
655639

656640
def m_text_document__definition(self, textDocument=None, position=None, **_kwargs):
657641
# 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)