示例#1
0
def do_stuff_with_a_langserver(tmp_path, filename, file_content, language_id,
                               command):
    path = tmp_path / filename
    path.write_text(file_content)

    with run_langserver(tmp_path, command) as (lsp_client, event_iter):
        inited = next(event_iter)
        assert isinstance(inited, lsp.Initialized)
        lsp_client.did_open(
            lsp.TextDocumentItem(uri=path.as_uri(),
                                 languageId=language_id,
                                 text=file_content,
                                 version=0))

        diagnostics = next(event_iter)
        assert isinstance(diagnostics, lsp.PublishDiagnostics)
        assert diagnostics.uri == path.as_uri()

        event_id = lsp_client.completions(
            text_document_position=lsp.TextDocumentPosition(
                textDocument=lsp.TextDocumentIdentifier(uri=path.as_uri()),
                position=lsp.Position(
                    # first line = 0, first column = 0
                    line=file_content.count("\n"),
                    character=len(file_content.split("\n")[-1]),
                ),
            ),
            context=lsp.CompletionContext(
                triggerKind=lsp.CompletionTriggerKind.INVOKED),
        )
        completions = next(event_iter)
        assert completions.message_id == event_id

        return (diagnostics, completions)
示例#2
0
    def _send_tab_opened_message(self, tab: tabs.FileTab) -> None:
        config = tab.settings.get('langserver', Optional[LangServerConfig])
        assert isinstance(config, LangServerConfig)
        assert tab.path is not None

        self._lsp_client.did_open(
            lsp.TextDocumentItem(
                uri=tab.path.as_uri(),
                languageId=config.language_id,
                text=tab.textwidget.get('1.0', 'end - 1 char'),
                version=0,
            ))
示例#3
0
def test_gopls(server_gopls):
    # lsp_client, project_root, event_iter = server_gopls
    tserver, project_root = server_gopls

    language_id = "go"

    # get text from `files_go` where file ends with '.go'
    filename = next(fn for fn in files_go if fn.endswith(".go"))
    path = project_root / filename
    text = files_go[filename]

    # Init #####
    inited = tserver.get_msg_by_type(lsp.Initialized)
    # DidOpen file #####
    tserver.lsp_client.did_open(
        lsp.TextDocumentItem(uri=path.as_uri(),
                             languageId=language_id,
                             text=text,
                             version=0))

    # Dignostics #####
    diagnostics = tserver.get_msg_by_type(lsp.PublishDiagnostics)
    assert diagnostics.uri == path.as_uri()
    assert [diag.message for diag in diagnostics.diagnostics
            ] == ["expected ';', found asdf"]

    do_meth_params = {
        "tserver": tserver,
        "text": text,
        "file_uri": path.as_uri()
    }

    # Completion #####
    completions = do_server_method(**do_meth_params, method=METHOD_COMPLETION)
    assert "doSomethingWithFoo" in [
        item.label for item in completions.completion_list.items
    ]

    # implementation #####
    # TODO - null result for some reason
    # implementation = do_server_method(**do_meth_params, method=METHOD_IMPLEMENTATION)
    # print(f' implementation: {implementation}')

    # typeDefinition #####
    typedef = do_server_method(**do_meth_params, method=METHOD_TYPEDEF)
    assert len(typedef.result) == 1
    assert typedef.result[0].uri == path.as_uri()
示例#4
0
def test_clangd_11(server_clangd_11):
    tserver, project_root = server_clangd_11

    filename, text, language_id = c_args
    path = project_root / filename
    path.write_text(text)

    # Init #####
    inited = tserver.get_msg_by_type(lsp.Initialized)
    # DidOpen file #####
    tserver.lsp_client.did_open(
        lsp.TextDocumentItem(uri=path.as_uri(),
                             languageId=language_id,
                             text=text,
                             version=0))

    # Dignostics #####
    diagnostics = tserver.get_msg_by_type(lsp.PublishDiagnostics)
    assert diagnostics.uri == path.as_uri()
    assert [diag.message for diag in diagnostics.diagnostics] == [
        "Non-void function does not return a value",
        "Use of undeclared identifier 'do_'",
        "Expected '}'",
    ]

    do_meth_params = {
        "tserver": tserver,
        "text": text,
        "file_uri": path.as_uri()
    }

    # Completion #####
    completions = do_server_method(**do_meth_params, method=METHOD_COMPLETION)
    completions = [item.label for item in completions.completion_list.items]
    assert " do_foo()" in completions
    assert " do_bar(char x, long y)" in completions

    # workspace/symbol #####
    # TODO - empty for some reason
    # tserver.lsp_client.workspace_symbol()
    # w_symb = tserver.get_msg_by_type(lsp.MWorkspaceSymbols)

    # declaration #####
    declaration = do_server_method(**do_meth_params, method=METHOD_DECLARATION)
    assert len(declaration.result) == 1
    assert declaration.result[0].uri == path.as_uri()
