]> git.eshelyaron.com Git - emacs.git/commitdiff
Support PHP 8.4 and more reliable indentation (bug#74525)
authorVincenzo Pupillo <v.pupillo@gmail.com>
Mon, 25 Nov 2024 10:06:02 +0000 (11:06 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 4 Dec 2024 16:59:47 +0000 (17:59 +0100)
Added support for PHP 8.4 property hook. More reliable CSS and
Javascript syntax indentation when there are attributes in
<script> and <style>.

* lisp/progmodes/php-ts-mode.el:
(php-ts-mode--language-source-alist): Switch to the latest php
grammar.
(php-ts-mode--js-css-tag-bol): CSS and Javascript indentation is
now more more reliable in different formatting styles.
(php-ts-mode--test-property-hook-clause-p): New function that
tests property_hook support.
(php-ts-mode--font-lock-settings): Use the new function.
(php-ts-mode--colorize-css-value): The function now behaves
exactly like the one in css-ts-mode.
(php-ts-mode--feature-list): Changed the list to match
css-ts-mode.

(cherry picked from commit 9fdb764898dd4f40f562f8540767a18c827fe7d3)

lisp/progmodes/php-ts-mode.el

index 550c87a1b81eb3fde687bb1b61e01c2879b57140..8a9f2a5ebe0b1ec8433b8d40b19f615bdd87e2c4 100644 (file)
@@ -84,7 +84,7 @@
 
 ;;; Install treesitter language parsers
 (defvar php-ts-mode--language-source-alist
-  '((php . ("https://github.com/tree-sitter/tree-sitter-php" "v0.23.5" "php/src"))
+  '((php . ("https://github.com/tree-sitter/tree-sitter-php" "v0.23.11" "php/src"))
     (phpdoc . ("https://github.com/claytonrcarter/tree-sitter-phpdoc"))
     (html . ("https://github.com/tree-sitter/tree-sitter-html"  "v0.23.0"))
     (javascript . ("https://github.com/tree-sitter/tree-sitter-javascript" "v0.23.0"))
@@ -477,16 +477,20 @@ PARENT is its parent."
               (treesit-node-start parent)
               (line-end-position))))))
 
-(defun php-ts-mode--js-css-tag-bol (node _parent &rest _)
+(defun php-ts-mode--js-css-tag-bol (_node parent &rest _)
   "Find the first non-space characters of html tags <script> or <style>.
 
-If NODE is nil return `line-beginning-position'.  PARENT is ignored.
-NODE is the node to match and PARENT is its parent."
-  (if (null node)
-      (line-beginning-position)
-    (save-excursion
-      (goto-char (treesit-node-start node))
-      (re-search-backward "<script>\\|<style>" nil t))))
+Return `line-beginning-position' when `treesit-node-at' is HTML or PHP.
+Otherwise go to the PARENT and search backward for <script> or <style> tags.
+Should be used only for Javascript or CSS indenting rules.
+NODE, ignored, is the node to match and PARENT is its parent."
+  (let ((lang (treesit-language-at (point))))
+    (if (or (eq lang 'javascript)
+           (eq lang 'css))
+       (save-excursion
+         (goto-char (treesit-node-start parent))
+         (re-search-backward "<script.*>\\|<style.*>" nil t))
+      (line-beginning-position))))
 
 (defun php-ts-mode--parent-eol (_node parent &rest _)
   "Find the last non-space characters of the PARENT of the current NODE.
@@ -840,6 +844,11 @@ characters of the current line."
   (ignore-errors
     (progn (treesit-query-compile 'php "(visibility_modifier (operation))" t) t)))
 
+(defun php-ts-mode--test-property-hook-clause-p ()
+  "Return t if property_hook is a named node, nil otherwise."
+  (ignore-errors
+    (progn (treesit-query-compile 'php "(property_hook)" t) t)))
+
 (defun php-ts-mode--font-lock-settings ()
   "Tree-sitter font-lock settings."
   (treesit-font-lock-rules
@@ -948,6 +957,8 @@ characters of the current line."
       name: (_) @font-lock-type-face)
      (function_definition
       name: (_) @font-lock-function-name-face)
+     ,@(when (php-ts-mode--test-property-hook-clause-p)
+        '((property_hook (name) @font-lock-function-name-face)))
      (method_declaration
       name: (_) @font-lock-function-name-face)
      (method_declaration
@@ -1108,14 +1119,13 @@ For NODE, OVERRIDE, START, and END, see `treesit-font-lock-rules'."
            (string-equal "plain_value" (treesit-node-type node)))
       (let ((color (css--compute-color start (treesit-node-text node t))))
         (when color
-          (treesit-fontify-with-override
-           (treesit-node-start node) (treesit-node-end node)
-           (list 'face
-                 (list :background color
-                       :foreground (readable-foreground-color
-                                    color)
-                       :box '(:line-width -1)))
-           override start end)))
+         (with-silent-modifications
+           (add-text-properties
+            (treesit-node-start node) (treesit-node-end node)
+            (list 'face (list :background color
+                              :foreground (readable-foreground-color
+                                            color)
+                              :box '(:line-width -1)))))))
     (treesit-fontify-with-override
      (treesit-node-start node) (treesit-node-end node)
      'font-lock-variable-name-face
@@ -1372,14 +1382,14 @@ Depends on `c-ts-common-comment-setup'."
      ;; PHPDOC specific
      document
      phpdoc-error)
-    (keyword string type name)
+    (keyword string property type name)
     (;; common
      attribute assignment constant escape-sequence function-scope
      base-clause literal variable-name variable
      ;; Javascript specific
      jsx number pattern string-interpolation)
     (;; common
-     argument bracket delimiter error function-call operator property
+     argument bracket delimiter error function-call operator
      ;; Javascript specific
      function)))