def test_change_range():
    # "foo\nbar2 --> "fOO\nbar"
    assert lsp.TextDocumentContentChangeEvent.range_change(
        lsp.Position(line=0, character=1),
        lsp.Position(line=0, character=3),
        "OO",
        "foo\nbar",
    ) == lsp.TextDocumentContentChangeEvent(
        range=lsp.Range(
            start=lsp.Position(line=0, character=1),  # f|oo
            end=lsp.Position(line=0, character=3),  # foo|
        ),
        rangeLength=len("oo"),
        text="OO",
    )

    # "foo\nbar\nbaz" --> "foLOLz"
    assert lsp.TextDocumentContentChangeEvent.range_change(
        lsp.Position(line=0, character=2),
        lsp.Position(line=2, character=2),
        "LOL",
        "foo\nbar\nbaz",
    ) == lsp.TextDocumentContentChangeEvent(
        range=lsp.Range(
            start=lsp.Position(line=0, character=2),  # fo|o
            end=lsp.Position(line=2, character=2),  # ba|z
        ),
        rangeLength=len("o" + "bar" + "ba"),  # FIXME: include newlines?
        text="LOL",
    )
Example #2
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)
Example #3
0
def _position_tk2lsp(tk_position: str) -> lsp.Position:
    # this can't use tab.textwidget.index, because it needs to handle text
    # locations that don't exist anymore when text has been deleted
    line, column = map(int, tk_position.split('.'))

    # lsp line numbering starts at 0
    # tk line numbering starts at 1
    # both column numberings start at 0
    return lsp.Position(line=line - 1, character=column)
Example #4
0
def _position_tk2lsp(tk_position: Union[str, List[int]]) -> lsp.Position:
    # this can't use tab.textwidget.index, because it needs to handle text
    # locations that don't exist anymore when text has been deleted
    if isinstance(tk_position, str):
        line, column = map(int, tk_position.split("."))
    else:
        line, column = tk_position

    # lsp line numbering starts at 0
    # tk line numbering starts at 1
    # both column numberings start at 0
    return lsp.Position(line=line - 1, character=column)
def test_as_tuple():
    assert lsp.Position(line=123, character=4).as_tuple() == (123, 4)
Example #6
0
 def doc_pos():  # SKIP
     x, y = get_meth_text_pos(text=text, method=method)
     return lsp.TextDocumentPosition(
         textDocument=lsp.TextDocumentIdentifier(uri=file_uri),
         position=lsp.Position(line=y, character=x),
     )