Last active
September 7, 2016 11:33
-
-
Save DanielleSucher/a56612c487c375328ce356f40d365c44 to your computer and use it in GitHub Desktop.
bits and pieces of feature explorer in vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" An example of the sort of feature table I'm parsing here: | |
" https://github.com/janestreet/iron/blob/91210e6cbfb950b9da32429a42985a0cffcd0913/tests/test-todo-suppresses-next-steps-when-not-helpful.t#L76-L84 | |
function! FeatureAtPoint() | |
" Save the starting position so we can return to it later. | |
let l:starting_pos = getpos('.') | |
" Initialize state for comparison. | |
let l:depth = 500 | |
let l:path = [] | |
" Get the contents of the current line. | |
let l:line = getline('.') | |
" Loop through the lines above it until we hit the top of the section. | |
while (l:line =~ '^| ') | |
let l:num_spaces = len(substitute(l:line,'^|\?\( \+\).*','\1','')) | |
" If this line's part is less indented than the last one we thought | |
" was part of our feature name, this must be the preceding part of our | |
" feature name! Let's update our reference depth and add this part to | |
" the list. | |
if (l:num_spaces < l:depth) | |
let l:part = substitute(l:line,'^|\? \+\([^ ]\+\) .*','\1','') | |
call add (l:path, l:part) | |
let l:depth = l:num_spaces | |
endif | |
" Go up one line. | |
exec 'normal! k' | |
" Get the contents of the current line. | |
let l:line = getline('.') | |
endwhile | |
" Return to the starting position. | |
call cursor(l:starting_pos[1], l:starting_pos[2]) | |
"Join the paths into the correct feature name string. | |
let l:feature = join(reverse(l:path), '/') | |
" Echo the full feature name. | |
echo l:feature | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment