From bc1e86d1c863a3846ec4fea02be213ddb3622b2f Mon Sep 17 00:00:00 2001 From: benj Date: Sat, 22 Dec 2018 18:46:33 -0800 Subject: add util to read compile_commands for clang-be --- init.el | 4 ++++ utils/fetch-includes.el | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 utils/fetch-includes.el diff --git a/init.el b/init.el index 27a7b7b..5e73a7d 100644 --- a/init.el +++ b/init.el @@ -69,6 +69,7 @@ haskell-mode highlight-escape-sequences ido-vertical-mode + json magit markdown-mode modern-cpp-font-lock @@ -141,6 +142,9 @@ (require 'setup-yaml-mode) (require 'setup-yasnippet) +;; utility globals +(require 'fetch-includes) + ;; full power (put 'downcase-region 'disabled nil) (put 'upcase-region 'disabled nil) diff --git a/utils/fetch-includes.el b/utils/fetch-includes.el new file mode 100644 index 0000000..ce386af --- /dev/null +++ b/utils/fetch-includes.el @@ -0,0 +1,59 @@ +;;; fetch-includes.el --- add headers to clang checker -*- lexical-binding: t; -*- + +;; Copyright (C) 2018 Benj Bellon + +;; Author: Benj Bellon +;; Keywords: lisp + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: + +;; Parses the compile_commands.json db and adds headers +;; to company-mode's company-clang backend. + +;;; Code: + +(require 'json) +(require 'dash) + +(defun get-commands (json-file) + "Retrieves list of command strings possibly including header paths. +JSON-FILE is the path to the json-file to parse." + (let* ((json-object-type 'hash-table) + (json-array-type 'list) + (json-key-type 'string) + (json (json-read-file json-file))) + (mapcar + (function (lambda (el) (gethash "command" el))) + json))) + +(defun get-include-paths (commands-list) + "Retrieve the include paths from a list of string which may be include paths. +COMMANDS-LIST a list of commands" + (remove nil + (mapcar + (function (lambda (cmd) (if (string-match "-I" cmd) cmd))) + (split-string commands-list)))) + +(defun company-clang-header-paths (db-path) + "Provides list of include paths to add to company-clang backend. +DB-PATH the path to compile_commands.json" + (-flatten (mapcar + (function (lambda(cmd-list) + (get-include-paths cmd-list))) + (get-commands db-path)))) + +(provide 'fetch-includes) +;;; fetch-includes.el ends here -- cgit v1.2.3