Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_document_line_edit():
doc = Document('file:///uri', u'itshelloworld')
change = TextDocumentContentChangeEvent(
Range(Position(0, 3), Position(0, 8)), 0, u'goodbye')
doc.apply_change(change)
assert doc.source == u'itsgoodbyeworld'
def test_document_empty_edit():
doc = Document('file:///uri', u'')
change = TextDocumentContentChangeEvent(
Range(Position(0, 0), Position(0, 0)), 0, u'f')
doc.apply_change(change)
assert doc.source == u'f'
def test_document_full_edit():
old = [
"def hello(a, b):\n",
" print a\n",
" print b\n"
]
doc = Document('file:///uri', u''.join(old), sync_kind=TextDocumentSyncKind.FULL)
change = TextDocumentContentChangeEvent(
Range(Position(1, 4), Position(2, 11)), 0, u'print a, b')
doc.apply_change(change)
assert doc.lines == [
"print a, b"
]
doc = Document('file:///uri', u''.join(old), sync_kind=TextDocumentSyncKind.FULL)
change = TextDocumentContentChangeEvent(range=None, text=u'print a, b')
doc.apply_change(change)
assert doc.lines == [
"print a, b"
]
def test_range_from_utf16():
assert range_from_utf16(
['x="😋"'], Range(Position(0, 3), Position(0, 5))
) == Range(Position(0, 3), Position(0, 4))
range = Range(Position(0, 3), Position(0, 5))
range_from_utf16(['x="😋"'], range)
assert range == Range(Position(0, 3), Position(0, 5))
def test_range_to_utf16():
assert range_to_utf16(
['x="😋"'], Range(Position(0, 3), Position(0, 4))
) == Range(Position(0, 3), Position(0, 5))
range = Range(Position(0, 3), Position(0, 4))
range_to_utf16(['x="😋"'], range)
assert range == Range(Position(0, 3), Position(0, 4))
def _get_range(p: SourcePosition = None):
if p is None:
return Range(
Position(),
Position(0, sys.maxsize),
)
else:
return Range(
Position(p.line - 1, p.column - 1),
Position(p.end_line - 1, p.end_column - 1),
)
def __eq__(self, other):
return (
isinstance(other, Range)
and self.start == other.start
and self.end == other.end)
def range_to_utf16(lines: List[str], range: Range) -> Range:
"""Convert range.[start|end].character from utf-32 to utf-16 code units.
Arguments:
lines (list):
The content of the document which the range refers to.
range (Range):
The line and character offset in utf-16 code units.
Returns:
The range with `character` offsets being converted to utf-32 code units.
"""
return Range(
position_to_utf16(lines, range.start),
position_to_utf16(lines, range.end)
)
def _validate_json(source):
"""Validates json file."""
diagnostics = []
try:
json.loads(source)
except JSONDecodeError as err:
msg = err.msg
col = err.colno
line = err.lineno
d = Diagnostic(
Range(
Position(line - 1, col - 1),
Position(line - 1, col)
),
msg,
source=type(json_server).__name__
)
diagnostics.append(d)
return diagnostics
def range_from_utf16(lines: List[str], range: Range) -> Range:
"""Convert range.[start|end].character from utf-16 code units to utf-32.
Arguments:
lines (list):
The content of the document which the range refers to.
range (Range):
The line and character offset in utf-32 code units.
Returns:
The range with `character` offsets being converted to utf-16 code units.
"""
return Range(
position_from_utf16(lines, range.start),
position_from_utf16(lines, range.end)
)