|
| 1 | +;;; lsp-postgres.el --- Postgres client settings. -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2025 Shen, Jen-Chieh |
| 4 | + |
| 5 | +;; This file is not part of GNU Emacs. |
| 6 | + |
| 7 | +;; This program is free software: you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation, either version 3 of the License, or |
| 10 | +;; (at your option) any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;; You should have received a copy of the GNU General Public License |
| 18 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;;; Commentary: |
| 21 | +;; |
| 22 | +;; LSP client for Postgres language server. |
| 23 | +;; |
| 24 | + |
| 25 | +;;; Code: |
| 26 | + |
| 27 | +(require 'lsp-mode) |
| 28 | + |
| 29 | +(defgroup lsp-postgres nil |
| 30 | + "LSP support for SQL, using Postgres language server." |
| 31 | + :group 'lsp-mode |
| 32 | + :link '(url-link "https://github.com/supabase-community/postgres-language-server") |
| 33 | + :package-version `(lsp-mode . "9.0.1")) |
| 34 | + |
| 35 | +(defcustom lsp-postgres-server-path nil |
| 36 | + "Path points for Postgres language server. |
| 37 | +
|
| 38 | +This is only for development use." |
| 39 | + :type 'string |
| 40 | + :group 'lsp-postgres) |
| 41 | + |
| 42 | +(defcustom lsp-postgres-server-store-path |
| 43 | + (expand-file-name "postgres-ls/" lsp-server-install-dir) |
| 44 | + "The path to the file in which Postgres ls will be stored." |
| 45 | + :type 'file |
| 46 | + :group 'lsp-postgres) |
| 47 | + |
| 48 | +(defconst lsp-postgres-download-url-format |
| 49 | + "https://github.com/supabase-community/postgres-language-server/releases/latest/download/postgrestools_%s-%s" |
| 50 | + "Format to the download url link.") |
| 51 | + |
| 52 | +(defun lsp-postgres--postgres-ls-url () |
| 53 | + "Return Url points to the zls' zip/tar file." |
| 54 | + (let* ((x86 (string-prefix-p "x86_64" system-configuration)) |
| 55 | + (arch (if x86 "x86_64" "aarch64"))) |
| 56 | + (cl-case system-type |
| 57 | + ((cygwin windows-nt ms-dos) |
| 58 | + (format lsp-postgres-download-url-format arch "pc-windows-msvc")) |
| 59 | + (darwin |
| 60 | + (format lsp-postgres-download-url-format arch "apple-darwin")) |
| 61 | + (gnu/linux |
| 62 | + (format lsp-postgres-download-url-format arch "unknown-linux-gnu"))))) |
| 63 | + |
| 64 | +(defun lsp-postgres--server-command () |
| 65 | + "Generate startup command for Postgres language server." |
| 66 | + (list (or lsp-postgres-server-path |
| 67 | + (lsp-package-path 'postgres-ls)) |
| 68 | + "lsp-proxy")) |
| 69 | + |
| 70 | +(lsp-dependency |
| 71 | + 'postgres-ls |
| 72 | + '(:system "postgres-ls") |
| 73 | + `(:download :url ,(lsp-postgres--postgres-ls-url) |
| 74 | + :store-path ,(f-join lsp-postgres-server-store-path |
| 75 | + (pcase system-type |
| 76 | + ('windows-nt "postgrestools.exe") |
| 77 | + (_ "postgrestools"))) |
| 78 | + :set-executable? t)) |
| 79 | + |
| 80 | +(lsp-register-client |
| 81 | + (make-lsp-client |
| 82 | + :new-connection (lsp-stdio-connection #'lsp-postgres--server-command) |
| 83 | + :major-modes '(sql-mode) |
| 84 | + :priority -2 |
| 85 | + :multi-root t |
| 86 | + :server-id 'postgres-ls |
| 87 | + :download-server-fn (lambda (_client callback error-callback _update?) |
| 88 | + (lsp-package-ensure 'postgres-ls callback error-callback)))) |
| 89 | + |
| 90 | +(lsp-consistency-check lsp-postgres) |
| 91 | + |
| 92 | +(provide 'lsp-postgres) |
| 93 | +;;; lsp-postgres.el ends here |
0 commit comments