forked from TxGVNN/emacs-k8s-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8s-mode.el
130 lines (104 loc) · 3.85 KB
/
k8s-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;;; k8s-mode.el --- Major mode for Kubernetes configuration file -*- lexical-binding: t -*-
;; Copyright (C) 2019 Giap Tran <txgvnn@gmail.com>
;; Author: Giap Tran <txgvnn@gmail.com>
;; URL: https://github.com/TxGVNN/emacs-k8s-mode
;; Version: 0.1.0
;; Package-Requires: ((emacs "24.3") (yaml-mode "0.0.10"))
;;; Commentary:
;; After open Kubernetes file, you have to M-x k8s-mode to enable this major
;; Put # -*- mode: k8s -*- in first line of file, if you want to autoload.
;;
;; If you're using yas-minor-mode and want to enable on k8s-mode
;; (add-hook 'k8s-mode-hook #'yas-minor-mode)
;;
;; With use-package style
;; (use-package k8s-mode
;; :ensure t
;; :config
;; (setq k8s-search-documentation-browser-function 'browse-url-firefox)
;; :hook (k8s-mode . yas-minor-mode))
;;; Code:
(require 'yaml-mode)
(defgroup k8s nil
"Major mode of K8s configuration file."
:group 'languages
:prefix "k8s-")
(defcustom k8s-mode-hook nil
"*Hook run by `k8s-mode'."
:type 'hook
:group 'k8s)
(defcustom k8s-mode-lighter "K8s"
"K8s-mode lighter."
:type 'string
:group 'k8s)
(defcustom k8s-indent-offset 2
"The tab width to use when indenting."
:type 'integer
:group 'k8s)
(defvar k8s-keywords
'("kind"))
(defvar k8s-imenu-generic-expression
'(("kind: " "^kind:\\(.*\\)" 1)))
(defvar k8s-font-lock-keywords
`((,(regexp-opt k8s-keywords) . font-lock-builtin-face)
,@yaml-font-lock-keywords))
;; Yasnippet
(defconst k8s-dir (file-name-directory (or load-file-name
buffer-file-name)))
(defconst k8s-snip-dir (expand-file-name "snippets" k8s-dir))
;; Completion
(defun k8s-complete-at-point ()
"Perform keyword completion on word before cursor."
(let* ((end (point))
(begin (save-excursion
(skip-chars-backward "^ \n\r\t,:")
(point))))
(list begin end k8s-keywords
:exclusive 'no
:company-docsig #'identity)))
;; Documents
(defcustom k8s-site-docs-url "https://kubernetes.io/docs/reference/generated/kubernetes-api/"
"Default kubernetes.io site URL, the URL to use open docs."
:group 'k8s
:type 'string)
(defcustom k8s-site-docs-version "v1.21"
"Default version API."
:group 'k8s
:type 'string)
(defcustom k8s-search-documentation-browser-function nil
"Function to display K8S documentation in a WWW browser.
If non-nil, this shadows the value of `browse-url-browser-function' when
calling `k8s-search-documentation'. This should be X11 browser as
`browse-url-mozilla`, `browse-url-chromium`"
:group 'k8s
:type '(choice (const :tag "default" nil) function)
:link '(variable-link browse-url-browser-function))
(defun k8s-browse-documentation-url (url)
"Browse a documentation URL using the configured browser function.
See `k8s-search-documentation-browser-function'."
(let ((browse-url-browser-function
(or k8s-search-documentation-browser-function
browse-url-browser-function)))
(browse-url url)))
(defsubst k8s-search-web-documentation ()
"Return Kubernetes docs URL."
(k8s-browse-documentation-url (concat k8s-site-docs-url k8s-site-docs-version)))
(defun k8s-goto-documents ()
"Go to Kubernetes documentations."
(interactive)
(k8s-search-web-documentation))
;;;###autoload
(define-derived-mode k8s-mode yaml-mode k8s-mode-lighter
"Major mode for editing Kubernetes configuration file."
(font-lock-add-keywords nil k8s-font-lock-keywords)
;; indentation
(set (make-local-variable 'yaml-indent-offset) k8s-indent-offset)
;; imenu
(set (make-local-variable 'yaml-imenu-generic-expression) k8s-imenu-generic-expression)
;; completion
(make-local-variable 'completion-at-point-functions)
(push 'k8s-complete-at-point completion-at-point-functions))
(eval-after-load 'yasnippet
'(when (file-directory-p k8s-snip-dir) (yas-load-directory k8s-snip-dir)))
(provide 'k8s-mode)
;;; k8s-mode.el ends here