示例#5
0
def test_javascript_typescript_langserver(server_js):
    tserver, project_root = server_js

    text = textwrap.dedent(f"""\
        function doSomethingWithFoo(x, y) {{
            const blah = x + y;
            return asdf asdf;
        }}

        doS //#{METHOD_COMPLETION}-3""")
    filename = "foo.js"
    path = project_root / filename
    path.write_text(text)
    language_id = "javascript"

    # Init #####
    inited = tserver.get_msg_by_type(lsp.Initialized)
    # DidOpen file #####
    tserver.lsp_client.did_open(
        lsp.TextDocumentItem(uri=path.as_uri(),
                             languageId=language_id,
                             text=text,
                             version=0))

    # Dignostics #####
    diagnostics = tserver.get_msg_by_type(lsp.PublishDiagnostics)
    assert diagnostics.uri == path.as_uri()
    assert [diag.message
            for diag in diagnostics.diagnostics] == ["';' expected."]

    do_meth_params = {
        "tserver": tserver,
        "text": text,
        "file_uri": path.as_uri()
    }

    # Completion #####
    completions = do_server_method(**do_meth_params, method=METHOD_COMPLETION)
    assert "doSomethingWithFoo" in [
        item.label for item in completions.completion_list.items
    ]
示例#6
0
def test_pyls(server_pyls):
    tserver, project_root = server_pyls

    text = textwrap.dedent(f"""\
        import sys
        def do_foo(): #{METHOD_DEFINITION}-5
            sys.getdefaultencoding() #{METHOD_HOVER}-5
        def do_bar(): #{METHOD_REFERENCES}-5
            sys.intern("hey") #{METHOD_SIG_HELP}-2

        do_ #{METHOD_COMPLETION}-1""")
    filename = "foo.py"
    path = project_root / filename
    path.write_text(text)
    language_id = "python"

    inited = tserver.get_msg_by_type(lsp.Initialized)

    tserver.lsp_client.did_open(
        lsp.TextDocumentItem(uri=path.as_uri(),
                             languageId=language_id,
                             text=text,
                             version=0))

    # Dignostics #####
    diagnostics = tserver.get_msg_by_type(lsp.PublishDiagnostics)
    assert diagnostics.uri == path.as_uri()

    diag_msgs = [diag.message for diag in diagnostics.diagnostics]
    assert "undefined name 'do_'" in diag_msgs
    assert "E302 expected 2 blank lines, found 0" in diag_msgs
    assert "W292 no newline at end of file" in diag_msgs

    do_meth_params = {
        "tserver": tserver,
        "text": text,
        "file_uri": path.as_uri()
    }

    # Completion #####
    completions = do_server_method(**do_meth_params, method=METHOD_COMPLETION)
    assert [item.label for item in completions.completion_list.items] == [
        "do_bar()",
        "do_foo()",
    ]

    # Hover #####
    hover = do_server_method(**do_meth_params, method=METHOD_HOVER)
    # NOTE: crude because response changes from one Python version to another
    assert "getdefaultencoding() -> str" in str(hover.contents)

    # signatureHelp #####
    sighelp = do_server_method(**do_meth_params, method=METHOD_SIG_HELP)

    assert len(sighelp.signatures) > 0
    active_sig = sighelp.signatures[sighelp.activeSignature]
    assert isinstance(active_sig, lsp.SignatureInformation)
    assert len(active_sig.parameters) > 0
    assert isinstance(active_sig.parameters[0], lsp.ParameterInformation)

    # definition #####
    definitions = do_server_method(**do_meth_params, method=METHOD_DEFINITION)

    assert isinstance(definitions.result, lsp.Location) or len(
        definitions.result) == 1
    item = (definitions.result[0]
            if isinstance(definitions.result, list) else definitions.result)
    assert isinstance(item, (lsp.Location, lsp.LocationLink))
    if isinstance(item, lsp.Location):
        assert item.uri == path.as_uri()
        definition_line = next(i for i, line in enumerate(text.splitlines())
                               if METHOD_DEFINITION in line)
        assert item.range.start.line == definition_line
    else:  # LocationLink
        raise NotImplementedError("pyls `LocationLink` definition results")

    # references #####
    refs = do_server_method(**do_meth_params, method=METHOD_REFERENCES)

    assert len(refs.result) == 1
    item = refs.result[0]
    assert isinstance(item, lsp.Location)
    ref_line = next(i for i, line in enumerate(text.splitlines())
                    if METHOD_REFERENCES in line)
    assert item.range.start.line == ref_line

    # documentSymbol #####
    doc_symbols = do_server_method(**do_meth_params, method=METHOD_DOC_SYMBOLS)
    assert len(doc_symbols.result) == 3
    symb_names = {s.name for s in doc_symbols.result}
    assert symb_names == {"sys", "do_foo", "do_bar"}

    # formatting #####
    tserver.lsp_client.formatting(
        text_document=lsp.TextDocumentIdentifier(uri=path.as_uri()),
        options=lsp.FormattingOptions(tabSize=4, insertSpaces=True),
    )
    formatting = tserver.get_msg_by_type(RESPONSE_TYPES[METHOD_FORMAT_DOC])
    assert formatting.result

    # Error -- method not supported by server #####
    tserver.lsp_client.workspace_symbol()
    err = tserver.get_msg_by_type(lsp.ResponseError)
    assert err.message == "Method Not Found: workspace/symbol"