Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# #
# http: // www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
import os
from pygls import uris
from pygls.types import TextDocumentItem, WorkspaceFolder
from pygls.workspace import Workspace
DOC_URI = uris.from_fs_path(__file__)
DOC_TEXT = '''test'''
DOC = TextDocumentItem(DOC_URI, 'plaintext', 0, DOC_TEXT)
def test_add_folder(workspace):
dir_uri = os.path.dirname(DOC_URI)
dir_name = 'test'
workspace.add_folder(WorkspaceFolder(dir_uri, dir_name))
assert workspace.folders[dir_uri].name == dir_name
def test_get_document(workspace):
workspace.put_document(DOC)
assert workspace.get_document(DOC_URI).source == DOC_TEXT
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_document_multiline_edit():
old = [
"def hello(a, b):\n",
" print a\n",
" print b\n"
]
doc = Document('file:///uri', u''.join(old), sync_kind=TextDocumentSyncKind.INCREMENTAL)
change = TextDocumentContentChangeEvent(
Range(Position(1, 4), Position(2, 11)), 0, u'print a, b')
doc.apply_change(change)
assert doc.lines == [
"def hello(a, b):\n",
" print a, b\n"
]
doc = Document('file:///uri', u''.join(old), sync_kind=TextDocumentSyncKind.INCREMENTAL)
change = TextDocumentContentChangeEvent(
range=Range(Position(1, 4), Position(2, 11)), text=u'print a, b')
doc.apply_change(change)
assert doc.lines == [
"def hello(a, b):\n",
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_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_document_end_of_file_edit():
old = [
"print 'a'\n",
"print 'b'\n"
]
doc = Document('file:///uri', u''.join(old))
change = TextDocumentContentChangeEvent(
Range(Position(2, 0), Position(2, 0)), 0, u'o')
doc.apply_change(change)
assert doc.lines == [
"print 'a'\n",
"print 'b'\n",
"o",
]
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_no_edit():
old = [
"def hello(a, b):\n",
" print a\n",
" print b\n"
]
doc = Document('file:///uri', u''.join(old), sync_kind=TextDocumentSyncKind.NONE)
change = TextDocumentContentChangeEvent(
Range(Position(1, 4), Position(2, 11)), 0, u'print a, b')
doc.apply_change(change)
assert doc.lines == old