|
8 | 8 | import pathlib |
9 | 9 | import shutil |
10 | 10 | import threading |
11 | | -from pathlib import Path |
| 11 | +from collections.abc import Callable |
| 12 | +from pathlib import Path, PurePath |
12 | 13 | from time import sleep |
13 | 14 | from typing import Any |
14 | 15 |
|
@@ -425,6 +426,70 @@ def request_rename_symbol_edit(self, relative_file_path: str, line: int, column: |
425 | 426 | with self._ts_server.open_file(relative_file_path): |
426 | 427 | return self._ts_server.request_rename_symbol_edit(relative_file_path, line, column, new_name) |
427 | 428 |
|
| 429 | + def _forward_edit_to_ts_server_if_needed(self, relative_file_path: str, edit_fn: Callable[[], object]) -> None: |
| 430 | + """ |
| 431 | + Calls ``edit_fn`` on the TypeScript server if the file is open there. |
| 432 | +
|
| 433 | + Only applicable to non-TypeScript files (i.e. .vue files) that have been |
| 434 | + indexed on the TypeScript server for cross-file reference support. |
| 435 | +
|
| 436 | + :param relative_file_path: the relative path of the file that was edited |
| 437 | + :param edit_fn: callable that performs the corresponding edit on ``_ts_server`` |
| 438 | + """ |
| 439 | + if self._ts_server is None or not self._ts_server_started: |
| 440 | + return |
| 441 | + if self._is_typescript_file(relative_file_path): |
| 442 | + return |
| 443 | + |
| 444 | + absolute_file_path = str(PurePath(self.repository_root_path, relative_file_path)) |
| 445 | + uri = pathlib.Path(absolute_file_path).as_uri() |
| 446 | + if uri in self._ts_server.open_file_buffers: |
| 447 | + edit_fn() |
| 448 | + |
| 449 | + @override |
| 450 | + def insert_text_at_position(self, relative_file_path: str, line: int, column: int, text_to_be_inserted: str) -> ls_types.Position: |
| 451 | + """ |
| 452 | + Inserts text at the given position, forwarding the change to the TypeScript server if it has the file open. |
| 453 | +
|
| 454 | + :param relative_file_path: the relative path of the file to edit |
| 455 | + :param line: the line number |
| 456 | + :param column: the column number |
| 457 | + :param text_to_be_inserted: the text to insert |
| 458 | + :return: updated cursor position |
| 459 | + """ |
| 460 | + result = super().insert_text_at_position(relative_file_path, line, column, text_to_be_inserted) |
| 461 | + self._forward_edit_to_ts_server_if_needed( |
| 462 | + relative_file_path, |
| 463 | + lambda: self._ts_server.insert_text_at_position( # type: ignore[union-attr] |
| 464 | + relative_file_path, line, column, text_to_be_inserted |
| 465 | + ), |
| 466 | + ) |
| 467 | + return result |
| 468 | + |
| 469 | + @override |
| 470 | + def delete_text_between_positions( |
| 471 | + self, |
| 472 | + relative_file_path: str, |
| 473 | + start: ls_types.Position, |
| 474 | + end: ls_types.Position, |
| 475 | + ) -> str: |
| 476 | + """ |
| 477 | + Deletes text between the given positions, forwarding the change to the TypeScript server if it has the file open. |
| 478 | +
|
| 479 | + :param relative_file_path: the relative path of the file to edit |
| 480 | + :param start: start position |
| 481 | + :param end: end position |
| 482 | + :return: deleted text |
| 483 | + """ |
| 484 | + deleted_text = super().delete_text_between_positions(relative_file_path, start, end) |
| 485 | + self._forward_edit_to_ts_server_if_needed( |
| 486 | + relative_file_path, |
| 487 | + lambda: self._ts_server.delete_text_between_positions( # type: ignore[union-attr] |
| 488 | + relative_file_path, start, end |
| 489 | + ), |
| 490 | + ) |
| 491 | + return deleted_text |
| 492 | + |
428 | 493 | @classmethod |
429 | 494 | def _setup_runtime_dependencies(cls, config: LanguageServerConfig, solidlsp_settings: SolidLSPSettings) -> tuple[list[str], str, str]: |
430 | 495 | is_node_installed = shutil.which("node") is not None |
|
0 commit comments