From 7f7627b4fbaa5bb9b32230c26641dc306b8c7b9c Mon Sep 17 00:00:00 2001 From: Thuna Date: Thu, 6 Oct 2022 17:13:36 +0200 Subject: [PATCH 1/3] Allow specifying project name in the dirconfig file * projectile.el (projectile-dirconfig-name-prefix): Add option. (projectile-project-default-project-name): Use the project name in dirconfig when given, use the directory name otherwise. (projectile-parse-dirconfig-file): Parse and return the project name as the fourth element. --- projectile.el | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/projectile.el b/projectile.el index 8bcb39646..909f39ee1 100644 --- a/projectile.el +++ b/projectile.el @@ -371,6 +371,15 @@ Similar to '#' in .gitignore files." :type 'character :package-version '(projectile . "2.2.0")) +(defcustom projectile-dirconfig-name-prefix + nil + "Projectile config file (.projectile) name start marker. +If specified, starting a line in a project's .projectile file with +`projectile-dirconfig-name-prefix' followed by this character marks that +line as the name of the project instead of a pattern." + :group 'projectile + :type 'character) + (defcustom projectile-globally-ignored-files (list projectile-tags-file-name) "A list of files globally ignored by projectile. @@ -1299,7 +1308,9 @@ explicitly." (defun projectile-default-project-name (project-root) "Default function used to create the project name. The project name is based on the value of PROJECT-ROOT." - (file-name-nondirectory (directory-file-name project-root))) + (or (let ((default-directory project-root)) + (nth 3 (projectile-parse-dirconfig-file))) + (file-name-nondirectory (directory-file-name project-root)))) (defun projectile-project-name (&optional project) "Return project name. @@ -1954,7 +1965,7 @@ Strings starting with + will be added to the list of directories to keep, and strings starting with - will be added to the list of directories to ignore. For backward compatibility, without a prefix the string will be assumed to be an ignore string." - (let (keep ignore ensure (dirconfig (projectile-dirconfig-file))) + (let (name keep ignore ensure (dirconfig (projectile-dirconfig-file))) (when (projectile-file-exists-p dirconfig) (with-temp-buffer (insert-file-contents dirconfig) @@ -1965,7 +1976,9 @@ prefix the string will be assumed to be an ignore string." (and projectile-dirconfig-comment-prefix (eql leading-char projectile-dirconfig-comment-prefix)))) - nil) + (when (eql projectile-dirconfig-name-prefix + (char-after (1+ (point)))) + (setq name (buffer-substring (+ (point) 2) (line-end-position))))) (?+ (push (buffer-substring (1+ (point)) (line-end-position)) keep)) (?- (push (buffer-substring (1+ (point)) (line-end-position)) ignore)) (?! (push (buffer-substring (1+ (point)) (line-end-position)) ensure)) @@ -1976,7 +1989,8 @@ prefix the string will be assumed to be an ignore string." (mapcar #'string-trim (delete "" (reverse ignore))) (mapcar #'string-trim - (delete "" (reverse ensure))))))) + (delete "" (reverse ensure))) + name)))) (defun projectile-expand-root (name) "Expand NAME to project root. From 2191583215a2ddbda9a99aa58796fd88a5bec9f4 Mon Sep 17 00:00:00 2001 From: Thuna Date: Thu, 6 Oct 2022 17:35:43 +0200 Subject: [PATCH 2/3] Correct tests for `projectile-parse-dirconfig-file' * test/projectile-test.el (projectile-parse-dirconfig-file): Expect the fourth element representing the project name. This is just to make sure the tests succeed. A proper test for project names should be written. --- test/projectile-test.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/projectile-test.el b/test/projectile-test.el index 875b01ec5..5ea3478bd 100644 --- a/test/projectile-test.el +++ b/test/projectile-test.el @@ -422,7 +422,7 @@ Just delegates OPERATION and ARGS for all operations except for`shell-command`'. "no-prefix" "left-wspace" "right-wspace") - nil)) + nil nil)) ;; same test - but with comment lines enabled using prefix '#' (let ((projectile-dirconfig-comment-prefix ?#)) (expect (projectile-parse-dirconfig-file) :to-equal '(("include/") @@ -430,7 +430,7 @@ Just delegates OPERATION and ARGS for all operations except for`shell-command`'. "no-prefix" "left-wspace" "right-wspace") - nil))) + nil nil))) )) (describe "projectile-get-project-directories" From e3a74a6e4cfadb4feb4107e81454d7b7a667e61f Mon Sep 17 00:00:00 2001 From: Thuna Date: Tue, 25 Oct 2022 17:46:05 +0200 Subject: [PATCH 3/3] Improve performance for `projectile-default-project-name' * projecile.el (projectile-default-project-name): Search only for the name instead of calling `projectile-parse-dirconfig-file' everytime. --- projectile.el | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/projectile.el b/projectile.el index 909f39ee1..8e30eebdd 100644 --- a/projectile.el +++ b/projectile.el @@ -1308,8 +1308,18 @@ explicitly." (defun projectile-default-project-name (project-root) "Default function used to create the project name. The project name is based on the value of PROJECT-ROOT." - (or (let ((default-directory project-root)) - (nth 3 (projectile-parse-dirconfig-file))) + (or (when-let* + ((comment-prefix projectile-dirconfig-comment-prefix) + (name-prefix projectile-dirconfig-name-prefix) + (default-directory project-root) + (dirconfig (projectile-dirconfig-file))) + (when (projectile-file-exists-p dirconfig) + (with-temp-buffer + (insert-file-contents dirconfig) + (when (re-search-forward + (format "^%c%c\\(.*\\)$" comment-prefix name-prefix) + nil t) + (match-string 1))))) (file-name-nondirectory (directory-file-name project-root)))) (defun projectile-project-name (&optional project)