Skip to content
This repository has been archived by the owner on Jul 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #36 from andreyorst/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
andreyorst authored May 29, 2018
2 parents b6f94c6 + 010d804 commit 4ee0dda
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ If you expanded a snippet, and you try to expand snippet inside this one, you wi
- [x] Command placeholders, that output is more then single line can't be jumped.
- [x] Trigger must be separated from everything
- [x] No back jumping.
- [x] Placeholders must be separated from each other and another text.

After reading this list you may want to ask me this question:

Expand Down
88 changes: 56 additions & 32 deletions autoload/SimpleSnippets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let s:current_file = ''
let s:snip_edit_buf = 0
let s:snip_edit_win = 0
let s:trigger = ''
let s:search_sequence = 0

let s:jump_stack = []
let s:type_stack = []
Expand Down Expand Up @@ -214,40 +215,35 @@ function! SimpleSnippets#jumpToLastPlaceholder()
endif
endfunction

" debug function
function! SimpleSnippets#printJumpStackState()
let l:i = 0
for item in s:jump_stack
if l:i != s:current_jump - 1
echon string(item)
else
echon "[".string(item)."]"
endif
if l:i != len(s:jump_stack) - 1
echon ", "
endif
let l:i += 1
endfor
echon " | current_jump = ". s:current_jump
sleep 1
endfunction

function! SimpleSnippets#jumpNormal(placeholder)
let l:ph = a:placeholder
let save_q_mark = getpos("'q")
let save_p_mark = getpos("'p")
call cursor(s:snip_start, 1)
if l:ph =~ "\\n"
let l:ph = join(split(l:ph), "\\n")
let l:echo = l:ph
elseif l:ph !~ "\\W"
call search(split(l:ph, '\\n')[0], 'c', s:snip_end)
normal! mq
call search(split(l:ph, '\\n')[-1], 'ce', s:snip_end)
normal! mp
else
let l:echo = a:placeholder
let l:ph = '\<' . l:ph . '\>'
if search('\<'.l:ph.'\>', 'c', s:snip_end) == 0
let s:search_sequence = 1
call search(l:ph, 'c', s:snip_end)
else
let s:search_sequence = 0
endif
normal! mq
if search('\<'.l:ph.'\>', 'ce', s:snip_end) == 0
let s:search_sequence = 1
call search(l:ph, 'ce', s:snip_end)
else
let s:search_sequence = 0
endif
normal! mp
endif
let save_q_mark = getpos("'q")
let save_p_mark = getpos("'p")
call search(split(l:ph, '\\n')[0], 'c', s:snip_end)
normal! mq
call search(split(l:ph, '\\n')[-1], 'ce', s:snip_end)
normal! mp
let s:ph_start = getpos("'q")
let s:ph_end = getpos("'p")
if &lazyredraw != 1
Expand All @@ -269,13 +265,22 @@ function! SimpleSnippets#checkIfChangesWereMade(jump)
let l:cursor_pos = getpos(".")
let l:prev_ph = get(s:jump_stack, a:jump)
if l:prev_ph !~ "\\W"
let l:prev_ph = '\<' . l:prev_ph . '\>'
call cursor(s:snip_start, 1)
if s:search_sequence == 1
if search(l:prev_ph, "c", s:snip_end) == 0
let s:jump_stack[a:jump] = SimpleSnippets#getLastInput()
endif
else
if search('\<'.l:prev_ph.'\>', "c", s:snip_end) == 0
let s:jump_stack[a:jump] = SimpleSnippets#getLastInput()
endif
endif
else
let l:prev_ph = escape(l:prev_ph, '/\*~')
endif
call cursor(s:snip_start, 1)
if search(l:prev_ph, "c", s:snip_end) == 0
let s:jump_stack[a:jump] = SimpleSnippets#getLastInput()
call cursor(s:snip_start, 1)
if search(l:prev_ph, "c", s:snip_end) == 0
let s:jump_stack[a:jump] = SimpleSnippets#getLastInput()
endif
endif
call cursor(l:cursor_pos[1], l:cursor_pos[2])
endif
Expand Down Expand Up @@ -949,7 +954,6 @@ endfunction


" 7.4 compability layer

function! SimpleSnippets#execute(command, ...)
if a:0 != 0
let l:silent = a:1
Expand Down Expand Up @@ -998,3 +1002,23 @@ function! SimpleSnippets#createSplit(path, trigger, filetype)
exec "setf " . a:filetype
endif
endfunction


" Debug functions
function! SimpleSnippets#printJumpStackState()
let l:i = 0
for item in s:jump_stack
if l:i != s:current_jump - 1
echon string(item)
else
echon "[".string(item)."]"
endif
if l:i != len(s:jump_stack) - 1
echon ", "
endif
let l:i += 1
endfor
echon " | jump ". s:current_jump
"sleep 1
endfunction

52 changes: 36 additions & 16 deletions doc/SimpleSnippets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ build with standard Vim features only, so it may be faster then any of those.
----------------

This plugin is tested against Vim 8.0, Vim 7.4.1689 and NeoVim 0.3.0. All
other versions are not supported, but might work too. This plungin works only
other versions are not supported, but might work too. This plugin works only
in 'nocompatible' mode.


