Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Created March 30, 2014 04:44
Show Gist options
  • Save PeterRincker/9867672 to your computer and use it in GitHub Desktop.
Save PeterRincker/9867672 to your computer and use it in GitHub Desktop.
if exists('b:loaded_phpthis') || &cp
finish
endif
let b:loaded_phpthis = 1
if !exists("b:undo_ftplugin")
let b:undo_ftplugin = ""
endif
function! s:synnames(...) abort
if a:0
let [line, col] = [a:1, a:2]
else
let [line, col] = [line('.'), col('.')]
endif
return reverse(map(synstack(line, col), 'synIDattr(v:val,"name")'))
endfunction
function! s:PHPThis()
let syn = join(s:synnames(line("."),col(".")-1), ' ')
if (syn =~? '\<phpstring' && syn !~? 'IdentifierComplex') || syn !~? '\<php' || syn =~? 'comment'
return '.'
endif
let pos = col('.') - 2
let line = getline('.')
let prev_char = line[pos]
let line = strpart(line, 0, pos + 1)
if prev_char == "$"
return "\<c-r>\<c-r>='this->'\<cr>"
elseif prev_char == ')' || prev_char == ']'
return "\<c-r>\<c-r>='->'\<cr>"
elseif match(line, '\k\+$') > -1
return "\<c-r>\<c-r>='->'\<cr>"
elseif match(line, '\$this->$') > -1
return "\<c-w>\<c-w>\<c-r>\<c-r>='...'\<cr>"
elseif prev_char == '.'
return "\<bs>\<c-r>\<c-r>='$this->'\<cr>"
elseif prev_char == '{' && syn =~? 'string' && syn =~? 'IdentifierComplex'
return "\<c-r>\<c-r>='$this->'\<cr>"
elseif prev_char == "'" || prev_char == '"' && syn =~? '\<phpregion'
return "\<c-r>\<c-r>=' => '\<cr>"
endif
return "."
endfunction
inoremap <buffer> <expr> . <SID>PHPThis()
let b:undo_ftplugin = b:undo_ftplugin . "|iunmap <buffer> ."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment