Skip to content

Commit ddbbb75

Browse files
authored
Debounce lint calls (palantir#107)
1 parent 0a5a8de commit ddbbb75

4 files changed

Lines changed: 48 additions & 2 deletions

File tree

pyls/_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2017 Palantir Technologies, Inc.
2+
import functools
3+
import threading
4+
5+
6+
def debounce(interval_s):
7+
"""Debounce calls to this function until interval_s seconds have passed."""
8+
def wrapper(func):
9+
@functools.wraps(func)
10+
def debounced(*args, **kwargs):
11+
if hasattr(debounced, '_timer'):
12+
debounced._timer.cancel()
13+
debounced._timer = threading.Timer(interval_s, func, args, kwargs)
14+
debounced._timer.start()
15+
return debounced
16+
return wrapper

pyls/python_ls.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
# Copyright 2017 Palantir Technologies, Inc.
12
import logging
2-
from . import config, lsp, plugins
3+
from . import config, lsp, plugins, _utils
34
from .language_server import LanguageServer
45
from .workspace import Workspace
56

67
log = logging.getLogger(__name__)
78

9+
LINT_DEBOUNCE_S = 0.5 # 500 ms
10+
811

912
class PythonLanguageServer(LanguageServer):
1013

@@ -87,6 +90,7 @@ def format_range(self, doc_uri, range):
8790
def hover(self, doc_uri, position):
8891
return self._hook(self._hooks.pyls_hover, doc_uri, position=position) or {'contents': ''}
8992

93+
@_utils.debounce(LINT_DEBOUNCE_S)
9094
def lint(self, doc_uri):
9195
self.workspace.publish_diagnostics(doc_uri, flatten(self._hook(
9296
self._hooks.pyls_lint, doc_uri

test/test_language_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_linting(client_server):
8585
assert 'capabilities' in response['result']
8686

8787
# didOpen
88-
client.call('textDocument/didOpen', {
88+
client.notify('textDocument/didOpen', {
8989
'textDocument': {'uri': 'file:///test', 'text': 'import sys'}
9090
})
9191
response = _get_notification(client)

test/test_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2017 Palantir Technologies, Inc.
2+
import time
3+
from pyls import _utils
4+
5+
6+
def test_debounce():
7+
interval = 0.1
8+
9+
@_utils.debounce(0.1)
10+
def call_m():
11+
call_m._count += 1
12+
13+
call_m._count = 0
14+
15+
call_m()
16+
call_m()
17+
call_m()
18+
assert call_m._count == 0
19+
20+
time.sleep(interval * 2)
21+
call_m()
22+
assert call_m._count == 1
23+
24+
time.sleep(interval * 2)
25+
call_m()
26+
assert call_m._count == 2

0 commit comments

Comments
 (0)