Example #1
0
def test_apply_text_edits_multiline(pylsp):
    pylsp.workspace.put_document(DOC_URI, '0\n1\n2\n3\n4')
    test_doc = pylsp.workspace.get_document(DOC_URI)

    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 2,
                "character": 0
            },
            "end": {
                "line": 3,
                "character": 0
            }
        },
        "newText": "Hello"
    }, {
        "range": {
            "start": {
                "line": 1,
                "character": 1
            },
            "end": {
                "line": 1,
                "character": 1
            }
        },
        "newText": "World"
    }]) == '0\n1World\nHello3\n4'
def test_line_endings(workspace, newline):
    doc = Document(DOC_URI, workspace,
                   f'import os;import sys{2 * newline}dict(a=1)')
    res = pylsp_format_document(doc)

    assert apply_text_edits(
        doc,
        res) == f'import os{newline}import sys{2 * newline}dict(a=1){newline}'
def test_config_file(tmpdir, workspace):
    # a config file in the same directory as the source file will be used
    conf = tmpdir.join('.style.yapf')
    conf.write('[style]\ncolumn_limit = 14')
    src = tmpdir.join('test.py')
    doc = Document(uris.from_fs_path(src.strpath), workspace, DOC)

    res = pylsp_format_document(doc)

    # A was split on multiple lines because of column_limit from config file
    assert apply_text_edits(
        doc, res) == "A = [\n    'h', 'w',\n    'a'\n]\n\nB = ['h', 'w']\n"
def test_range_format(workspace):
    doc = Document(DOC_URI, workspace, DOC)

    def_range = {
        'start': {
            'line': 0,
            'character': 0
        },
        'end': {
            'line': 4,
            'character': 10
        }
    }
    res = pylsp_format_range(doc, def_range)

    # Make sure B is still badly formatted
    assert apply_text_edits(
        doc, res) == "A = ['h', 'w', 'a']\n\nB = ['h',\n\n\n'w']\n"
Example #5
0
def test_apply_text_edits_insert(pylsp):
    pylsp.workspace.put_document(DOC_URI, '012345678901234567890123456789')
    test_doc = pylsp.workspace.get_document(DOC_URI)

    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 0
            },
            "end": {
                "line": 0,
                "character": 0
            }
        },
        "newText": "Hello"
    }]) == 'Hello012345678901234567890123456789'
    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 1
            },
            "end": {
                "line": 0,
                "character": 1
            }
        },
        "newText": "Hello"
    }]) == '0Hello12345678901234567890123456789'
    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 1
            },
            "end": {
                "line": 0,
                "character": 1
            }
        },
        "newText": "Hello"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 1
            },
            "end": {
                "line": 0,
                "character": 1
            }
        },
        "newText": "World"
    }]) == '0HelloWorld12345678901234567890123456789'
    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 2
            },
            "end": {
                "line": 0,
                "character": 2
            }
        },
        "newText": "One"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 1
            },
            "end": {
                "line": 0,
                "character": 1
            }
        },
        "newText": "Hello"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 1
            },
            "end": {
                "line": 0,
                "character": 1
            }
        },
        "newText": "World"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 2
            },
            "end": {
                "line": 0,
                "character": 2
            }
        },
        "newText": "Two"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 2
            },
            "end": {
                "line": 0,
                "character": 2
            }
        },
        "newText": "Three"
    }]) == '0HelloWorld1OneTwoThree2345678901234567890123456789'
Example #6
0
def test_apply_text_edits_overlap(pylsp):
    pylsp.workspace.put_document(DOC_URI, '012345678901234567890123456789')
    test_doc = pylsp.workspace.get_document(DOC_URI)

    did_throw = False
    try:
        apply_text_edits(test_doc, [{
            "range": {
                "start": {
                    "line": 0,
                    "character": 3
                },
                "end": {
                    "line": 0,
                    "character": 6
                }
            },
            "newText": "Hello"
        }, {
            "range": {
                "start": {
                    "line": 0,
                    "character": 3
                },
                "end": {
                    "line": 0,
                    "character": 3
                }
            },
            "newText": "World"
        }])
    except OverLappingTextEditException:
        did_throw = True

    assert did_throw

    did_throw = False

    try:
        apply_text_edits(test_doc, [{
            "range": {
                "start": {
                    "line": 0,
                    "character": 3
                },
                "end": {
                    "line": 0,
                    "character": 6
                }
            },
            "newText": "Hello"
        }, {
            "range": {
                "start": {
                    "line": 0,
                    "character": 4
                },
                "end": {
                    "line": 0,
                    "character": 4
                }
            },
            "newText": "World"
        }])
    except OverLappingTextEditException:
        did_throw = True

    assert did_throw
Example #7
0
def test_apply_text_edits_replace(pylsp):
    pylsp.workspace.put_document(DOC_URI, '012345678901234567890123456789')
    test_doc = pylsp.workspace.get_document(DOC_URI)

    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 3
            },
            "end": {
                "line": 0,
                "character": 6
            }
        },
        "newText": "Hello"
    }]) == '012Hello678901234567890123456789'
    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 3
            },
            "end": {
                "line": 0,
                "character": 6
            }
        },
        "newText": "Hello"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 6
            },
            "end": {
                "line": 0,
                "character": 9
            }
        },
        "newText": "World"
    }]) == '012HelloWorld901234567890123456789'
    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 3
            },
            "end": {
                "line": 0,
                "character": 6
            }
        },
        "newText": "Hello"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 6
            },
            "end": {
                "line": 0,
                "character": 6
            }
        },
        "newText": "World"
    }]) == '012HelloWorld678901234567890123456789'
    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 6
            },
            "end": {
                "line": 0,
                "character": 6
            }
        },
        "newText": "World"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 3
            },
            "end": {
                "line": 0,
                "character": 6
            }
        },
        "newText": "Hello"
    }]) == '012HelloWorld678901234567890123456789'
    assert apply_text_edits(test_doc, [{
        "range": {
            "start": {
                "line": 0,
                "character": 3
            },
            "end": {
                "line": 0,
                "character": 3
            }
        },
        "newText": "World"
    }, {
        "range": {
            "start": {
                "line": 0,
                "character": 3
            },
            "end": {
                "line": 0,
                "character": 6
            }
        },
        "newText": "Hello"
    }]) == '012WorldHello678901234567890123456789'
def test_format_with_insert_spaces_option(workspace):
    doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
    res = pylsp_format_document(doc, {"insertSpaces": False})

    assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace("    ", "\t")
def test_format_with_tab_size_option(workspace):
    doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
    res = pylsp_format_document(doc, {"tabSize": "8"})

    assert apply_text_edits(doc,
                            res) == FOUR_SPACE_DOC.replace("    ", "        ")
def test_format(workspace):
    doc = Document(DOC_URI, workspace, DOC)
    res = pylsp_format_document(doc)

    assert apply_text_edits(doc,
                            res) == "A = ['h', 'w', 'a']\n\nB = ['h', 'w']\n"
def test_format_with_yapf_specific_option(workspace):
    doc = Document(DOC_URI, workspace, FOUR_SPACE_DOC)
    res = pylsp_format_document(doc, {"USE_TABS": True})

    assert apply_text_edits(doc, res) == FOUR_SPACE_DOC.replace("    ", "\t")