Skip to content

Commit

Permalink
Add optional argument to :RelatedFile to open in other modes
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldducky committed Apr 14, 2013
1 parent fa70249 commit 27b453f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
13 changes: 9 additions & 4 deletions README.md
Expand Up @@ -6,9 +6,9 @@ A vim plugin for opening a related file based on the file path.
Installation
------------

### Vundle
I prefer to use [Vundle](https://github.com/gmarik/vundle) but it's just as easy using other methods.

I prefer to use [Vundle](https://github.com/gmarik/vundle).
### Vundle

Install Vundle, following the [instructions](https://github.com/gmarik/vundle#quick-start).

Expand All @@ -31,8 +31,11 @@ Commands

There are two commands currently exposed:

1. `:RelatedFile` - Opens the related file in a vertical split
2. `:RelatedTests` - Runs the unit tests for the plugin
1. `:RelatedFile` - Opens the related file in a vertical split. Optionally accepts an argument to open the related file using other methods:
* `:RelatedFile e` opens using `:e[dit]` command in the current window
* `:RelatedFile sp` opens using `:sp[lit]` command in a horizontal split window
* `:RelatedFile vs` opens using `:vs[plit]` command in a vertical split window
2. `:RelatedTests` - Runs the unit tests for the plugin.

Workflow
--------
Expand All @@ -46,6 +49,8 @@ I bind the `:RelatedFile` command to a `<leader>` mapping in my `.vimrc`:
let mapleader = ","
let g:mapleader = ","
map <Leader>rf :RelatedFile<CR>
map <Leader>re :RelatedFile e<CR>
map <Leader>rs :RelatedFile sp<CR>

Inspiration
-----------
Expand Down
22 changes: 18 additions & 4 deletions plugin/related.vim
@@ -1,16 +1,22 @@
" Author: Donald Chea
" Version: 0.1.0
" Version: 0.1.1
" Description: Open a related file based on the path.

" Open the related file in a vsplit
function! s:RelatedFile()
function! s:RelatedFile(open_method)
let related = s:GetRelated(expand('%:p'))
if related ==# ''
echo "Unable to find related file."
return
endif

exec('vsplit ' . related)
exec(s:OpenCommand(a:open_method) . ' ' . related)
endfunction

" Return the command to open the related file
function! s:OpenCommand(cmd)
let open_commands = { 'sp': 'split', 'vs': 'vsplit', 'e': 'edit' }
return get(open_commands, a:cmd, 'vsplit')
endfunction

" Find src or test file based on a file path.
Expand Down Expand Up @@ -65,6 +71,14 @@ function! s:RunTests()
" Proper format, test file, no namespace
call s:Equals('src/Vendor/Class.php', s:GetRelated('tests/Vendor/Tests/ClassTest.php'))

echo "Test s:OpenCommand"
call s:Equals('vsplit', s:OpenCommand('vs'))
call s:Equals('split', s:OpenCommand('sp'))
call s:Equals('edit', s:OpenCommand('e'))
" Defaults to vsplit
call s:Equals('vsplit', s:OpenCommand(''))
call s:Equals('vsplit', s:OpenCommand('does-not-exist'))

echo printf("\nFinished test suite - %d of %d tests passed (%d%%)", s:passes, s:test_number, s:passes*100/s:test_number)
endfunction

Expand All @@ -81,5 +95,5 @@ function! s:Equals(expect, actual)
endif
endfunction

command! RelatedFile :call <SID>RelatedFile()
command! -nargs=? RelatedFile :call <SID>RelatedFile(<q-args>)
command! RelatedTests :call <SID>RunTests()

0 comments on commit 27b453f

Please sign in to comment.