Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor s:EditUrlUnderCursor(), and add tests for ge when not on a markdown link #586

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,10 @@ endfunction
" script while this function is running. We must not replace it.
if !exists('*s:EditUrlUnderCursor')
function s:EditUrlUnderCursor()
let l:editmethod = ''
let l:url = s:Markdown_GetUrlForPosition(line('.'), col('.'))

" determine how to open the linked file (split, tab, etc)
let l:editmethod = ''
if exists('g:vim_markdown_edit_url_in')
if g:vim_markdown_edit_url_in ==# 'tab'
let l:editmethod = 'tabnew'
Expand All @@ -680,42 +682,47 @@ if !exists('*s:EditUrlUnderCursor')
" default to current buffer
let l:editmethod = 'edit'
endif
let l:url = s:Markdown_GetUrlForPosition(line('.'), col('.'))
if l:url !=# ''
if get(g:, 'vim_markdown_autowrite', 0)
write
endif
let l:anchor = ''
if get(g:, 'vim_markdown_follow_anchor', 0)
let l:parts = split(l:url, '#', 1)
if len(l:parts) == 2
let [l:url, l:anchor] = parts
let l:anchorexpr = get(g:, 'vim_markdown_anchorexpr', '')
if l:anchorexpr !=# ''
let l:anchor = eval(substitute(
\ l:anchorexpr, 'v:anchor',
\ escape('"'.l:anchor.'"', '"'), ''))
endif
endif
endif
if l:url !=# ''
let l:ext = ''
if get(g:, 'vim_markdown_no_extensions_in_markdown', 0)
" use another file extension if preferred
if exists('g:vim_markdown_auto_extension_ext')
let l:ext = '.'.g:vim_markdown_auto_extension_ext
else
let l:ext = '.md'
endif

" fallback if cursor is not on a markdown link
if l:url ==# ''
execute l:editmethod . ' <cfile>'
return
endif

if get(g:, 'vim_markdown_autowrite', 0)
write
endif

" parse anchor
let l:anchor = ''
if get(g:, 'vim_markdown_follow_anchor', 0)
let l:parts = split(l:url, '#', 1)
if len(l:parts) == 2
let [l:url, l:anchor] = parts
let l:anchorexpr = get(g:, 'vim_markdown_anchorexpr', '')
if l:anchorexpr !=# ''
let l:anchor = eval(substitute(
\ l:anchorexpr, 'v:anchor',
\ escape('"'.l:anchor.'"', '"'), ''))
endif
let l:url = fnameescape(fnamemodify(expand('%:h').'/'.l:url.l:ext, ':.'))
execute l:editmethod l:url
endif
if l:anchor !=# ''
silent! execute '/'.l:anchor
endif

" add file extension
let l:ext = ''
if get(g:, 'vim_markdown_no_extensions_in_markdown', 0)
" use another file extension if preferred
if exists('g:vim_markdown_auto_extension_ext')
let l:ext = '.'.g:vim_markdown_auto_extension_ext
else
let l:ext = '.md'
endif
else
execute l:editmethod . ' <cfile>'
endif
let l:url = fnameescape(fnamemodify(expand('%:h').'/'.l:url.l:ext, ':.'))

execute l:editmethod l:url
if l:anchor !=# ''
silent! execute '/'.l:anchor
endif
endfunction
endif
Expand Down
35 changes: 35 additions & 0 deletions test/map.vader
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,41 @@ Execute (ge auto-write before opening file):
AssertEqual g:vader_exception, 'Vim(write):E382: Cannot write, ''buftype'' option is set'
unlet g:vim_markdown_autowrite

Given markdown;
ge_test.md

Execute (ge falls back to edit <cfile> when not on a markdown link):
normal ge
AssertEqual @%, 'ge_test.md'
AssertEqual getline(1), 'ge test'

Given markdown;

Execute (ge falls back to edit <cfile> which throws if cursor is not on a link):
" TODO: Fix this to be consistent with gx
AssertThrows normal ge
AssertEqual g:vader_exception, 'Vim(edit):E446: No file name under cursor'

Given markdown;
ge_test

Execute (ge fallback does not respect g:vim_markdown_no_extensions_in_markdown):
let g:vim_markdown_no_extensions_in_markdown = 1
normal ge
AssertEqual @%, 'ge_test'
unlet g:vim_markdown_no_extensions_in_markdown

Given markdown;
ge_test.md

Execute (ge fallback does not respect g:vim_markdown_autowrite):
" TODO: Fix this
let g:vim_markdown_autowrite = 1
normal ia
normal l
normal ge " this would throw if file write was attempted
unlet g:vim_markdown_autowrite

Given markdown;
# a

Expand Down