From ac6121486845963000096bec0d81b81742f13702 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Mon, 6 May 2013 10:19:39 +0400 Subject: [PATCH] Close #19 --- NEWS.md | 2 ++ company-clang.el | 19 ++++++++++++++++++- company.el | 18 +++++++++--------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/NEWS.md b/NEWS.md index db8cbe052..d8a0c9113 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## Next +* `company-backends`: `prefix` command can return `t` in the cdr. +* `company-clang-begin-after-member-access`: New option. * Mouse click outside the tooltip aborts completion. * `company-clang` uses standard input to pass the contents of current buffer to Clang 2.9+, otherwise saves the buffer and passes the path to the file. diff --git a/company-clang.el b/company-clang.el index 947afe239..6abebe7e3 100644 --- a/company-clang.el +++ b/company-clang.el @@ -38,6 +38,12 @@ "Location of clang executable." :type 'file) +(defcustom company-clang-begin-after-member-access t + "When non-nil, automatic completion will start whenever the current symbol +is preceded by \".\" or \"->\", ignoring `company-minimum-prefix-length'. + +If `company-begin-commands' is a list, it should include `c-electric-lt-gt'.") + (defcustom company-clang-arguments nil "Additional arguments to pass to clang when completing. Prefix files (-include ...) can be selected with @@ -201,6 +207,17 @@ Prefix files (-include ...) can be selected with prefix (company-clang--build-complete-args (- (point) (length prefix))))) +(defun company-clang--prefix () + (let ((symbol (company-grab-symbol))) + (if symbol + (if (and company-clang-begin-after-member-access + (save-excursion + (forward-char (- (length symbol))) + (looking-back "\\.\\|->" (- (point) 3)))) + (cons symbol t) + symbol) + 'stop))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defconst company-clang-required-version 1.1) @@ -263,7 +280,7 @@ input." buffer-file-name company-clang-executable (not (company-in-string-or-comment)) - (or (company-grab-symbol) 'stop))) + (company-clang--prefix))) (candidates (company-clang--candidates arg)) (meta (gethash arg company-clang--meta-cache)) (crop (and (string-match ":\\|(" arg) diff --git a/company.el b/company.el index 2a9b9db32..901aac1cb 100644 --- a/company.el +++ b/company.el @@ -280,13 +280,12 @@ Each back-end is a function that takes a variable number of arguments. The first argument is the command requested from the back-end. It is one of the following: -`prefix': The back-end should return the text to be completed. It must be -text immediately before `point'. Returning nil passes control to the next -back-end. The function should return 'stop if it should complete but cannot -\(e.g. if it is in the middle of a string\). If the returned value is only -part of the prefix (e.g. the part after \"->\" in C), the back-end may return a -cons of prefix and prefix length, which is then used in the -`company-minimum-prefix-length' test. +`prefix': The back-end should return the text to be completed. It must be text +immediately before `point'. Returning nil passes control to the next back-end. +The function should return 'stop if it should complete but cannot \(e.g. if it +is in the middle of a string\). Instead of a string, the back-end may return a +cons where car is the prefix and cdr is used in `company-minimum-prefix-length' +test. It's either number or t, in which case the test automatically succeeds. `candidates': The second argument is the prefix to be completed. The return value should be a list of candidates that start with the prefix. @@ -951,8 +950,9 @@ can retrieve meta-data for them." (defun company--good-prefix-p (prefix) (and (or (company-explicit-action-p) (unless (eq prefix 'stop) - (>= (or (cdr-safe prefix) (length prefix)) - company-minimum-prefix-length))) + (or (eq (cdr-safe prefix) t) + (>= (or (cdr-safe prefix) (length prefix)) + company-minimum-prefix-length)))) (stringp (or (car-safe prefix) prefix)))) (defun company--continue () -- 2.39.2