From bf28ac7e4e8a80daae955fba5b02bbd2b0ea5d67 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Mon, 5 Sep 2022 15:27:00 +1000 Subject: Adding elisp client. --- lisp/hcel-client.el | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 lisp/hcel-client.el (limited to 'lisp/hcel-client.el') diff --git a/lisp/hcel-client.el b/lisp/hcel-client.el new file mode 100644 index 0000000..68f9507 --- /dev/null +++ b/lisp/hcel-client.el @@ -0,0 +1,149 @@ +;; -*- lexical-binding: t; -*- +(defcustom hcel-endpoint "localhost:8080" + "hcel endpoint" + :group 'hcel) +(defcustom hcel-indexed-dir "/.haskell-code-explorer" + "hcel indexed dir" + :group 'hcel) + +(defvar hcel-client-buffer-name "*hcel-client*") + +(defun hcel-api-packages () + (let ((packages + (hcel-url-fetch-json (concat hcel-endpoint "/api/packages")))) + (mapcan + (lambda (package) + (mapcar + (lambda (version) (list (cons 'name (alist-get 'name package)) + (cons 'version version))) + (alist-get 'versions package))) + packages))) + +(defun hcel-api-package-info (package-id) + (mapcar + (lambda (tuple) + (prin1-to-string (car tuple) t)) + (alist-get + 'modules + (hcel-url-fetch-json (concat + hcel-endpoint "/files/" (hcel-format-package-id package-id "-") + hcel-indexed-dir "/packageInfo.json"))))) + +(defun hcel-api-definition-site + (package-id component-id module-name entity name) + (hcel-url-fetch-json + (concat hcel-endpoint "/api/definitionSite/" + (hcel-format-package-id package-id "-") + "/" component-id "/" module-name "/" entity "/" name))) + +(defun hcel-definition-site-location-info (approx-location-info) + "Call definitionSite with info from an approximate location." + (when (string= (hcel-location-tag approx-location-info) "ExactLocation") + (error "An ExactLocation supplied.")) + (when-let* ((package-id (alist-get 'packageId approx-location-info)) + (component-id (alist-get 'componentId approx-location-info)) + (module-name (alist-get 'moduleName approx-location-info)) + (entity (alist-get 'entity approx-location-info)) + (name (alist-get 'name approx-location-info))) + (hcel-api-definition-site package-id component-id module-name entity name))) + +(defun hcel-api-module-info (package-id module-path) + (hcel-url-fetch-json + (concat + hcel-endpoint "/files/" (hcel-format-package-id package-id "-") + hcel-indexed-dir + "/" (replace-regexp-in-string "/" "%252F" module-path) ".json.gz") + t)) + +(defun hcel-api-expressions + (package-id module-path line-beg col-beg line-end col-end) + (hcel-url-fetch-json + (concat + hcel-endpoint "/api/expressions/" (hcel-format-package-id package-id "-") + "/" (replace-regexp-in-string "/" "%2F" module-path) + "/" (number-to-string (1+ line-beg)) + "/" (number-to-string (1+ col-beg)) + "/" (number-to-string (1+ line-end)) + "/" (number-to-string (1+ col-end))))) + +(defun hcel-api-hoogle-docs (package-id module-name entity name) + (hcel-url-fetch-json + (concat hcel-endpoint "/api/hoogleDocs/" + (hcel-format-package-id package-id "-") "/" + module-name "/" entity "/" name))) + +(defun hcel-format-pagination-query (page per-page) + (when (or page per-page) + (concat "?" + (string-join + (list + (when page (concat "page=" page)) + (when per-page (concat "per_page=" per-page))) + (when (and page per-page) "&"))))) + +(defun hcel-api-references (package-id name &optional page per-page) + (hcel-url-fetch-json + (concat hcel-endpoint "/api/references/" + (hcel-format-package-id package-id "-") "/" + name + (hcel-format-pagination-query page per-page)))) + +(defun hcel-api-identifiers (scope query package-id &optional page per-page + with-header) + (hcel-url-fetch-json + (concat hcel-endpoint + (if (eq scope 'global) + "/api/globalIdentifiers/" + (concat "/api/identifiers/" + (hcel-format-package-id package-id "-") + "/")) + query + (hcel-format-pagination-query page per-page)) + nil with-header)) + +(defun hcel-api-global-references (name) + (hcel-url-fetch-json (concat hcel-endpoint "/api/globalReferences/" name))) + +(defun http-status (header) + (save-excursion + (goto-char (point-min)) + (string-match "^HTTP.*\\([0-9]\\{3\\}\\).*$" header) + (match-string 1 header))) + +(defun parse-http-header (text) + (let ((status) (fields)) + (with-temp-buffer + (insert text) + (goto-char (point-min)) + (re-search-forward "^HTTP.*\\([0-9]\\{3\\}\\).*$") + (setq status (match-string 1)) + (while (re-search-forward "^\\(.*?\\): \\(.*\\)$" nil t) + (push (cons (intern (match-string 1)) (match-string 2)) fields))) + (list (cons 'status status) (cons 'fields fields)))) + +(defun hcel-url-fetch-json (url &optional decompression with-header) + (with-current-buffer (get-buffer-create hcel-client-buffer-name) + (goto-char (point-max)) + (insert "[" (current-time-string) "] Request: " url "\n")) + (with-current-buffer (url-retrieve-synchronously url t) + (let ((header) (status) (fields) (json)) + (delete-http-header) + (setq header (parse-http-header (car kill-ring)) + status (alist-get 'status header) + fields (alist-get 'fields header)) + (with-current-buffer hcel-client-buffer-name + (insert "[" (current-time-string) "] Response: " status "\n")) + (when decompression + (call-process-region (point) (point-max) "gunzip" t t t) + (goto-char (point-min))) + (call-interactively 'delete-trailing-whitespace) + (if (string= status "200") + (unless (= (point) (point-max)) + (if with-header + (list + (cons 'header fields) + (cons 'json (json-read))) + (json-read))) + (error "HTTP error: %s" (buffer-substring (point) (point-max))))))) + +(provide 'hcel-client) -- cgit v1.2.3