server: implement references

This commit is contained in:
dece 2022-12-03 19:41:32 +01:00
parent 0ff5453c99
commit c7b38e7998
2 changed files with 18 additions and 4 deletions

View file

@ -12,10 +12,11 @@ Supported features:
| LSP method | Description | | LSP method | Description |
|-------------------------------|-----------------------| |-------------------------------|-----------------------|
| `textDocument/completion` | Completions | | `textDocument/completion` | Complete |
| `textDocument/definition` | Go to definition | | `textDocument/definition` | Go to definition |
| `textDocument/typeDefinition` | Go to type definition | | `textDocument/typeDefinition` | Go to type definition |
| `textDocument/hover` | Documentation | | `textDocument/hover` | Show documentation |
| `textDocument/references` | Show references |

View file

@ -3,12 +3,13 @@ from typing import Optional
from jedi import Script from jedi import Script
from jedi.api.classes import Name from jedi.api.classes import Name
from pygls.lsp.methods import COMPLETION, DEFINITION, HOVER, TYPE_DEFINITION from pygls.lsp.methods import (COMPLETION, DEFINITION, HOVER, REFERENCES,
TYPE_DEFINITION)
from pygls.lsp.types import (CompletionItem, CompletionItemKind, from pygls.lsp.types import (CompletionItem, CompletionItemKind,
CompletionList, CompletionOptions, CompletionList, CompletionOptions,
CompletionParams, DefinitionParams, Hover, CompletionParams, DefinitionParams, Hover,
HoverParams, InsertTextFormat, HoverParams, InsertTextFormat,
Location, Position, Range, Location, Position, Range, ReferenceParams,
TextDocumentPositionParams, TypeDefinitionParams) TextDocumentPositionParams, TypeDefinitionParams)
from pygls.server import LanguageServer from pygls.server import LanguageServer
from pygls.workspace import Document from pygls.workspace import Document
@ -153,6 +154,18 @@ async def do_type_definition(
return get_lsp_locations(jedi_names) or None return get_lsp_locations(jedi_names) or None
@LS.feature(REFERENCES)
async def do_references(
server: LanguageServer,
params: ReferenceParams,
) -> Optional[list[Location]]:
"""Return the type definition location(s) of the target symbol."""
script = get_jedi_script_from_params(params, server)
jedi_position = get_jedi_position(params.position)
jedi_names = script.get_references(*jedi_position)
return get_lsp_locations(jedi_names) or None
@LS.feature(HOVER) @LS.feature(HOVER)
async def do_hover( async def do_hover(
server: LanguageServer, server: LanguageServer,