~theo/tree-sitter-modes

ce873814f792050f5d4c74a98c6ce7dd213e0c97 — Theodor Thornhill a month ago 5754b61
Edits
2 files changed, 56 insertions(+), 46 deletions(-)

M go-mode.el
M typescript-mode.el
M go-mode.el => go-mode.el +18 -19
@@ 46,33 46,32 @@
    table)
  "Syntax table for `go-mode'.")

(defun go-backward-up-list ()
  (lambda (node parent bol &rest _)
    (save-excursion
      (backward-up-list 1 nil t)
      (goto-char
       (treesit-node-start
        (treesit-node-at (point) (point) 'go)))
      (back-to-indentation)
      (treesit-node-start
       (treesit-node-at (point) (point) 'go)))))

(defvar go-indent-rules
  `((go
     (no-node (go-backward-up-list) 4)
     ((node-is "}") parent-bol 0)
     ((node-is ")") parent-bol 0)
     ((node-is "]") parent-bol 0)
     ((parent-is "source_file") parent-bol 0)
     ((parent-is "short_var_declaration") parent-bol 4)
     ((parent-is "parameter_list") parent-bol 4)
     ((parent-is "assignment_statement") parent-bol 4)

     ((match nil "selector_expression" "field") parent-bol 4)
     ((match nil "binary_expression" "right") parent-bol 4)
     ((match nil "assignment_statement" "right") parent-bol 4)
     ((parent-is "literal_value") parent-bol 4)
     ((parent-is "import_declaration") parent-bol 4)
     ((parent-is "import_spec_list") parent-bol 4)
     ((parent-is "field_declaration_list") parent-bol 4)
     ((parent-is "selector_expression") parent-bol 4)
     ((parent-is "argument_list") parent-bol 4)
     ((parent-is "literal_value") parent-bol 4)
     ((parent-is "const_declaration") parent-bol 4)
     ((parent-is "var_declaration") parent-bol 4)
     ((parent-is "struct_type") parent-bol 4)
     ((parent-is "interface_type") parent-bol 4)
     ((parent-is "type_declaration") parent-bol 4)
     ((parent-is "composite_literal") parent-bol 4)
     ((parent-is "func_literal") parent-bol 4)
     ((parent-is "expression_case") parent-bol 4)
     ((parent-is "block") parent-bol 4)
     ((parent-is "call_expression") parent-bol 4)
     ((parent-is "parameter_list") parent-bol 4)
     ((parent-is "argument_list") parent-bol 4)
     (no-node parent-bol 0)
     )))

(defvar go-font-lock-settings-1

M typescript-mode.el => typescript-mode.el +38 -27
@@ 52,25 52,14 @@
    table)
  "Syntax table for `typescript-mode'.")

(defun ts-backward-up-list ()
  (lambda (node parent bol &rest _)
    (save-excursion
      (backward-up-list 1 nil t)
      (goto-char
       (treesit-node-start
        (treesit-node-at (point) (point) 'tsx)))
      (back-to-indentation)
      (treesit-node-start
       (treesit-node-at (point) (point) 'tsx)))))

(defvar typescript-mode-indent-rules
  `((tsx
     (no-node (ts-backward-up-list) ,typescript-mode-indent-offset)
     ((node-is "}") parent-bol 0)
     ((node-is ")") parent-bol 0)
     ((node-is "]") parent-bol 0)
     ((node-is ">") parent-bol 0)
     ((node-is ".") parent-bol ,typescript-mode-indent-offset)
     ((parent-is "ternary_expression") parent-bol ,typescript-mode-indent-offset)
     ((parent-is "named_imports") parent-bol ,typescript-mode-indent-offset)
     ((parent-is "statement_block") parent-bol ,typescript-mode-indent-offset)
     ((parent-is "type_arguments") parent-bol ,typescript-mode-indent-offset)


@@ 93,10 82,11 @@
     ;; TODO(Theo): This one is a little off.  Meant to hit the dangling '/' in
     ;; a jsx-element.  But it is also division operator...
     ((node-is "/") parent 0)
     ((parent-is "jsx_self_closing_element") parent ,typescript-mode-indent-offset))))
     ((parent-is "jsx_self_closing_element") parent ,typescript-mode-indent-offset)
     (no-node parent-bol 0))))

(defvar typescript-mode-font-lock-settings-1
  '((tsx
  `((tsx
     (
      ((identifier) @font-lock-constant-face
       (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))


@@ 186,20 176,19 @@
      [(this) (super)] @font-lock-keyword-face
      
      [(true) (false) (null)] @font-lock-constant-face
      ;; (regex pattern: (regex_pattern))
      (regex pattern: (regex_pattern)) @font-lock-string-face
      (number) @font-lock-constant-face

      (string) @font-lock-string-face

      ;; template strings need to be last in the file for embedded expressions
      ;; to work properly
      (template_string) @font-lock-string-face

      (template_substitution
       "${" @font-lock-constant-face
       (_)
       "}" @font-lock-constant-face
       )
       ["${" "}"] @font-lock-constant-face)

      (template_substitution
       "${"
       [(identifier) @font-lock-variable-name-face]
       "}")

      ["!"
       "abstract"


@@ 260,28 249,45 @@
      (comment) @font-lock-comment-face
      ))))

(defun typescript-mode-move-to-node (fn)
(defun typescript-mode-move-to-node-1 (fn)
  (when-let ((found-node (treesit-parent-until
                          (treesit-node-at (point) (point) 'tsx)
                          (treesit-node-on (point) (point) 'tsx)
                          (lambda (parent)
                            (let ((parent-type (treesit-node-type parent)))
                              (or (equal "function_declaration" parent-type)
                                  (equal "interface_declaration" parent-type)))))))
    (goto-char (funcall fn found-node))))

(defun typescript-mode-move-to-node (fn)
  (when-let ((found-node
              (treesit-parent-until
               (treesit-node-at (point))
               (lambda (parent)
                 (treesit-query-capture
                  parent
                  typescript-mode--defun-query)))))
    (goto-char (funcall fn found-node))))

(defun typescript-mode-beginning-of-defun (&optional arg)
  (typescript-mode-move-to-node #'treesit-node-start))

(defun typescript-mode-end-of-defun (&optional arg)
  (typescript-mode-move-to-node #'treesit-node-end))

(defvar typescript-mode--defun-query
  "[(import_statement)
    (function_declaration)
    (type_alias_declaration)
    (interface_declaration)
    (lexical_declaration)] @defun")

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-mode))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.tsx\\'" . typescript-mode))

(define-derived-mode typescript-mode prog-mode "typescriptreact"
(define-derived-mode typescript-mode prog-mode "TypeScript"
  "Major mode for editing typescript.

Key bindings:


@@ 303,8 309,13 @@ Key bindings:
  (treesit-get-parser-create 'tsx)
  (setq-local treesit-simple-indent-rules typescript-mode-indent-rules)
  (setq-local indent-line-function #'treesit-indent)
  (setq-local beginning-of-defun-function #'typescript-mode-beginning-of-defun)
  (setq-local end-of-defun-function #'typescript-mode-end-of-defun)

  (setq-local treesit-defun-query typescript-mode--defun-query)
  (setq-local beginning-of-defun-function #'treesit-beginning-of-defun)
  (setq-local end-of-defun-function #'treesit-end-of-defun)
  ;; (setq-local beginning-of-defun-function #'typescript-mode-beginning-of-defun)
  ;; (setq-local end-of-defun-function #'typescript-mode-end-of-defun)


  ;; This needs to be non-nil, because reasons
  (unless font-lock-defaults