branch: master
commit 7f3ae9011c658c1d5d03559e291688ca1deb60f7
Author: Dmitry Gutov <[email protected]>
Commit: Dmitry Gutov <[email protected]>
Add option company-clang-use-compile-flags-txt
Closes #933
---
NEWS.md | 2 ++
company-clang.el | 27 ++++++++++++++++++++++++++-
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/NEWS.md b/NEWS.md
index 9ffc8a3..7c01c1f 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -2,6 +2,8 @@
## Next
+* New user option `company-clang-use-compile-flags-txt`
+ ([#933](https://github.com/company-mode/company-mode/issues/933)).
* Support for completion style specific sorting (Emacs 27 feature).
* Snippet/template field interaction is inhibited while completion is active
(where by default `TAB` calls `company-complete-common`, clashing with
snippet
diff --git a/company-clang.el b/company-clang.el
index d764905..55d9d9b 100644
--- a/company-clang.el
+++ b/company-clang.el
@@ -48,6 +48,13 @@ and `c-electric-colon', for automatic completion right after
\">\" and
\":\"."
:type 'boolean)
+(defcustom company-clang-use-compile-flags-txt nil
+ "When non-nil, use flags from compile_flags.txt if present.
+
+The lines from that files will be appended to `company-clang-arguments'."
+ :type 'boolean
+ :safe 'booleanp)
+
(defcustom company-clang-arguments nil
"Additional arguments to pass to clang when completing.
Prefix files (-include ...) can be selected with `company-clang-set-prefix'
@@ -249,13 +256,31 @@ or automatically through a custom
`company-clang-prefix-guesser'."
(append '("-fsyntax-only" "-Xclang" "-code-completion-macros")
(unless (company-clang--auto-save-p)
(list "-x" (company-clang--lang-option)))
- company-clang-arguments
+ (company-clang--arguments)
(when (stringp company-clang--prefix)
(list "-include" (expand-file-name company-clang--prefix)))
(list "-Xclang" (format "-code-completion-at=%s"
(company-clang--build-location pos)))
(list (if (company-clang--auto-save-p) buffer-file-name "-"))))
+(defun company-clang--arguments ()
+ (let ((fname "compile_flags.txt")
+ (args company-clang-arguments))
+ (when company-clang-use-compile-flags-txt
+ (let ((dir (locate-dominating-file default-directory fname)))
+ (when dir
+ (with-temp-buffer
+ (insert-file-contents (expand-file-name fname dir))
+ (setq args
+ (append
+ args
+ (split-string (buffer-substring-no-properties
+ (point-min) (point-max))
+ "[\n\r]+"
+ t
+ "[ \t]+")))))))
+ args))
+
(defun company-clang--candidates (prefix callback)
(and (company-clang--auto-save-p)
(buffer-modified-p)