From f19aaecef9b70071c73b7d2f491396722a444fc1 Mon Sep 17 00:00:00 2001 From: z80 Date: Fri, 27 Oct 2023 14:17:44 -0400 Subject: [PATCH] add debounce --- vyper_lsp/debounce.py | 19 +++++++++++++++++++ vyper_lsp/main.py | 5 +++++ 2 files changed, 24 insertions(+) create mode 100644 vyper_lsp/debounce.py diff --git a/vyper_lsp/debounce.py b/vyper_lsp/debounce.py new file mode 100644 index 0000000..96ded20 --- /dev/null +++ b/vyper_lsp/debounce.py @@ -0,0 +1,19 @@ +import threading + + +class Debouncer: + def __init__(self, wait): + self.wait = wait + self.timer = None + self.lock = threading.Lock() + + def debounce(self, func): + def debounced(*args, **kwargs): + with self.lock: + if self.timer is not None: + self.timer.cancel() # Cancel the existing timer if there is one + # Create a new timer that will call func with the latest arguments + self.timer = threading.Timer(self.wait, lambda: func(*args, **kwargs)) + self.timer.start() + + return debounced diff --git a/vyper_lsp/main.py b/vyper_lsp/main.py index ae83a51..c6b5ca2 100755 --- a/vyper_lsp/main.py +++ b/vyper_lsp/main.py @@ -29,10 +29,12 @@ from pygls.server import LanguageServer from vyper_lsp.analyzer.AstAnalyzer import AstAnalyzer from vyper_lsp.analyzer.SourceAnalyzer import SourceAnalyzer +from vyper_lsp.debounce import Debouncer from vyper_lsp.navigation import ASTNavigator from vyper_lsp.utils import get_installed_vyper_version + from .ast import AST server = LanguageServer("vyper", "v0.0.1") @@ -48,6 +50,8 @@ ast = AST() +debouncer = Debouncer(wait=0.5) + def check_minimum_vyper_version(): vy_version = get_installed_vyper_version() @@ -58,6 +62,7 @@ def check_minimum_vyper_version(): ) +@debouncer.debounce def validate_doc(ls, params): text_doc = ls.workspace.get_document(params.text_document.uri) source_diagnostics = source_analyzer.get_diagnostics(text_doc)