Skip to content

Commit

Permalink
Implement rbs-ts-mode powered by tree-sitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jun 24, 2024
1 parent d397a6e commit 5ceb9d8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

# Undo-tree save-files
*.~undo-tree

# Temporary directories
tmp/
105 changes: 81 additions & 24 deletions rbs-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
;;; Code:
(require 'rx)

(when (version< "29" emacs-version)
(require 'treesit))

Check failure on line 33 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

You should depend on (emacs "29.1") if you need `treesit'.

Check failure on line 33 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

You should depend on (emacs "29.1") if you need `treesit'.

Check failure on line 33 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

You should depend on (emacs "29.1") if you need `treesit'.

Check failure on line 33 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

You should depend on (emacs "29.1") if you need `treesit'.

Check failure on line 33 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

You should depend on (emacs "29.1") if you need `treesit'.

Check failure on line 33 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (29.3)

You should depend on (emacs "29.1") if you need `treesit'.

(defgroup rbs nil
"Major mode for editing RBS code."
:group 'languages
Expand Down Expand Up @@ -73,30 +76,31 @@
(forward-line -1)
(current-indentation)))

(defconst rbs-mode--keywords
'("alias"
"attr_accessor"
"attr_reader"
"attr_writer"
"class"
"def"
"end"
"extend"
"extension"
"in"
"include"
"interface"
"module"
"unchecked"
"out"
"prepend"
"private"
"public"
"singleton"
"super"
"type"))

(defconst rbs-mode--keyword-regexp
(regexp-opt
'("alias"
"attr_accessor"
"attr_reader"
"attr_writer"
"class"
"def"
"end"
"extend"
"extension"
"in"
"include"
"interface"
"module"
"unchecked"
"out"
"prepend"
"private"
"public"
"singleton"
"super"
"type")
'symbols))
(regexp-opt rbs-mode--keywords 'symbols))

(defconst rbs-mode--builtin-type-regexp
(rx
Expand Down Expand Up @@ -282,7 +286,8 @@

;;;###autoload
(define-derived-mode rbs-mode prog-mode "RBS"
"Major mode for editing RBS code."
"Major mode for editing RBS."
:group 'rbs
:syntax-table rbs-mode--syntax-table
(setq-local comment-start "#")
(setq-local comment-start-skip "#+[ \t]*")
Expand All @@ -294,5 +299,57 @@
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rbs\\'" . rbs-mode))

;; -- rbs-ts-mode -- ;;

(defconst rbs-ts-mode--font-lock-settings
(treesit-font-lock-rules

Check failure on line 305 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

You should depend on (emacs "29.1") if you need `treesit-font-lock-rules'.

Check failure on line 305 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

You should depend on (emacs "29.1") if you need `treesit-font-lock-rules'.

Check warning on line 305 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

the function ‘treesit-font-lock-rules’ is not known to be defined.

Check failure on line 305 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

You should depend on (emacs "29.1") if you need `treesit-font-lock-rules'.

Check failure on line 305 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

You should depend on (emacs "29.1") if you need `treesit-font-lock-rules'.

Check failure on line 305 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

You should depend on (emacs "29.1") if you need `treesit-font-lock-rules'.

Check failure on line 305 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (29.3)

You should depend on (emacs "29.1") if you need `treesit-font-lock-rules'.
:language 'rbs
:feature 'comment
'((comment) @font-lock-comment-face)

:language 'rbs
:feature 'type-declaration
'((class (name) @font-lock-type-face))

:language 'rbs
:feature 'keyword
`([,@rbs-mode--keywords] @font-lock-keyword-face)))

(defconst rbs-ts-mode--method-regex
(rx string-start
(or "method" "singleton_method")
string-end))

;;;###autoload
(define-derived-mode rbs-ts-mode prog-mode "RBS"

Check warning on line 324 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

Unused lexical variable `treesit-language-source-alist'
"Major mode for editing RBS, powered by tree-sitter."
:group 'rbs

Check warning on line 326 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

Unused lexical variable ‘treesit-language-source-alist’

Check warning on line 326 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

Unused lexical variable ‘treesit-language-source-alist’

Check warning on line 326 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

Unused lexical variable ‘treesit-language-source-alist’
:syntax-table rbs-mode--syntax-table

(unless (treesit-language-available-p 'rbs)

Check failure on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

You should depend on (emacs "29.1") if you need `treesit-language-available-p'.

Check failure on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

You should depend on (emacs "29.1") if you need `treesit-language-available-p'.

Check warning on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

Unused lexical variable `treesit-language-source-alist'

Check warning on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

the function ‘treesit-language-available-p’ is not known to be defined.

Check failure on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

You should depend on (emacs "29.1") if you need `treesit-language-available-p'.

Check failure on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

You should depend on (emacs "29.1") if you need `treesit-language-available-p'.

Check failure on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

You should depend on (emacs "29.1") if you need `treesit-language-available-p'.

Check failure on line 329 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (29.3)

You should depend on (emacs "29.1") if you need `treesit-language-available-p'.
(let ((treesit-language-source-alist '((rbs "https://github.com/joker1007/tree-sitter-rbs"))))
(treesit-install-language-grammar 'rbs)))

Check warning on line 331 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

the function ‘treesit-install-language-grammar’ is not known to be defined.

(unless (treesit-ready-p 'rbs)

Check failure on line 333 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

You should depend on (emacs "29.1") if you need `treesit-ready-p'.

Check failure on line 333 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

You should depend on (emacs "29.1") if you need `treesit-ready-p'.

Check warning on line 333 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

the function ‘treesit-ready-p’ is not known to be defined.

Check failure on line 333 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

You should depend on (emacs "29.1") if you need `treesit-ready-p'.

Check failure on line 333 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

You should depend on (emacs "29.1") if you need `treesit-ready-p'.

Check failure on line 333 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

You should depend on (emacs "29.1") if you need `treesit-ready-p'.

Check failure on line 333 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (29.3)

You should depend on (emacs "29.1") if you need `treesit-ready-p'.
(error "Tree-sitter for RBS is unavailable"))

(treesit-parser-create 'rbs)

Check failure on line 336 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

You should depend on (emacs "29.1") if you need `treesit-parser-create'.

Check failure on line 336 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

You should depend on (emacs "29.1") if you need `treesit-parser-create'.

Check warning on line 336 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

the function ‘treesit-parser-create’ is not known to be defined.

Check failure on line 336 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

You should depend on (emacs "29.1") if you need `treesit-parser-create'.

Check failure on line 336 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

You should depend on (emacs "29.1") if you need `treesit-parser-create'.

Check failure on line 336 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

You should depend on (emacs "29.1") if you need `treesit-parser-create'.

Check failure on line 336 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (29.3)

You should depend on (emacs "29.1") if you need `treesit-parser-create'.

(setq-local comment-start "#")
(setq-local comment-start-skip "#+[ \t]*")
(setq-local comment-end "")

(setq-local treesit-font-lock-feature-list

Check warning on line 342 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

assignment to free variable ‘treesit-font-lock-feature-list’

Check warning on line 342 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

assignment to free variable ‘treesit-font-lock-feature-list’

Check warning on line 342 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

assignment to free variable ‘treesit-font-lock-feature-list’

Check warning on line 342 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

assignment to free variable `treesit-font-lock-feature-list'
'((comment type-declaration)
(keyword)))

(setq-local treesit-font-lock-settings rbs-ts-mode--font-lock-settings)

Check warning on line 346 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

assignment to free variable ‘treesit-font-lock-settings’

Check warning on line 346 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

assignment to free variable ‘treesit-font-lock-settings’

Check warning on line 346 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

assignment to free variable ‘treesit-font-lock-settings’

Check warning on line 346 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

assignment to free variable `treesit-font-lock-settings'

(setq-local treesit-defun-type-regexp rbs-ts-mode--method-regex)

Check warning on line 348 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

assignment to free variable ‘treesit-defun-type-regexp’

Check warning on line 348 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

assignment to free variable ‘treesit-defun-type-regexp’

Check warning on line 348 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

assignment to free variable ‘treesit-defun-type-regexp’

Check warning on line 348 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

assignment to free variable `treesit-defun-type-regexp'

(treesit-major-mode-setup)

Check failure on line 350 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (25.3)

You should depend on (emacs "29.1") if you need `treesit-major-mode-setup'.

Check failure on line 350 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (26.3)

You should depend on (emacs "29.1") if you need `treesit-major-mode-setup'.

Check warning on line 350 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

the function ‘treesit-major-mode-setup’ is not known to be defined.

Check failure on line 350 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (28.2)

You should depend on (emacs "29.1") if you need `treesit-major-mode-setup'.

Check failure on line 350 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (27.2)

You should depend on (emacs "29.1") if you need `treesit-major-mode-setup'.

Check failure on line 350 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (24.5)

You should depend on (emacs "29.1") if you need `treesit-major-mode-setup'.

Check failure on line 350 in rbs-mode.el

View workflow job for this annotation

GitHub Actions / lint (29.3)

You should depend on (emacs "29.1") if you need `treesit-major-mode-setup'.

(add-to-list 'auto-mode-alist '("\\.rbs\\'" . rbs-ts-mode)))

(provide 'rbs-mode)
;;; rbs-mode.el ends here

0 comments on commit 5ceb9d8

Please sign in to comment.