aboutsummaryrefslogtreecommitdiffstats
path: root/oni-csharp
diff options
context:
space:
mode:
authorGravatar Tom Willemse2023-05-25 00:18:35 -0700
committerGravatar Tom Willemse2023-05-25 00:18:35 -0700
commit67159d71e5cecbb58922b62be2886a6ce17bf1d1 (patch)
tree283d04db280fb4f49668d0a516631658bb4dbbb8 /oni-csharp
parent4a8c54f922a241ab298e8ef688ea10ebf9988b5f (diff)
downloademacs-config-67159d71e5cecbb58922b62be2886a6ce17bf1d1.tar.gz
emacs-config-67159d71e5cecbb58922b62be2886a6ce17bf1d1.zip
[oni-csharp] Add command to add a file to ‘.csproj’
Diffstat (limited to 'oni-csharp')
-rw-r--r--oni-csharp/oni-csharp.el34
1 files changed, 33 insertions, 1 deletions
diff --git a/oni-csharp/oni-csharp.el b/oni-csharp/oni-csharp.el
index 1eb20a4..8451d93 100644
--- a/oni-csharp/oni-csharp.el
+++ b/oni-csharp/oni-csharp.el
@@ -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