Skip to content

Initialize LSP progress token before using it and remove progress for sync plugins #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pylsp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ def is_process_alive(pid):
os.kill(pid, 0)
except OSError as e:
return e.errno == errno.EPERM
else:
return True
return True


def get_eol_chars(text):
Expand Down
17 changes: 8 additions & 9 deletions pylsp/plugins/autopep8_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ def pylsp_format_document(config, workspace, document, options): # pylint: disa
def pylsp_format_range(
config, workspace, document, range, options
): # pylint: disable=redefined-builtin,unused-argument
with workspace.report_progress("format_range: autopep8"):
log.info("Formatting document %s in range %s with autopep8", document, range)
log.info("Formatting document %s in range %s with autopep8", document, range)

# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0
# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

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


def _format(config, document, line_range=None):
Expand Down
35 changes: 17 additions & 18 deletions pylsp/plugins/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@


@hookimpl
def pylsp_definitions(config, workspace, document, position):
with workspace.report_progress("go to definitions"):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)
def pylsp_definitions(config, document, position):
settings = config.plugin_settings('jedi_definition')
code_position = _utils.position_to_jedi_linecolumn(document, position)
definitions = document.jedi_script(use_document_path=True).goto(
follow_imports=settings.get('follow_imports', True),
follow_builtin_imports=settings.get('follow_builtin_imports', True),
**code_position)

follow_builtin_defns = settings.get("follow_builtin_definitions", True)
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
follow_builtin_defns = settings.get("follow_builtin_definitions", True)
return [
{
'uri': uris.uri_with(document.uri, path=str(d.module_path)),
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
for d in definitions if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
]
}
for d in definitions if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
]


def _not_internal_definition(definition):
Expand Down
72 changes: 35 additions & 37 deletions pylsp/plugins/jedi_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,42 @@


@hookimpl
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument,too-many-locals
with workspace.report_progress("rename", percentage=0) as report_progress:
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
report_progress("refactoring")
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for n, (file_path, changed_file) in enumerate(changed_files.items()):
report_progress(changed_file, percentage=n/len(changed_files)*100)
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
def pylsp_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument
log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name)
kwargs = _utils.position_to_jedi_linecolumn(document, position)
kwargs['new_name'] = new_name
try:
refactoring = document.jedi_script().rename(**kwargs)
except NotImplementedError as exc:
# pylint: disable=broad-exception-raised
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
'Consider using the rope_rename plugin instead') from exc
log.debug('Finished rename: %s', refactoring.get_diff())
changes = []

changed_files = refactoring.get_changed_files()
for file_path, changed_file in changed_files.items():
uri = uris.from_fs_path(str(file_path))
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(changed_file.get_new_code()),
'character': 0,
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}
},
'newText': changed_file.get_new_code(),
}
],
})
return {'documentChanges': changes}


def _num_lines(file_contents):
Expand Down
29 changes: 14 additions & 15 deletions pylsp/plugins/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@


@hookimpl
def pylsp_references(document, workspace, position, exclude_declaration=False):
with workspace.report_progress("references"):
code_position = _utils.position_to_jedi_linecolumn(document, position)
usages = document.jedi_script().get_references(**code_position)
def pylsp_references(document, position, exclude_declaration=False):
code_position = _utils.position_to_jedi_linecolumn(document, position)
usages = document.jedi_script().get_references(**code_position)

if exclude_declaration:
# Filter out if the usage is the actual declaration of the thing
usages = [d for d in usages if not d.is_definition()]
if exclude_declaration:
# Filter out if the usage is the actual declaration of the thing
usages = [d for d in usages if not d.is_definition()]

# Filter out builtin modules
return [{
'uri': uris.uri_with(document.uri, path=str(d.module_path)) if d.module_path else document.uri,
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)}
}
} for d in usages if not d.in_builtin_module()]
# Filter out builtin modules
return [{
'uri': uris.uri_with(document.uri, path=str(d.module_path)) if d.module_path else document.uri,
'range': {
'start': {'line': d.line - 1, 'character': d.column},
'end': {'line': d.line - 1, 'character': d.column + len(d.name)}
}
} for d in usages if not d.in_builtin_module()]
69 changes: 34 additions & 35 deletions pylsp/plugins/rope_rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,41 @@ def pylsp_settings():

@hookimpl
def pylsp_rename(config, workspace, document, position, new_name):
with workspace.report_progress("rename"):
rope_config = config.settings(document_path=document.path).get('rope', {})
rope_project = workspace._rope_project_builder(rope_config)

