1;;; gn-mode.el - A major mode for editing gn files. 2 3;; Copyright 2015 The Chromium Authors. All rights reserved. 4;; Use of this source code is governed by a BSD-style license that can be 5;; found in the LICENSE file. 6 7;; Author: Elliot Glaysher <erg@chromium.org> 8;; Created: April 03, 2015 9;; Keywords: tools, gn, ninja, chromium 10 11;; This file is not part of GNU Emacs. 12 13;;; Commentary: 14 15;; A major mode for editing GN files. GN stands for Generate Ninja. GN is the 16;; meta build system used in Chromium. For more information on GN, see the GN 17;; manual: <https://gn.googlesource.com/gn/+/refs/heads/master/README.md> 18 19;;; To Do: 20 21;; - We syntax highlight builtin actions, but don't highlight instantiations of 22;; templates. Should we? 23 24 25 26(require 'smie) 27 28(defgroup gn nil 29 "Major mode for editing Generate Ninja files." 30 :prefix "gn-" 31 :group 'languages) 32 33(defcustom gn-indent-basic 2 34 "The number of spaces to indent a new scope." 35 :group 'gn 36 :type 'integer) 37 38(defcustom gn-format-command "gn format --stdin" 39 "The command to run to format gn files in place." 40 :group 'gn 41 :type 'string) 42 43(defgroup gn-faces nil 44 "Faces used in Generate Ninja mode." 45 :group 'gn 46 :group 'faces) 47 48(defface gn-embedded-variable 49 '((t :inherit font-lock-variable-name-face)) 50 "Font lock face used to highlight variable names in strings." 51 :group 'gn-faces) 52 53(defface gn-embedded-variable-boundary 54 '((t :bold t 55 :inherit gn-embedded-variable)) 56 "Font lock face used to highlight the '$' that starts a 57variable name or the '{{' and '}}' which surround it." 58 :group 'gn-faces) 59 60(defvar gn-font-lock-reserved-keywords 61 '("true" "false" "if" "else")) 62 63(defvar gn-font-lock-target-declaration-keywords 64 '("action" "action_foreach" "bundle_data" "copy" "create_bundle" "executable" 65 "group" "loadable_module" "shared_library" "source_set" "static_library" 66 "generated_file" "target" "rust_library" "rust_proc_macro")) 67 68;; pool() is handled specially since it's also a variable name 69(defvar gn-font-lock-buildfile-fun-keywords 70 '("assert" "config" "declare_args" "defined" "exec_script" "foreach" 71 "forward_variables_from" "get_label_info" "get_path_info" 72 "get_target_outputs" "getenv" "import" "not_needed" "print" 73 "process_file_template" "read_file" "rebase_path" "set_default_toolchain" 74 "set_defaults" "split_list" "string_join" "string_split" "template" "tool" 75 "toolchain" "propagates_configs" "write_file")) 76 77(defvar gn-font-lock-predefined-var-keywords 78 '("current_cpu" "current_os" "current_toolchain" "default_toolchain" 79 "host_cpu" "host_os" "invoker" "python_path" "root_build_dir" "root_gen_dir" 80 "root_out_dir" "target_cpu" "target_gen_dir" "target_name" "target_os" 81 "target_out_dir")) 82 83(defvar gn-font-lock-var-keywords 84 '("all_dependent_configs" "allow_circular_includes_from" "arflags" "args" 85 "asmflags" "assert_no_deps" "bundle_deps_filter" "bundle_executable_dir" 86 "bundle_resources_dir" "bundle_root_dir" "cflags" "cflags_c" "cflags_cc" 87 "cflags_objc" "cflags_objcc" "check_includes" "code_signing_args" 88 "code_signing_outputs" "code_signing_script" "code_signing_sources" 89 "complete_static_lib" "configs" "data" "data_deps" "defines" "depfile" 90 "deps" "framework_dir" "frameworks" "include_dirs" "inputs" "ldflags" 91 "lib_dirs" "libs" "output_dir" "output_extension" "output_name" 92 "output_prefix_override" "outputs" "pool" "precompiled_header" 93 "precompiled_header_type" "precompiled_source" "product_type" "public" 94 "public_configs" "public_deps" "response_file_contents" "script" "sources" 95 "testonly" "visibility" "write_runtime_deps" "bundle_contents_dir" 96 "contents" "output_conversion" "rebase" "data_keys" "walk_keys")) 97 98(defconst gn-font-lock-keywords 99 `((,(regexp-opt gn-font-lock-reserved-keywords 'words) . 100 font-lock-keyword-face) 101 (,(regexp-opt gn-font-lock-target-declaration-keywords 'words) . 102 font-lock-type-face) 103 (,(regexp-opt gn-font-lock-buildfile-fun-keywords 'words) . 104 font-lock-function-name-face) 105 ;; pool() as a function 106 ("\\<\\(pool\\)\\s-*(" 107 (1 font-lock-function-name-face)) 108 (,(regexp-opt gn-font-lock-predefined-var-keywords 'words) . 109 font-lock-constant-face) 110 (,(regexp-opt gn-font-lock-var-keywords 'words) . 111 font-lock-variable-name-face) 112 ;; $variables_like_this 113 ("\\(\\$\\)\\([a-zA-Z0-9_]+\\)" 114 (1 'gn-embedded-variable-boundary t) 115 (2 'gn-embedded-variable t)) 116 ;; ${variables_like_this} 117 ("\\(\\${\\)\\([^\n }]+\\)\\(}\\)" 118 (1 'gn-embedded-variable-boundary t) 119 (2 'gn-embedded-variable t) 120 (3 'gn-embedded-variable-boundary t)) 121 ;; {{placeholders}} (see substitute_type.h) 122 ("\\({{\\)\\([^\n }]+\\)\\(}}\\)" 123 (1 'gn-embedded-variable-boundary t) 124 (2 'gn-embedded-variable t) 125 (3 'gn-embedded-variable-boundary t)))) 126 127(defun gn-smie-rules (kind token) 128 "These are slightly modified indentation rules from the SMIE 129 Indentation Example info page. This changes the :before rule 130 and adds a :list-intro to handle our x = [ ] syntax." 131 (pcase (cons kind token) 132 (`(:elem . basic) gn-indent-basic) 133 (`(,_ . ",") (smie-rule-separator kind)) 134 (`(:list-intro . "") gn-indent-basic) 135 (`(:before . ,(or `"[" `"(" `"{")) 136 (if (smie-rule-hanging-p) (smie-rule-parent))) 137 (`(:before . "if") 138 (and (not (smie-rule-bolp)) (smie-rule-prev-p "else") 139 (smie-rule-parent))))) 140 141(defun gn-fill-paragraph (&optional justify) 142 "We only fill inside of comments in GN mode." 143 (interactive "P") 144 (or (fill-comment-paragraph justify) 145 ;; Never return nil; `fill-paragraph' will perform its default behavior 146 ;; if we do. 147 t)) 148 149(defun gn-run-format () 150 "Run 'gn format' on the buffer in place." 151 (interactive) 152 ;; We can't `save-excursion' here; that will put us at the beginning of the 153 ;; shell output, aka the beginning of the document. 154 (let ((my-start-line (line-number-at-pos))) 155 (shell-command-on-region (point-min) (point-max) gn-format-command nil t) 156 (goto-line my-start-line))) 157 158(defvar gn-mode-map 159 (let ((map (make-sparse-keymap))) 160 (define-key map "\C-c\C-f" 'gn-run-format) 161 map)) 162 163;;;###autoload 164(define-derived-mode gn-mode prog-mode "GN" 165 "Major mode for editing gn (Generate Ninja)." 166 :group 'gn 167 168 (setq-local comment-use-syntax t) 169 (setq-local comment-start "#") 170 (setq-local comment-end "") 171 (setq-local indent-tabs-mode nil) 172 173 (setq-local fill-paragraph-function 'gn-fill-paragraph) 174 175 (setq-local font-lock-defaults '(gn-font-lock-keywords)) 176 177 ;; For every 'rule("name") {', adds "name" to the imenu for quick navigation. 178 (setq-local imenu-generic-expression 179 '((nil "^\s*[a-zA-Z0-9_]+(\"\\([a-zA-Z0-9_]+\\)\")\s*{" 1))) 180 181 (smie-setup nil #'gn-smie-rules) 182 (setq-local smie-indent-basic gn-indent-basic) 183 184 ;; python style comment: “# …” 185 (modify-syntax-entry ?# "< b" gn-mode-syntax-table) 186 (modify-syntax-entry ?\n "> b" gn-mode-syntax-table) 187 (modify-syntax-entry ?_ "w" gn-mode-syntax-table)) 188 189;;;###autoload 190(add-to-list 'auto-mode-alist '("\\.gni?\\'" . gn-mode)) 191 192(provide 'gn-mode) 193