Skip to content

Instantly share code, notes, and snippets.

@ubermuda
Created March 24, 2009 13:17
Show Gist options
  • Save ubermuda/84077 to your computer and use it in GitHub Desktop.
Save ubermuda/84077 to your computer and use it in GitHub Desktop.
noremap <silent> <F1> :call sf:EditInDirectory(sf:FindCurrentModulePath().'/actions/')<CR>
noremap <silent> <F2> :call sf:EditInDirectory(sf:FindCurrentModulePath().'/templates/')<CR>
noremap <silent> <F11> :call sf:SwitchFileForCurrentApp('config/routing.yml')<CR>
noremap <silent> <F12> :call sf:SwitchFile('config/doctrine/schema.yml')<CR>
" indentation is two espaces
set expandtab
set shiftwidth=2
set tabstop=2
"-------------------------------------------------------------------------------
" Description: Efficient symfony editing with vim
" Copyright: Copyright (C) 2009 Geoffrey Bachelet
" Maintainer: Geoffrey Bachelet
" Version: 1.0
"-------------------------------------------------------------------------------
if exists('symfony_loaded')
" finish
endif
let symfony_loaded = 1
source ~/.vim/symfony/utils.vim
source ~/.vim/symfony/standard.vim
source ~/.vim/symfony/bindings.vim
if ! exists('symfony_executable')
let symfony_executable = 'symfony'
endif
let symfony_current_app = 0
let symfony_current_module = 0
" root detection when opening a new vim seems to work
" only if these two events are bound. not sure why.
autocmd BufWinEnter,BufRead * call sf:InitBuffer()
function! sf:InitBuffer()
call sf:CdIfNotEmpty(sf:FindRoot())
call sf:FindCurrentApp()
endfunction
" opens or close a file
"
" @param string file
function! sf:SwitchFile(file)
if expand('%') == a:file
exec 'q'
else
let l:file = a:file
if match(l:file, '/') != 0
let l:file = sf:FindRoot().'/'.a:file
endif
exec 'split! '.l:file
endif
endfunction
" opens of close a file in the current app
"
" @param string file
function! sf:SwitchFileForCurrentApp(file)
let l:current_app = sf:FindCurrentApp()
if strlen(l:current_app) > 0
let l:filename = 'apps/'.l:current_app.'/'.a:file
call sf:SwitchFile(l:filename)
else
echo 'No current app detected'
endif
endfunction
" looks in a directory for files to edit,
" open it if there's only one file
" or ask for user which file to edit
"
" based on work from [email protected]
"
" @param string path
function! sf:EditInDirectory(path)
let l:files = split(glob(sf:FindRoot().a:path.'/*'))
if len(l:files) == 1
call sf:SwitchFile(l:files[0])
else
let l:index = 0
let l:_files = []
while l:index < len(l:files)
let l:matches = matchlist(l:files[l:index], '\/\([a-zA-Z0-9_.]\+\)$')
call add(l:_files, '['.(l:index + 1).'] '.l:matches[1])
let l:index = l:index + 1
endwhile
let l:index = inputlist(['Select a file:'] + l:_files)
if l:index
call sf:SwitchFile(l:files[index - 1])
endif
endif
endfunction
" Executes a symfony task and echoes the result
" @param string task
function! sf:ExecuteTask(task)
if sf:CdIfNotEmpty(sf:FindRoot())
echo system('./symfony '.a:task)
else
echo 'Not in a symfony project ?'
endif
endfunction
" Returns the current app you're currently in
" or the last app you were in (if you're not
" in an app)
"
" @return string
function! sf:FindCurrentApp()
if sf:CdIfNotEmpty(sf:FindRoot())
let l:matches = matchlist(expand('%'), 'apps\/\(.\{-1,}\)\/.\+')
if len(l:matches) > 1
let g:symfony_current_app = l:matches[1]
endif
return g:symfony_current_app
endif
return ''
endfunction
" returns the current app path
"
" @return string
function sf:FindCurrentAppPath()
return 'apps/'.sf:FindCurrentApp()
endfunction
" Returns the current module you're currently in
" or the last module you were in (if you're not
" in an module)
"
" @return string
function! sf:FindCurrentModule()
if sf:CdIfNotEmpty(sf:FindRoot())
let l:matches = matchlist(expand('%'), 'modules\/\(.\{-1,}\)\/.\+')
if len(l:matches) > 1
let g:symfony_current_module = l:matches[1]
endif
return g:symfony_current_module
endif
return ''
endfunction
" returns the current module path
"
" @return string
function! sf:FindCurrentModulePath()
return sf:FindCurrentAppPath().'/modules/'.sf:FindCurrentModule()
endfunction
" Returns the symfony root for the current file
" @return string
function! sf:FindRoot()
let l:cwd = sf:GetAbsoluteDirname(@%)
let l:symfony_root = findfile(g:symfony_executable, l:cwd.';')
let l:symfony_root = sf:GetAbsoluteDirname(l:symfony_root)
return l:symfony_root
endfunction
"-------------------------------------------------------------------------------
" Utils
"-------------------------------------------------------------------------------
" returns the absolute dirname of a path
" @param string path
" @return string
function! sf:GetAbsoluteDirname(path)
let l:path = a:path
" gets the dirname
if !isdirectory(l:path)
let l:path = strpart(l:path, 0, strridx(l:path, '/'))
endif
" makes it absolute
if match(l:path, '/') != 0
let l:path = getcwd().'/'.l:path
endif
return l:path
endfunction
" cd to the given path if not empty
" @param string path
" @return bool
function! sf:CdIfNotEmpty(path)
if strlen(a:path) != 0
execute 'cd '.a:path
return 1
endif
return 0
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment