aboutsummaryrefslogtreecommitdiffstats
path: root/oni-csharp/oni-csharp.el
blob: 8451d93b122344b4071fbaaee8a8c38d711b3eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
;;; oni-csharp.el --- C# Configuration                   -*- lexical-binding: t; -*-

;; Copyright (C) 2019  Tom Willemse

;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local
;; 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
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My C# configuration. This configuration enables ‘rainbow-delimiters-mode’.

;;; Code:

(require 'company)
(require 'csharp-mode)
(require 'hydra)
(require 'yasnippet)

(defconst oni-csharp-root
  (file-name-directory
   (or load-file-name
       (buffer-file-name)))
  "The directory where ‘oni-csharp’ was loaded from.")

(defconst oni-csharp-snippets-dir
  (expand-file-name "snippets" oni-csharp-root)
  "The directory where ‘oni-csharp’ stores its snippets.")

(defcustom oni-csharp-devenv-executable
  "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\Common7\\IDE\\devenv.com\""
  "The location of the devenv.com executable."
  :group 'oni-csharp)

(defvar oni-csharp--projects (make-hash-table :test 'equal))

(defhydra oni-csharp-hydra (:color teal :hint nil)
  "
^Compile^
^^------------
_cp_: Project
_cs_: Solution
^^
"
  ("cp" oni-csharp-compile-project)
  ("cs" oni-csharp-compile-solution))

(defun oni-csharp-compile-solution ()
  (interactive)
  (let ((compile-command (concat oni-csharp-devenv-executable " /Build Debug " (oni-csharp-locate-dominating-file-wildcard "*.sln"))))
    (compile compile-command)))

(defun oni-csharp-locate-dominating-file-wildcard (filename)
  (let (file-list
        (default-directory default-directory))
    (while (and (null file-list)
                (string-prefix-p (projectile-project-root) default-directory))
      (setq file-list (file-expand-wildcards filename t)
            default-directory (file-name-directory (directory-file-name default-directory))))
    (car file-list)))

(defun oni-csharp-compile-project ()
  (interactive)
  (let ((compile-command (concat oni-csharp-devenv-executable " /Build Debug " (oni-csharp-locate-dominating-file-wildcard "*.sln") " /project " (oni-csharp-locate-dominating-file-wildcard "*.csproj"))))
    (compile compile-command)))

(defun oni-csharp--auto-fill-mode ()
  "Enable ‘auto-fill-mode’ only for comments."
  (setq-local comment-auto-fill-only-comments t)
  (auto-fill-mode))

(defun oni-csharp-snippets-initialize ()
  "Initialize the snippets for ‘oni-csharp’."
  (when (boundp 'yas-snippet-dirs)
    (add-to-list 'yas-snippet-dirs oni-csharp-snippets-dir t))
  (yas-load-directory oni-csharp-snippets-dir))

(defun oni-csharp--update-style ()
  "Set style rules according to personal (or recommended) preference."
  (c-set-offset 'arglist-intro '+)
  (c-set-offset 'arglist-cont-nonempty '+))

(add-hook 'csharp-mode-hook 'abbrev-mode)
(add-hook 'csharp-mode-hook 'company-mode)
(add-hook 'csharp-mode-hook 'display-fill-column-indicator-mode)
(add-hook 'csharp-mode-hook 'electric-indent-local-mode)
(add-hook 'csharp-mode-hook 'flycheck-mode)
(add-hook 'csharp-mode-hook 'lsp)
(add-hook 'csharp-mode-hook 'oni-csharp--auto-fill-mode)
(add-hook 'csharp-mode-hook 'oni-csharp--update-style)
(add-hook 'csharp-mode-hook 'rainbow-delimiters-mode)
(add-hook 'csharp-mode-hook 'smartparens-mode)

(define-key csharp-mode-map (kbd "C-c SPC") 'oni-csharp-hydra/body)

(define-abbrev csharp-mode-abbrev-table "and" "&&" nil :system t)
(define-abbrev csharp-mode-abbrev-table "or" "||" nil :system t)

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.xaml\\'" . nxml-mode))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.targets\\'" . nxml-mode))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.proj\\'". nxml-mode))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.csproj\\'" . nxml-mode))

(with-eval-after-load 'autoinsert
  (add-to-list 'auto-insert-alist
               `(,(rx ".proj" string-end)
                 nil
                 "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" ToolsVersion=\"4.0\">\n  " _ "\n</Project>")))

(with-eval-after-load 'lsp-csharp
  (when (eq system-type 'windows-nt)
    (setq lsp-csharp-omnisharp-roslyn-server-dir
          (expand-file-name "~/scoop/apps/omnisharp/current"))))

(with-eval-after-load 'compile
  (add-to-list 'compilation-error-regexp-alist
               `(,(rx (minimal-match (one-or-more digit)) ">"
                      (group (any alpha)
                             ":\\"
                             (minimal-match (one-or-more (any alpha "\\" "."))))
                      "("
                      (group (minimal-match (one-or-more digit)))
                      ","
                      (group (minimal-match (one-or-more digit)))
                      ","
                      (group (minimal-match (one-or-more digit)))
                      ","
                      (group (minimal-match (one-or-more digit)))
                      "): "
                      (group "error" (one-or-more any)))
                 1 (2 . 4) (3 . 5) 2 6)))

(with-eval-after-load 'csharp-mode
  (with-eval-after-load 'yasnippet
    (oni-csharp-snippets-initialize)))

;;; Solution mode

(defun oni-csharp-collect-project-names ()
  "Go through the current buffer and collect all the project names."
  (interactive)
  (clrhash oni-csharp--projects)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward
            (rx bol
                "Project(\"{"
                (group (minimal-match (1+ (or alphanumeric "-"))))
                "}\") = \""
                (group (minimal-match (1+ any)))
                "\", \""
                (minimal-match (1+ any))
                "\", \"{"
                (group (minimal-match (1+ any)))
                "}\""
                eol)
            nil
            :noerror)
      (puthash (match-string-no-properties 3)
               (match-string-no-properties 2)
               oni-csharp--projects))))

(defun oni-csharp-display-project-names ()
  "Display the project names in the current buffer."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward
            (rx bol
                (zero-or-more whitespace)
                (group
                 "{"
                 (group (minimal-match (one-or-more (or alphanumeric "-"))))
                 "}")
                " = "
                (group
                 "{"
                 (group (minimal-match (one-or-more (or alphanumeric "-"))))
                 "}")
                eol)
            nil
            :noerror)
      (add-text-properties
       (match-beginning 1) (match-end 1)
       (list 'display (gethash (match-string 2) oni-csharp--projects)
             'face 'font-lock-variable-name-face
             'help-echo (buffer-substring-no-properties
                         (match-beginning 1) (match-end 1))
             'intangile nil))
      (add-text-properties
       (match-beginning 3) (match-end 3)
       (list 'display (gethash (match-string 4) oni-csharp--projects)
             'face 'font-lock-variable-name-face
             'help-echo (buffer-substring-no-properties
                         (match-beginning 3) (match-end 3)))))))

(define-derived-mode csharp-solution-mode prog-mode "Sln"
  "A major mode for viewing and editing C# ‘.sln’ files."
  (oni-csharp-collect-project-names)
  (oni-csharp-display-project-names))

;;;###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