rename = Rename(
rope_project,
libutils.path_to_resource(rope_project, document.path),
document.offset_at_position(position)
)

log.debug("Executing rename of %s to %s", document.word_at_position(position), new_name)
changeset = rename.get_changes(new_name, in_hierarchy=True, docs=True)
log.debug("Finished rename: %s", changeset.changes)
changes = []
for change in changeset.changes:
uri = uris.from_fs_path(change.resource.path)
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(change.resource),
'character': 0,
},
rope_config = config.settings(document_path=document.path).get('rope', {})
rope_project = workspace._rope_project_builder(rope_config)

rename = Rename(
rope_project,
libutils.path_to_resource(rope_project, document.path),
document.offset_at_position(position)
)

log.debug("Executing rename of %s to %s", document.word_at_position(position), new_name)
changeset = rename.get_changes(new_name, in_hierarchy=True, docs=True)
log.debug("Finished rename: %s", changeset.changes)
changes = []
for change in changeset.changes:
uri = uris.from_fs_path(change.resource.path)
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(change.resource),
'character': 0,
},
'newText': change.new_contents,
}
]
})
return {'documentChanges': changes}
},
'newText': change.new_contents,
}
]
})
return {'documentChanges': changes}


def _num_lines(resource):
Expand Down
31 changes: 15 additions & 16 deletions pylsp/plugins/yapf_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@ def pylsp_format_document(workspace, document, options):


@hookimpl
def pylsp_format_range(workspace, document, range, options): # pylint: disable=redefined-builtin
def pylsp_format_range(document, range, options): # pylint: disable=redefined-builtin
log.info("Formatting document %s in range %s with yapf", document, range)
with workspace.report_progress("format_range: yapf"):
# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# From Yapf docs:
# lines: (list of tuples of integers) A list of tuples of lines, [start, end],
# that we want to format. The lines are 1-based indexed. It can be used by
# third-party code (e.g., IDEs) when reformatting a snippet of code rather
# than a whole file.

# Add 1 for 1-indexing vs LSP's 0-indexing
lines = [(range['start']['line'] + 1, range['end']['line'] + 1)]
return _format(document, lines=lines, options=options)
# First we 'round' the range up/down to full lines only
range['start']['character'] = 0
range['end']['line'] += 1
range['end']['character'] = 0

# From Yapf docs:
# lines: (list of tuples of integers) A list of tuples of lines, [start, end],
# that we want to format. The lines are 1-based indexed. It can be used by
# third-party code (e.g., IDEs) when reformatting a snippet of code rather
# than a whole file.

# Add 1 for 1-indexing vs LSP's 0-indexing
lines = [(range['start']['line'] + 1, range['end']['line'] + 1)]
return _format(document, lines=lines, options=options)


def get_style_config(document_path, options=None):
Expand Down
10 changes: 6 additions & 4 deletions pylsp/python_lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class PythonLSPServer(MethodDispatcher):

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

def __init__(self, rx, tx, check_parent_process=False, consumer=None):
def __init__(self, rx, tx, check_parent_process=False, consumer=None, *, endpoint_cls=None):
self.workspace = None
self.config = None
self.root_uri = None
Expand All @@ -172,11 +172,13 @@ def __init__(self, rx, tx, check_parent_process=False, consumer=None):
else:
self._jsonrpc_stream_writer = None

endpoint_cls = endpoint_cls or Endpoint

# if consumer is None, it is assumed that the default streams-based approach is being used
if consumer is None:
self._endpoint = Endpoint(self, self._jsonrpc_stream_writer.write, max_workers=MAX_WORKERS)
self._endpoint = endpoint_cls(self, self._jsonrpc_stream_writer.write, max_workers=MAX_WORKERS)
else:
self._endpoint = Endpoint(self, consumer, max_workers=MAX_WORKERS)
self._endpoint = endpoint_cls(self, consumer, max_workers=MAX_WORKERS)

self._dispatchers = []
self._shutdown = False
Expand Down Expand Up @@ -358,7 +360,7 @@ def execute_command(self, command, arguments):
return self._hook('pylsp_execute_command', command=command, arguments=arguments)

def format_document(self, doc_uri, options):
return self._hook('pylsp_format_document', doc_uri, options=options)
return lambda: self._hook('pylsp_format_document', doc_uri, options=options)

def format_range(self, doc_uri, range, options):
return self._hook('pylsp_format_range', doc_uri, range=range, options=options)
Expand Down
Loading