Add language detection to YAML scalar editing

Integrated language-detection to automatically detect and set major modes based on buffer/string content. Enhanced YAML editing with language detection for buffer initialization.
This commit is contained in:
Jeremy Dormitzer 2024-07-24 10:26:33 -04:00
parent d60fa4e873
commit 3141955f9c
2 changed files with 89 additions and 0 deletions

View File

@ -174,4 +174,82 @@
(evil-local-set-key 'normal (kbd "zM") #'origami-toggle-all-nodes-same-level))
(add-hook 'origami-mode-hook #'origami-setup))
;; Language detection of arbitrary strings/buffers
(use-package language-detection
:commands (language-detection-buffer
language-detection-string)
:init
(defun language-detection-detect-mode (string)
(let* ((map '((ada ada-mode)
(awk awk-mode)
(c c-mode)
(cpp c++-mode)
(clojure clojure-mode lisp-mode)
(csharp csharp-mode java-mode)
(css css-mode)
(dart dart-mode)
(delphi delphi-mode)
(emacslisp emacs-lisp-mode)
(erlang erlang-mode)
(fortran fortran-mode)
(fsharp fsharp-mode)
(go go-mode)
(groovy groovy-mode)
(haskell haskell-mode)
(html html-mode)
(java java-mode)
(javascript javascript-mode)
(json json-mode javascript-mode)
(latex latex-mode)
(lisp lisp-mode)
(lua lua-mode)
(matlab matlab-mode octave-mode)
(objc objc-mode c-mode)
(perl perl-mode)
(php php-mode)
(prolog prolog-mode)
(python python-mode)
(r r-mode)
(ruby ruby-mode)
(rust rust-mode)
(scala scala-mode)
(shell shell-script-mode)
(smalltalk smalltalk-mode)
(sql sql-mode)
(swift swift-mode)
(visualbasic visual-basic-mode)
(xml sgml-mode)))
(language (language-detection-string string))
(modes (cdr (assoc language map)))
(mode (cl-loop for mode in modes
when (fboundp mode)
return mode)))
(when (fboundp mode)
mode)))
(defun fontify-with-mode (mode text)
(with-temp-buffer
(insert text)
(delay-mode-hooks (funcall mode))
(font-lock-default-function mode)
(font-lock-default-fontify-region (point-min) (point-max) nil)
(buffer-string)))
(defun fontify-using-faces (text)
(let ((pos 0))
(while (setq next (next-single-property-change pos 'face text))
(put-text-property pos next 'font-lock-face (get-text-property pos 'face text) text)
(setq pos next))
(add-text-properties 0 (length text) '(fontified t) text)
text))
(defun language-detection-fontify-region (start end)
(interactive "r")
(let* ((text (buffer-substring-no-properties start end))
(mode (language-detection-detect-mode text))
(fontified (fontify-using-faces (fontify-with-mode mode text))))
(delete-region start end)
(insert fontified)))
(defun language-detection-fontify-buffer ()
(interactive)
(language-detection-fontify-region (point-min) (point-max))))
(provide 'init-editing)

View File

@ -9,6 +9,17 @@
(use-package yaml-pro
:when (treesit-ready-p 'yaml)
:hook (yaml-ts-mode . yaml-pro-ts-mode)
:config
(defun yaml-pro-edit-initialize-buffer-filter-args-advice (args)
(if-let ((mode (language-detection-detect-mode (buffer-string))))
(cl-destructuring-bind (parent-buffer buffer initial-text type initialize path) args
(let ((init-func (lambda ()
(funcall mode)
(when initialize
(call-interactively initialize)))))
(list parent-buffer buffer initial-text type init-func path)))
args))
(advice-add 'yaml-pro-edit-initialize-buffer :filter-args #'yaml-pro-edit-initialize-buffer-filter-args-advice)
:general
(yaml-pro-ts-mode-map [remap evil-backward-section-begin] #'yaml-pro-ts-prev-subtree
[remap evil-forward-section-begin] #'yaml-pro-ts-next-subtree