Question: Namespaces autocompletion
Hi.
Is there any way to search and insert class namespaces, like auto-complete for use keyword?
i not know what you say . show me exmaple please:>
Thanks for answer.
namespace Tests;
class Test {
public function load($id) {
// UserModel namespace is \App\Models\UserModel
// How do I automatically insert "use App\Models\UserModel" to file header?
UserModel::find($id);
}
}
@firstrow This makes no sense and is a job for the php-mode Emacs extension if ANYTHING.
"ac-php" is an autocompleter to find function definitions.
"php-mode" is an extension responsible for PHP code formatting etc...
Furthermore: Why would you automatically insert "\App\Models\UserModel" when your file is in "namespace Tests"? Trying to guess that is dangerous. It would need some kind of map of the whole filesystem and find that file.
Either way this is not a job for ac-php, you can close this @xcwen...
It's a job for "php-mode" or for a brand new, different emacs plugin. Not a job for ac-php.
Edit: I thought about it again. Here is ONE area where it IS a good idea: Instead of being "automatic" (dangerous), make it manual... Perhaps add a new command such as (ac-php-autocomplete-classname) which takes the word at CURSOR and shows a POPUP MENU with "Match A, Match B, etc" and you PICK the one you want -- and THEN it adds the correct class "use blabla" at the top of the file... That way you can safely bind this feature to a keyboard key. Then just type some stuff... Then press the key, and since ac-php already knows the whole project map it can show all classes that match it! As long as it is not fully automatic, and always asks the user to pick the choice from a menu (even if just 1 choice), then it is safe since it lets us verify the choice/mapping first. :)
Also: after you select a class from the popup, ac-php should check a few things, and NOT insert use X; if any of these are already true: 1. don't insert anything if the target class is a sub-class of the current namespace (for example namespace Foo; Bar $x; does not need use Foo\Bar; since it is already in the same namespace), 2. don't insert anything if it already exists in the "use". :)
@aitte2 yes, popup with matches and manual selection is better option.
So here is code I came up with. It helps me to save a lot of time. It would be nice to add support for helm/ido also and make custom variable to choose "popup" completion frontend. Can you guys help with this? I also create a new PR so anybody interested can help me to finish this feature.
(defun ac-php-add-use ()
(interactive)
(save-excursion
(ac-php-goto-use)
(end-of-line)
(newline)
;; get rid of leading slash and insert use statement
(insert (format "use %s;" (subseq (ac-php-classes-popup) 1)))))
(defun ac-php-classes-popup ()
(ivy-completing-read "Enter class name: " (ac-php-g--class-map (ac-php-get-tags-data)) nil nil ""))
(defun ac-php-goto-use ()
(interactive)
(xref-push-marker-stack)
(goto-char (point-min))
;; try to find first "namespace" keyword to add new "use"s after it.
(re-search-forward "^namespace [a-zA-Z0-9\].*;$" nil t)
;; find last "use" keyword.
(while (re-search-forward "^use [a-zA-Z0-9\].*;$" nil t)))
(evil-leader/set-key-for-mode 'php-mode "ia" 'ac-php-add-use)
(evil-leader/set-key-for-mode 'php-mode "ig" 'ac-php-goto-use)
Cool. I have no time to help but I love that you are making progress. Awesome!