1
0
Fork 0

[oni-csharp] Add command to add a file to ‘.csproj’

This commit is contained in:
Tom Willemse 2023-05-25 00:18:35 -07:00
parent 4a8c54f922
commit 67159d71e5

View file

@ -4,7 +4,7 @@
;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local
;; Version: 2022.0202.115001
;; Version: 2023.0525.001714
;; Package-Requires: (csharp-mode oni-company oni-flycheck oni-yasnippet oni-hydra oni-lsp oni-smartparens)
;; This program is free software; you can redistribute it and/or modify
@ -223,5 +223,37 @@ _cs_: Solution
;;;###autoload
(add-to-list 'auto-mode-alist `(,(rx ".sln" eos) . csharp-solution-mode))
(defun oni-csharp-add-file-to-project ()
(interactive)
(let* ((file-name (buffer-file-name))
(project-dir (locate-dominating-file file-name
(lambda (file)
(if (file-directory-p file)
(directory-files file nil (rx ".csproj" eos))
(string-suffix-p ".csproj" file)))))
(project-files (directory-files project-dir nil (rx ".csproj" eos)))
(project-file (cl-case (length project-files)
(0 (error "No project files found in %s" project-dir))
(1 (car project-files))
(t (completing-read "Which project file? " project-files))))
(relative-file-name (file-relative-name file-name project-dir))
(project-file-absolute-path (expand-file-name project-file project-dir)))
(with-current-buffer (find-file-noselect project-file-absolute-path)
(save-excursion
(re-search-forward (rx "<Compile"))
(end-of-line)
(when (and buffer-file-read-only
(vc-registered project-file-absolute-path))
(vc-checkout project-file-absolute-path))
(insert "\n" "<Compile Include=\"" (string-replace "/" "\\" relative-file-name) "\" />")
(re-search-backward (rx "<ItemGroup>"))
(forward-line)
(let ((start (line-beginning-position)))
(re-search-forward (rx "</ItemGroup>"))
(forward-line -1)
(indent-region start (line-end-position))
(sort-lines nil start (line-end-position)))
(save-buffer)))))
(provide 'oni-csharp)
;;; oni-csharp.el ends here