Skip to content

Commit 714a582

Browse files
authored
Initialize LSP progress token before using it and remove progress for sync plugins (#328)
1 parent e341198 commit 714a582

14 files changed

+279
-167
lines changed

Diff for: pylsp/_utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ def is_process_alive(pid):
293293
os.kill(pid, 0)
294294
except OSError as e:
295295
return e.errno == errno.EPERM
296-
else:
297-
return True
296+
return True
298297

299298

300299
def get_eol_chars(text):

Diff for: pylsp/plugins/autopep8_format.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ def pylsp_format_document(config, workspace, document, options): # pylint: disa
2323
def pylsp_format_range(
2424
config, workspace, document, range, options
2525
): # pylint: disable=redefined-builtin,unused-argument
26-
with workspace.report_progress("format_range: autopep8"):
27-
log.info("Formatting document %s in range %s with autopep8", document, range)
26+
log.info("Formatting document %s in range %s with autopep8", document, range)
2827

29-
# First we 'round' the range up/down to full lines only
30-
range['start']['character'] = 0
31-
range['end']['line'] += 1
32-
range['end']['character'] = 0
28+
# First we 'round' the range up/down to full lines only
29+
range['start']['character'] = 0
30+
range['end']['line'] += 1
31+
range['end']['character'] = 0
3332

34-
# Add 1 for 1-indexing vs LSP's 0-indexing
35-
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
36-
return _format(config, document, line_range=line_range)
33+
# Add 1 for 1-indexing vs LSP's 0-indexing
34+
line_range = (range['start']['line'] + 1, range['end']['line'] + 1)
35+
return _format(config, document, line_range=line_range)
3736

3837

3938
def _format(config, document, line_range=None):

Diff for: pylsp/plugins/definition.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,25 @@
88

99

1010
@hookimpl
11-
def pylsp_definitions(config, workspace, document, position):
12-
with workspace.report_progress("go to definitions"):
13-
settings = config.plugin_settings('jedi_definition')
14-
code_position = _utils.position_to_jedi_linecolumn(document, position)
15-
definitions = document.jedi_script(use_document_path=True).goto(
16-
follow_imports=settings.get('follow_imports', True),
17-
follow_builtin_imports=settings.get('follow_builtin_imports', True),
18-
**code_position)
11+
def pylsp_definitions(config, document, position):
12+
settings = config.plugin_settings('jedi_definition')
13+
code_position = _utils.position_to_jedi_linecolumn(document, position)
14+
definitions = document.jedi_script(use_document_path=True).goto(
15+
follow_imports=settings.get('follow_imports', True),
16+
follow_builtin_imports=settings.get('follow_builtin_imports', True),
17+
**code_position)
1918

20-
follow_builtin_defns = settings.get("follow_builtin_definitions", True)
21-
return [
22-
{
23-
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
24-
'range': {
25-
'start': {'line': d.line - 1, 'character': d.column},
26-
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
27-
}
19+
follow_builtin_defns = settings.get("follow_builtin_definitions", True)
20+
return [
21+
{
22+
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
23+
'range': {
24+
'start': {'line': d.line - 1, 'character': d.column},
25+
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
2826
}
29-
for d in definitions if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
30-
]
27+
}
28+
for d in definitions if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
29+
]
3130

3231

3332
def _not_internal_definition(definition):

Diff for: pylsp/plugins/jedi_rename.py

+35-37
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,42 @@
99

1010

1111
@hookimpl
12-
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
13-
with workspace.report_progress("rename", percentage=0) as report_progress:
14-
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
15-
kwargs = _utils.position_to_jedi_linecolumn(document, position)
16-
kwargs['new_name'] = new_name
17-
report_progress("refactoring")
18-
try:
19-
refactoring = document.jedi_script().rename(**kwargs)
20-
except NotImplementedError as exc:
21-
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
22-
'Consider using the rope_rename plugin instead') from exc
23-
log.debug('Finished rename: %s', refactoring.get_diff())
24-
changes = []
25-
26-
changed_files = refactoring.get_changed_files()
27-
for n, (file_path, changed_file) in enumerate(changed_files.items()):
28-
report_progress(changed_file, percentage=n/len(changed_files)*100)
29-
uri = uris.from_fs_path(str(file_path))
30-
doc = workspace.get_maybe_document(uri)
31-
changes.append({
32-
'textDocument': {
33-
'uri': uri,
34-
'version': doc.version if doc else None
35-
},
36-
'edits': [
37-
{
38-
'range': {
39-
'start': {'line': 0, 'character': 0},
40-
'end': {
41-
'line': _num_lines(changed_file.get_new_code()),
42-
'character': 0,
43-
},
12+
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
13+
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
14+
kwargs = _utils.position_to_jedi_linecolumn(document, position)
15+
kwargs['new_name'] = new_name
16+
try:
17+
refactoring = document.jedi_script().rename(**kwargs)
18+
except NotImplementedError as exc:
19+
# pylint: disable=broad-exception-raised
20+
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
21+
'Consider using the rope_rename plugin instead') from exc
22+
log.debug('Finished rename: %s', refactoring.get_diff())
23+
changes = []
24+
25+
changed_files = refactoring.get_changed_files()
26+
for file_path, changed_file in changed_files.items():
27+
uri = uris.from_fs_path(str(file_path))
28+
doc = workspace.get_maybe_document(uri)
29+
changes.append({
30+
'textDocument': {
31+
'uri': uri,
32+
'version': doc.version if doc else None
33+
},
34+
'edits': [
35+
{
36+
'range': {
37+
'start': {'line': 0, 'character': 0},
38+
'end': {
39+
'line': _num_lines(changed_file.get_new_code()),
40+
'character': 0,
4441
},
45-
'newText': changed_file.get_new_code(),
46-
}
47-
],
48-
})
49-
return {'documentChanges': changes}
42+
},
43+
'newText': changed_file.get_new_code(),
44+
}
45+
],
46+
})
47+
return {'documentChanges': changes}
5048

5149

5250
def _num_lines(file_contents):

Diff for: pylsp/plugins/references.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@
88

99

1010
@hookimpl
11-
def pylsp_references(document, workspace, position, exclude_declaration=False):
12-
with workspace.report_progress("references"):
13-
code_position = _utils.position_to_jedi_linecolumn(document, position)
14-
usages = document.jedi_script().get_references(**code_position)
11+
def pylsp_references(document, position, exclude_declaration=False):
12+
code_position = _utils.position_to_jedi_linecolumn(document, position)
13+
usages = document.jedi_script().get_references(**code_position)
1514

16-
if exclude_declaration:
17-
# Filter out if the usage is the actual declaration of the thing
18-
usages = [d for d in usages if not d.is_definition()]
15+
if exclude_declaration:
16+
# Filter out if the usage is the actual declaration of the thing
17+
usages = [d for d in usages if not d.is_definition()]
1918

20-
# Filter out builtin modules
21-
return [{
22-
'uri': uris.uri_with(document.uri, path=str(d.module_path)) if d.module_path else document.uri,
23-
'range': {
24-
'start': {'line': d.line - 1, 'character': d.column},
25-
'end': {'line': d.line - 1, 'character': d.column + len(d.name)}
26-
}
27-
} for d in usages if not d.in_builtin_module()]
19+
# Filter out builtin modules
20+
return [{
21+
'uri': uris.uri_with(document.uri, path=str(d.module_path)) if d.module_path else document.uri,
22+
'range': {
23+
'start': {'line': d.line - 1, 'character': d.column},
24+
'end': {'line': d.line - 1, 'character': d.column + len(d.name)}
25+
}
26+
} for d in usages if not d.in_builtin_module()]

Diff for: pylsp/plugins/rope_rename.py

+34-35
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,41 @@ def pylsp_settings():
1919

2020
@hookimpl
2121
def pylsp_rename(config, workspace, document, position, new_name):
22-
with workspace.report_progress("rename"):
23-
rope_config = config.settings(document_path=document.path).get('rope', {})
24-
rope_project = workspace._rope_project_builder(rope_config)
25-
26-
rename = Rename(
27-
rope_project,
28-
libutils.path_to_resource(rope_project, document.path),
29-
document.offset_at_position(position)
30-
)
31-
32-
log.debug("Executing rename of %s to %s", document.word_at_position(position), new_name)
33-
changeset = rename.get_changes(new_name, in_hierarchy=True, docs=True)
34-
log.debug("Finished rename: %s", changeset.changes)
35-
changes = []
36-
for change in changeset.changes:
37-
uri = uris.from_fs_path(change.resource.path)
38-
doc = workspace.get_maybe_document(uri)
39-
changes.append({
40-
'textDocument': {
41-
'uri': uri,
42-
'version': doc.version if doc else None
43-
},
44-
'edits': [
45-
{
46-
'range': {
47-
'start': {'line': 0, 'character': 0},
48-
'end': {
49-
'line': _num_lines(change.resource),
50-
'character': 0,
51-
},
22+
rope_config = config.settings(document_path=document.path).get('rope', {})
23+
rope_project = workspace._rope_project_builder(rope_config)
24+
25+
rename = Rename(
26+
rope_project,
27+
libutils.path_to_resource(rope_project, document.path),
28+
document.offset_at_position(position)
29+
)
30+
31+
log.debug("Executing rename of %s to %s", document.word_at_position(position), new_name)
32+
changeset = rename.get_changes(new_name, in_hierarchy=True, docs=True)
33+
log.debug("Finished rename: %s", changeset.changes)
34+
changes = []
35+
for change in changeset.changes:
36+
uri = uris.from_fs_path(change.resource.path)
37+
doc = workspace.get_maybe_document(uri)
38+
changes.append({
39+
'textDocument': {
40+
'uri': uri,
41+
'version': doc.version if doc else None
42+
},
43+
'edits': [
44+
{
45+
'range': {
46+
'start': {'line': 0, 'character': 0},
47+
'end': {
48+
'line': _num_lines(change.resource),
49+
'character': 0,
5250
},
53-
'newText': change.new_contents,
54-
}
55-
]
56-
})
57-
return {'documentChanges': changes}
51+
},
52+
'newText': change.new_contents,
53+
}
54+
]
55+
})
56+
return {'documentChanges': changes}
5857

5958

6059
def _num_lines(resource):

Diff for: pylsp/plugins/yapf_format.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,22 @@ def pylsp_format_document(workspace, document, options):
2323

2424

2525
@hookimpl
26-
def pylsp_format_range(workspace, document, range, options): # pylint: disable=redefined-builtin
26+
def pylsp_format_range(document, range, options): # pylint: disable=redefined-builtin
2727
log.info("Formatting document %s in range %s with yapf", document, range)
28-
with workspace.report_progress("format_range: yapf"):
29-
# First we 'round' the range up/down to full lines only
30-
range['start']['character'] = 0
31-
range['end']['line'] += 1
32-
range['end']['character'] = 0
33-
34-
# From Yapf docs:
35-
# lines: (list of tuples of integers) A list of tuples of lines, [start, end],
36-
# that we want to format. The lines are 1-based indexed. It can be used by
37-
# third-party code (e.g., IDEs) when reformatting a snippet of code rather
38-
# than a whole file.
39-
40-
# Add 1 for 1-indexing vs LSP's 0-indexing
41-
lines = [(range['start']['line'] + 1, range['end']['line'] + 1)]
42-
return _format(document, lines=lines, options=options)
28+
# First we 'round' the range up/down to full lines only
29+
range['start']['character'] = 0
30+
range['end']['line'] += 1
31+
range['end']['character'] = 0
32+
33+
# From Yapf docs:
34+
# lines: (list of tuples of integers) A list of tuples of lines, [start, end],
35+
# that we want to format. The lines are 1-based indexed. It can be used by
36+
# third-party code (e.g., IDEs) when reformatting a snippet of code rather
37+
# than a whole file.
38+
39+
# Add 1 for 1-indexing vs LSP's 0-indexing
40+
lines = [(range['start']['line'] + 1, range['end']['line'] + 1)]
41+
return _format(document, lines=lines, options=options)
4342

4443

4544
def get_style_config(document_path, options=None):

Diff for: pylsp/python_lsp.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class PythonLSPServer(MethodDispatcher):
152152

153153
# pylint: disable=too-many-public-methods,redefined-builtin
154154

155-
def __init__(self, rx, tx, check_parent_process=False, consumer=None):
155+
def __init__(self, rx, tx, check_parent_process=False, consumer=None, *, endpoint_cls=None):
156156
self.workspace = None
157157
self.config = None
158158
self.root_uri = None
@@ -172,11 +172,13 @@ def __init__(self, rx, tx, check_parent_process=False, consumer=None):
172172
else:
173173
self._jsonrpc_stream_writer = None
174174

175+
endpoint_cls = endpoint_cls or Endpoint
176+
175177
# if consumer is None, it is assumed that the default streams-based approach is being used
176178
if consumer is None:
177-
self._endpoint = Endpoint(self, self._jsonrpc_stream_writer.write, max_workers=MAX_WORKERS)
179+
self._endpoint = endpoint_cls(self, self._jsonrpc_stream_writer.write, max_workers=MAX_WORKERS)
178180
else:
179-
self._endpoint = Endpoint(self, consumer, max_workers=MAX_WORKERS)
181+
self._endpoint = endpoint_cls(self, consumer, max_workers=MAX_WORKERS)
180182

181183
self._dispatchers = []
182184
self._shutdown = False
@@ -358,7 +360,7 @@ def execute_command(self, command, arguments):
358360
return self._hook('pylsp_execute_command', command=command, arguments=arguments)
359361

360362
def format_document(self, doc_uri, options):
361-
return self._hook('pylsp_format_document', doc_uri, options=options)
363+
return lambda: self._hook('pylsp_format_document', doc_uri, options=options)
362364

363365
def format_range(self, doc_uri, range, options):
364366
return self._hook('pylsp_format_range', doc_uri, range=range, options=options)

0 commit comments

Comments
 (0)