示例#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))
示例#2
0
def _completions_for_dicts(inference_state, dicts, literal_string,
                           cut_end_quote, fuzzy):
    for dict_key in sorted(_get_python_keys(dicts), key=lambda x: repr(x)):
        dict_key_str = _create_repr_string(literal_string, dict_key)
        if dict_key_str.startswith(literal_string):
            name = StringName(inference_state,
                              dict_key_str[:-len(cut_end_quote) or None])
            yield Completion(inference_state,
                             name,
                             stack=None,
                             like_name_length=len(literal_string),
                             is_fuzzy=fuzzy)
示例#3
0
 def build_completion(completion: JediCompletion):
     try:
         completion_type = completion.type
         annotation = (completion._get_docstring_signature()
                       if completion_type in {"class", "function"} else "")
         return {
             "label": completion.name,
             "annotation": annotation,
             "type": completion_type,
         }
     except Exception as err:
         LOGGER.debug("parsing completion error: %s", repr(err))
         return None
示例#4
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
示例#5
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))
示例#6
0
def lsp_completion_item(
    completion: Completion,
    char_before_cursor: str,
    enable_snippets: bool,
    resolve_eagerly: bool,
    markup_kind: MarkupKind,
) -> CompletionItem:
    """Using a Jedi completion, obtain a jedi completion item."""
    completion_name = completion.name
    name_clean = clean_completion_name(completion_name, char_before_cursor)
    lsp_type = get_lsp_completion_type(completion.type)
    completion_item = CompletionItem(
        label=completion_name,
        filter_text=completion_name,
        kind=lsp_type,
        sort_text=complete_sort_name(completion),
        insert_text=name_clean,
        insert_text_format=InsertTextFormat.PlainText,
    )

    _MOST_RECENT_COMPLETIONS[completion_name] = completion
    if resolve_eagerly:
        completion_item = lsp_completion_item_resolve(
            completion_item, markup_kind=markup_kind
        )

    if not enable_snippets:
        return completion_item
    if lsp_type not in _LSP_TYPE_FOR_SNIPPET:
        return completion_item

    signatures = completion.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 = completion_name + snippet_signature
    completion_item.insert_text = new_text
    completion_item.insert_text_format = InsertTextFormat.Snippet
    return completion_item