2022-12-11 18:41:16 -05:00
|
|
|
;;; go-ts-mode.el --- tree-sitter support for Go -*- lexical-binding: t; -*-
|
|
|
|
|
2023-01-01 05:31:12 -05:00
|
|
|
;; Copyright (C) 2022-2023 Free Software Foundation, Inc.
|
2022-12-11 18:41:16 -05:00
|
|
|
|
|
|
|
;; Author : Randy Taylor <dev@rjt.dev>
|
|
|
|
;; Maintainer : Randy Taylor <dev@rjt.dev>
|
|
|
|
;; Created : December 2022
|
|
|
|
;; Keywords : go languages tree-sitter
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs 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.
|
|
|
|
|
|
|
|
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'treesit)
|
|
|
|
(eval-when-compile (require 'rx))
|
|
|
|
|
|
|
|
(declare-function treesit-parser-create "treesit.c")
|
|
|
|
(declare-function treesit-induce-sparse-tree "treesit.c")
|
|
|
|
(declare-function treesit-node-child "treesit.c")
|
|
|
|
(declare-function treesit-node-child-by-field-name "treesit.c")
|
|
|
|
(declare-function treesit-node-start "treesit.c")
|
2023-01-28 15:42:57 +02:00
|
|
|
(declare-function treesit-node-end "treesit.c")
|
2022-12-11 18:41:16 -05:00
|
|
|
(declare-function treesit-node-type "treesit.c")
|
2022-12-29 17:49:40 +02:00
|
|
|
(declare-function treesit-search-subtree "treesit.c")
|
2022-12-11 18:41:16 -05:00
|
|
|
|
2023-01-24 21:20:48 -05:00
|
|
|
(defcustom go-ts-mode-indent-offset 8
|
2022-12-11 18:41:16 -05:00
|
|
|
"Number of spaces for each indentation step in `go-ts-mode'."
|
|
|
|
:version "29.1"
|
|
|
|
:type 'integer
|
|
|
|
:safe 'integerp
|
|
|
|
:group 'go)
|
|
|
|
|
|
|
|
(defvar go-ts-mode--syntax-table
|
|
|
|
(let ((table (make-syntax-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 ?/ ". 124b" table)
|
|
|
|
(modify-syntax-entry ?* ". 23" table)
|
|
|
|
(modify-syntax-entry ?\n "> b" table)
|
|
|
|
table)
|
|
|
|
"Syntax table for `go-ts-mode'.")
|
|
|
|
|
|
|
|
(defvar go-ts-mode--indent-rules
|
|
|
|
`((go
|
2023-03-04 19:45:39 +02:00
|
|
|
((parent-is "source_file") column-0 0)
|
2022-12-11 18:41:16 -05:00
|
|
|
((node-is ")") parent-bol 0)
|
|
|
|
((node-is "]") parent-bol 0)
|
|
|
|
((node-is "}") parent-bol 0)
|
2023-03-04 00:37:03 -08:00
|
|
|
((node-is "labeled_statement") no-indent 0)
|
|
|
|
((parent-is "raw_string_literal") no-indent 0)
|
2022-12-11 18:41:16 -05:00
|
|
|
((parent-is "argument_list") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "block") parent-bol go-ts-mode-indent-offset)
|
2023-02-02 21:00:02 +01:00
|
|
|
((parent-is "communication_case") parent-bol go-ts-mode-indent-offset)
|
2022-12-11 18:41:16 -05:00
|
|
|
((parent-is "const_declaration") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "default_case") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "expression_case") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "expression_switch_statement") parent-bol 0)
|
|
|
|
((parent-is "field_declaration_list") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "import_spec_list") parent-bol go-ts-mode-indent-offset)
|
2023-01-24 21:20:48 -05:00
|
|
|
((parent-is "interface_type") parent-bol go-ts-mode-indent-offset)
|
2022-12-11 18:41:16 -05:00
|
|
|
((parent-is "labeled_statement") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "literal_value") parent-bol go-ts-mode-indent-offset)
|
2023-01-24 21:20:48 -05:00
|
|
|
((parent-is "parameter_list") parent-bol go-ts-mode-indent-offset)
|
2023-02-02 21:00:02 +01:00
|
|
|
((parent-is "select_statement") parent-bol 0)
|
|
|
|
((parent-is "type_case") parent-bol go-ts-mode-indent-offset)
|
2022-12-11 18:41:16 -05:00
|
|
|
((parent-is "type_spec") parent-bol go-ts-mode-indent-offset)
|
2023-02-02 21:00:02 +01:00
|
|
|
((parent-is "type_switch_statement") parent-bol 0)
|
2022-12-11 18:41:16 -05:00
|
|
|
((parent-is "var_declaration") parent-bol go-ts-mode-indent-offset)
|
|
|
|
(no-node parent-bol 0)))
|
|
|
|
"Tree-sitter indent rules for `go-ts-mode'.")
|
|
|
|
|
|
|
|
(defvar go-ts-mode--keywords
|
|
|
|
'("break" "case" "chan" "const" "continue" "default" "defer" "else"
|
|
|
|
"fallthrough" "for" "func" "go" "goto" "if" "import" "interface" "map"
|
|
|
|
"package" "range" "return" "select" "struct" "switch" "type" "var")
|
|
|
|
"Go keywords for tree-sitter font-locking.")
|
|
|
|
|
|
|
|
(defvar go-ts-mode--operators
|
|
|
|
'("+" "&" "+=" "&=" "&&" "==" "!=" "-" "|" "-=" "|=" "||" "<" "<="
|
|
|
|
"*" "^" "*=" "^=" "<-" ">" ">=" "/" "<<" "/=" "<<=" "++" "=" ":=" "%"
|
|
|
|
">>" "%=" ">>=" "--" "!" "..." "&^" "&^=" "~")
|
|
|
|
"Go operators for tree-sitter font-locking.")
|
|
|
|
|
|
|
|
(defvar go-ts-mode--font-lock-settings
|
|
|
|
(treesit-font-lock-rules
|
|
|
|
:language 'go
|
|
|
|
:feature 'bracket
|
|
|
|
'((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'comment
|
|
|
|
'((comment) @font-lock-comment-face)
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'constant
|
|
|
|
'([(false) (iota) (nil) (true)] @font-lock-constant-face
|
|
|
|
(const_declaration
|
|
|
|
(const_spec name: (identifier) @font-lock-constant-face)))
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'delimiter
|
|
|
|
'((["," "." ";" ":"]) @font-lock-delimiter-face)
|
|
|
|
|
2023-02-05 15:46:28 +02:00
|
|
|
:language 'go
|
|
|
|
:feature 'definition
|
|
|
|
'((function_declaration
|
|
|
|
name: (identifier) @font-lock-function-name-face)
|
|
|
|
(method_declaration
|
|
|
|
name: (field_identifier) @font-lock-function-name-face)
|
|
|
|
(method_spec
|
|
|
|
name: (field_identifier) @font-lock-function-name-face)
|
|
|
|
(field_declaration
|
2023-02-25 03:15:46 +02:00
|
|
|
name: (field_identifier) @font-lock-property-name-face)
|
2023-02-05 15:46:28 +02:00
|
|
|
(parameter_declaration
|
2023-02-06 04:42:12 +02:00
|
|
|
name: (identifier) @font-lock-variable-name-face)
|
|
|
|
(short_var_declaration
|
|
|
|
left: (expression_list
|
|
|
|
(identifier) @font-lock-variable-name-face
|
|
|
|
("," (identifier) @font-lock-variable-name-face)*))
|
|
|
|
(var_spec name: (identifier) @font-lock-variable-name-face
|
|
|
|
("," name: (identifier) @font-lock-variable-name-face)*))
|
2023-02-05 15:46:28 +02:00
|
|
|
|
2022-12-11 18:41:16 -05:00
|
|
|
:language 'go
|
|
|
|
:feature 'function
|
|
|
|
'((call_expression
|
2023-02-25 03:15:46 +02:00
|
|
|
function: (identifier) @font-lock-function-call-face)
|
2022-12-11 18:41:16 -05:00
|
|
|
(call_expression
|
|
|
|
function: (selector_expression
|
2023-02-25 03:15:46 +02:00
|
|
|
field: (field_identifier) @font-lock-function-call-face)))
|
2022-12-11 18:41:16 -05:00
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'keyword
|
|
|
|
`([,@go-ts-mode--keywords] @font-lock-keyword-face)
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'label
|
|
|
|
'((label_name) @font-lock-constant-face)
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'number
|
|
|
|
'([(float_literal)
|
|
|
|
(imaginary_literal)
|
|
|
|
(int_literal)] @font-lock-number-face)
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'string
|
|
|
|
'([(interpreted_string_literal)
|
|
|
|
(raw_string_literal)
|
|
|
|
(rune_literal)] @font-lock-string-face)
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'type
|
|
|
|
'([(package_identifier) (type_identifier)] @font-lock-type-face)
|
|
|
|
|
2023-02-06 04:12:25 +02:00
|
|
|
:language 'go
|
|
|
|
:feature 'property
|
2023-02-28 04:07:55 +02:00
|
|
|
'((selector_expression field: (field_identifier) @font-lock-property-use-face)
|
|
|
|
(keyed_element (_ (identifier) @font-lock-property-use-face)))
|
2023-02-06 04:12:25 +02:00
|
|
|
|
2022-12-11 18:41:16 -05:00
|
|
|
:language 'go
|
|
|
|
:feature 'variable
|
2023-02-28 04:07:55 +02:00
|
|
|
'((identifier) @font-lock-variable-use-face)
|
2022-12-11 18:41:16 -05:00
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'escape-sequence
|
|
|
|
:override t
|
|
|
|
'((escape_sequence) @font-lock-escape-face)
|
|
|
|
|
|
|
|
:language 'go
|
|
|
|
:feature 'error
|
|
|
|
:override t
|
|
|
|
'((ERROR) @font-lock-warning-face))
|
|
|
|
"Tree-sitter font-lock settings for `go-ts-mode'.")
|
|
|
|
|
2023-01-14 08:28:06 +02:00
|
|
|
(defvar-keymap go-ts-mode-map
|
|
|
|
:doc "Keymap used in Go mode, powered by tree-sitter"
|
|
|
|
:parent prog-mode-map
|
|
|
|
"C-c C-d" #'go-ts-mode-docstring)
|
|
|
|
|
2022-12-11 18:41:16 -05:00
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode go-ts-mode prog-mode "Go"
|
2023-01-14 08:28:06 +02:00
|
|
|
"Major mode for editing Go, powered by tree-sitter.
|
|
|
|
|
|
|
|
\\{go-ts-mode-map}"
|
2022-12-11 18:41:16 -05:00
|
|
|
:group 'go
|
|
|
|
:syntax-table go-ts-mode--syntax-table
|
|
|
|
|
|
|
|
(when (treesit-ready-p 'go)
|
|
|
|
(treesit-parser-create 'go)
|
|
|
|
|
|
|
|
;; Comments.
|
|
|
|
(setq-local comment-start "// ")
|
|
|
|
(setq-local comment-end "")
|
|
|
|
(setq-local comment-start-skip (rx "//" (* (syntax whitespace))))
|
|
|
|
|
2022-12-29 17:49:40 +02:00
|
|
|
;; Navigation.
|
|
|
|
(setq-local treesit-defun-type-regexp
|
|
|
|
(regexp-opt '("method_declaration"
|
|
|
|
"function_declaration"
|
|
|
|
"type_declaration")))
|
|
|
|
(setq-local treesit-defun-name-function #'go-ts-mode--defun-name)
|
|
|
|
|
2022-12-11 18:41:16 -05:00
|
|
|
;; Imenu.
|
2022-12-29 17:49:40 +02:00
|
|
|
(setq-local treesit-simple-imenu-settings
|
|
|
|
`(("Function" "\\`function_declaration\\'" nil nil)
|
|
|
|
("Method" "\\`method_declaration\\'" nil nil)
|
|
|
|
("Struct" "\\`type_declaration\\'" go-ts-mode--struct-node-p nil)
|
|
|
|
("Interface" "\\`type_declaration\\'" go-ts-mode--interface-node-p nil)
|
|
|
|
("Type" "\\`type_declaration\\'" go-ts-mode--other-type-node-p nil)
|
|
|
|
("Alias" "\\`type_declaration\\'" go-ts-mode--alias-node-p nil)))
|
2022-12-11 18:41:16 -05:00
|
|
|
|
|
|
|
;; Indent.
|
|
|
|
(setq-local indent-tabs-mode t
|
|
|
|
treesit-simple-indent-rules go-ts-mode--indent-rules)
|
|
|
|
|
2022-12-29 17:49:40 +02:00
|
|
|
;; Electric
|
|
|
|
(setq-local electric-indent-chars
|
|
|
|
(append "{}()" electric-indent-chars))
|
|
|
|
|
2022-12-11 18:41:16 -05:00
|
|
|
;; Font-lock.
|
|
|
|
(setq-local treesit-font-lock-settings go-ts-mode--font-lock-settings)
|
|
|
|
(setq-local treesit-font-lock-feature-list
|
2023-02-05 15:46:28 +02:00
|
|
|
'(( comment definition)
|
2022-12-11 18:41:16 -05:00
|
|
|
( keyword string type)
|
2023-02-05 15:46:28 +02:00
|
|
|
( constant escape-sequence label number)
|
|
|
|
( bracket delimiter error function operator property variable)))
|
2022-12-11 18:41:16 -05:00
|
|
|
|
|
|
|
(treesit-major-mode-setup)))
|
|
|
|
|
2023-01-20 10:28:26 +02:00
|
|
|
(if (treesit-ready-p 'go)
|
|
|
|
(add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode)))
|
|
|
|
|
2023-02-08 17:16:02 +02:00
|
|
|
(defun go-ts-mode--defun-name (node &optional skip-prefix)
|
2022-12-29 17:49:40 +02:00
|
|
|
"Return the defun name of NODE.
|
2023-02-08 17:16:02 +02:00
|
|
|
Return nil if there is no name or if NODE is not a defun node.
|
|
|
|
Methods are prefixed with the receiver name, unless SKIP-PREFIX is t."
|
2022-12-29 17:49:40 +02:00
|
|
|
(pcase (treesit-node-type node)
|
|
|
|
("function_declaration"
|
|
|
|
(treesit-node-text
|
|
|
|
(treesit-node-child-by-field-name
|
|
|
|
node "name")
|
|
|
|
t))
|
|
|
|
("method_declaration"
|
|
|
|
(let* ((receiver-node (treesit-node-child-by-field-name node "receiver"))
|
2023-02-08 17:16:02 +02:00
|
|
|
(receiver (treesit-node-text (treesit-search-subtree receiver-node "type_identifier")))
|
|
|
|
(method (treesit-node-text (treesit-node-child-by-field-name node "name"))))
|
|
|
|
(if skip-prefix method
|
|
|
|
(concat "(" receiver ")." method))))
|
2022-12-29 17:49:40 +02:00
|
|
|
("type_declaration"
|
|
|
|
(treesit-node-text
|
|
|
|
(treesit-node-child-by-field-name
|
|
|
|
(treesit-node-child node 0 t) "name")
|
|
|
|
t))))
|
|
|
|
|
|
|
|
(defun go-ts-mode--interface-node-p (node)
|
|
|
|
"Return t if NODE is an interface."
|
|
|
|
(and
|
|
|
|
(string-equal "type_declaration" (treesit-node-type node))
|
|
|
|
(treesit-search-subtree node "interface_type" nil nil 2)))
|
|
|
|
|
|
|
|
(defun go-ts-mode--struct-node-p (node)
|
|
|
|
"Return t if NODE is a struct."
|
|
|
|
(and
|
|
|
|
(string-equal "type_declaration" (treesit-node-type node))
|
|
|
|
(treesit-search-subtree node "struct_type" nil nil 2)))
|
|
|
|
|
|
|
|
(defun go-ts-mode--alias-node-p (node)
|
|
|
|
"Return t if NODE is a type alias."
|
|
|
|
(and
|
|
|
|
(string-equal "type_declaration" (treesit-node-type node))
|
|
|
|
(treesit-search-subtree node "type_alias" nil nil 1)))
|
|
|
|
|
|
|
|
(defun go-ts-mode--other-type-node-p (node)
|
|
|
|
"Return t if NODE is a type, other than interface, struct or alias."
|
|
|
|
(and
|
|
|
|
(string-equal "type_declaration" (treesit-node-type node))
|
|
|
|
(not (go-ts-mode--interface-node-p node))
|
|
|
|
(not (go-ts-mode--struct-node-p node))
|
|
|
|
(not (go-ts-mode--alias-node-p node))))
|
|
|
|
|
2023-01-14 08:28:06 +02:00
|
|
|
(defun go-ts-mode-docstring ()
|
|
|
|
"Add a docstring comment for the current defun.
|
|
|
|
The added docstring is prefilled with the defun's name. If the
|
|
|
|
comment already exists, jump to it."
|
|
|
|
(interactive)
|
|
|
|
(when-let ((defun-node (treesit-defun-at-point)))
|
|
|
|
(goto-char (treesit-node-start defun-node))
|
|
|
|
(if (go-ts-mode--comment-on-previous-line-p)
|
|
|
|
;; go to top comment line
|
|
|
|
(while (go-ts-mode--comment-on-previous-line-p)
|
|
|
|
(forward-line -1))
|
2023-02-08 17:16:02 +02:00
|
|
|
(insert "// " (go-ts-mode--defun-name defun-node t))
|
2023-01-14 08:28:06 +02:00
|
|
|
(newline)
|
|
|
|
(backward-char))))
|
|
|
|
|
|
|
|
(defun go-ts-mode--comment-on-previous-line-p ()
|
|
|
|
"Return t if the previous line is a comment."
|
|
|
|
(when-let ((point (- (pos-bol) 1))
|
|
|
|
((> point 0))
|
|
|
|
(node (treesit-node-at point)))
|
|
|
|
(and
|
|
|
|
;; check point is actually inside the found node
|
|
|
|
;; treesit-node-at can return nodes after point
|
|
|
|
(<= (treesit-node-start node) point (treesit-node-end node))
|
|
|
|
(string-equal "comment" (treesit-node-type node)))))
|
|
|
|
|
2022-12-11 18:41:16 -05:00
|
|
|
;; go.mod support.
|
|
|
|
|
|
|
|
(defvar go-mod-ts-mode--syntax-table
|
|
|
|
(let ((table (make-syntax-table)))
|
|
|
|
(modify-syntax-entry ?/ ". 124b" table)
|
|
|
|
(modify-syntax-entry ?\n "> b" table)
|
|
|
|
table)
|
|
|
|
"Syntax table for `go-mod-ts-mode'.")
|
|
|
|
|
|
|
|
(defvar go-mod-ts-mode--indent-rules
|
|
|
|
`((gomod
|
|
|
|
((node-is ")") parent-bol 0)
|
|
|
|
((parent-is "exclude_directive") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "module_directive") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "replace_directive") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "require_directive") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((parent-is "retract_directive") parent-bol go-ts-mode-indent-offset)
|
|
|
|
((go-mod-ts-mode--in-directive-p) no-indent go-ts-mode-indent-offset)
|
|
|
|
(no-node no-indent 0)))
|
|
|
|
"Tree-sitter indent rules for `go-mod-ts-mode'.")
|
|
|
|
|
|
|
|
(defun go-mod-ts-mode--in-directive-p ()
|
|
|
|
"Return non-nil if inside a directive.
|
|
|
|
When entering an empty directive or adding a new entry to one, no node
|
|
|
|
will be present meaning none of the indentation rules will match,
|
|
|
|
because there is no parent to match against. This function determines
|
|
|
|
what the parent of the node would be if it were a node."
|
|
|
|
(lambda (node _ _ &rest _)
|
|
|
|
(unless (treesit-node-type node)
|
|
|
|
(save-excursion
|
|
|
|
(backward-up-list)
|
|
|
|
(back-to-indentation)
|
|
|
|
(pcase (treesit-node-type (treesit-node-at (point)))
|
|
|
|
("exclude" t)
|
|
|
|
("module" t)
|
|
|
|
("replace" t)
|
|
|
|
("require" t)
|
|
|
|
("retract" t))))))
|
|
|
|
|
|
|
|
(defvar go-mod-ts-mode--keywords
|
|
|
|
'("exclude" "go" "module" "replace" "require" "retract")
|
|
|
|
"go.mod keywords for tree-sitter font-locking.")
|
|
|
|
|
|
|
|
(defvar go-mod-ts-mode--font-lock-settings
|
|
|
|
(treesit-font-lock-rules
|
|
|
|
:language 'gomod
|
|
|
|
:feature 'bracket
|
|
|
|
'((["(" ")"]) @font-lock-bracket-face)
|
|
|
|
|
|
|
|
:language 'gomod
|
|
|
|
:feature 'comment
|
|
|
|
'((comment) @font-lock-comment-face)
|
|
|
|
|
|
|
|
:language 'gomod
|
|
|
|
:feature 'keyword
|
|
|
|
`([,@go-mod-ts-mode--keywords] @font-lock-keyword-face)
|
|
|
|
|
|
|
|
:language 'gomod
|
|
|
|
:feature 'number
|
|
|
|
'([(go_version) (version)] @font-lock-number-face)
|
|
|
|
|
|
|
|
:language 'gomod
|
|
|
|
:feature 'operator
|
|
|
|
'((["=>"]) @font-lock-operator-face)
|
|
|
|
|
|
|
|
:language 'gomod
|
|
|
|
:feature 'error
|
|
|
|
:override t
|
|
|
|
'((ERROR) @font-lock-warning-face))
|
|
|
|
"Tree-sitter font-lock settings for `go-mod-ts-mode'.")
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode go-mod-ts-mode prog-mode "Go Mod"
|
|
|
|
"Major mode for editing go.mod files, powered by tree-sitter."
|
|
|
|
:group 'go
|
|
|
|
:syntax-table go-mod-ts-mode--syntax-table
|
|
|
|
|
|
|
|
(when (treesit-ready-p 'gomod)
|
|
|
|
(treesit-parser-create 'gomod)
|
|
|
|
|
|
|
|
;; Comments.
|
|
|
|
(setq-local comment-start "// ")
|
|
|
|
(setq-local comment-end "")
|
|
|
|
(setq-local comment-start-skip (rx "//" (* (syntax whitespace))))
|
|
|
|
|
|
|
|
;; Indent.
|
|
|
|
(setq-local indent-tabs-mode t
|
|
|
|
treesit-simple-indent-rules go-mod-ts-mode--indent-rules)
|
|
|
|
|
|
|
|
;; Font-lock.
|
|
|
|
(setq-local treesit-font-lock-settings go-mod-ts-mode--font-lock-settings)
|
|
|
|
(setq-local treesit-font-lock-feature-list
|
|
|
|
'((comment)
|
|
|
|
(keyword)
|
|
|
|
(number)
|
|
|
|
(bracket error operator)))
|
|
|
|
|
|
|
|
(treesit-major-mode-setup)))
|
|
|
|
|
2023-03-03 13:59:54 -08:00
|
|
|
(if (treesit-ready-p 'gomod)
|
2023-01-20 10:28:26 +02:00
|
|
|
(add-to-list 'auto-mode-alist '("/go\\.mod\\'" . go-mod-ts-mode)))
|
|
|
|
|
2022-12-11 18:41:16 -05:00
|
|
|
(provide 'go-ts-mode)
|
|
|
|
|
|
|
|
;;; go-ts-mode.el ends here
|