]> git.donarmstrong.com Git - lib.git/commitdiff
first stab at working with tree-sitter parsing
authorDon Armstrong <don@donarmstrong.com>
Sun, 1 Jan 2023 19:07:08 +0000 (11:07 -0800)
committerDon Armstrong <don@donarmstrong.com>
Sun, 1 Jan 2023 19:07:08 +0000 (11:07 -0800)
emacs_el/configuration/don-configuration.org

index 003a8188062e24dbba26cc85a0113b40fe1087f5..a00859a30e33b6a2cf4a23f4b631d3e1d7e374ab 100644 (file)
   (add-hook 'minibuffer-exit-hook #'don/minibuffer-exit-hook)
 #+END_SRC
 * Modules
+** Tree sitter
+#+BEGIN_SRC emacs-lisp
+(use-package tree-sitter
+  :ensure t
+  :defer 1
+  :config
+  (global-tree-sitter-mode)
+  )
+(use-package tree-sitter-langs
+  :ensure t
+  :defer 1
+  :after tree-sitter
+  :config
+  (defun dla/python-function-at-point ()
+    "Return a list of function arguments
+
+Borrowed from https://xenodium.com/emacs-generate-a-swift-initializer/
+"
+  (interactive)
+  (cl-assert (seq-contains local-minor-modes 'tree-sitter-mode) "tree-sitter-mode not enabled")
+  (let* ((node (tree-sitter-node-at-point 'function_definition)
+               )
+         (args)
+         (arg)
+         (ret))
+    (unless node
+      (error "Not in function"))
+    (mapc
+     (lambda (item)
+       (cond ((eq 'func_name
+                  (car item))
+              ; (when arg
+              ;   (setq args (append args (list arg)))
+              ;   )
+              (setq arg (list (cons 'function (tsc-node-text
+                                           (cdr item))))))
+             ((eq 'ident
+                  (car item))
+              (setq arg (map-insert arg 'ident (tsc-node-text
+                                               (cdr item)))))
+             ((eq 'ident_type
+                  (car item))
+              (setq arg (map-insert arg 'ident_type (tsc-node-text
+                                               (cdr item)))))
+             ((eq 'ret_type
+                  (car item))
+              (setq arg (map-insert arg 'ret_type (tsc-node-text
+                                               (cdr item)))))
+             ))
+     (tsc-query-captures
+      (tsc-make-query tree-sitter-language
+                      "(function_definition (identifier) @func_name (parameters [(identifier) @ident (typed_parameter (identifier) @ident (type) @ident_type)]) (type)? @ret_type)")
+      (tree-sitter-node-at-point 'function_definition) nil))
+    (when arg
+      (setq args (append args (list arg))))
+    args))
+
+)
+#+END_SRC
 ** Spacemacs theme
 #+BEGIN_SRC emacs-lisp
 (use-package spacemacs-common