Expand Down Expand Up @@ -91,16 +91,36 @@ Limitations:~

Known bugs / design flaws~

1. Placeholders must be separated form other text with non-word characters.
You can't use something like `${1:hello}_${2:world}` because resulting
text would be `hello_world` which Vim understands as single word or
`<cword>`. Jumping is based on text searching, and SimpleSnippets looks
for `\<placeholder_body\>` while searching over snippet's body. So in
this case first jump would look for `\<hello\>` in `\<hello_world\>` and
it will not find anything. However I can't search for just `hello`
instead, because it would break snippets with single letter placeholders.
Like `for(int ${1:i} = 0; $1 < 10; $1++)` if we would search for `i` we
would find `i` inside `int`, but not placeholder's `i`.
1. If you replace one placeholder with body of another placeholder, changes
of this placeholder's text can't be tracked later on, until the second
placeholder is changed too. For example:

`for (int i = 0; i < [10]; i++) {}`

And current jump stack state is:

`[10], i, 0, <, ++, ... | jump 1`

Imagine that `10` is selected, and you replace it with `0`, and jump to `i`

`for (int [i] = 0; [i] < 0; [i]++) {}`

Accordingly to jump stack:

`0, [i], 0, <, ++, ... | jump 2`

Next jump will be `0` of `i = 0` part. Which is fine, however after every
jump SimpleSnippets checks if current placeholder was changed, by searching
for old placeholder's body, which is `0` in this case. So imagine that you
have changed `0` to `10`, and moved on to `<`. Simple snippets will look
over the snippets body and will found `0` in `i < 0` part. It will assume
that no changes were made. Therefore jump stack state will be:

`0, i, 0, [<], ++, ... | jump 3`

You will end with two placeholders which state is deadlocked with `0` state.
To workaround this complex constructions inside placeholder's body can be
used: `${3:= 0}`, however this will lead to more keystrokes.

Even with these limitations you can use advanced snippets, with vimscript
functions, shell commands, mirroring, and more. If You still want to use this
Expand Down Expand Up @@ -839,12 +859,12 @@ file should contain expanded and edited snippet.

Test consists of test script `test.sh` and reference file, both located at
`testing/tests/testname/` folder. Reference file must be called `reference`
and contain expanded and edited snipppet. Test script consists of three
and contain expanded and edited snippet. Test script consists of three
functions: `before_test()`, `test_function()` and `after_test()`. This script
defines a bash variable called `test_file` and creates this file with touch.
Then `before_test()` sends input to tmus session, which name is stored in
Then `before_test()` sends input to tmux session, which name is stored in
`$tmux_session` variable, and opens `$test_file` editor stored in `$vim`
variable. Then it starts the redir to log file, to log messages. Sampple
variable. Then it starts the `redir` to log file, to log messages. Sample
`before_test()` function:

`test_file=lorem`
Expand All @@ -862,7 +882,7 @@ to tmux with keys needed to expand a snippet:

Note that at the end of key sequence you can spot strange `Qq`. This is test
inoremap that emulates <Esc> key in tmux. I don't know why, but sometimes
Escape just do not work. there is another `inoremap` for save and quit from
Escape just do not work. There is another `inoremap` for save and quit from
insert mode: `Qw`.

The last function is `after_test()`, which stops redir and saves the file:
Expand All @@ -871,7 +891,7 @@ The last function is `after_test()`, which stops redir and saves the file:
` tmux send-keys -t $tmux_session ":redir END" enter ":x" enter`
`}`

Theese functions are being sourced by another script, therefore no need to
These functions are being sourced by another script, therefore no need to
include shebang in this file.


Expand Down
13 changes: 12 additions & 1 deletion testing/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ for dep in ${dependencies[*]}; do
done

vim_versions=("nvim" "vim" "vim-7.4.1689" "vim-7.4.001")
tests=("lorem" "forward_jumping" "backward_jumping" "for" "cla" "shell" "create_snippet" "flash")

tests=(
"lorem"
"forward_jumping"
"backward_jumping"
"for"
"cla"
"shell"
"create_snippet"
"flash"
"inword_placeholders"
)

cd $(dirname $0)

Expand Down
1 change: 1 addition & 0 deletions testing/snippets/all/inword
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${1:lorem}${2:ipsum}
3 changes: 3 additions & 0 deletions testing/tests/inword_placeholders/reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test start
SimpleSnippets
test end
12 changes: 12 additions & 0 deletions testing/tests/inword_placeholders/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_file=for.c
before_test() {
touch $test_file
tmux send-keys -t $tmux_session "$vim -n -u ../../testrc $test_file" enter ":redir > $log_file" enter
}
test_func() {
tmux send-keys -t $tmux_session "ggdGitest start" enter "inword" escape "a" tab "Simp" tab "Snipp" c-k "Simple" tab "Snippets" tab enter "test endQq"
}
after_test() {
tmux send-keys -t $tmux_session ":redir END" enter ":x" enter
}

0 comments on commit 4ee0dda

Please sign in to comment.