Beispiel #1
0
def _completion_item(completion: Completion, r: types.Range) -> Dict:
    label = completion.name
    if not completion.complete.startswith("'") and label.startswith("'"):
        label = label[1:]
    return dict(label=label,
                kind=_COMPLETION_TYPES.get(completion.type,
                                           types.CompletionItemKind.Text),
                documentation=completion.docstring(raw=True),
                sort_text=_completion_sort_key(completion),
                text_edit=types.TextEdit(r, completion.complete))
Beispiel #2
0
def _completion_item(completion: Completion, r: types.Range) -> Dict:
    label = completion.name
    _r = r
    lnm = completion._like_name_length
    if lnm == 1 and label[0] in {'"', "'"}:
        lnm = 0
        label = label[1:]
    elif lnm:
        _r = types.Range(start=types.Position(line=r.start.line,
                                              character=r.start.character -
                                              lnm),
                         end=r.end)
    return dict(label=label,
                kind=_COMPLETION_TYPES.get(completion.type,
                                           types.CompletionItemKind.Text),
                documentation=completion.docstring(raw=True),
                text_edit=types.TextEdit(range=_r, new_text=label))
Beispiel #3
0
def lsp_completion_item(
    name: Completion,
    char_before_cursor: str,
    enable_snippets: bool,
    markup_kind: MarkupKind,
) -> CompletionItem:
    """Using a Jedi completion, obtain a jedi completion item."""
    name_name = name.name
    name_clean = clean_completion_name(name_name, char_before_cursor)
    lsp_type = get_lsp_completion_type(name.type)
    completion_item = CompletionItem(
        label=name_name,
        filter_text=name_name,
        kind=lsp_type,
        detail=name.description,
        documentation=MarkupContent(kind=markup_kind, value=name.docstring()),
        sort_text=complete_sort_name(name),
        insert_text=name_clean,
        insert_text_format=InsertTextFormat.PlainText,
    )
    if not enable_snippets:
        return completion_item
    if lsp_type not in _LSP_TYPE_FOR_SNIPPET:
        return completion_item

    signatures = name.get_signatures()
    if not signatures:
        return completion_item

    try:
        snippet_signature = get_snippet_signature(signatures[0])
    except Exception:  # pylint: disable=broad-except
        return completion_item
    new_text = name_name + snippet_signature
    completion_item.insertText = new_text
    completion_item.insertTextFormat = InsertTextFormat.Snippet
    return completion_item