NeoVim LSP with Intelephense for PHP and WordPress (and others)

A lot of people got troubles on configuring Intelephense with NeoVim but also for WordPress.

I will show you how to achieve everything you need.

Installation

First of all you need to download Intelephense from NPM.

Now you need to install NeoVim 0.5 or superior with the plugin neovim/nvim-lspconfig. Without this plugin you are not able to use the feature as it is embedded but some APIs are exposed from this plugin.
To install a NeoVim plugin there are various ways so I won’t talk about that but my config files will be available at the end of the post.

Next step is to download some php stubs. As WordPress is not PSR-4 and not all the plugins/themes are compatible with this technology the language server cannot detect a lot of stuff. So stubs were invented to create php files that include the documentation and just the function or method mention but they are empty in this way this tools can parse them like as they were part of the namespace as example.

composer global require php-stubs/wordpress-globals php-stubs/wordpress-stubs php-stubs/woocommerce-stubs php-stubs/acf-pro-stubs wpsyntex/polylang-stubs php-stubs/genesis-stubs php-stubs/wp-cli-stubs

This command will install globally in your machine some stubs that you can remove based on your needs. The funny part is that of wp-cli-stubs and genesis-stubs I am the author 😀

My NeoVim-Qt with LSP running

Configure

Now you need Lua as you need to code a bit to specify the settings for NeoVim. A good guide about it it’s there.
This is an extract of my settings:

local nvim_lsp = require'lspconfig'

nvim_lsp.intelephense.setup({
    settings = {
        intelephense = {
            stubs = { 
                "bcmath",
                "bz2",
                "calendar",
                "Core",
                "curl",
                ...
                "zip",
                "zlib",
                "wordpress",
                "woocommerce",
                "acf-pro",
                "wordpress-globals",
                "wp-cli",
                "genesis",
                "polylang"
            },
            files = {
                maxSize = 5000000;
            };
        };
    }
});

This code is truncated or it will be too much long but you can find everything also with the integration with other plugins.

You have integrated a LSP in your neovim for PHP with WordPress integration.
Neovim supports also PHPActor as LSP but I am still testing it as it was introduced recently. I have also integrated Psalm in this way as it is not official:

vim.api.nvim_command([[
    au User lsp_setup call lsp#register_server({
     \ 'name': 'psalm-language-server',
     \ 'cmd': '/home/mte90/.composer/vendor/bin/psalm-language-server',
     \ 'whitelist': ['php'],
     \ })                
]]);

Probably this code can be written as pure Lua but right now I don’t care.

Also for NeoVim LSP there are various plugins that let you to install automatically in a neovim folder the LSP supported natively but I prefer global solution so I can integrate also in other editor like Kate that I use too.

nvim_lsp.phpactor.setup{}
nvim_lsp.cssls.setup{}
nvim_lsp.html.setup{}
nvim_lsp.bashls.setup{}

As example in this way I am initializing without parameters sole LSP but if you look in my repository’s code you can see also other stuff.

Now basically the LSP setup as some events or callback and the most used by some plugin to get executed is the on_attach.
I am using the ray-x/lsp_signature.nvim plugin to that event to add a tiny modal with information of the method where the cursor is.

Also there is some code in the same file to integrate better with another that is hrsh7th/nvim-compe. This plugin adds the support to snippets for autocomplete but also to other sources.
My plan in fact is to refactor a Deoplete plugin (another autocomplete plugin that I am not using anymore) that I wrote to add support for the action/filters. I opened a ticket to get help as I am not so skilled in Lua.
I have also the SirVer/ultisnips (supported by compe) with the plugin of the sniphpets/sniphpets to get everything I need.

EDIT: I am using COQ now and I did a plugin for this that integrate WordPress autocompletion for hooks. Also COQ has snippet support so you can do them on your own.

Anyway it is just matter of trying until works and create the best settings with the plugins that fit your needs.

Extra

This is my Kate LSP settings.json

{
    "servers": {
  "php": {
    "command": "intelephense --stdio"
        	},
  "bash": {
    "command": "bash-language-server start"
        	},
  "css": {
    "command": "vscode-css-language-server --stdio"
  },
  "css": {
    "command": "vscode-css-language-server --stdio"
  },
  "html": {
    "command": "vscode-css-language-server --stdio"
  }
    }
}

My dotfiles also include a way to run Nvim in a way with less plugins that i called WordPress mode that I use when I have to contribute to WP and I require less stuff that can create troubles.

Thanks to the PHP-Stubs project for the stubs and Intelephense also if it is not open source sadly.
Neovim of course 😀

Liked it? Take a second to support Mte90 on Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *

NeoVim LSP with Intelephense for PHP and WordPress (and others)

time to read: 3 min
0