Created
February 25, 2014 23:30
-
-
Save justinpage/9220256 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Prepare a new PHP class | |
function! s:Class() | |
let name = input('Class name: ') | |
let namespace = input('Any Namespace: ') | |
if strlen(namespace) | |
exec 'normal i<?php namespace ' . namespace . '; x27' | |
else | |
exec 'normal i<?php x27' | |
endif | |
" Open class | |
exec 'normal iclass ' . name . ' { }x27Ox27' | |
exec 'normal i public function __construct() { }' | |
endfunction | |
nnoremap <buffer> <leader>1 :call <SID>Class()<CR> | |
" add dependecies to constructor while using proper namespacing | |
function! s:AddDependency() | |
let dependency = input('Var Name: ') | |
let namespace = input('Class Path: ') | |
let segments = split(namespace, '\') | |
let typehint = segments[-1] | |
exec 'normal gg/construct %i, ' . typehint . ' $' . dependency . | |
\ 'x27/} O$this->' . dependency . ' = $' . dependency . | |
\ ';x27==x27?\{ kOprotected $' . dependency . | |
\ '; x27?\{ Ouse ' . namespace . '; x27' | |
" Remove opening comma if there is only one dependency | |
exec 'normal :%s/(, /(/ge ' | |
endfunction | |
nnoremap <buffer> <leader>2 :call <SID>AddDependency()<CR> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I went ahead and addressed an improvement to Jeffrey Way's vim script functions.
The Class function will create the proper tags, optional namespace, and the constructor. It appears that you may not not need the constructor every time you create the class. Because of this, I adjusted the function to insert the constructor while assuming the developer the option of inserting themselves back into the constructor via uppercase "O". If you decide to delete the constructor, then you can visually select and cut it -three, instead of four lines. In both the Class function and the Add Dependency, I assumed that vim will auto indent your insertions for proper indentation. Preventing manual tabbing.
The Add Dependency, however, required a bit of improvement. I notice the majority of vim users were having trouble implementing the second function. Altogether, it was because of three contingencies:
Taking all these into consideration improved the functionality of the first executable in Add Dependency. However, the second executable had one little flaw. What if the user adds a second dependency? Well, technically, the second executable will return an error because no match was found. Sure you can ignore it, but that's cumbersome. To suppress the error, I added an "\e" flag. We don't have to worry about the second executable reporting an error if no match was found.
The .vimrc, contains a problem of organization. Is it best to have these language specific functions inside a general .vimrc file? No. you want to separate these two functions to an ft plugin folder. That is: ".vim/ftplugin/php/laravel.vim".
I also added scoping to both functions in addition to improved buffer specific loading under PHP. Doing so, improves performance and scope --striving away from global.
For proof of prettier format:
http://imgur.com/U7JAcHA
By no means is this a perfect function implementation, but I hope it improves upon an already fantastic idea.