~ruther/emacs.d

520ed2eb8a2a780c433e4db6bbcb4c17d1d4cbf0 — Rutherther 10 days ago 3235e7b
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.
1 files changed, 35 insertions(+), 5 deletions(-)

M init.el
M init.el => init.el +35 -5
@@ 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


Do not follow this link