Skip to content

Commit

Permalink
Aovid same directory send notify twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
manateelazycat committed Oct 6, 2024
1 parent d0f7a1d commit d6b54fe
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions core/lspserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,25 @@
class MultiFileHandler(FileSystemEventHandler):
def __init__(self, lsp_server):
self.lsp_server = lsp_server
self.file_handlers = {}
self.file_path_dict = {}
self.dir_path_dict = {}

def add_file(self, file_path):
self.file_handlers[os.path.abspath(file_path)] = file_path
self.file_path_dict[os.path.abspath(file_path)] = file_path

def add_dir(self, dir_path):
self.dir_path_dict[dir_path] = dir_path

def on_created(self, event):
if not event.is_directory and event.src_path in self.file_handlers:
if not event.is_directory and event.src_path in self.file_path_dict:
self.lsp_server.send_workspace_did_change_watched_files(event.src_path, 1)

def on_modified(self, event):
if not event.is_directory and event.src_path in self.file_handlers:
if not event.is_directory and event.src_path in self.file_path_dict:
self.lsp_server.send_workspace_did_change_watched_files(event.src_path, 2)

def on_deleted(self, event):
if not event.is_directory and event.src_path in self.file_handlers:
if not event.is_directory and event.src_path in self.file_path_dict:
self.lsp_server.send_workspace_did_change_watched_files(event.src_path, 3)

class LspServerSender(MessageSender):
Expand Down Expand Up @@ -277,6 +281,7 @@ def __init__(self, message_queue, project_path, server_info, server_name, enable
self.work_done_progress_title = ""

self.workspace_file_watcher = None
self.workspace_file_watch_handler = None

self.code_action_kinds = [
"quickfix",
Expand Down Expand Up @@ -849,24 +854,37 @@ def handle_recv_message(self, message: dict):

logger.debug(json.dumps(message, indent=3))

def monitor_workspace_files(self, file_paths):
if len(file_paths) > 0:
if self.workspace_file_watcher is None:
self.workspace_file_watcher = Observer()
self.workspace_file_watcher.start()
def start_workspace_watch_files(self):
if self.workspace_file_watcher is None:
self.workspace_file_watcher = Observer()
self.workspace_file_watcher.start()

if self.workspace_file_watch_handler is None:
self.workspace_file_watch_handler = MultiFileHandler(self)

multi_handler = MultiFileHandler(self)
def stop_workspace_watch_files(self):
if self.workspace_file_watcher:
self.workspace_file_watcher.unschedule_all()
self.workspace_file_watcher.stop()

watched_dirs = set()
self.workspace_file_watcher = None
self.workspace_file_watch_handler = None

def monitor_workspace_files(self, file_paths):
if len(file_paths) > 0:
# Init workspace watch files vars.
self.start_workspace_watch_files()

# Add workspace file in monitor list.
for file_path in file_paths:
multi_handler.add_file(file_path)
# Add file path in notify list.
self.workspace_file_watch_handler.add_file(file_path)

# Only monitor directory once.
target_dir = os.path.dirname(file_path)

if target_dir not in watched_dirs:
self.workspace_file_watcher.schedule(multi_handler, target_dir, recursive=False)
watched_dirs.add(target_dir)
if target_dir not in self.workspace_file_watch_handler.dir_path_dict:
self.workspace_file_watcher.schedule(self.workspace_file_watch_handler, target_dir, recursive=False)
self.workspace_file_watch_handler.add_dir(target_dir)

def parse_workspace_watch_files(self, params):
patterns = []
Expand Down Expand Up @@ -922,10 +940,7 @@ def close_file(self, filepath):
# We need shutdown LSP server when last file closed, to save system memory.
if len(self.files) == 0:
# Stop workspace file watcher.
if self.workspace_file_watcher:
self.workspace_file_watcher.unschedule_all()
self.workspace_file_watcher.stop()
self.workspace_file_watcher = None
self.stop_workspace_watch_files()

self.message_queue.put({
"name": "server_process_exit",
Expand Down

0 comments on commit d6b54fe

Please sign in to comment.