From 6b81f256f3baa46ec9ceaff48d2d0e8ee8ead0c9 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Mon, 5 Jul 2021 20:04:44 +0200 Subject: [PATCH 1/3] Even more fixes for matching up buffers with URIs If the paths compare equal as strings, then we consider this a match, *even* if the candidate file does not actually exist on-disk. This catches a case where you delete a file on the file system but you have a view of it open in Sublime Text. There is no callback in the API for that. And even so, view.file_name() will still return the (now non-existing) file name. --- plugin/core/sessions.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 3809f4217..998a324e1 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -933,7 +933,14 @@ def compare_by_samefile(sb: Optional[SessionBufferProtocol]) -> bool: if not isinstance(candidate, str): return False candidate_scheme, candidate_path = parse_uri(candidate) - return candidate_scheme == "file" and os.path.samefile(parsed, candidate_path) + if candidate_scheme != "file": + return False + if parsed == candidate_path: + return True + try: + return os.path.samefile(parsed, candidate_path) + except FileNotFoundError: + return False predicate = compare_by_samefile else: From 99cb18cbea4c3061fc588e733c7099652653e861 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Mon, 5 Jul 2021 21:15:42 +0200 Subject: [PATCH 2/3] rename --- plugin/core/sessions.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index feb5d8869..292a6772f 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -926,9 +926,9 @@ def session_buffers_async(self) -> Generator[SessionBufferProtocol, None, None]: yield from self._session_buffers def get_session_buffer_for_uri_async(self, uri: DocumentUri) -> Optional[SessionBufferProtocol]: - scheme, parsed = parse_uri(uri) + scheme, path = parse_uri(uri) if scheme == "file": - if not os.path.exists(parsed): + if not os.path.exists(path): return None def compare_by_samefile(sb: Optional[SessionBufferProtocol]) -> bool: @@ -940,10 +940,10 @@ def compare_by_samefile(sb: Optional[SessionBufferProtocol]) -> bool: candidate_scheme, candidate_path = parse_uri(candidate) if candidate_scheme != "file": return False - if parsed == candidate_path: + if path == candidate_path: return True try: - return os.path.samefile(parsed, candidate_path) + return os.path.samefile(path, candidate_path) except FileNotFoundError: return False @@ -951,7 +951,7 @@ def compare_by_samefile(sb: Optional[SessionBufferProtocol]) -> bool: else: def compare_by_string(sb: Optional[SessionBufferProtocol]) -> bool: - return sb.get_uri() == parsed if sb else False + return sb.get_uri() == path if sb else False predicate = compare_by_string return next(filter(predicate, self.session_buffers_async()), None) From 651d26f51581854163c51ec30b58090f514c9246 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Mon, 5 Jul 2021 21:18:21 +0200 Subject: [PATCH 3/3] checking if the target path exists is no longer needed --- plugin/core/sessions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugin/core/sessions.py b/plugin/core/sessions.py index 292a6772f..579dedc26 100644 --- a/plugin/core/sessions.py +++ b/plugin/core/sessions.py @@ -928,8 +928,6 @@ def session_buffers_async(self) -> Generator[SessionBufferProtocol, None, None]: def get_session_buffer_for_uri_async(self, uri: DocumentUri) -> Optional[SessionBufferProtocol]: scheme, path = parse_uri(uri) if scheme == "file": - if not os.path.exists(path): - return None def compare_by_samefile(sb: Optional[SessionBufferProtocol]) -> bool: if not sb: