From 0b9f2c5f8b1b8d3debceb566114e8ca6a48ef406 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Thu, 10 Jul 2025 10:07:08 -0500 Subject: [PATCH] adding wgsl-mode --- README.org | 5 ++ init.el | 3 ++ wgsl-mode.el | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 wgsl-mode.el diff --git a/README.org b/README.org index 5700707f..9d7b6162 100644 --- a/README.org +++ b/README.org @@ -3820,6 +3820,10 @@ I'd like to start learning and using rust if I can. ;; (load-file "./wgsl-ts-mode.el") #+end_src +#+begin_src emacs-lisp +(load (concat user-emacs-directory "wgsl-mode.el")) +#+end_src + *** Web For developing websites, I like to use web-mode #+begin_src emacs-lisp @@ -3939,6 +3943,7 @@ Let's give eglot a try. #'cape-file)))) (add-hook 'eglot-managed-mode-hook #'chris/eglot-capf) + (add-to-list 'eglot-server-programs '(wgsl-mode "wgsl-analyzer")) :general (general-def 'normal eglot-mode-map diff --git a/init.el b/init.el index cbb38db2..659ac548 100644 --- a/init.el +++ b/init.el @@ -2825,6 +2825,8 @@ targets." ;; (load-file "./wgsl-ts-mode.el") +(load (concat user-emacs-directory "wgsl-mode.el")) + (defun chris/web-mode-setup () "some setup for web development" (setq-local completion-at-point-functions @@ -2882,6 +2884,7 @@ targets." #'cape-file)))) (add-hook 'eglot-managed-mode-hook #'chris/eglot-capf) + (add-to-list 'eglot-server-programs '(wgsl-mode "wgsl-analyzer")) :general (general-def 'normal eglot-mode-map diff --git a/wgsl-mode.el b/wgsl-mode.el new file mode 100644 index 00000000..a99e4009 --- /dev/null +++ b/wgsl-mode.el @@ -0,0 +1,126 @@ +;;; wgsl-mode.el --- Syntax highlighting for the WebGPU Shading Language -*- lexical-binding: t; -*- + +;; Copyright (C) 2022 Anthony Cowley +;; Author: Anthony Cowley +;; URL: https://github.com/acowley/wgsl-mode +;; Package-Requires: ((emacs "24")) +;; Keywords: wgsl, c +;; Version: 1.0 + +;; 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: + +;; Syntax highlighting for the WebGPU Shading Language (WGSL). + +;;; Code: + +(require 'cc-fonts) + +(defconst wgsl-keywords-regexp + (rx (seq (group (or "struct" "fn" "var" "let" "ptr" "type")) (or space "<")))) + +(defconst wgsl-keywords-regexp2 + (rx (seq (group (or "if" "else" "elseif" + "switch" "case" "default" "break" "fallthrough" + "loop" "continuing" "continue" + "return" "for")) + (or space "<" "(" ";" "{")))) + +(defconst wgsl-attributes-regexp + (rx (seq + (or "[[" "," "@" space) + (group (or "builtin" "block" "location" "group" "binding" "stage" "workgroup_size" "access" + "stride")) + (or "]]" "(")))) + +(defconst wgsl-storage-classes-regexp + (rx (seq "<" + (* (not ?>)) + (group (or "in" "out" "function" "private" "workgroup" "uniform" + "storage" "handle")) + (or ">" ",")))) + +(defconst wgsl-builtins-regexp + (rx (seq + "@builtin(" + symbol-start + (group (or "vertex_index" + "instance_index" + "position" + "frag_coord" + "front_facing" + "frag_depth" + "local_invocation_id" + "local_invocation_index" + "global_invocation_id" + "workgroup_id" + "workgroup_size" + "num_workgroups" + "sample_index" + "sample_mask_in" + "sample_mask_out")) + symbol-end + ")"))) + +(defconst wgsl-constants-regexp + (rx (seq symbol-start + (group (or "compute" "vertex" "fragment" "read" "write" "read_write")) + symbol-end))) + +(defconst wgsl-scalar-types-regexp + (rx (or "f32" "u32" "i32" "bool" "void"))) + +(defconst wgsl-types-regexp + (rx symbol-start + (or (regexp wgsl-scalar-types-regexp) + (seq "vec" (or "2" "3" "4")) + (seq "mat" (or "2" "3" "4") "x" (or "2" "3" "4")) + (seq "array") + (seq "texture" + (or "" "_storage" "_depth") + (opt "_multisampled") + (or "_1d" "_2d" "_3d" "_cube") + (opt "_array"))) + symbol-end)) + +(defconst wgsl-variable-name-regexp + (rx (seq (group (regexp "[a-zA-Z][0-9a-zA-Z_]*")) (* space) (or ":" "=")))) + +(defconst wgsl-function-name-regexp + (rx (seq "fn" (+ space) (group (regexp "[a-zA-Z][0-9a-zA-Z_]*")) (* space) "("))) + +(defconst wgsl-font-lock-keywords + `((,wgsl-builtins-regexp 1 font-lock-builtin-face) + (,wgsl-constants-regexp 1 font-lock-constant-face) + (,wgsl-storage-classes-regexp 1 font-lock-constant-face) + (,wgsl-types-regexp . font-lock-type-face) + (,wgsl-attributes-regexp 1 font-lock-builtin-face) + (,wgsl-keywords-regexp 1 font-lock-keyword-face) + (,wgsl-keywords-regexp2 1 font-lock-keyword-face) + (,wgsl-variable-name-regexp 1 font-lock-variable-name-face) + (,wgsl-function-name-regexp 1 font-lock-function-name-face))) + +;;;###autoload +(define-derived-mode wgsl-mode c++-mode "WGSL" + "Major mode for WGSL source." + (add-to-list 'c-offsets-alist '(access-label . 0)) + (font-lock-remove-keywords 'wgsl-mode c++-font-lock-keywords-3) + (font-lock-add-keywords nil wgsl-font-lock-keywords)) + +;;;###autoload +(add-to-list 'auto-mode-alist '("\\.wgsl\\'" . wgsl-mode)) + +(provide 'wgsl-mode) +;;; wgsl-mode.el ends here