Skip to content
Permalink
Browse files

Hello, world!

  • Loading branch information
arp242 committed Oct 25, 2019
0 parents commit 2e46f148d6a2f5e6ec6644029670bf2e29f026fb
Showing with 108 additions and 0 deletions.
  1. +21 −0 LICENSE
  2. +47 −0 README.markdown
  3. +40 −0 autoload/switchy.vim
21 LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2019 Martin Tournoij

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising
from, out of or in connection with the software or the use or other dealings
in the software.
@@ -0,0 +1,47 @@
Switch to related files.

Usage: map `switchy#do()` to something, for example:

nnoremap <Leader>a :call switchy#do('edit', 'buf')<CR>

command! Switch call switchy#do('split', 'sbuf')

The first function argument is the command to use when editing a file (e.g.
`edit`, `split`, `tabedit`, `sbuf`, etc.), the second argument is the command to
use when the buffer is already loaded. This is useful to switch to an existing
split/tab instead of opening a new one.

---

Supported filetypes:

- Go: `a.go` -> `a_test.go`

Yeah, it's early days for this plugin. More filetypes soon™

---

Survey of existing methods:

- [a.vim](https://www.vim.org/scripts/script.php?script_id=31)
Too many commands, only supports C/C++ out of the box.

- [FSwitch](https://www.vim.org/scripts/script.php?script_id=2590)
Too many commands, only supports C/C++ out of the box.

- [vim-altr](https://github.com/kana/vim-altr)
Doesn't create file if they don't exist, don't like the code.

- [alternate-lite](https://github.com/LucHermitte/alternate-lite)
Needs lh-vim-lib, too many commands.

- [unimpaired.vim](https://github.com/tpope/vim-unimpaired)
Kinda works, not really intended for this and can't create new file easily.

- Manual:

:e %<.c
nnoremap <F4> :e %:p:s,.h$,.X123X,:s,.cpp$,.h,:s,.X123X$,.cpp,<CR>

It's okay, but not as fast as e.g. `<Leader>a`, also not flexible to multiple
filetypes etc.
@@ -0,0 +1,40 @@
fun! switchy#do(cmd, cmd_loaded) abort
let l:Fun = get(s:map, expand('%:e'), v:null)
if l:Fun is# v:null
echoerr 'Nothing defined'
return
endif

let l:cmd = (empty(a:cmd) ? 'edit' : a:cmd)
let l:cmd_loaded = (empty(a:cmd_loaded) ? 'sbuf' : a:cmd_loaded)
let l:file = call(l:Fun, [])
exe printf(':%s %s',
\ (bufloaded(l:file) ? l:cmd_loaded : l:cmd),
\ l:file)
endfun

" Report if s begins with prefix.
fun! switchy#has_prefix(s, prefix) abort
return a:s[:len(a:prefix) - 1] is# a:prefix
endfun

" Report if s ends with suffix.
fun! switchy#has_suffix(s, suffix) abort
return a:s[-len(a:suffix):] is# a:suffix
endfun

" TODO: ideally I'd like this to be in ftplugin/go.vim, but not sure how to
" organize that elegantly.
"
" I also want to make it easy to have people define their own callbacks, because
" some languages have multiple testing frameworks etc. and don't want to cycle
" through all of it.
fun! s:go() abort
return switchy#has_suffix(bufname(''), '_test.go')
\ ? bufname('')[:-9] . '.go'
\ : bufname('')[:-4] . '_test.go'
endfun

let s:map = {

This comment has been minimized.

Copy link
@benknoble

benknoble Oct 25, 2019

I wonder if this could be made a public map (switch#ft_map or something): then ftplugins could write their own functions and add to the dict. Since switchy#has_suffix is already a "public" interface, it could be used directly. If other functions crop up repeatedly, switchy could provide a library of helpful functions?

This comment has been minimized.

Copy link
@arp242

arp242 Oct 25, 2019

Author Owner

I originally experimented with this in ftplugin/go.vim:

fun! s:go() abort
	return switchy#has_suffix(bufname(''), '_test.go')
            \ ? bufname('')[:-9] . '.go'
            \ : bufname('')[:-4] . '_test.go'
endfun

call switchy#map(function('s:go'))

But I wasn't sure how to then map/bind it to be user-callable since you can't use that in :map or :command. I guess just adding to s:map would work, not sure why I didn't think of that 🤔 I mostly just wanted to get this out there so I can finish gopher.vim 1.0 (which has full feature parity with vim-go, and no existing plugins did what I wanted for this particular feature IMHO).

I'll play ayound some more later; cheers!

switchy#has_suffix is already a "public" interface, it could be used directly. If other functions crop up repeatedly, switchy could provide a library of helpful functions?

Yeah, that was the idea with making it public.

This comment has been minimized.

Copy link
@arp242

arp242 Oct 26, 2019

Author Owner

Alright, changed it in 7dc11d3; thanks!

\ 'go': function('s:go'),
\ }

0 comments on commit 2e46f14

Please sign in to comment.
You can’t perform that action at this time.