|
16 | 16 |
|
17 | 17 | from . import lsp, _utils, uris
|
18 | 18 | from .config import config
|
19 |
| -from .workspace import Workspace, Document, Notebook |
| 19 | +from .workspace import Workspace, Document, Notebook, Cell |
20 | 20 | from ._version import __version__
|
21 | 21 |
|
22 | 22 | log = logging.getLogger(__name__)
|
@@ -480,7 +480,7 @@ def m_notebook_document__did_open(self, notebookDocument=None, cellTextDocuments
|
480 | 480 | cells=notebookDocument['cells'], version=notebookDocument.get('version'),
|
481 | 481 | metadata=notebookDocument.get('metadata'))
|
482 | 482 | for cell in (cellTextDocuments or []):
|
483 |
| - workspace.put_cell_document(cell['uri'], cell['languageId'], cell['text'], version=cell.get('version')) |
| 483 | + workspace.put_cell_document(cell['uri'], notebookDocument['uri'], cell['languageId'], cell['text'], version=cell.get('version')) |
484 | 484 | self.lint(notebookDocument['uri'], is_saved=True)
|
485 | 485 |
|
486 | 486 | def m_notebook_document__did_close(self, notebookDocument=None, cellTextDocuments=None, **_kwargs):
|
@@ -521,7 +521,7 @@ def m_notebook_document__did_change(self, notebookDocument=None, change=None, **
|
521 | 521 | # Case 2
|
522 | 522 | # Cell documents
|
523 | 523 | for cell_document in structure['didOpen']:
|
524 |
| - workspace.put_cell_document(cell_document['uri'], cell_document['languageId'], |
| 524 | + workspace.put_cell_document(cell_document['uri'], notebookDocument['uri'], cell_document['languageId'], |
525 | 525 | cell_document['text'], cell_document.get('version'))
|
526 | 526 | # Cell metadata which is added to Notebook
|
527 | 527 | workspace.add_notebook_cells(notebookDocument['uri'], notebook_cell_array_change['cells'], start)
|
@@ -585,7 +585,80 @@ def m_text_document__code_lens(self, textDocument=None, **_kwargs):
|
585 | 585 | def m_text_document__completion(self, textDocument=None, position=None, **_kwargs):
|
586 | 586 | return self.completions(textDocument['uri'], position)
|
587 | 587 |
|
| 588 | + def _cell_document__definition(self, cellDocument=None, position=None, **_kwargs): |
| 589 | + # First, we create a temp TextDocument to send to the hook that represents the whole notebook |
| 590 | + # contents. |
| 591 | + workspace = self._match_uri_to_workspace(cellDocument.notebook_uri) |
| 592 | + notebookDocument = workspace.get_maybe_document(cellDocument.notebook_uri) |
| 593 | + if notebookDocument is None: |
| 594 | + raise ValueError("Invalid notebook document") |
| 595 | + |
| 596 | + random_uri = str(uuid.uuid4()) |
| 597 | + # cell_list helps us map the diagnostics back to the correct cell later. |
| 598 | + cell_list: List[Dict[str, Any]] = [] |
| 599 | + |
| 600 | + offset = 0 |
| 601 | + total_source = "" |
| 602 | + for cell in notebookDocument.cells: |
| 603 | + cell_uri = cell['document'] |
| 604 | + cell_document = workspace.get_cell_document(cell_uri) |
| 605 | + |
| 606 | + num_lines = cell_document.line_count |
| 607 | + |
| 608 | + data = { |
| 609 | + 'uri': cell_uri, |
| 610 | + 'line_start': offset, |
| 611 | + 'line_end': offset + num_lines - 1, |
| 612 | + 'source': cell_document.source |
| 613 | + } |
| 614 | + |
| 615 | + if position is not None and cell_uri == cellDocument.uri: |
| 616 | + position['line'] += offset |
| 617 | + |
| 618 | + cell_list.append(data) |
| 619 | + if offset == 0: |
| 620 | + total_source = cell_document.source |
| 621 | + else: |
| 622 | + total_source += ("\n" + cell_document.source) |
| 623 | + |
| 624 | + offset += num_lines |
| 625 | + |
| 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: |
| 630 | + 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) |
| 641 | + for definition in definitions: |
| 642 | + # TODO: a better test for if a definition is the random_uri |
| 643 | + if random_uri in definition['uri']: |
| 644 | + # Find the cell the start is in |
| 645 | + for cell in cell_list: |
| 646 | + # TODO: perhaps it is more correct to check definition['range']['end']['line'] <= cell['line_end'], but |
| 647 | + # that would mess up if a definition was split over cells |
| 648 | + if cell['line_start'] <= definition['range']['start']['line'] <= cell['line_end']: |
| 649 | + definition['uri'] = cell['uri'] |
| 650 | + definition['range']['start']['line'] -= cell['line_start'] |
| 651 | + definition['range']['end']['line'] -= cell['line_start'] |
| 652 | + return definitions |
| 653 | + finally: |
| 654 | + workspace.rm_document(random_uri) |
| 655 | + |
588 | 656 | def m_text_document__definition(self, textDocument=None, position=None, **_kwargs):
|
| 657 | + # textDocument here is just a dict with a uri |
| 658 | + workspace = self._match_uri_to_workspace(textDocument['uri']) |
| 659 | + document = workspace.get_document(textDocument['uri']) |
| 660 | + if isinstance(document, Cell): |
| 661 | + return self._cell_document__definition(document, position, **_kwargs) |
589 | 662 | return self.definitions(textDocument['uri'], position)
|
590 | 663 |
|
591 | 664 | def m_text_document__document_highlight(self, textDocument=None, position=None, **_kwargs):
|
|
0 commit comments