Skip to content

Commit

Permalink
Merge pull request #199 from KazuakiM/master
Browse files Browse the repository at this point in the history
Update Markdown_GetUrlForPosition function b: to s:
  • Loading branch information
shirosaki committed Apr 24, 2015
2 parents c4975a4 + ae271c7 commit 028a761
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
6 changes: 2 additions & 4 deletions ftplugin/mkd.vim
Expand Up @@ -464,14 +464,12 @@ endfunction
" - a string with the the URL for the link under the cursor
" - an empty string if the cursor is not on a link
"
" `b:` instead of `s:` to make it testable.
"
" TODO
"
" - multiline support
" - give an error if the separator does is not on a link
"
function! b:Markdown_GetUrlForPosition(lnum, col)
function! s:Markdown_GetUrlForPosition(lnum, col)
let l:lnum = a:lnum
let l:col = a:col
let l:syn = synIDattr(synID(l:lnum, l:col, 1), 'name')
Expand Down Expand Up @@ -504,7 +502,7 @@ endfunction
" Front end for GetUrlForPosition.
"
function! s:OpenUrlUnderCursor()
let l:url = b:Markdown_GetUrlForPosition(line('.'), col('.'))
let l:url = s:Markdown_GetUrlForPosition(line('.'), col('.'))
if l:url != ''
call netrw#NetrwBrowseX(l:url, 0)
else
Expand Down
42 changes: 23 additions & 19 deletions test/map.vader
Expand Up @@ -4,47 +4,51 @@ a <http://b> c
Execute (gx autolink):
let b:url = 'http://b'
let b:line = getline(1)
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'a') + 1), ''
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, '<') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'h') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, '>') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'c') + 1), ''
let b:func = Markdown_GetFunc('vim-markdown/ftplugin/mkd.vim', 'Markdown_GetUrlForPosition')
AssertEqual b:func(1, match(b:line, 'a') + 1), ''
AssertEqual b:func(1, match(b:line, '<') + 1), b:url
AssertEqual b:func(1, match(b:line, 'h') + 1), b:url
AssertEqual b:func(1, match(b:line, '>') + 1), b:url
AssertEqual b:func(1, match(b:line, 'c') + 1), ''

Given mkd;
a http://b.bb c

Execute (gx implicit autolink):
let b:url = 'http://b.bb'
let b:line = getline(1)
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'a') + 1), ''
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'h') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'c') + 1), ''
let b:func = Markdown_GetFunc('vim-markdown/ftplugin/mkd.vim', 'Markdown_GetUrlForPosition')
AssertEqual b:func(1, match(b:line, 'a') + 1), ''
AssertEqual b:func(1, match(b:line, 'h') + 1), b:url
AssertEqual b:func(1, match(b:line, 'c') + 1), ''

Given mkd;
[a]: http://b "c"

Execute (gx link reference definition):
let b:url = 'http://b'
let b:line = getline(1)
let b:func = Markdown_GetFunc('vim-markdown/ftplugin/mkd.vim', 'Markdown_GetUrlForPosition')
" TODO would be cool if all of the following gave the link.
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'a') + 1), ''
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'h') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'c') + 1), ''
AssertEqual b:func(1, match(b:line, 'a') + 1), ''
AssertEqual b:func(1, match(b:line, 'h') + 1), b:url
AssertEqual b:func(1, match(b:line, 'c') + 1), ''

Given mkd;
a [b](c) d

Execute (gx autolink):
let b:url = 'c'
let b:line = getline(1)
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'a') + 1), ''
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, '[') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'b') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, ']') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, '(') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'c') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, ')') + 1), b:url
AssertEqual b:Markdown_GetUrlForPosition(1, match(b:line, 'd') + 1), ''
let b:func = Markdown_GetFunc('vim-markdown/ftplugin/mkd.vim', 'Markdown_GetUrlForPosition')
AssertEqual b:func(1, match(b:line, 'a') + 1), ''
AssertEqual b:func(1, match(b:line, '[') + 1), b:url
AssertEqual b:func(1, match(b:line, 'b') + 1), b:url
AssertEqual b:func(1, match(b:line, ']') + 1), b:url
AssertEqual b:func(1, match(b:line, '(') + 1), b:url
AssertEqual b:func(1, match(b:line, 'c') + 1), b:url
AssertEqual b:func(1, match(b:line, ')') + 1), b:url
AssertEqual b:func(1, match(b:line, 'd') + 1), ''

Given mkd;
# a
Expand Down
16 changes: 16 additions & 0 deletions test/vimrc
Expand Up @@ -6,3 +6,19 @@ filetype on
filetype plugin on
filetype indent on
syntax on

function! Markdown_GetScriptID(fname) abort
let a:snlist = ''
redir => a:snlist
silent! scriptnames
redir END
let a:mx = '^\s*\(\d\+\):\s*\(.*\)$'
for a:line in split(a:snlist, "\n")
if stridx(a:line, a:fname) >= 0
return substitute(a:line, a:mx, '\1', '')
endif
endfor
endfunction
function! Markdown_GetFunc(fname, funcname) abort
return function('<SNR>' . Markdown_GetScriptID(a:fname) . '_' . a:funcname)
endfunction

0 comments on commit 028a761

Please sign in to comment.