Skip to content

Commit

Permalink
Add a test for edits to the end of the file
Browse files Browse the repository at this point in the history
Towards #317

Demonstrates the problem reported. When an edit sets the end position
to be the line following the last line of the file the cursor cannot be
placed in at that position so the edit will be done incorrectly, in this
scenario the edit should be done to the end of the last line.
  • Loading branch information
natebosch committed Jul 12, 2020
1 parent e24d328 commit c67d8aa
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions test/integration/test/edit_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'dart:async';
import 'dart:io';

import 'package:_test/vim_remote.dart';
import 'package:test/test.dart';
import 'package:json_rpc_2/json_rpc_2.dart';
import 'package:lsp/lsp.dart'
show lspChannel, WorkspaceEdit, TextEdit, Range, Position;

void main() {
Stream<Peer> clients;
ServerSocket serverSocket;
Vim vim;
Peer client;

setUpAll(() async {
serverSocket = await ServerSocket.bind('localhost', 0);

clients = serverSocket.map((socket) {
return Peer(lspChannel(socket, socket), onUnhandledError: (error, stack) {
fail('Unhandled server error: $error');
});
}).asBroadcastStream();
vim = await Vim.start();
await vim.expr('RegisterLanguageServer("text", {'
'"command":"localhost:${serverSocket.port}",'
'"enabled":v:false,'
'})');
});

setUp(() async {
final nextClient = clients.first;
await vim.edit('foo.txt');
await vim.sendKeys(':LSClientEnable<cr>');
client = await nextClient;
});

tearDown(() async {
await vim.sendKeys(':LSClientDisable<cr>');
await vim.sendKeys(':%bwipeout!<cr>');
final file = File('foo.txt');
if (await file.exists()) await file.delete();
await client.done;
client = null;
});

tearDownAll(() async {
await vim.quit();
final log = File(vim.name);
print(await log.readAsString());
await log.delete();
await serverSocket.close();
});

test('edit of entire file', () async {
final renameDone = Completer<void>();
client
..registerLifecycleMethods({})
..registerMethod('textDocument/rename', (Parameters params) {
renameDone.complete();
final uri = params['textDocument']['uri'].asString;
return WorkspaceEdit((b) => b
..changes = {
uri: [
TextEdit((b) => b
..newText = 'bar\nbar\n'
..range = Range((b) => b
..start = Position((b) => b
..line = 0
..character = 0)
..end = Position((b) => b
..line = 2
..character = 0)))
]
});
})
..listen();
await vim.sendKeys('ifoo<cr>foo<esc>');
await vim.sendKeys(':LSClientRename \'bar\'<cr>');
await renameDone;
await Future.delayed(const Duration(milliseconds: 100));
expect(await vim.expr(r'getline(1, "$")'), 'bar\nbar');
}, skip: 'https://github.com/natebosch/vim-lsc/issues/317');
}

extension LSP on Peer {
void registerLifecycleMethods(Map<String, dynamic> capabilities) {
registerMethod('initialize', (_) {
return {'capabilities': capabilities};
});
registerMethod('initialized', (_) {});
registerMethod('textDocument/didOpen', (_) {});
registerMethod('textDocument/didChange', (_) {});
registerMethod('shutdown', (_) {});
registerMethod('exit', (_) {
close();
});
}
}

0 comments on commit c67d8aa

Please sign in to comment.