aboutsummaryrefslogtreecommitdiffstats
path: root/scratch
diff options
context:
space:
mode:
Diffstat (limited to 'scratch')
-rw-r--r--scratch/semgrep/semgrep.el63
1 files changed, 40 insertions, 23 deletions
diff --git a/scratch/semgrep/semgrep.el b/scratch/semgrep/semgrep.el
index 5752a20..37ba800 100644
--- a/scratch/semgrep/semgrep.el
+++ b/scratch/semgrep/semgrep.el
@@ -23,6 +23,9 @@
(require 'org-roam-db)
(require 'dash)
+;; Silence byte-compiler.
+(defvar url-http-end-of-headers)
+
(defun semgrep--connect (method data)
(let ((url-request-method "POST")
(url-request-extra-headers '(("Content-Type" . "application/json")))
@@ -35,15 +38,18 @@
(json-read))))
(defun semgrep--get-node-id (paragraph &optional default)
- (thread-first
- (org-element-map
- (org-element-property :parent paragraph)
- 'node-property
- (lambda (np)
- (org-element-property :value np)))
- (car)
- (org-string-nw-p)
- (or default)))
+ (-->
+ (org-element-map
+ (org-element-property :parent paragraph)
+ 'node-property
+ (lambda (np)
+ (cons
+ (org-element-property :key np)
+ (org-element-property :value np))))
+ (assoc 'ID it #'string=)
+ (cdr it)
+ (org-string-nw-p it)
+ (or it default)))
(defun semgrep--prepare-paragraph (file-id)
(lambda (paragraph)
@@ -66,7 +72,7 @@
;; (json-serialize it)
;; (f-write it 'utf-8 "/tmp/out.json")
;; (message "%S" it)
- (semgrep--connect :store it))
+ (semgrep--connect :insert it))
(user-error "This only works on org-mode")))
(defun semgrep--roam-data (entries)
@@ -76,12 +82,24 @@
entries)
(delete-dups)
(vconcat)
- (org-roam-db-query [:select [id title]
+ (org-roam-db-query [:select [id title file]
:from nodes
:where (in id $v1)])))
+(defun semgrep-pick-org-element ()
+ (when-let ((context (ignore-errors (org-element-context))))
+ (filter-buffer-substring (org-element-property :begin context)
+ (org-element-property :end context))))
+
+(defun semgrep--sync-db ()
+ (org-roam-dolist-with-progress (file (nreverse (org-roam-list-files)))
+ "importing to semantic search"
+ (org-roam-with-file file nil
+ (semgrep--add-buffer))))
+
+;; (semgrep--sync-db)
(defun semgrep-search (text)
- (interactive (list (or (thing-at-point 'paragraph)
+ (interactive (list (or (semgrep-pick-org-element)
(read-from-minibuffer "What are you looking for? "))))
(-let (((&alist 'distances 'documents 'metadatas)
(semgrep--connect :query text)))
@@ -94,23 +112,22 @@
(let ((data (semgrep--roam-data entry-metadatas)))
(cl-mapc
(lambda (d paragraph meta)
- (let* ((node-id (or (alist-get 'node-id meta) ""))
- (title (cadr (assoc node-id data #'string=))))
- (unless (zerop d)
+ (unless (zerop d)
+ (-let* ((node-id (alist-get 'node-id meta))
+ ((_ title file) (assoc node-id data #'string=))
+ (pos
+ (with-temp-buffer
+ (insert-file-contents file)
+ (line-number-at-pos (or (alist-get 'start-point meta) 1)))))
(insert
- (format "* [[id:%s][%s]]\n" node-id title)
+ (format "* [[file:%s::%d][%s]]\n" file pos title)
"- Distance :: " (number-to-string d) "\n"
- "- point :: " (number-to-string (or (alist-get 'start-point meta) -1)) "\n"
- (string-trim paragraph) ?\n))))
+ paragraph ?\n)
+ (fill-paragraph))))
entry-distances entry-document entry-metadatas)))
distances documents metadatas)
(goto-char (point-min))
(display-buffer (current-buffer)))))
-;; (org-roam-dolist-with-progress (file (org-roam-list-files))
-;; "importing to semantic search"
-;; (org-roam-with-file file nil
-;; (semgrep--add-buffer)))
-
(provide 'semgrep)
;;; semgrep.el ends here