Skip to content

Instantly share code, notes, and snippets.

@erickgnavar
Created November 1, 2022 15:38
Show Gist options
  • Save erickgnavar/fdb5abc207c89b8b1477ede0ab917dbc to your computer and use it in GitHub Desktop.
Save erickgnavar/fdb5abc207c89b8b1477ede0ab917dbc to your computer and use it in GitHub Desktop.
(require 'tree-sitter)
(require 'seq)
(require 'cl-lib)
;;TODO: mapp grammar for other langauges and complete grammar for haskell
(setq modes-mapping '((haskell-mode . (exp_apply exp_infix exp_in exp_cond exp_literal))))
(defun expr-at-point ()
"Highlight expression at point."
(interactive)
;; remove all previous applied overlays
(remove-overlays (point-min) (point-max) 'face 'tree-sitter-query-match)
(when-let* ((node (get-text-node-at-point))
(overlay (make-overlay (tsc-node-start-position node) (tsc-node-end-position node))))
;; Ensure the overlay is deleted when it becomes empty.
(overlay-put overlay 'evaporate t)
;; defined in tsc package
(overlay-put overlay 'face 'tree-sitter-query-match)))
(defun get-text-node-at-point ()
"Get text node at point using predefined major mode options."
(let ((types (alist-get major-mode modes-mapping)))
(seq-some (lambda (x) (tree-sitter-node-at-pos x (point) t)) types)))
(defun mouse-click-handler (event)
"Run `expr-at-point' using information of EVENT."
(interactive "e")
(save-excursion
(goto-char (posn-point (event-start event)))
(expr-at-point)))
(global-set-key [mouse-1] #'mouse-click-handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment