From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Emacs development discussions." Newsgroups: gmane.emacs.devel Subject: Re: TypeScript support for tree-sitter (was Re: Call for volunteers: add tree-sitter support to major modes) Date: Tue, 11 Oct 2022 22:07:01 +0200 Message-ID: <87r0zektui.fsf@thornhill.no> References: <83czb1jrm3.fsf@gnu.org> <87h70b8zqs.fsf@thornhill.no> <835ygrhd1p.fsf@gnu.org> <87bkqj8w8n.fsf@thornhill.no> <877d178t90.fsf@thornhill.no> <871qrf8shm.fsf@thornhill.no> <87tu4btss0.fsf@thornhill.no> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6501"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , emacs-devel@gnu.org, Lars Ingebrigtsen To: Yuan Fu Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Oct 11 22:36:09 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oiLyy-0001PQ-LY for ged-emacs-devel@m.gmane-mx.org; Tue, 11 Oct 2022 22:36:08 +0200 Original-Received: from localhost ([::1]:39868 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oiLyx-0007AA-G4 for ged-emacs-devel@m.gmane-mx.org; Tue, 11 Oct 2022 16:36:07 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42064) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiLWy-0007Ae-Dt for emacs-devel@gnu.org; Tue, 11 Oct 2022 16:07:12 -0400 Original-Received: from out0.migadu.com ([2001:41d0:2:267::]:52864) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oiLWt-0003q8-HX; Tue, 11 Oct 2022 16:07:12 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1665518823; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=yXkddoI4zMnzEfyd64bYmZ7f869G0r8DVt2aOkwzd6k=; b=vy7cnnWrggwOqVats8sF1StmFHuTgNYpiMbYu51IUbnIht1r5fR6MfWHlRN92cpa/bBA/6 caZMWNdYJNsbz59r96eWuzXyJXzZ4Qe824iXsaRfYa6UpLjxeDbv8om1qpmnliWjiSG9hf C782PuPg7rmacGKA0Nchgvnwc9+tq+jWF6N4ayP9WuSYC3/FnEVltYcubWIM+6j9MwPu2h lu/5Gy2y0MIBFtCJR84MmqbXWg23jvvM0OIxGglKd/vdVKfliPY9D27LfVnYJuBbw+FLdK IlOuByMGSQrNMWvkhmJCd0TosaSBIQAeeeygs+KkZorKTH7DJBmMYBfoSluTgg== In-Reply-To: <87tu4btss0.fsf@thornhill.no> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=2001:41d0:2:267::; envelope-from=theo@thornhill.no; helo=out0.migadu.com X-Spam_score_int: -23 X-Spam_score: -2.4 X-Spam_bar: -- X-Spam_report: (-2.4 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:297537 Archived-At: --=-=-= Content-Type: text/plain Hi Yuan! My guess is that as Philip suggested maybe 'ts-mode' is just a better name than 'typescript-mode' anyways, so let's just do it. Attached is the new 'ts-mode' - I hope it will fit nicely now without interrupting the future of 'typescript.el'. Thanks, Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-Add-TypeScript-support-with-tree-sitter.patch >From 80711908e853344fa6a5223af6c27bfc01667d12 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Tue, 11 Oct 2022 10:27:55 +0200 Subject: [PATCH 2/2] Add TypeScript support with tree-sitter * lisp/progmodes/typescript-mode.el (typescript-mode): New major mode for TypeScript with support for tree-sitter. It uses the TSX parser, so that we get support for TSX as well as TypeScript. If we cannot find tree-sitter, we default to using js-mode. --- etc/NEWS | 7 + lisp/progmodes/typescript-mode.el | 364 ++++++++++++++++++++++++++++++ 2 files changed, 371 insertions(+) create mode 100644 lisp/progmodes/typescript-mode.el diff --git a/etc/NEWS b/etc/NEWS index 88b1431d6a..97cf95bc1e 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2774,6 +2774,13 @@ Emacs buffers, like indentation and the like. The new ert function This is a lightweight variant of 'js-mode' that is used by default when visiting JSON files. + +** New mode typescript-mode'. +Support is added for TypeScript, based on the new integration with +Tree-Sitter. There's support for font-locking, indentation and +navigation. Tree-Sitter is required for this mode to function, but if +it is not available, we will default to use 'js-mode'. + * Incompatible Lisp Changes in Emacs 29.1 diff --git a/lisp/progmodes/typescript-mode.el b/lisp/progmodes/typescript-mode.el new file mode 100644 index 0000000000..0a805ef554 --- /dev/null +++ b/lisp/progmodes/typescript-mode.el @@ -0,0 +1,364 @@ +;;; typescript-mode.el --- tree sitter support for TypeScript -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Free Software Foundation, Inc. + +;; Author : Theodor Thornhill +;; Maintainer : Theodor Thornhill +;; Created : October 2022 +;; Keywords : typescript tsx languages tree-sitter + +;; This file is part of GNU Emacs. + +;; This program 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. + +;; This program 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 this program. If not, see . + +(require 'treesit) +(require 'rx) +(require 'js) + +(defcustom typescript-mode-indent-offset 2 + "Number of spaces for each indentation step in `typescript-mode'." + :type 'integer + :safe 'integerp + :group 'typescript) + +(defvar typescript-mode--syntax-table + (let ((table (make-syntax-table))) + ;; Taken from the cc-langs version + (modify-syntax-entry ?_ "_" table) + (modify-syntax-entry ?$ "_" table) + (modify-syntax-entry ?\\ "\\" table) + (modify-syntax-entry ?+ "." table) + (modify-syntax-entry ?- "." table) + (modify-syntax-entry ?= "." table) + (modify-syntax-entry ?% "." table) + (modify-syntax-entry ?< "." table) + (modify-syntax-entry ?> "." table) + (modify-syntax-entry ?& "." table) + (modify-syntax-entry ?| "." table) + (modify-syntax-entry ?` "\"" table) + (modify-syntax-entry ?\240 "." table) + table) + "Syntax table for `typescript-mode'.") + +(defvar typescript-mode--indent-rules + `((tsx + ((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) + ((parent-is "variable_declarator") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "arguments") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "array") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "formal_parameters") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "template_substitution") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "object_pattern") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "object") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "object_type") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "enum_body") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "arrow_function") + parent-bol ,typescript-mode-indent-offset) + ((parent-is "parenthesized_expression") + parent-bol ,typescript-mode-indent-offset) + + ;; TSX + ((parent-is "jsx_opening_element") + parent ,typescript-mode-indent-offset) + ((node-is "jsx_closing_element") parent 0) + ((parent-is "jsx_element") + parent ,typescript-mode-indent-offset) + ((node-is "/") parent 0) + ((parent-is "jsx_self_closing_element") + parent ,typescript-mode-indent-offset) + (no-node parent-bol 0)))) + +(defvar typescript-mode--settings + (treesit-font-lock-rules + :language 'tsx + :override t + '( + (template_string) @font-lock-string-face + + ((identifier) @font-lock-constant-face + (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face)) + + (nested_type_identifier + module: (identifier) @font-lock-type-face) + (type_identifier) @font-lock-type-face + (predefined_type) @font-lock-type-face + + (new_expression + constructor: (identifier) @font-lock-type-face) + + (function + name: (identifier) @font-lock-function-name-face) + + (function_declaration + name: (identifier) @font-lock-function-name-face) + + (method_definition + name: (property_identifier) @font-lock-function-name-face) + + (variable_declarator + name: (identifier) @font-lock-function-name-face + value: [(function) (arrow_function)]) + + (variable_declarator + name: (array_pattern + (identifier) + (identifier) @font-lock-function-name-face) + value: (array (number) (function))) + + (assignment_expression + left: [(identifier) @font-lock-function-name-face + (member_expression + property: (property_identifier) @font-lock-function-name-face)] + right: [(function) (arrow_function)]) + + (call_expression + function: + [(identifier) @font-lock-function-name-face + (member_expression + property: (property_identifier) @font-lock-function-name-face)]) + + (variable_declarator + name: (identifier) @font-lock-variable-name-face) + + (enum_declaration (identifier) @font-lock-type-face) + + (enum_body (property_identifier) @font-lock-type-face) + + (enum_assignment name: (property_identifier) @font-lock-type-face) + + (assignment_expression + left: [(identifier) @font-lock-variable-name-face + (member_expression + property: (property_identifier) @font-lock-variable-name-face)]) + + (for_in_statement + left: (identifier) @font-lock-variable-name-face) + + (arrow_function + parameter: (identifier) @font-lock-variable-name-face) + + (arrow_function + parameters: + [(_ (identifier) @font-lock-variable-name-face) + (_ (_ (identifier) @font-lock-variable-name-face)) + (_ (_ (_ (identifier) @font-lock-variable-name-face)))]) + + + (pair key: (property_identifier) @font-lock-variable-name-face) + + (pair value: (identifier) @font-lock-variable-name-face) + + (pair + key: (property_identifier) @font-lock-function-name-face + value: [(function) (arrow_function)]) + + (property_signature + name: (property_identifier) @font-lock-variable-name-face) + + ((shorthand_property_identifier) @font-lock-variable-name-face) + + (pair_pattern + key: (property_identifier) @font-lock-variable-name-face) + + ((shorthand_property_identifier_pattern) + @font-lock-variable-name-face) + + (array_pattern (identifier) @font-lock-variable-name-face) + + (jsx_opening_element + [(nested_identifier (identifier)) (identifier)] + @font-lock-function-name-face) + + (jsx_closing_element + [(nested_identifier (identifier)) (identifier)] + @font-lock-function-name-face) + + (jsx_self_closing_element + [(nested_identifier (identifier)) (identifier)] + @font-lock-function-name-face) + + (jsx_attribute (property_identifier) @font-lock-constant-face) + + [(this) (super)] @font-lock-keyword-face + + [(true) (false) (null)] @font-lock-constant-face + (regex pattern: (regex_pattern)) @font-lock-string-face + (number) @font-lock-constant-face + + (string) @font-lock-string-face + (template_string) @font-lock-string-face + + (template_substitution + ["${" "}"] @font-lock-constant-face) + + ["!" + "abstract" + "as" + "async" + "await" + "break" + "case" + "catch" + "class" + "const" + "continue" + "debugger" + "declare" + "default" + "delete" + "do" + "else" + "enum" + "export" + "extends" + "finally" + "for" + "from" + "function" + "get" + "if" + "implements" + "import" + "in" + "instanceof" + "interface" + "keyof" + "let" + "namespace" + "new" + "of" + "private" + "protected" + "public" + "readonly" + "return" + "set" + "static" + "switch" + "target" + "throw" + "try" + "type" + "typeof" + "var" + "void" + "while" + "with" + "yield" + ] @font-lock-keyword-face + + (comment) @font-lock-comment-face + ))) + +(defvar typescript-mode--defun-type-regexp + (rx (or "class_declaration" + "method_definition" + "function_declaration" + "lexical_declaration")) + "Regular expression that matches type of defun nodes. +Used in `typescript-mode--beginning-of-defun' and friends.") + +(defun typescript-mode--beginning-of-defun (&optional arg) + "Tree-sitter `beginning-of-defun' function. +ARG is the same as in `beginning-of-defun." + (let ((arg (or arg 1))) + (if (> arg 0) + ;; Go backward. + (while (and (> arg 0) + (treesit-search-forward-goto + typescript-mode--defun-type-regexp 'start nil t)) + (setq arg (1- arg))) + ;; Go forward. + (while (and (< arg 0) + (treesit-search-forward-goto + typescript-mode--defun-type-regexp 'start)) + (setq arg (1+ arg)))))) + +(defun typescript-mode--end-of-defun (&optional arg) + "Tree-sitter `end-of-defun' function. +ARG is the same as in `end-of-defun." + (let ((arg (or arg 1))) + (if (< arg 0) + ;; Go backward. + (while (and (< arg 0) + (treesit-search-forward-goto + typescript-mode--defun-type-regexp 'end nil t)) + (setq arg (1+ arg))) + ;; Go forward. + (while (and (> arg 0) + (treesit-search-forward-goto + typescript-mode--defun-type-regexp 'end)) + (setq arg (1- arg)))))) + +;;;###autoload +(add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-mode)) + +;;;###autoload +(add-to-list 'auto-mode-alist '("\\.typescript\\'" . typescript-mode)) + +;;;###autoload +(define-derived-mode typescript-mode prog-mode "TypeScript" + "Major mode for editing TypeScript." + :group 'typescript + :syntax-table typescript-mode--syntax-table + + (cond + ((and (treesit-can-enable-p) + (treesit-language-available-p 'tsx)) + ;; Comments + (setq-local comment-start "// ") + (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *") + (setq-local comment-end "") + + (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) + + (unless font-lock-defaults + (setq font-lock-defaults '(nil t))) + + (setq-local treesit-font-lock-settings typescript-mode--settings) + + (treesit-font-lock-enable)) + (t + (message "Tree sitter for TypeScript isn't available, defaulting to js-mode") + (js-mode)))) + +(provide 'typescript-mode) + +;;; typescript-mode.el ends here -- 2.34.1 --=-=-=--