Skip to content

Commit

Permalink
global, enh: unify internal API usage and check if database exists be…
Browse files Browse the repository at this point in the history
…fore getting identifiers
  • Loading branch information
AmaiKinono committed May 8, 2024
1 parent b362354 commit fe670ff
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions citre-global.el
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ database in the project directory."
(defun citre-global--get-output-lines (args)
"Get output from global program.
ARGS is the arguments passed to the program."
(citre-get-output-lines
(append (list (or citre-global-program "global"))
args)))
(let ((prog (or citre-global-program "global")))
(when (citre-executable-find prog)
(citre-get-output-lines (append (list prog) args)))))

(defun citre-global--get-lines (name &optional mode case-fold start-file)
"Find tags related to NAME using global and return the outputed lines.
Expand All @@ -105,7 +105,6 @@ result by nearness (see the help message of global) start from
START-FILE."
(let* ((name (when name (substring-no-properties name)))
cmd)
(push (or citre-global-program "global") cmd)
(pcase mode
('completion (push "--completion" cmd))
('definition (push "--definition" cmd))
Expand All @@ -127,18 +126,15 @@ START-FILE."
"--result=grep"
"--literal"
"--" name)))
(citre-get-output-lines cmd)))
(citre-global--get-output-lines cmd)))

(defun citre-global--get-tag-lines-in-file (&optional file)
"Find definitions in FILE using global and return the outputed lines.
The output is in cxref format. FILE can be absolute or relative
to `default-directory', and is the current file if it's nil."
(let ((file (or file (file-relative-name buffer-file-name))))
(citre-get-output-lines
(list (or citre-global-program "global")
"--file"
file))))
(citre-global--get-output-lines (list "--file" file))))

(defun citre-global--parse-path (path)
"Translate escaped sequences in PATH.
Expand Down Expand Up @@ -212,17 +208,16 @@ database of current directory.
When the global program is not found on the machine, return nil
as global program is needed to get the database path."
(when (citre-executable-find (or citre-global-program "global") t)
(pcase citre--global-dbpath
('none nil)
((and val (pred stringp) (pred citre-dir-exists-p)) val)
(_ (let ((default-directory (or dir default-directory)))
(condition-case nil
(setq citre--global-dbpath
(car (citre-global--get-output-lines
'("--print-dbpath"))))
(error (setq citre--global-dbpath 'none)
nil)))))))
(pcase citre--global-dbpath
('none nil)
((and val (pred stringp) (pred citre-dir-exists-p)) val)
(_ (let ((default-directory (or dir default-directory)))
(condition-case nil
(setq citre--global-dbpath
(car (citre-global--get-output-lines
'("--print-dbpath"))))
(error (setq citre--global-dbpath 'none)
nil))))))

(defun citre-global-clear-dbpath-cache ()
"Clear the cache of buffer -> global database path.
Expand Down Expand Up @@ -347,8 +342,9 @@ See *citre-global-update* buffer" s))))

(defun citre-global-get-identifiers ()
"Get a list of identifiers in current project."
(citre-global--get-output-lines
'("--completion")))
(when (citre-global-dbpath)
(citre-global--get-output-lines
'("--completion"))))

;;;; Completion backend

Expand Down

0 comments on commit fe670ff

Please sign in to comment.