Skip to content

Expose setting to follow builtin and extension definitions to stub files #321

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 3 commits into from
Dec 28, 2022
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
1 change: 1 addition & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This server can be configured using the `workspace/didChangeConfiguration` metho
| `pylsp.plugins.jedi_definition.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_definition.follow_imports` | `boolean` | The goto call will follow imports. | `true` |
| `pylsp.plugins.jedi_definition.follow_builtin_imports` | `boolean` | If follow_imports is True will decide if it follow builtin imports. | `true` |
| `pylsp.plugins.jedi_definition.follow_builtin_definitions` | `boolean` | Follow builtin and extension definitions to stubs. | `true` |
| `pylsp.plugins.jedi_hover.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_references.enabled` | `boolean` | Enable or disable the plugin. | `true` |
| `pylsp.plugins.jedi_signature_help.enabled` | `boolean` | Enable or disable the plugin. | `true` |
Expand Down
5 changes: 5 additions & 0 deletions pylsp/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@
"default": true,
"description": "If follow_imports is True will decide if it follow builtin imports."
},
"pylsp.plugins.jedi_definition.follow_builtin_definitions": {
"type": "boolean",
"default": true,
"description": "Follow builtin and extension definitions to stubs."
},
"pylsp.plugins.jedi_hover.enabled": {
"type": "boolean",
"default": true,
Expand Down
3 changes: 2 additions & 1 deletion pylsp/plugins/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def pylsp_definitions(config, workspace, document, position):
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)),
Expand All @@ -25,7 +26,7 @@ def pylsp_definitions(config, workspace, document, position):
'end': {'line': d.line - 1, 'character': d.column + len(d.name)},
}
}
for d in definitions if d.is_definition() and _not_internal_definition(d)
for d in definitions if d.is_definition() and (follow_builtin_defns or _not_internal_definition(d))
]


Expand Down
19 changes: 17 additions & 2 deletions test/plugins/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,24 @@ def test_builtin_definition(config, workspace):
# Over 'i' in dict
cursor_pos = {'line': 8, 'character': 24}

# No go-to def for builtins
doc = Document(DOC_URI, workspace, DOC)
assert not pylsp_definitions(config, workspace, doc, cursor_pos)
orig_settings = config.settings()

# Check definition for `dict` goes to `builtins.pyi::dict`
follow_defns_setting = {'follow_builtin_definitions': True}
settings = {'plugins': {'jedi_definition': follow_defns_setting}}
config.update(settings)
defns = pylsp_definitions(config, workspace, doc, cursor_pos)
assert len(defns) == 1
assert defns[0]["uri"].endswith("builtins.pyi")

# Check no definitions for `dict`
follow_defns_setting['follow_builtin_definitions'] = False
config.update(settings)
defns = pylsp_definitions(config, workspace, doc, cursor_pos)
assert not defns

config.update(orig_settings)


def test_assignment(config, workspace):
Expand Down