Extract PHP support into an extension (#9966)

This PR extracts PHP support into an extension and removes the built-in
PHP support from Zed.

There's a small workaround necessary in order for us to provide the
`language_ids` on the `LspAdapter` that are needed for the language
server to run properly. Eventually we'll want to build this into the
extension API, but for now we're just hard-coding it on the host side.

Release Notes:

- Removed built-in support for PHP, in favor of making it available as
an extension. The PHP extension will be suggested for download when you
open a `.php` file.
This commit is contained in:
Marshall Bowers 2024-03-29 14:51:54 -04:00 committed by GitHub
parent 764e256755
commit e252f90e30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 143 additions and 157 deletions

16
extensions/php/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "zed_php"
version = "0.0.1"
edition = "2021"
publish = false
license = "Apache-2.0"
[lints]
workspace = true
[lib]
path = "src/php.rs"
crate-type = ["cdylib"]
[dependencies]
zed_extension_api = "0.0.4"

View file

@ -0,0 +1 @@
../../LICENSE-APACHE

View file

@ -0,0 +1,16 @@
id = "php"
name = "PHP"
description = "PHP support."
version = "0.0.1"
schema_version = 1
authors = ["Piotr Osiewicz <piotr@zed.dev>"]
repository = "https://github.com/zed-industries/zed"
[language_servers.intelephense]
name = "Intelephense"
language = "PHP"
[grammars.php]
repository = "https://github.com/tree-sitter/tree-sitter-php"
commit = "8ab93274065cbaf529ea15c24360cfa3348ec9e4"
path = "php"

View file

@ -0,0 +1,18 @@
name = "PHP"
grammar = "php"
path_suffixes = ["php"]
first_line_pattern = '^#!.*php'
line_comments = ["// ", "# "]
autoclose_before = ";:.,=}])>"
brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "[", end = "]", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = true },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
{ start = "'", end = "'", close = true, newline = false, not_in = ["string"] },
]
collapsed_placeholder = "/* ... */"
word_characters = ["$"]
scope_opt_in_language_servers = ["tailwindcss-language-server"]
prettier_parser_name = "php"
prettier_plugins = ["@prettier/plugin-php"]

View file

@ -0,0 +1,36 @@
(
(comment)* @context
.
[
(function_definition
"function" @name
name: (_) @name
body: (_
"{" @keep
"}" @keep) @collapse
)
(trait_declaration
"trait" @name
name: (_) @name)
(method_declaration
"function" @name
name: (_) @name
body: (_
"{" @keep
"}" @keep) @collapse
)
(interface_declaration
"interface" @name
name: (_) @name
)
(enum_declaration
"enum" @name
name: (_) @name
)
] @item
)

View file

