From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: mail@ookami.one Newsgroups: gmane.emacs.devel Subject: protobuf-ts-mode.el -- draft for new ts-mode Date: Fri, 06 Jan 2023 21:47:17 +0800 Message-ID: <87tu1322sa.fsf@ookami.one> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="20125"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jan 06 14:48:10 2023 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 1pDn4s-00052Q-6P for ged-emacs-devel@m.gmane-mx.org; Fri, 06 Jan 2023 14:48:10 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDn4F-0008N7-ER; Fri, 06 Jan 2023 08:47:31 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDn4B-0008Mc-Mt for emacs-devel@gnu.org; Fri, 06 Jan 2023 08:47:27 -0500 Original-Received: from ookami.one ([107.191.58.109]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDn49-0005st-Jw for emacs-devel@gnu.org; Fri, 06 Jan 2023 08:47:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ookami.one; s=main; t=1673012842; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=wEIcOolj2FZCdzxx3sxXSrQrX5AMXE4N/ZA2cHjotY0=; b=JYPwYLNXW1Vep/xSJNBYzEh78RZ4pWkMCdFA5CBO35bf1kmLMVHGMmgME3fHN1inoPIxAB A51owdWS1bcG10wQOHlq5eXabZAEXkfOeTA24QaMGX4SHtA/LIjTzU8/5rgMQbAgSDY8a+ QA/2/3DXkx6rRkIK4G/Ph3J1WCd/6L9xTrr6cAZmQ7nzQDEqgqhDwmjAiZvY/61h8a9PT1 84QZHxg6kRFseeDdmK97U9l9mJxVCdiaC6YeP87VTZZgVLhQTpKv8yHEsbWO1ORy2MtGRp JeDYIbv1VUozsnQZLFbKgVmSRCaj6+uUvuf/ilccPnAd7G59vmqU/WXXZdRG2w== Original-Received: from localhost ( [219.140.142.90]) by ookami.one (OpenSMTPD) with ESMTPSA id c2bf0f02 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 6 Jan 2023 13:47:21 +0000 (UTC) Received-SPF: pass client-ip=107.191.58.109; envelope-from=mail@ookami.one; helo=ookami.one 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, 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-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:302251 Archived-At: It's quite a fun to implement new major modes using tree-sitter :) Here is the file: ;;; protobuf-ts-mode.el --- tree-sitter support for Protocol Buffers -*- lexical-binding: t; -*- ;; Author : ookami ;; Created : January 2023 ;; Keywords : protobuf languages tree-sitter ;; 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 . ;;; Commentary: ;; ;;; Code: (require 'treesit) (eval-when-compile (require 'rx)) (defcustom protobuf-ts-mode-indent-offset 2 "Number of spaces for each indentation step in `protobuf-ts-mode'" :type 'integer :safe 'integerp :group 'protobuf) (defvar protobuf-ts-mode--indent-rules `((proto ((node-is ")") parent-bol 0) ((node-is "}") parent-bol 0) ((parent-is "service") parent-bol protobuf-ts-mode-indent-offset) ((parent-is "rpc") parent-bol protobuf-ts-mode-indent-offset) ((parent-is "message_body") parent-bol protobuf-ts-mode-indent-offset) ((parent-is "enum_body") parent-bol protobuf-ts-mode-indent-offset)))) (defvar protobuf-ts-mode--keywords '("optional" "repeated" "message" "enum" "service" "rpc" "syntax" "package" "import" "option" "returns")) (defvar protobuf-ts-mode--font-lock-settings (treesit-font-lock-rules :language 'proto :feature 'comment '((comment) @font-lock-comment-face) :language 'proto :feature 'keyword `([,@protobuf-ts-mode--keywords] @font-lock-keyword-face) :language 'proto :feature 'string '((string) @font-lock-string-face) :language 'proto :feature 'type '( (service_name (identifier) @font-lock-type-face) (message_name (identifier) @font-lock-type-face) (enum_name (identifier) @font-lock-type-face) (package (full_ident) @font-lock-type-face) ((key_type) @font-lock-type-face) ((type) @font-lock-type-face) ((message_or_enum_type) @font-lock-type-face) ) :language 'proto :feature 'function '((rpc (rpc_name (identifier) @font-lock-function-name-face))) :language 'proto :feature 'variable '((identifier) @font-lock-variable-name-face))) (defun protobuf-ts-mode--defun-name (node) (treesit-node-text (treesit-search-subtree node "^identifier$" nil t) t)) ;;;###autoload (define-derived-mode protobuf-ts-mode prog-mode "Protocol-Buffers" "Major mode for editing Protocol Buffers description language." :group 'protobuf (when (treesit-ready-p 'proto) (treesit-parser-create 'proto) ;; Comments (setq-local comment-start "/* ") (setq-local comment-end "*/ ") (setq-local comment-start-skip (rx (or (seq "/" (+ "/")) (seq "/" (+ "*"))) (* (syntax whitespace)))) ;; Font-lock (setq-local treesit-font-lock-settings protobuf-ts-mode--font-lock-settings) (setq-local treesit-font-lock-feature-list '(( comment) ( keyword string) ( type function variable))) ;; Imenu (setq-local treesit-simple-imenu-settings `(("Service" "\\`service_name\\'" nil nil) ("RPC" "\\`rpc_name\\'" nil nil) ("Message" "\\`message_name\\'" nil nil) ("Enum" "\\`enum_name'" nil nil))) ;; Indent (setq-local treesit-simple-indent-rules protobuf-ts-mode--indent-rules) ;; Navigation (setq-local treesit-defun-type-regexp (rx (or "service" "rpc" "message" "enum"))) (setq-local treesit-defun-name-function 'protobuf-ts-mode--defun-name) (treesit-major-mode-setup))) ;;;###autoload (add-to-list 'auto-mode-alist '("\\.proto\\'" . protobuf-ts-mode)) (provide 'protobuf-ts-mode) ;;; protobuf-ts-mode.el ends here