From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: Tree-sitter integration on feature/tree-sitter Date: Thu, 19 May 2022 19:01:19 -0700 Message-ID: <27630AA3-8026-4E24-8852-ACCD9325B99D@gmail.com> References: <2c2746e5f2558a87e8eab6f0914264a020173a9d.camel@pm.me> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.100.31\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_5BA6B257-0730-4E60-8C08-394F5EDB6898" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1260"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Kiong-Ge Liau Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri May 20 04:03:07 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 1nrrys-00006j-TF for ged-emacs-devel@m.gmane-mx.org; Fri, 20 May 2022 04:03:07 +0200 Original-Received: from localhost ([::1]:40448 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nrryr-0002QW-9U for ged-emacs-devel@m.gmane-mx.org; Thu, 19 May 2022 22:03:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46722) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nrrxK-0001Ze-E8 for emacs-devel@gnu.org; Thu, 19 May 2022 22:01:30 -0400 Original-Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]:56075) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nrrxI-0002cz-2K for emacs-devel@gnu.org; Thu, 19 May 2022 22:01:29 -0400 Original-Received: by mail-pj1-x1033.google.com with SMTP id n10so6783326pjh.5 for ; Thu, 19 May 2022 19:01:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=s8m4GiORzkdRebQd3wnSPFGavXhOb3sVeEAoj6uYGqc=; b=pA9T80PO8rScLguYGDlpkVKBDMqeiYiV95yW2VV60eIHfX8C/3a0NZD4YHR3oUARDv xJ5L1Yt/U8/ogI8pIOwUPT+MdJo7NJ3wzEvTOML7Jv9t0x0XQWo/wHadWcI6ayVyZQRc MFr2XVQyfHNLEBLvMUiNb3nv/ag+05vLkEvvwang56N0WMXw/QBfEmBkC5mgdFlkjKIy 1GU7zVIzg1+F43e6Id5ObKrrluEUrONTvMiEdfs2n5Vpqx9QdWuIFtDItErvPgzpJdBm 2TA4DkdpGWSYThGEdnJ8c2hcbSynfnTYdrnMoCozl4aENMIcctkHTbn6ir0fjo5+2t4t CJvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=s8m4GiORzkdRebQd3wnSPFGavXhOb3sVeEAoj6uYGqc=; b=BshZAA7TOOFCrf10ePYzt1BIuuTVsB8ptgx2vlxep5lQIpx+FFgA3LHUOYT8T8D7yX ysXkMuX/dsuuwtnFFbkxg2fnLc/vIdJD5dky6NnUlvGV9Ul9LvfZXH9ObPYoWLRIiXm8 kmNYF5leBpeJmIY3g443ocdE2y2yMarqTHYjYpXIfDyqUf3AKBdXiDj8CztxLrc09/xM met2oJTv1x3bSb/yA5f8/RmPCuZCYdNZxfs533sqJRf9GJHJmSiPy7229ongELdhCQQ4 CPp03FtFuuf4DVVNmUQVez3Vsmi7OXNN9JW4Z3Xj/7zOJzx+onnJlHdQEDgvBU/kUf4U gscw== X-Gm-Message-State: AOAM531hqbJBUOEw5/QpMDYtpEn6NMeohaD9Q1eEEqHl0feCOEXFCY0H GltUWB7oiG2vzVcmRI3xQjs= X-Google-Smtp-Source: ABdhPJyoZBW6bFTg2Xn738ZsO83rxcqlje0ELgEimk4q3wty3jZkL9PhIQZ8lt5mgCgHaaKC115v1Q== X-Received: by 2002:a17:90b:291:b0:1df:6662:922d with SMTP id az17-20020a17090b029100b001df6662922dmr8069453pjb.211.1653012081534; Thu, 19 May 2022 19:01:21 -0700 (PDT) Original-Received: from smtpclient.apple ([2600:1700:2ec7:8c90:c53a:918e:2856:437a]) by smtp.gmail.com with ESMTPSA id p9-20020a1709027ec900b0015e8d4eb1b8sm4605401plb.2.2022.05.19.19.01.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 May 2022 19:01:20 -0700 (PDT) In-Reply-To: <2c2746e5f2558a87e8eab6f0914264a020173a9d.camel@pm.me> X-Mailer: Apple Mail (2.3696.100.31) Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=casouri@gmail.com; helo=mail-pj1-x1033.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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:289992 Archived-At: --Apple-Mail=_5BA6B257-0730-4E60-8C08-394F5EDB6898 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii > On May 18, 2022, at 6:35 PM, Kiong-Ge Liau wrote: > > Can you please share the mentioned "treesit-demo.el" file? I cannot see > it attached to any messages on emacs-devel mailing list. > > Thanks. > Here it is, take it with a grain of salt :-) Yuan --Apple-Mail=_5BA6B257-0730-4E60-8C08-394F5EDB6898 Content-Disposition: attachment; filename=treesit-demo.el Content-Type: application/octet-stream; x-unix-mode=0644; name="treesit-demo.el" Content-Transfer-Encoding: 7bit ;; -*- lexical-binding: t -*- (require 'treesit) (require 'pp) (require 'rx) (defun treesit-show-buffer-tree () "Show the AST in a popup buffer." (interactive) (let ((root-node (treesit-parser-root-node (treesit-get-parser-create 'c))) (pp-use-max-width t)) (pop-to-buffer (get-buffer-create "*treesit-show-tree*")) (erase-buffer) (insert (treesit-node-string root-node)) ;; Format the output. (goto-char (point-min)) (while (re-search-forward (rx (or "(" (seq (+ word) ":"))) nil t) (goto-char (match-beginning 0)) (insert "\n") (goto-char (1+ (match-end 0)))) (setq indent-line-function #'lisp-indent-line) (indent-region (point-min) (point-max)))) (defun ts-c-fontify-system-lib (beg end _) "Fortify a #include . Fortify the angled brackets in preprocessor-face, and the lib name in string-face." (put-text-property beg (1+ beg) 'face 'font-lock-preprocessor-face) (put-text-property (1- end) end 'face 'font-lock-preprocessor-face) (put-text-property (1+ beg) (1- end) 'face 'font-lock-string-face)) ;; Please compiler. (defvar ts-c-treesit-indent-rules) (define-derived-mode ts-c-mode prog-mode "TS C" "C mode with tree-sitter support." (if (treesit-should-enable-p) (progn (setq-local treesit-font-lock-defaults '((ts-c-treesit-settings-1)) font-lock-defaults '(nil t) indent-line-function #'treesit-indent treesit-simple-indent-rules ts-c-treesit-indent-rules) (treesit-font-lock-enable)) ;; Copied from cc-mode. (setq-local font-lock-defaults '((c-font-lock-keywords c-font-lock-keywords-1 c-font-lock-keywords-2 c-font-lock-keywords-3) nil nil ((95 . "w") (36 . "w")) c-beginning-of-syntax (font-lock-mark-block-function . c-mark-function))))) (defvar ts-c-treesit-indent-rules `((c ;; Empty line. (no-node prev-line 0) ;; Function/struct definition body {}. ((match nil "function_definition" "body") parent 0) ((node-is "field_declaration_list") parent 0) ;; Call expression. ((parent-is "call_expression") parent 2) ;; If-else. ((match nil "if_statement" "condition") parent 2) ((match nil "if_statement" "consequence") parent 2) ((match nil "if_statement" "alternative") parent 2) ((match nil "switch_statement" "condition") parent 2) ((node-is "else") parent 0) ;; Switch case. ((parent-is "case_statement") parent 2) ((node-is "case_statement") parent 0) ;; { and }. ((node-is "compound_statement") parent 2) ((node-is "}") parent 0) ;; Multi-line string. ((parent-is "string_literal") no-indent 0) ;; List. ,@(cl-loop for type in '("compound_statement" "initializer_list" "argument_list" "parameter_list" "field_declaration_list") collect `((match nil ,type nil 0 0) parent 2) collect `((match nil ,type nil 1) first-sibling 0))))) (defvar ts-c-treesit-settings-1 `((c ,(treesit-expand-query '((null) @font-lock-constant-face (true) @font-lock-constant-face (false) @font-lock-constant-face (comment) @font-lock-comment-face (system_lib_string) @ts-c-fontify-system-lib (unary_expression operator: _ @font-lock-negation-char-face) (string_literal) @font-lock-string-face (char_literal) @font-lock-string-face (function_definition declarator: (identifier) @font-lock-function-name-face) (declaration declarator: (identifier) @font-lock-function-name-face) (function_declarator declarator: (identifier) @font-lock-function-name-face) (init_declarator declarator: (identifier) @font-lock-variable-name-face) (parameter_declaration declarator: (identifier) @font-lock-variable-name-face) (preproc_def name: (identifier) @font-lock-variable-name-face) (enumerator name: (identifier) @font-lock-variable-name-face) (field_identifier) @font-lock-variable-name-face (parameter_list (parameter_declaration (identifier) @font-lock-variable-name-face)) (pointer_declarator declarator: (identifier) @font-lock-variable-name-face) (array_declarator declarator: (identifier) @font-lock-variable-name-face) (preproc_function_def name: (identifier) @font-lock-variable-name-face parameters: (preproc_params (identifier) @font-lock-variable-name-face)) (type_identifier) @font-lock-type-face (primitive_type) @font-lock-type-face "auto" @font-lock-keyword-face "break" @font-lock-keyword-face "case" @font-lock-keyword-face "const" @font-lock-keyword-face "continue" @font-lock-keyword-face "default" @font-lock-keyword-face "do" @font-lock-keyword-face "else" @font-lock-keyword-face "enum" @font-lock-keyword-face "extern" @font-lock-keyword-face "for" @font-lock-keyword-face "goto" @font-lock-keyword-face "if" @font-lock-keyword-face "register" @font-lock-keyword-face "return" @font-lock-keyword-face "sizeof" @font-lock-keyword-face "static" @font-lock-keyword-face "struct" @font-lock-keyword-face "switch" @font-lock-keyword-face "typedef" @font-lock-keyword-face "union" @font-lock-keyword-face "volatile" @font-lock-keyword-face "while" @font-lock-keyword-face "long" @font-lock-type-face "short" @font-lock-type-face "signed" @font-lock-type-face "unsigned" @font-lock-type-face "#include" @font-lock-preprocessor-face "#define" @font-lock-preprocessor-face "#ifdef" @font-lock-preprocessor-face "#ifndef" @font-lock-preprocessor-face "#endif" @font-lock-preprocessor-face "#else" @font-lock-preprocessor-face "#elif" @font-lock-preprocessor-face ))))) ;;; Config (add-to-list 'auto-mode-alist '("\\.tsc\\'" . ts-c-mode)) --Apple-Mail=_5BA6B257-0730-4E60-8C08-394F5EDB6898--