@ -0,0 +1,124 @@
(php_tag) @tag
"?>" @tag
; Types
(primitive_type) @type.builtin
(cast_type) @type.builtin
(named_type (name) @type) @type
(named_type (qualified_name) @type) @type
; Functions
(array_creation_expression "array" @function.builtin)
(list_literal "list" @function.builtin)
(method_declaration
name: (name) @function.method)
(function_call_expression
function: [(qualified_name (name)) (name)] @function)
(scoped_call_expression
name: (name) @function)
(member_call_expression
name: (name) @function.method)
(function_definition
name: (name) @function)
; Member
(property_element
(variable_name) @property)
(member_access_expression
name: (variable_name (name)) @property)
(member_access_expression
name: (name) @property)
; Variables
(relative_scope) @variable.builtin
((name) @constant
(#match? @constant "^_?[A-Z][A-Z\\d_]+$"))
((name) @constant.builtin
(#match? @constant.builtin "^__[A-Z][A-Z\d_]+__$"))
((name) @constructor
(#match? @constructor "^[A-Z]"))
((name) @variable.builtin
(#eq? @variable.builtin "this"))
(variable_name) @variable
; Basic tokens
[
(string)
(string_value)
(encapsed_string)
(heredoc)
(heredoc_body)
(nowdoc_body)
] @string
(boolean) @constant.builtin
(null) @constant.builtin
(integer) @number
(float) @number
(comment) @comment
"$" @operator
; Keywords
"abstract" @keyword
"as" @keyword
"break" @keyword
"case" @keyword
"catch" @keyword
"class" @keyword
"const" @keyword
"continue" @keyword
"declare" @keyword
"default" @keyword
"do" @keyword
"echo" @keyword
"else" @keyword
"elseif" @keyword
"enum" @keyword
"enddeclare" @keyword
"endforeach" @keyword
"endif" @keyword
"endswitch" @keyword
"endwhile" @keyword
"extends" @keyword
"final" @keyword
"readonly" @keyword
"finally" @keyword
"foreach" @keyword
"function" @keyword
"global" @keyword
"if" @keyword
"implements" @keyword
"include_once" @keyword
"include" @keyword
"insteadof" @keyword
"interface" @keyword
"namespace" @keyword
"new" @keyword
"private" @keyword
"protected" @keyword
"public" @keyword
"require_once" @keyword
"require" @keyword
"return" @keyword
"static" @keyword
"switch" @keyword
"throw" @keyword
"trait" @keyword
"try" @keyword
"use" @keyword
"while" @keyword

View file

@ -0,0 +1,3 @@
((text) @content
(#set! "language" "html")
(#set! "combined"))

View file

@ -0,0 +1,29 @@
(class_declaration
"class" @context
name: (name) @name
) @item
(function_definition
"function" @context
name: (_) @name
) @item
(method_declaration
"function" @context
name: (_) @name
) @item
(interface_declaration
"interface" @context
name: (_) @name
) @item
(enum_declaration
"enum" @context
name: (_) @name
) @item
(trait_declaration
"trait" @context
name: (_) @name
) @item

View file

@ -0,0 +1,40 @@
(namespace_definition
name: (namespace_name) @name) @module
(interface_declaration
name: (name) @name) @definition.interface
(trait_declaration
name: (name) @name) @definition.interface
(class_declaration
name: (name) @name) @definition.class
(class_interface_clause [(name) (qualified_name)] @name) @impl
(property_declaration
(property_element (variable_name (name) @name))) @definition.field
(function_definition
name: (name) @name) @definition.function
(method_declaration
name: (name) @name) @definition.function
(object_creation_expression
[
(qualified_name (name) @name)
(variable_name (name) @name)
]) @reference.class
(function_call_expression
function: [
(qualified_name (name) @name)
(variable_name (name)) @name
]) @reference.call
(scoped_call_expression
name: (name) @name) @reference.call
(member_call_expression
name: (name) @name) @reference.call

85
extensions/php/src/php.rs Normal file
View file

@ -0,0 +1,85 @@
use std::{env, fs};
use zed_extension_api::{self as zed, Result};
const SERVER_PATH: &str = "node_modules/intelephense/lib/intelephense.js";
const PACKAGE_NAME: &str = "intelephense";
struct PhpExtension {
did_find_server: bool,
}
impl PhpExtension {
fn server_exists(&self) -> bool {
fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
}
fn server_script_path(&mut self, config: zed::LanguageServerConfig) -> Result<String> {
let server_exists = self.server_exists();
if self.did_find_server && server_exists {
return Ok(SERVER_PATH.to_string());
}
zed::set_language_server_installation_status(
&config.name,
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
);
let version = zed::npm_package_latest_version(PACKAGE_NAME)?;
if !server_exists
|| zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
{
zed::set_language_server_installation_status(
&config.name,
&zed::LanguageServerInstallationStatus::Downloading,
);
let result = zed::npm_install_package(PACKAGE_NAME, &version);
match result {
Ok(()) => {
if !self.server_exists() {
Err(format!(
"installed package '{PACKAGE_NAME}' did not contain expected path '{SERVER_PATH}'",
))?;
}
}
Err(error) => {
if !self.server_exists() {
Err(error)?;
}
}
}
}
self.did_find_server = true;
Ok(SERVER_PATH.to_string())
}
}
impl zed::Extension for PhpExtension {
fn new() -> Self {
Self {
did_find_server: false,
}
}
fn language_server_command(
&mut self,
config: zed::LanguageServerConfig,
_worktree: &zed::Worktree,
) -> Result<zed::Command> {
let server_path = self.server_script_path(config)?;
Ok(zed::Command {
command: zed::node_binary_path()?,
args: vec![
env::current_dir()
.unwrap()
.join(&server_path)
.to_string_lossy()
.to_string(),
"--stdio".to_string(),
],
env: Default::default(),
})
}
}
zed::register_extension!(PhpExtension);