PHP support for lsp-mode using felixfbecker/php-language-server.
php-lsp
until this issue is resolved.
Clone this repository, emacs-lsp/lsp-mode, and emacs-lsp/lsp-ui to suitable paths. Then add the following settings to your Emacs configuration.
(add-to-list 'load-path "<path to lsp-php>")
(add-to-list 'load-path "<path to lsp-mode>")
(add-to-list 'load-path "<path to lsp-ui>")
(require 'lsp-mode)
(require 'lsp-ui)
(add-hook 'lsp-mode-hook 'lsp-ui-mode)
(require 'lsp-php)
(add-hook 'php-mode-hook #'lsp-mode)
You will need to install felixfbecker/php-language-server. Package defaults assume it is either installed in ~/.emacs.d/php-language-server/vendor/felixfbecker/language-server
or installed globally via Composer.
Create a directory for php-language-server in .emacs.d/php-language-server
. Create a composer.json
file in it, with the following contents:
{
"minimum-stability": "dev",
"prefer-stable": true
}
Then, in the directory, run the following commands:
composer require felixfbecker/language-server
composer run-script --working-dir=vendor/felixfbecker/language-server parse-stubs
Before installing php-language-server, make sure your ~/.config/composer/composer.json
includes the lines below. The settings apply to all globally installed Composer packages, so proceed with caution. If you do not want to edit your global Composer configuration, see the section for local installation below.
{
"minimum-stability": "dev",
"prefer-stable": true
}
After editing your composer.json
, you can install felixfbecker/php-language-server.
The following instructions have been adapted from the installation section of php-language-server
composer global require felixfbecker/language-server
composer global run-script --working-dir=vendor/felixfbecker/language-server parse-stubs
By default, lsp-php
determines the workspace root by the following detectors (in order.)
lsp-php-root-composer-json
: Find a directory that contains acomposer.json
, but is not two levels undervendor
. I.e. for the path/your/project/vendor/felixfbecker/language-server/src/
this would return/your/project
, assuming it contains acomposer.json
.lsp-php-root-projectile
: Use the current Projectile project as the root directory.lsp-php-root-vcs
: Use the current version control system root as the project.".dir-locals.el"
: Use the nearest parent directory (or self) containing a.dir-locals.el
file.".project"
: Use the nearest parent directory (or self) containing a.project
file."index.php"
: Use the nearest parent directory (or self) containing anindex.php
file."robots.txt"
: Use the nearest parent directory (or self) containing arobots.txt
file.- Default to
default-directory
.
The order of the detectors is configurable. You can also specify additional files to look for in project directories.
You can configure lsp-php
with the following customization options:
Option | Description |
---|---|
lsp-php-server-install-dir |
Directory of a Composer project requiring felixfbecker/language-server . Defaults to ~/.emacs.d/php-language-server . If it is not accessible, defaults to ~/.config/composer (for globally required php-language-server). |
lsp-php-language-server-command |
Command to run php-language-server-with. Separate arguments Should be separate entries in the list. Defaults to (list "php" (expand-file-name "vendor/bin/php-language-server.php" lsp-php-server-install-dir)) |
lsp-php-show-file-parse-notifications |
If nil , hide the Parsing file:///var/www/index.php and Restored monolog/monolog:1.23.0 from cache messages. Defaults to t . |
lsp-php-workspace-root-detectors |
List of strings naming dominating, or the special symbols (not strings) lsp-php-root-vcs , lsp-php-root-projectile , and lsp-php-root-composer-json . The detectors are evaluated in order, and the first one with a match will determine the workspace root. Defaults to (quote (lsp-php-root-composer-json lsp-php-root-projectile lsp-php-root-vcs ".dir-locals.el" ".project" "index.php" "robots.txt")) |
(custom-set-variables
;; Run php-language server with a different php interpreter and from a different location.
'(lsp-php-language-server-command (quote ("/usr/local/bin/php7" "/opt/php-language-server/bin/php-language-server.php")))
;; Hide noisy messages when opening a large project
'(lsp-php-show-file-parse-notifications nil)
;; Composer.json detection after Projectile.
'(lsp-php-workspace-root-detectors (quote (lsp-php-root-projectile lsp-php-root-composer-json "index.php" "robots.txt")))
)
This package is licensed under the GPLv3 license. For more information, see LICENSE
.