Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/akirak/github-linguist.el

Run GitHub Linguist on projects to collect information
https://github.com/akirak/github-linguist.el

Last synced: 7 days ago
JSON representation

Run GitHub Linguist on projects to collect information

Awesome Lists containing this project

README

        

* github-linguist.el
# Add CI badges here
[[https://melpa.org/#/github-linguist][file:https://melpa.org/packages/github-linguist-badge.svg]]
[[https://github.com/akirak/github-linguist.el/actions/workflows/lint.yml][file:https://github.com/akirak/github-linguist.el/actions/workflows/lint.yml/badge.svg]]

This is a library that detects languages of your repositories using [[https://github.com/github/linguist][GitHub Linguist]].
You can use the data for various purposes, such as annotating completion candidates, grouping projects, etc.

It integrates with project.el, either shipped with Emacs 28 or later, or from [[http://elpa.gnu.org/packages/project.html][GNU ELPA]].
** Installation
You can install =github-linguist= package from [[https://melpa.org/#/][MELPA]].

You also will need an executable of linguist.
Set =github-linguist-executable= variable to the name of the executable.
** Usage
*** Collecting information
To display languages in the current project directory, run =github-linguist-run= command.

To scan all known projects (as per =project-known-project-roots=), run =github-linguist-update-projects=.
It would be possible to perform scanning periodically using =run-with-idle-timer=:

#+begin_src emacs-lisp
;; Run every time the user is away for 5 minutes
(run-with-idle-timer 300 nil #'github-linguist-update-projects)
#+end_src
*** Using the information
=github-linguist-lookup= function returns the languages of a given project directory.
This function can be used, for example, for annotating completion candidates:

#+begin_src emacs-lisp
(defun akirak-prompt-project-root (prompt)
"Select a project root with a PROMPT string."
(completing-read prompt (akirak-project-root-completions
(project-known-project-roots))
nil t))

;; Based on `project--file-completion-table' from project.el 0.8.1, but with a
;; different category.
(defun akirak-project-root-completions (roots)
"Return a completion table for project ROOTS."
(lambda (string pred action)
(if (eq action 'metadata)
'(metadata . ((category . project-root)
(annotation-function . akirak-project-root-annotator)))
(complete-with-action action roots string pred))))

(defun akirak-project-root-annotator (root)
(when-let (language-alist (github-linguist-lookup root))
(cl-labels
((dim (str) (propertize str 'face 'font-lock-comment-face))
(propertize-name (str) (propertize str 'face 'marginalia-string)) )
(concat (dim " (")
(mapconcat #'propertize-name
(thread-last language-alist
(seq-take-while (pcase-lambda (`(,_language . ,percent))
(> percent 30.0)))
(mapcar #'car))
(dim ", "))
(dim ")")))))
#+end_src

Below is an example screenshot:

[[https:/raw.githubusercontent.com/akirak/github-linguist.el/screenshots/completion.png][https://raw.githubusercontent.com/akirak/github-linguist.el/screenshots/completion.png]]
** See also
- [[https://github.com/lassik/emacs-language-id][language-id]] provides mappings from language names to Emacs major modes.