Skip to content

Instantly share code, notes, and snippets.

@justinpage
Created February 25, 2014 23:30
Show Gist options
  • Save justinpage/9220256 to your computer and use it in GitHub Desktop.
Save justinpage/9220256 to your computer and use it in GitHub Desktop.
" 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<?phpx27'
endif
" Open class
exec 'normal iclass ' . name . ' {}x27Ox27'
exec 'normal ipublic 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>
@justinpage
Copy link
Author

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:

  1. Physically copying the file to clipboard, may not be interpreted equally in vim as it original should. Especially keyboard inputs, such as, ^M and ^{
  2. When finding opening/closing brackets or parenthesis, some users may have "\v" appended to there regular expression search. That is, referring to certain characters as possessing meanings. Therefore, when searching backwards for brackets, we may need to escape it: "{".
  3. Vim has a max width until wrapping. You need to prepend your script, on each line, with a slash "". This will append the long executable without making a mess of your script file.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment