From 520ed2eb8a2a780c433e4db6bbcb4c17d1d4cbf0 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sat, 21 Jun 2025 10:51:53 +0200 Subject: [PATCH] fix: Properly find .NET namespace for a file Looks up the csproj file, assumes project name is same as base of csproj file. Then looks at the subdirectory of the project and sets namespace by concatenating the name of the project with the subdirectory. --- init.el | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/init.el b/init.el index 2d2b7cc..4117eab 100644 --- a/init.el +++ b/init.el @@ -1273,15 +1273,45 @@ minibuffer, even without explicitly focusing it." (defun find-git-repo (dir) (if (string= "/" dir) nil - (if (file-exists-p (expand-file-name "../.git/" dir)) + (if (file-exists-p (expand-file-name "./.git/" dir)) dir - (find-git-repo (expand-file-name "../" dir))))) + (find-git-repo (file-name-parent-directory dir))))) + + (defun get-csproj-in-directory (dir) + (car (directory-files dir + :mach-regexp ".*\\.csproj" + :count 1))) + + ;; Finds .csproj above current buffer directory, and returns path to the csproj + (defun find-csproj (dir root) + (if (string-match-p (regexp-quote root) dir) + (let ((csproj (get-csproj-in-directory dir))) + (if csproj + csproj + (find-csproj (file-name-parent-directory dir) root))) + nil)) + + (defun find-current-csproj () + (let ((root (find-project-root)) + (dir (file-name-directory buffer-file-name))) + (find-csproj dir root))) + + (defun extract-project-name () + (interactive) + (let ((csproj (find-current-csproj))) + (file-name-base csproj))) (defun file-path-to-namespace () (interactive) - (let ((root (find-project-root)) - (base (file-name-nondirectory buffer-file-name))) - (substring (replace-regexp-in-string "/" "\." (substring buffer-file-name (length root) (* -1 (length base))) t t) 0 -1)))) + (let ((root (file-name-directory (find-current-csproj))) + (base (file-name-nondirectory buffer-file-name)) + (project-name (extract-project-name))) + (concat + project-name + "." + (substring + (replace-regexp-in-string "/" "\." (substring buffer-file-name (length root) (* -1 (length base))) t t) + 0 -1))))) ;; Sh, Bash -- 2.49.0