Vim: PHP Auto-Completion Mit Intelligenter Tab-Taste

Aktuell habe ich mir eine Vim-Config fuer das Arbeiten mit PHP zusammengestellt:

https://github.com/phux/phuxvim

Gesondert moechte ich dir hier die Autocompletion-Konfiguration vorstellen, weil es mich einiges Suchen gekostet hat, um das perfekte Setup zu finden

PHP Completion: Intelligente Tab-Taste

Mit folgender Funktion in deiner .vimrc triggerst du per Tab

  • am Anfang der Zeile (falls keine Buchstaben vor dem Cursor sind) Tabs/Whitespaces
  • falls ein normales Wort vor dem Cursor ist die Word-Completion
  • falls ein angefangener Pfad vor dem Cursor ist die Pfad-Completion
  • und falls ein -> vor dem Cursor ist die PHP-Completion
function! Smart_TabComplete()
  let line = getline('.') " current line

  let substr = strpart(line, -1, col('.')+1) " from the start of the current
" line to one character right
" of the cursor
  let substr = matchstr(substr, "[^ \t]*$") " word till cursor
  if (strlen(substr)==0) " nothing to match on empty string
    return "\tab"
  endif
  let has_period = match(substr, '\.') != -1 " position of period, if any
  let has_gt = match(substr, '\>') != -1 " position of slash, if any
  if ( has_gt )
    return "\<C-X>\<C-O>" " php matching
  elseif (!has_period && !has_slash)
    return "\<C-X>\<C-P>" " existing text matching
  elseif ( has_slash )
    return "\<C-X>\<C-F>" " file matching
  else
    return "\<C-X>\<C-O>" " plugin matching
  endif
endfunction
inoremap <tab> <c-r>=Smart_TabComplete()<CR>

PHP Omnicompletion aktivieren

Stelle sicher, dass du durch folgende Zeile in der .vimrc die PHP-Omnicompletion aktiviert hast:

autocmd FileType php set omnifunc=phpcomplete#CompletePHP

phpcomplete.vim

phpcomplete.vim ist ein Plugin, dass die Completion in Vim um ‚in-file-checking‘ von Objekten erweitert, auch wenn du noch kein tag-file hast, das die Klasse des Objekts indiziert hat.

D.h., der Workaround mit einem /* @var $yourvar YourClass */ ueber der Variablen ist damit hinfaellig :)

Die weiteren wirklich guten Features kannst du auf der Pluginwebsite nachlesen.

 

Fazit

Mit wenig Aufwand (~60 Sekunden?) ein komfortables Autocompletion-System fuer PHP eingerichtet – i like!

Werbung

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden /  Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s