aboutsummaryrefslogtreecommitdiffstats
path: root/bundle/github-url/autoload
diff options
context:
space:
mode:
authorTeddy Wing2018-04-06 01:33:08 +0200
committerTeddy Wing2018-04-06 01:33:55 +0200
commit2e3d5630df8ce9ffacc9f03c5f7ec5327f0b9c59 (patch)
treec252e5a2ffd5bdba84dc90a68f4a8e8775a09312 /bundle/github-url/autoload
parent78a23cd259c7dbca34d0dcf78b8b99aa0a5db1a5 (diff)
downloaddotvim-2e3d5630df8ce9ffacc9f03c5f7ec5327f0b9c59.tar.bz2
github-url: Move functions into `autoload`
Improve performance by eliminating the functions from the `plugin` file. Also introduces a new `github_url#GitHubURL()` function that serves as an entry point into the plugin. Add a `g:loaded_github_url` check to prevent plugin re-sourcing and be a good Vim citizen.
Diffstat (limited to 'bundle/github-url/autoload')
-rw-r--r--bundle/github-url/autoload/github_url.vim35
1 files changed, 35 insertions, 0 deletions
diff --git a/bundle/github-url/autoload/github_url.vim b/bundle/github-url/autoload/github_url.vim
new file mode 100644
index 0000000..11110a8
--- /dev/null
+++ b/bundle/github-url/autoload/github_url.vim
@@ -0,0 +1,35 @@
+" Constructs a GitHub URL for the current Git repo by making substitutions to
+" the SSH location to turn it into an HTTP URL.
+function! s:BaseRepoURL()
+ let remote = system('git remote get-url origin')
+ let base_url = substitute(remote, ':', '/', '')
+ let base_url = substitute(base_url, '^git@', 'https://', '')
+ let base_url = substitute(base_url, '.git\n$', '', '')
+
+ return base_url
+endfunction
+
+" Constructs a GitHub URL to the current file using the current HEAD's SHA. If
+" lines are passed in, these are included in the URL. The `include_lines`
+" argument comes from a `:command`'s `<count>`, and tells the function whether
+" a range was given.
+function! s:FileURL(include_lines, start_line, end_line)
+ let current_sha = system('git show --format="format:%H"')
+ let current_sha = substitute(current_sha, '\n$', '', '')
+ let current_filename = expand('%')
+ let lines = ''
+
+ if a:include_lines
+ let lines = '#L' . a:start_line
+
+ if a:start_line != a:end_line
+ let lines = lines . '-L' . a:end_line
+ endif
+ endif
+
+ return s:BaseRepoURL() . '/blob/' . current_sha . '/' . current_filename . lines
+endfunction
+
+function! github_url#GitHubURL(include_lines, start_line, end_line)
+ return s:FileURL(a:include_lines, a:start_line, a:end_line)
+endfunction