aboutsummaryrefslogtreecommitdiff
path: root/.local/share/nvim/site/autoload/brackify.vim
blob: ca2a8340e4877bcd901773b5e1953a93dc4a1722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function! CharAtIdx(str, idx) abort
  return strcharpart(a:str, a:idx, 1)
endfunction

function! CursorCharIdx() abort
  let cursor_byte_idx = col('.') 
  if cursor_byte_idx == 1
    return 0
  endif

  let pre_cursor_text = getline('.')[:col('.')-2]
  return strchars(pre_cursor_text)
endfunction

function brackify#putbracket(bracket)
	let cur_char_idx = CursorCharIdx()
	let cur_char = CharAtIdx(getline('.'), cur_char_idx + 1)

	if cur_char != a:bracket
		let line = getline('.')[:col('.') - 1] . a:bracket . getline('.')[col('.'):]
		call setline(line("."), line)
	endif
endfunction

function brackify#putquotes(quote)
	let cur_char_idx = CursorCharIdx()
	let cur_char = CharAtIdx(getline('.'), cur_char_idx + 1)

	if cur_char != a:quote
		let line = getline('.')[:col('.') - 1] . a:quote . a:quote . getline('.')[col('.'):]
		call setline(line("."), line)

		if col('.') > 1
			call cursor(getpos('.')[1], col('.') + 1)
		endif
	else
		call cursor(getpos('.')[1], col('.') + 1)
	endif
endfunction