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

Questo articolo รจ stato scritto oltre 1 years, il contenuto potrebbe essere datato.

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. Also this is the most read article of my website so for any feedback let me know (the article get updates during the time).

Intelephense 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 (it’s an incomplete example code):

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"
            },
            environment = {
              includePaths = '/home/your-user/.composer/vendor/php-stubs/' -- this line forces the composer path for the stubs in case inteliphense don't find it...
            },
            files = {
                maxSize = 5000000;
            };
        };
    }
});

This code is truncated (or it will be too much long) but you can find here the complete one also with the integration with other plugins.

You have integrated a LSP in your neovim for PHP with WordPress integration.
Neovim supports also PHPActor/PHPStan as LSP, so this is on you.

phpActor installation

There are various ways to install this LSP that is in pure PHP and supports also a lot of stuff for refactoring etc.
I suggest to use it in combination with the other one in this way you get the best from both together.

To install phpActor I suggest to follow the official guide so you can pick the solution you prefer.
Next step is to load it in your nvim as below (there are also other LSP for other languages so you need to install cssls in your machine if you need it as example).

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

In this way I am initializing without parameters the 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. In my config you can find also other settings for other plugins like aerial or trouble.

Also for NeoVim LSP there are various plugins that let you to install automatically in a neovim folder the LSP supported natively.

I am using COQ and I did a plugin for this that integrate WordPress autocompletion for hooks. This plugin autocomplete for you add_action('the hook name here' and is very handy. 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 are here!

Thanks to the PHP-Stubs project for the stubs and Intelephense also if it is not open source sadly, and phpActor.
Neovim of course ๐Ÿ˜€

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

Leave a Reply

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