From 430382668698b299040699785ba9ea0279f85a5f Mon Sep 17 00:00:00 2001 From: Vincenzo Pupillo Date: Wed, 5 Jun 2024 15:54:37 +0200 Subject: [PATCH] Add php-ts-mode * etc/NEWS: Mention the new mode. * lisp/progmodes/php-ts-mode.el: New file. --- etc/NEWS | 5 + lisp/progmodes/php-ts-mode.el | 1640 +++++++++++++++++++++++++++++++++ 2 files changed, 1645 insertions(+) create mode 100644 lisp/progmodes/php-ts-mode.el diff --git a/etc/NEWS b/etc/NEWS index 922721f143c..72abedb043a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1926,6 +1926,11 @@ A major mode based on the tree-sitter library for editing Elixir files. *** New major mode 'lua-ts-mode'. A major mode based on the tree-sitter library for editing Lua files. +--- +*** New major mode 'php-ts-mode'. +A major mode based on the tree-sitter library for editing + + ** Minibuffer and Completions +++ diff --git a/lisp/progmodes/php-ts-mode.el b/lisp/progmodes/php-ts-mode.el new file mode 100644 index 00000000000..bb036fa6dad --- /dev/null +++ b/lisp/progmodes/php-ts-mode.el @@ -0,0 +1,1640 @@ +;;; php-ts-mode.el --- Major mode PHP using tree-sitter -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 Free Software Foundation, Inc. + +;; Author: Vincenzo Pupillo +;; Created: Jun 2024 +;; Keywords: PHP language tree-sitter + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: +;; +;; This package provides `php-ts-mode' which is a major mode +;; for editing PHP files with embedded HTML, JavaScript, CSS and phpdoc. +;; Tree Sitter is used to parse each of these languages. +;; +;; This package is compatible and has been tested with the following +;; tree-sitter grammars: +;; * https://github.com/tree-sitter/tree-sitter-php +;; * https://github.com/tree-sitter/tree-sitter-html +;; * https://github.com/tree-sitter/tree-sitter-javascript +;; * https://github.com/tree-sitter/tree-sitter-css +;; * https://github.com/claytonrcarter/tree-sitter-phpdoc +;; +;; Features +;; +;; * Indent +;; * IMenu +;; * Navigation +;; * Which-function +;; * Flymake +;; * Tree-sitter parser installation helper +;; * PHP built-in server support +;; * Shell interaction: execute PHP code in a inferior PHP process + +;;; Code: + +(require 'treesit) +(require 'c-ts-common) ;; For comment indent and filling. +(require 'html-ts-mode) ;; For embed html +(require 'css-mode) ;; for embed css into html +(require 'js) ;; for embed javascript into html +(require 'comint) + +(eval-when-compile + (require 'cl-lib) + (require 'rx) + (require 'subr-x)) + +(declare-function treesit-node-child "treesit.c") +(declare-function treesit-node-child-by-field-name "treesit.c") +(declare-function treesit-node-end "treesit.c") +(declare-function treesit-node-parent "treesit.c") +(declare-function treesit-node-start "treesit.c") +(declare-function treesit-node-string "treesit.c") +(declare-function treesit-node-type "treesit.c") +(declare-function treesit-parser-add-notifier "treesit.c") +(declare-function treesit-parser-buffer "treesit.c") +(declare-function treesit-parser-create "treesit.c") +(declare-function treesit-parser-included-ranges "treesit.c") +(declare-function treesit-parser-list "treesit.c") +(declare-function treesit-parser-language "treesit.c") + +;;; Install treesitter language parsers +(defvar php-ts-mode--language-source-alist + '((php . ("https://github.com/tree-sitter/tree-sitter-php" "v0.22.5")) + (phpdoc . ("https://github.com/claytonrcarter/tree-sitter-phpdoc")) + (html . ("https://github.com/tree-sitter/tree-sitter-html" "v0.20.3")) + (javascript . ("https://github.com/tree-sitter/tree-sitter-javascript" "v0.21.2")) + (css . ("https://github.com/tree-sitter/tree-sitter-css" "v0.21.0"))) + "Treesitter language parsers required by `php-ts-mode'. +You can customize this variable if you want to stick to a specific +commit and/or use different parsers.") + +(defun php-ts-mode-install-parsers () + "Install all the required treesitter parser. +`php-ts-mode--language-source-alist' define which parsers to install." + (interactive) + (let ((treesit-language-source-alist php-ts-mode--language-source-alist)) + (dolist (item php-ts-mode--language-source-alist) + (treesit-install-language-grammar (car item))))) + +;;; Custom variables + +(defgroup php-ts-mode nil + "Major mode for editing PHP files." + :prefix "php-ts-mode-" + :group 'languages) + +(defcustom php-ts-mode-indent-offset 4 + "Number of spaces for each indentation step (default) in `php-ts-mode'." + :tag "PHP indent offset" + :version "30.1" + :type 'integer + :safe 'integerp) + +(defcustom php-ts-mode-js-css-indent-offset html-ts-mode-indent-offset + "JavaScript and CSS indent spaces related to the