Skip to content

Commit

Permalink
Add support for including function objects (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
llan-ml committed Aug 27, 2022
1 parent 98dc9c0 commit fea0e71
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This server can be configured using `workspace/didChangeConfiguration` method. E
| `pylsp.plugins.jedi_completion.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_completion.include_params` | `boolean` | Auto-completes methods and classes with tabstops for each parameter. | `true` |
| `pylsp.plugins.jedi_completion.include_class_objects` | `boolean` | Adds class objects as a separate completion item. | `true` |
| `pylsp.plugins.jedi_completion.include_function_objects` | `boolean` | Adds function objects as a separate completion item. | `true` |
| `pylsp.plugins.jedi_completion.fuzzy` | `boolean` | Enable fuzzy when requesting autocomplete. | `false` |
| `pylsp.plugins.jedi_completion.eager` | `boolean` | Resolve documentation and detail eagerly. | `false` |
| `pylsp.plugins.jedi_completion.resolve_at_most` | `number` | How many labels and snippets (at most) should be resolved? | `25` |
Expand Down
5 changes: 5 additions & 0 deletions pylsp/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
"default": true,
"description": "Adds class objects as a separate completion item."
},
"pylsp.plugins.jedi_completion.include_function_objects": {
"type": "boolean",
"default": true,
"description": "Adds function objects as a separate completion item."
},
"pylsp.plugins.jedi_completion.fuzzy": {
"type": "boolean",
"default": false,
Expand Down
15 changes: 15 additions & 0 deletions pylsp/plugins/jedi_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def pylsp_completions(config, document, position):

should_include_params = settings.get('include_params')
should_include_class_objects = settings.get('include_class_objects', True)
should_include_function_objects = settings.get('include_function_objects', True)

max_to_resolve = settings.get('resolve_at_most', 25)
modules_to_cache_for = settings.get('cache_for', None)
Expand All @@ -63,6 +64,7 @@ def pylsp_completions(config, document, position):

include_params = snippet_support and should_include_params and use_snippets(document, position)
include_class_objects = snippet_support and should_include_class_objects and use_snippets(document, position)
include_function_objects = snippet_support and should_include_function_objects and use_snippets(document, position)

ready_completions = [
_format_completion(
Expand All @@ -88,6 +90,19 @@ def pylsp_completions(config, document, position):
completion_dict['label'] += ' object'
ready_completions.append(completion_dict)

if include_function_objects:
for i, c in enumerate(completions):
if c.type == 'function':
completion_dict = _format_completion(
c,
False,
resolve=resolve_eagerly,
resolve_label_or_snippet=(i < max_to_resolve)
)
completion_dict['kind'] = lsp.CompletionItemKind.TypeParameter
completion_dict['label'] += ' object'
ready_completions.append(completion_dict)

for completion_dict in ready_completions:
completion_dict['data'] = {
'doc_uri': document.uri
Expand Down
20 changes: 20 additions & 0 deletions test/plugins/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,26 @@ def test_completion_with_class_objects(config, workspace):
assert completions[1]['kind'] == lsp.CompletionItemKind.TypeParameter


def test_completion_with_function_objects(config, workspace):
doc_text = 'def foobar(): pass\nfoob'
com_position = {'line': 1, 'character': 4}
doc = Document(DOC_URI, workspace, doc_text)
config.capabilities['textDocument'] = {
'completion': {'completionItem': {'snippetSupport': True}}}
config.update({'plugins': {'jedi_completion': {
'include_params': True,
'include_function_objects': True,
}}})
completions = pylsp_jedi_completions(config, doc, com_position)
assert len(completions) == 2

assert completions[0]['label'] == 'foobar()'
assert completions[0]['kind'] == lsp.CompletionItemKind.Function

assert completions[1]['label'] == 'foobar() object'
assert completions[1]['kind'] == lsp.CompletionItemKind.TypeParameter


def test_snippet_parsing(config, workspace):
doc = 'divmod'
completion_position = {'line': 0, 'character': 6}
Expand Down

0 comments on commit fea0e71

Please sign in to comment.