From 07ad5d71ab97a84dc9c59b3507bf7898040d24cf Mon Sep 17 00:00:00 2001 From: Rui Abreu Ferreira Date: Sat, 1 Dec 2018 15:30:50 +0000 Subject: clipboard: Support custom VimL functions #9304 Up to now g:clipboard["copy"] only supported string values invoked as system commands. This commit enables the use of VimL functions instead. The function signatures are the same as in provider/clipboard.vim. A clipboard provider is expected to store and return a list of lines (i.e. the text) and a register type (as seen in setreg()). cache_enabled is ignored if "copy" is provided by a VimL function. --- .../clipboard/clipboard_provider_spec.lua | 89 +++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/functional/clipboard/clipboard_provider_spec.lua b/test/functional/clipboard/clipboard_provider_spec.lua index a40c080a6d..2bbc678a02 100644 --- a/test/functional/clipboard/clipboard_provider_spec.lua +++ b/test/functional/clipboard/clipboard_provider_spec.lua @@ -3,7 +3,7 @@ local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert -local feed_command, expect, eq, eval = helpers.feed_command, helpers.expect, helpers.eq, helpers.eval +local feed_command, expect, eq, eval, source = helpers.feed_command, helpers.expect, helpers.eq, helpers.eval, helpers.source local command = helpers.command local meths = helpers.meths @@ -147,6 +147,93 @@ describe('clipboard', function() eq('clippy!', eval('provider#clipboard#Executable()')) eq('', eval('provider#clipboard#Error()')) end) + + it('g:clipboard using VimL functions', function() + -- Implements a fake clipboard provider. cache_enabled is meaningless here. + source([[let g:clipboard = { + \ 'name': 'custom', + \ 'copy': { + \ '+': {lines, regtype -> extend(g:, {'dummy_clipboard_plus': [lines, regtype]}) }, + \ '*': {lines, regtype -> extend(g:, {'dummy_clipboard_star': [lines, regtype]}) }, + \ }, + \ 'paste': { + \ '+': {-> get(g:, 'dummy_clipboard_plus', [])}, + \ '*': {-> get(g:, 'dummy_clipboard_star', [])}, + \ }, + \ 'cache_enabled': 1, + \}]]) + + eq('', eval('provider#clipboard#Error()')) + eq('custom', eval('provider#clipboard#Executable()')) + + eq('', eval("getreg('*')")) + eq('', eval("getreg('+')")) + + command('call setreg("*", "star")') + command('call setreg("+", "plus")') + eq('star', eval("getreg('*')")) + eq('plus', eval("getreg('+')")) + + command('call setreg("*", "star", "v")') + eq({{'star'}, 'v'}, eval("g:dummy_clipboard_star")) + command('call setreg("*", "star", "V")') + eq({{'star', ''}, 'V'}, eval("g:dummy_clipboard_star")) + command('call setreg("*", "star", "b")') + eq({{'star', ''}, 'b'}, eval("g:dummy_clipboard_star")) + end) + + describe('g:clipboard[paste] VimL function', function() + it('can return empty list for empty clipboard', function() + source([[let g:dummy_clipboard = [] + let g:clipboard = { + \ 'name': 'custom', + \ 'copy': { '*': {lines, regtype -> 0} }, + \ 'paste': { '*': {-> g:dummy_clipboard} }, + \}]]) + eq('', eval('provider#clipboard#Error()')) + eq('custom', eval('provider#clipboard#Executable()')) + eq('', eval("getreg('*')")) + end) + + it('can return a list with a single string', function() + source([=[let g:dummy_clipboard = ['hello'] + let g:clipboard = { + \ 'name': 'custom', + \ 'copy': { '*': {lines, regtype -> 0} }, + \ 'paste': { '*': {-> g:dummy_clipboard} }, + \}]=]) + eq('', eval('provider#clipboard#Error()')) + eq('custom', eval('provider#clipboard#Executable()')) + + eq('hello', eval("getreg('*')")) + source([[let g:dummy_clipboard = [''] ]]) + eq('', eval("getreg('*')")) + end) + + it('can return a list of lines if a regtype is provided', function() + source([=[let g:dummy_clipboard = [['hello'], 'v'] + let g:clipboard = { + \ 'name': 'custom', + \ 'copy': { '*': {lines, regtype -> 0} }, + \ 'paste': { '*': {-> g:dummy_clipboard} }, + \}]=]) + eq('', eval('provider#clipboard#Error()')) + eq('custom', eval('provider#clipboard#Executable()')) + eq('hello', eval("getreg('*')")) + end) + + it('can return a list of lines instead of [lines, regtype]', function() + source([=[let g:dummy_clipboard = ['hello', 'v'] + let g:clipboard = { + \ 'name': 'custom', + \ 'copy': { '*': {lines, regtype -> 0} }, + \ 'paste': { '*': {-> g:dummy_clipboard} }, + \}]=]) + eq('', eval('provider#clipboard#Error()')) + eq('custom', eval('provider#clipboard#Executable()')) + eq('hello\nv', eval("getreg('*')")) + end) + end) end) describe('clipboard', function() -- cgit