Skip to content

Commit

Permalink
Improve watching
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Jun 6, 2024
1 parent ea17565 commit 03eade4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
20 changes: 18 additions & 2 deletions coconut/command/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import sys

from functools import partial

from coconut.terminal import logger
from coconut.exceptions import CoconutException

Expand Down Expand Up @@ -48,13 +50,27 @@ def __init__(self, recompile, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.saw = set()
self.saw_twice = set()

def on_modified(self, event):
"""Handle a file modified event."""
path = event.src_path
self.handle(event.src_path)

def handle(self, path):
"""Handle a potential recompilation event for the given path."""
if path in self.saw:
logger.log("Skipping watch event for: " + repr(path) + "\n\t(currently compiling: " + repr(self.saw) + ")")
self.saw_twice.add(path)
else:
logger.log("Handling watch event for: " + repr(path) + "\n\t(currently compiling: " + repr(self.saw) + ")")
self.saw.add(path)
self.recompile(path, callback=lambda: self.saw.discard(path), *self.args, **self.kwargs)
self.saw_twice.discard(path)
self.recompile(path, callback=partial(self.callback, path), *self.args, **self.kwargs)

def callback(self, path):
"""Callback for after recompiling the given path."""
self.saw.discard(path)
if path in self.saw_twice:
logger.log("Submitting deferred watch event for: " + repr(path) + "\n\t(currently deferred: " + repr(self.saw_twice) + ")")
self.saw_twice.discard(path)
self.handle(path)
2 changes: 1 addition & 1 deletion coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5209,7 +5209,7 @@ def warm_up(self, streamline=False, enable_incremental_mode=False, set_debug_nam
self.streamline(self.file_parser, force=streamline)
self.streamline(self.eval_parser, force=streamline)
if enable_incremental_mode:
enable_incremental_parsing()
enable_incremental_parsing(reason="explicit warm_up call")


# end: ENDPOINTS
Expand Down
6 changes: 3 additions & 3 deletions coconut/compiler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ def get_highest_parse_loc(original):
return highest_loc


def enable_incremental_parsing():
def enable_incremental_parsing(reason="explicit enable_incremental_parsing call"):
"""Enable incremental parsing mode where prefix/suffix parses are reused."""
if not SUPPORTS_INCREMENTAL:
return False
Expand All @@ -1051,7 +1051,7 @@ def enable_incremental_parsing():
)
except ImportError as err:
raise CoconutException(str(err))
logger.log("Incremental parsing mode enabled.")
logger.log("Incremental parsing mode enabled due to {reason}.".format(reason=reason))
return True


Expand Down Expand Up @@ -1199,7 +1199,7 @@ def load_cache_for(inputstring, codepath):
incremental_enabled = True
incremental_info = "using incremental parsing mode since it was already enabled"
elif len(inputstring) < disable_incremental_for_len:
incremental_enabled = enable_incremental_parsing()
incremental_enabled = enable_incremental_parsing(reason="input length")
if incremental_enabled:
incremental_info = "incremental parsing mode enabled due to len == {input_len} < {max_len}".format(
input_len=len(inputstring),
Expand Down

0 comments on commit 03eade4

Please sign in to comment.