diff --git a/.gitignore b/.gitignore index 0d51897..b0a7e33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ *.elc php-ts-mode-autoloads.el +php-ts-mode-pkg.el /.eask /dist +# emacs tmp files +*~ +*# diff --git a/README.md b/README.md index ee2129f..1d2bd8b 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,25 @@ If you haven't installed Tree-sitter yet, please read [How to Get Started with T [How to Get Started with Tree-Sitter - Mastering Emacs]: https://www.masteringemacs.org/article/how-to-get-started-tree-sitter +Package can be installed by running the following command. + +``` +M-x package-vc-install [RET] https://github.com/emacs-php/php-ts-mode +``` + +### Configuration + +Example configuration that you can put in your `.emacs` file + +``` +;; Enable variables highlighting +(customize-set-variable 'treesit-font-lock-level 4) + +(add-hook 'php-ts-mode-hook (lambda () + ;; Use spaces for indent + (setq-local indent-tabs-mode nil))) +``` + ### Grammer installation If you don't already have `php-ts-mode` installed, please evaluate the Lisp code below. @@ -36,7 +55,7 @@ In `php-ts-mode`, syntax elements are classified as follows. * **Level 1**: `comment` `definition` `preprocessor` * **Level 2**: `keyword` `string` `type` * **Level 3**: `function` `constant` `label` - * **Level 4**: `bracket` `delimiter` `operator` `variables` + * **Level 4**: `bracket` `delimiter` `operator` `variables` `this` By default, up to **Level 3** will be highlighted. diff --git a/php-ts-mode.el b/php-ts-mode.el index 99028ab..35016b8 100644 --- a/php-ts-mode.el +++ b/php-ts-mode.el @@ -77,6 +77,7 @@ ;; "compound_statement" contains the body of many statements. ;; For example function_definition, foreach_statement, etc. ((parent-is "compound_statement") parent-bol ,offset) + ((parent-is "method_declaration") parent-bol 0) ((parent-is "array_creation_expression") parent-bol ,offset) ((parent-is "base_clause") parent-bol ,offset) ((parent-is "class_interface_clause") parent-bol ,offset) @@ -86,6 +87,8 @@ ((parent-is "binary_expression") parent-bol, 0) ((parent-is "switch_block") parent-bol ,offset) ((parent-is "case_statement") parent-bol ,offset) + ((parent-is "default_statement") parent-bol ,offset) + ((parent-is "match_block") parent-bol ,offset) ((parent-is "assignment_expression") parent-bol ,offset) ((parent-is "return_statement") parent-bol ,offset)))) "Tree-sitter indent rules.") @@ -96,12 +99,16 @@ "elseif" "enddeclare" "endforeach" "endif" "endswitch" "endwhile" "enum" "extends" "final" "finally" "for" "foreach" "fn" "function" "global" "if" "implements" "include_once" - "include" "insteadof" "interface" "namespace" "new" + "include" "instanceof" "insteadof" "interface" "match" "namespace" "new" "private" "protected" "public" "readonly" "require_once" "require" "return" "static" "switch" "throw" "trait" "try" "use" "while" "yield") "PHP keywords for tree-sitter font-locking.") +(defvar php-ts-mode--built-in-functions + '("die" "empty" "isset") + "PHP built-in functions for tree-sitter font-locking.") + (defvar php-ts-mode--operators '("!=" "!==" "%" "%=" "&" "&&" "&=" "*" "**" "*=" "+" "++" "+=" "," "-" "-" "--" "-=" "->" "." @@ -145,9 +152,10 @@ see https://www.php.net/manual/language.constants.predefined.php") (named_type (name) @php-type) (named_type (qualified_name) @php-type) (namespace_use_clause) - (namespace_name (name))] + (namespace_name (name)) + (optional_type "?" @php-type)] @php-type - (class_interface_clause (name) @php-class) + (class_interface_clause [(name) (qualified_name)] @php-class) (class_constant_access_expression (name) @php-keyword (:match ,(rx bos "class" eos) @@ -157,16 +165,20 @@ see https://www.php.net/manual/language.constants.predefined.php") (:match ,(rx bos (? "_") (in "A-Z") (+ (in "0-9A-Z_")) eos) @php-constant)) (class_constant_access_expression - (name) @php-class) + [(name) (qualified_name)] @php-class) [(boolean) (null)] @php-constant [(integer) (float)] - @font-lock-number-face) + @font-lock-number-face + (binary_expression + operator: "instanceof" + right: [(name) (qualified_name)] @php-class)) :language 'php :feature 'definition + :override t `((class_declaration name: (name) @php-class) (interface_declaration @@ -176,10 +188,13 @@ see https://www.php.net/manual/language.constants.predefined.php") (trait_declaration name: (name) @php-class) (enum_case - name: (name) @php-class)) + name: (name) @php-class) + (base_clause [(name) (qualified_name)] @php-class) + (use_declaration [(name) (qualified_name)] @php-class)) :language 'php :feature 'function + :override t `((array_creation_expression "array" @php-builtin) (list_literal "list" @php-builtin) (method_declaration @@ -187,15 +202,14 @@ see https://www.php.net/manual/language.constants.predefined.php") (function_call_expression function: [(qualified_name (name)) (name)] @php-function-call) (scoped_call_expression - scope: (name) @php-class) - (scoped_call_expression + scope: [(name) (qualified_name)] @php-class name: (name) @php-static-method-call) + (scoped_property_access_expression + scope: [(name) (qualified_name)] @php-class) (member_call_expression name: (name) @php-method-call) - (object_creation_expression (name) @php-class) - (attribute (name) @php-class) - (attribute (qualified_name) @php-class) - + (object_creation_expression [(name) (qualified_name)] @php-class) + (attribute [(name) (qualified_name)] @php-class) (function_definition name: (name) @php-function-name)) @@ -207,15 +221,17 @@ see https://www.php.net/manual/language.constants.predefined.php") ;; ((name) @constructor ;; (:match ,(rx-to-string '(: bos (in "A-Z"))))) - - ;; (variable_name (name) @php-$this - ;; (:match ,(rx bos "this" eos) - ;; @php-$this)) (member_access_expression name: (name) @php-property-name) ;;(variable_name (name) @font-lock-variable-name-face) (variable_name (name) @php-variable-name) (variable_name "$" @php-variable-sigil)) + :language 'php + :feature 'this + :override t + `((variable_name "$" @php-this-sigil (name) @php-this + (:match ,(rx bos "this" eos) @php-this))) + :language 'php :feature 'comment `(((comment) @font-lock-doc-face @@ -226,7 +242,7 @@ see https://www.php.net/manual/language.constants.predefined.php") :language 'php :feature 'string `([(string) - (string_value) + (string_content) (encapsed_string) (heredoc) (heredoc_body) @@ -240,10 +256,18 @@ see https://www.php.net/manual/language.constants.predefined.php") :language 'php :feature 'keyword + :override t `([,@php-ts-mode--keywords] @php-keyword (print_intrinsic "print" @php-keyword) (goto_statement "goto" @php-keyword) - (yield_expression "from" @php-keyword)) + (yield_expression "from" @php-keyword) + (function_call_expression + function: (name) @php-keyword + (:match ,(rx-to-string + `(seq bol + (or ,@php-ts-mode--built-in-functions) + eol)) @php-keyword)) + (unset_statement "unset" @php-keyword)) :language 'php :feature 'label @@ -347,7 +371,7 @@ Currently there are `php-mode' and `php-ts-mode'." '((comment definition preprocessor) (keyword string type) (function constant label) - (bracket delimiter operator variables))) + (bracket delimiter operator variables this))) ;; Imenu. (setq-local treesit-simple-imenu-settings diff --git a/tests/php-ts-mode-resources/face.erts b/tests/php-ts-mode-resources/face.erts new file mode 100644 index 0000000..329d0e2 --- /dev/null +++ b/tests/php-ts-mode-resources/face.erts @@ -0,0 +1,224 @@ +Code: + (lambda () + (php-ts-mode) + (customize-set-variable 'treesit-font-lock-level 4) + (let ((face (symbol-name (face-at-point)))) + (erase-buffer) + (insert face))) + +Name: Open php tag +Point-Char: | +No-After-Newline: t + +=-= + 1 +}; +=-= + 1 +}; +=-=-= diff --git a/tests/php-ts-mode-tests.el b/tests/php-ts-mode-tests.el index 3ae8a9d..a01effb 100644 --- a/tests/php-ts-mode-tests.el +++ b/tests/php-ts-mode-tests.el @@ -35,7 +35,11 @@ ; FIXME: implement basic movements (ert-deftest php-ts-mode-test-movement () (skip-unless (treesit-ready-p 'php)) -(ert-test-erts-file (ert-resource-file "movement.erts"))) + (ert-test-erts-file (ert-resource-file "movement.erts"))) + +(ert-deftest php-ts-mode-test-face () + (skip-unless (treesit-ready-p 'php)) + (ert-test-erts-file (ert-resource-file "face.erts"))) (provide 'php-ts-mode-tests) ;;; php-ts-mode-tests.el ends here