-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcsharp-mode.el
More file actions
2766 lines (2300 loc) · 109 KB
/
csharp-mode.el
File metadata and controls
2766 lines (2300 loc) · 109 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; csharp-mode.el --- C# mode derived mode
;; Author : Dylan R. E. Moonfire (original)
;; Maintainer : Dino Chiesa <dpchiesa@hotmail.com>
;; Created : Feburary 2005
;; Modified : April 2010
;; Version : 0.7.6
;; Keywords : c# languages oop mode
;; X-URL : http://code.google.com/p/csharpmode/
;; Last-saved : <2010-May-24 21:53:58>
;;
;; 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 2 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; This is a separate mode to implement the C# constructs and
;; font-locking. It is based on the java-mode example from cc-mode.
;;
;; csharp-mode requires CC Mode 5.30 or later. It works with
;; cc-mode 5.31.3, which is current at this time.
;;
;; Features:
;;
;; - font-lock and indent of C# syntax including:
;; all c# keywords and major syntax
;; attributes that decorate methods, classes, fields, properties
;; enum types
;; #if/#endif #region/#endregion
;; instance initializers
;; anonymous functions and methods
;; verbatim literal strings (those that begin with @)
;; generics
;;
;; - automagic code-doc generation when you type three slashes.
;;
;; - intelligent inserttion of matched pairs of curly braces.
;;
;; - sets the compiler regex for next-error, for csc.exe output.
;;
;;
;;; To use:
;;
;; put this in your .emacs:
;;
;; (autoload 'csharp-mode "csharp-mode" "Major mode for editing C# code." t)
;;
;; or:
;;
;; (require 'csharp-mode)
;;
;;
;; AND:
;;
;; (setq auto-mode-alist
;; (append '(("\\.cs$" . csharp-mode)) auto-mode-alist))
;; (defun my-csharp-mode-fn ()
;; "function that runs when csharp-mode is initialized for a buffer."
;; ...insert your code here...
;; ...most commonly, your custom key bindings ...
;; )
;; (add-hook 'csharp-mode-hook 'my-csharp-mode-fn t)
;;
;;
;;; Known Bugs:
;;
;; Leading identifiers are no longer being fontified, for some reason.
;; See matchers-before.
;;
;; Method names with a preceding attribute are not fontified.
;;
;; The symbol followng #if is not fontified. It should be treated like
;; define and get font-lock-variable-name-face .
;;
;; This code doesn't seem to work when you compile it, then
;; load/require in the emacs file. You will get an error (error
;; "`c-lang-defconst' must be used in a file") which happens because
;; cc-mode doesn't think it is in a buffer while loading directly
;; from the init. However, if you call it based on a file extension,
;; it works properly. Interestingly enough, this doesn't happen if
;; you don't byte-compile cc-mode.
;;
;;
;;
;; Todo:
;;
;; Get csharp-mode.el accepted as part of the emacs standard distribution.
;; Must contact monnier at iro.umontreal.ca to make this happen.
;;
;;
;;
;; Acknowledgements:
;;
;; Thanks to Alan Mackenzie and Stefan Monnier for answering questions
;; and making suggestions. And to Trey Jackson for sharing his
;; knowledge of emacs lisp.
;;
;;
;;; Versions:
;;
;; 0.1.0 - Initial release.
;; 0.2.0 - Fixed the identification on the "enum" keyword.
;; - Fixed the font-lock on the "base" keyword
;; 0.3.0 - Added a regex to fontify attributes. It isn't the
;; the best method, but it handles single-like attributes
;; well.
;; - Got "super" not to fontify as a keyword.
;; - Got extending classes and interfaces to fontify as something.
;; 0.4.0 - Removed the attribute matching because it broke more than
;; it fixed.
;; - Corrected a bug with namespace not being properly identified
;; and treating the class level as an inner object, which screwed
;; up formatting.
;; - Added "partial" to the keywords.
;; 0.5.0 - Found bugs with compiled cc-mode and loading from init files.
;; - Updated the eval-when-compile to code to let the mode be
;; compiled.
;; 0.6.0 - Added the c-filter-ops patch for 5.31.1 which made that
;; function in cc-langs.el unavailable.
;; - Added a csharp-lineup-region for indention #region and
;; #endregion block differently.
;; 0.7.0 - Added autoload so update-directory-autoloads works
;; (Thank you, Nikolaj Schumacher)
;; - Fontified the entire #region and #endregion lines.
;; - Initial work to get get, set, add, remove font-locked.
;; 0.7.1 - Added option to indent #if/endif with code
;; - Fixed c-opt-cpp-prefix defn (it must not include the BOL
;; char (^).
;; - proper fontification and indent of classes that inherit
;; (previously the colon was confusing the parser)
;; - reclassified namespace as a block beginner
;; - removed $ as a legal symbol char - not legal in C#.
;; - added struct to c-class-decl-kwds so indent is correct
;; within a struct.
;; 0.7.2 - Added automatic codedoc insertion.
;; 0.7.3 - Instance initializers (new Type { ... } ) and
;; (new Type() { ...} ) are now indented properly.
;; - proper fontification and indent of enums as brace-list-*,
;; including special treatment for enums that explicitly
;; inherit from an int type. Previously the colon was
;; confusing the parser.
;; - proper fontification of verbatim literal strings,
;; including those that end in slash. This edge case was not
;; handled at all before; it is now handled correctly.
;; - code cleanup and organization; removed the linefeed.
;; - intelligent curly-brace insertion
;; 0.7.4 - added a C# style
;; - using is now a keyword and gets fontified correctly
;; - fixed a bug that had crept into the codedoc insertion
;; 0.7.5 - now fontify namespaces in the using statements. This is
;; done in the csharp value for c-basic-matchers-before .
;; - also fontify the name following namespace decl.
;; This is done in the csharp value for c-basic-matchers-after .
;; - turn on recognition of generic types. They are now
;; fontified correctly.
;; - <> are now treated as syntactic parens and can be jumped
;; over with c-forward-sexp.
;; - Constructors are now fontified.
;; - Field/Prop names inside object initializers are now fontified.
;;
;;
(require 'cc-mode)
(message (concat "Loading " load-file-name))
;; ==================================================================
;; c# upfront stuff
;; ==================================================================
;; This is a copy of the function in cc-mode which is used to handle
;; the eval-when-compile which is needed during other times.
(defun c-filter-ops (ops opgroup-filter op-filter &optional xlate)
;; See cc-langs.el, a direct copy.
(unless (listp (car-safe ops))
(setq ops (list ops)))
(cond ((eq opgroup-filter t)
(setq opgroup-filter (lambda (opgroup) t)))
((not (functionp opgroup-filter))
(setq opgroup-filter `(lambda (opgroup)
(memq opgroup ',opgroup-filter)))))
(cond ((eq op-filter t)
(setq op-filter (lambda (op) t)))
((stringp op-filter)
(setq op-filter `(lambda (op)
(string-match ,op-filter op)))))
(unless xlate
(setq xlate 'identity))
(c-with-syntax-table (c-lang-const c-mode-syntax-table)
(delete-duplicates
(mapcan (lambda (opgroup)
(when (if (symbolp (car opgroup))
(when (funcall opgroup-filter (car opgroup))
(setq opgroup (cdr opgroup))
t)
t)
(mapcan (lambda (op)
(when (funcall op-filter op)
(let ((res (funcall xlate op)))
(if (listp res) res (list res)))))
opgroup)))
ops)
:test 'equal)))
;; These are only required at compile time to get the sources for the
;; language constants. (The cc-fonts require and the font-lock
;; related constants could additionally be put inside an
;; (eval-after-load "font-lock" ...) but then some trickery is
;; necessary to get them compiled.)
(eval-when-compile
(let ((load-path
(if (and (boundp 'byte-compile-dest-file)
(stringp byte-compile-dest-file))
(cons (file-name-directory byte-compile-dest-file) load-path)
load-path)))
(load "cc-mode" nil t)
(load "cc-fonts" nil t)
(load "cc-langs" nil t)))
(eval-and-compile
;; Make our mode known to the language constant system. Use Java
;; mode as the fallback for the constants we don't change here.
;; This needs to be done also at compile time since the language
;; constants are evaluated then.
(c-add-language 'csharp-mode 'java-mode))
;; ==================================================================
;; end of c# upfront stuff
;; ==================================================================
;; ==================================================================
;; csharp-mode utility and feature defuns
;; ==================================================================
(defun csharp-at-vsemi-p (&optional pos)
"Determines if there is a virtual semicolon at POS or point.
This is the C# version of the function.
A vsemi is a cc-mode concept implying end-of-statement, without
a semicolon or close-brace. This happens in 2 cases in C#:
- after an attribute that decorates a class, method, field, or
property.
- after an ASPNET directive, that appears in a aspx/ashx/ascx file
An example of the former is [WebMethod] or [XmlElement].
An example of the latter is something like this:
<%@ WebHandler Language=\"C#\" Class=\"Handler\" %>
Providing this function allows the indenting in csharp-mode
to work properly with code that includes attributes and ASPNET
directives.
Returns t if at a position where a virtual-semicolon is.
Otherwise nil.
"
(save-excursion
(let ((pos-or-point (progn (if pos (goto-char pos)) (point))))
(cond
;; put a vsemi after an ASPNET directive, like
;; <%@ WebHandler Language="C#" Class="Handler" %>
((looking-back (concat csharp-aspnet-directive-re "$") nil t)
t)
;; put a vsemi after an attribute, as with
;; [XmlElement]
((c-safe (backward-sexp) t)
(cond
((re-search-forward
(concat
"\\(\\["
"[ \t\n\r\f\v]*"
"\\("
"\\(?:[A-Za-z_][[:alnum:]]*\\.\\)*"
"[A-Za-z_][[:alnum:]]*"
"\\)"
"[^]]*\\]\\)"
)
(1+ pos-or-point) t)
(c-safe (backward-sexp))
(c-backward-syntactic-ws)
(cond
((eq (char-before) 93) ;; close sq brace
(csharp-at-vsemi-p (point)))
((or
(eq (char-before) 59) ;; semicolon
(eq (char-before) 123) ;; open curly
(eq (char-before) 125)) ;; close curly
t)
(t nil)))
(t nil)))
(t nil))
)))
(defun csharp-lineup-region (langelem)
"Indent all #region and #endregion blocks inline with code while
retaining normal column-zero indention for #if and the other
processing blocks.
To use this indenting just put the following in your emacs file:
(c-set-offset 'cpp-macro 'csharp-lineup-region)
An alternative is to use `csharp-lineup-if-and-region'.
"
(save-excursion
(back-to-indentation)
(if (re-search-forward "#\\(end\\)?region" (c-point 'eol) [0]) 0 [0])))
(defun csharp-lineup-if-and-region (langelem)
"Indent all #region/endregion blocks and #if/endif blocks inline
with code while retaining normal column-zero indention for any
other processing blocks.
To use this indenting just put the following in your emacs file:
(c-set-offset 'cpp-macro 'csharp-lineup-if-and-region)
Another option is to use `csharp-lineup-region'.
"
(save-excursion
(back-to-indentation)
(if (re-search-forward "#\\(\\(end\\)?\\(if\\|region\\)\\|else\\)" (c-point 'eol) [0]) 0 [0])))
(defun csharp-insert-open-brace ()
"Intelligently insert a pair of curly braces. This fn is most
often bound to the open-curly brace, with
(local-set-key (kbd \"{\") 'csharp-insert-open-brace)
The default binding for an open curly brace in cc-modes is often
`c-electric-brace' or `skeleton-pair-insert-maybe'. The former
can be configured to insert newlines around braces in various
syntactic positions. The latter inserts a pair of braces and
then does not insert a newline, and does not indent.
This fn provides another option, with some additional
intelligence for csharp-mode. When you type an open curly, the
appropriate pair of braces appears, with spacing and indent set
in a context-sensitive manner.
Within a string literal, you just get a pair of braces, and
point is set between them. Following an equals sign, you get
a pair of braces, with a semincolon appended. Otherwise, you
get the open brace on a new line, followed by an empty line
and the closing brace on the line following, with point on
the empty line.
There may be another way to get this to happen appropriately just
within emacs, but I could not figure out how to do it. So I
wrote this alternative.
"
(interactive)
(let
(tpoint
(in-string (string= (csharp-in-literal) "string"))
(preceding3
(save-excursion
(and
(skip-chars-backward " \t")
(> (- (point) 2) (point-min))
(buffer-substring-no-properties (point) (- (point) 3)))))
(one-word-back
(save-excursion
(backward-word 2)
(thing-at-point 'word))))
(cond
;; Case 1: inside a string literal?
;; --------------------------------------------
;; If so, then just insert a pair of braces and put the point
;; between them. The most common case is a format string for
;; String.Format() or Console.WriteLine().
(in-string
(self-insert-command 1)
(insert "}")
(backward-char))
;; Case 2: the open brace starts an array initializer.
;; --------------------------------------------
;; When the last non-space was an equals sign or square brackets,
;; then it's an initializer.
((save-excursion
(and (c-safe (backward-sexp) t)
(looking-at "\\(\\w+\\b *=\\|[[]]+\\)")))
(self-insert-command 1)
(insert " };")
(backward-char 3))
;; Case 3: the open brace starts an instance initializer
;; --------------------------------------------
;; If one-word-back was "new", then it's an object initializer.
((string= one-word-back "new")
(save-excursion
(message "object initializer")
(setq tpoint (point)) ;; prepare to indent-region later
(newline)
(self-insert-command 1)
(newline-and-indent)
(newline)
(insert "};")
(c-indent-region tpoint (point))
(previous-line)
(indent-according-to-mode)
(end-of-line)
(setq tpoint (point)))
(goto-char tpoint))
;; Case 4: a lambda initialier.
;; --------------------------------------------
;; If the open curly follows =>, then it's a lambda initializer.
((string= (substring preceding3 -2) "=>")
(message "lambda init")
(self-insert-command 1)
(insert " }")
(backward-char 2))
;; else, it's a new scope. (if, while, class, etc)
(t
(save-excursion
(message "new scope")
(set-mark (point)) ;; prepare to indent-region later
;; check if the prior sexp is on the same line
(if (save-excursion
(let ((curline (line-number-at-pos))
(aftline (progn
(if (c-safe (backward-sexp) t)
(line-number-at-pos)
-1))))
(= curline aftline)))
(newline-and-indent))
(self-insert-command 1)
(c-indent-line-or-region)
(end-of-line)
(newline)
(insert "}")
;;(c-indent-command) ;; not sure of the difference here
(c-indent-line-or-region)
(previous-line)
(end-of-line)
(newline-and-indent)
;; point ends up on an empty line, within the braces, properly indented
(setq tpoint (point)))
(goto-char tpoint)))))
;; ==================================================================
;; end of csharp-mode utility and feature defuns
;; ==================================================================
;; ==================================================================
;; c# values for "language constants" defined in cc-langs.el
;; ==================================================================
;; Java uses a series of regexes to change the font-lock for class
;; references. The problem comes in because Java uses Pascal (leading
;; space in names, SomeClass) for class and package names, but
;; Camel-casing (initial lowercase, upper case in words,
;; i.e. someVariable) for variables. The notation suggested by EMCA for C# is
;; to use Pascal notation for everything, except inner variables. So,
;; the Java regex and formatting produces very wrong results in C#.
;;(error (byte-compile-dest-file))
;;(error (c-get-current-file))
(defconst csharp-aspnet-directive-re
"<%@.+?%>"
"Regex for matching directive blocks in ASP.NET files (.aspx, .ashx, .ascx)")
(defconst csharp-enum-decl-re
(concat
"\\<enum[ \t\n\r\f\v]+"
"\\([[:alpha:]_][[:alnum:]_]*\\)"
"[ \t\n\r\f\v]*"
"\\(:[ \t\n\r\f\v]*"
"\\("
(c-make-keywords-re nil
(list "sbyte" "byte" "short" "ushort" "int" "uint" "long" "ulong"))
"\\)"
"\\)?")
"Regex that captures an enum declaration in C#"
)
;; X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+
;; vsemi's allow proper indentation of code that includes inline
;; attributes and ASPNET directives. These are c#-specific things that
;; need custom treatment.
(c-lang-defconst c-at-vsemi-p-fn
csharp 'csharp-at-vsemi-p)
;; This c-opt-after-id-concat-key is a regexp that matches
;; dot. In other words: "\\(\\.\\)"
;; Not sure why this needs to be so complicated.
;; This const is now internal (obsolete); need to move to
;; c-after-id-concat-ops. I don't yet understand the meaning
;; of that variable, so for now. . . .
(c-lang-defconst c-opt-after-id-concat-key
csharp (if (c-lang-const c-opt-identifier-concat-key)
(c-lang-const c-symbol-start)))
;; The matchers elements can be of many forms. It gets pretty
;; complicated. Do a describe-variable on font-lock-keywords to get a
;; description. (Why on font-lock-keywords? I don't know, but that's
;; where you get the help.)
;;
;; Aside from the provided documentation, the other option of course, is
;; to look in the source code as an example for what to do. The source
;; in cc-fonts uses a defun c-make-font-lock-search-function to produce
;; most of the matchers. Called this way:
;;
;; (c-make-font-lock-search-function regexp '(A B c))
;;
;; The REGEXP is used in re-search-forward, and if there's a match, the
;; A B and C are three forms that are called in a weird combination.
;;
;; Anyway the c-make-font-lock-search-function works for a single regex,
;; but more complicated scenarios such as those intended to match and
;; fontify object initializers, call for a hand-crafted lambda.
;;
;; The object initializer is special because, matching on it must
;; allow nesting.
;;
;; In c#, the object initializer block is used directly after a
;; constructor, like this:
;;
;; new MyType {
;; Prop1 = "foo"
;; }
;;
;; csharp-mode needs to fontify the properties in the
;; initializer block in font-lock-variable-name-face. The key thing is
;; to set the text property on the open curly, using type c-type and
;; value c-decl-id-start. This apparently allows `parse-partial-sexp' to
;; do the right thing, later.
;;
;; This simple case is easy to handle in a regex, using the basic
;; `c-make-font-lock-search-function' form. But the general syntax for a
;; constructor + object initializer in C# is more complex:
;;
;; new MyType(..arglist..) {
;; Prop1 = "foo"
;; }
;;
;; A simple regex match won't satisfy here, because the ..arglist.. can
;; be anything, including calls to other constructors, potentially with
;; object initializer blocks. This may nest arbitrarily deeply, and the
;; regex in emacs doesn't support balanced matching. Therefore there's
;; no way to match on the "outside" pair of parens, to find the relevant
;; open curly. What's necessary is to do the match on "new MyType" then
;; skip over the sexp defined by the parens, then set the text property on
;; the appropriate open-curly.
;;
;; To make that happen, it's good to have insight into what the matcher
;; really does. The output of `c-make-font-lock-search-function' before
;; byte-compiling, is:
;;
;; (lambda (limit)
;; (let ((parse-sexp-lookup-properties
;; (cc-eval-when-compile
;; (boundp 'parse-sexp-lookup-properties))))
;; (while (re-search-forward REGEX limit t)
;; (unless
;; (progn
;; (goto-char (match-beginning 0))
;; (c-skip-comments-and-strings limit))
;; (goto-char (match-end 0))
;; (progn
;; B
;; (save-match-data A)
;; C ))))
;; nil)
;;
;; csharp-mode uses this hand-crafted form of a matcher to handle the
;; general case for constructor + object initializer, within
;; `c-basic-matchers-after' .
;;
;; (defun c-make-font-lock-search-function (regexp &rest highlights)
;; ;; This function makes a byte compiled function that works much like
;; ;; a matcher element in `font-lock-keywords'. It cuts out a little
;; ;; bit of the overhead compared to a real matcher. The main reason
;; ;; is however to pass the real search limit to the anchored
;; ;; matcher(s), since most (if not all) font-lock implementations
;; ;; arbitrarily limits anchored matchers to the same line, and also
;; ;; to insulate against various other irritating differences between
;; ;; the different (X)Emacs font-lock packages.
;; ;;
;; ;; REGEXP is the matcher, which must be a regexp. Only matches
;; ;; where the beginning is outside any comment or string literal are
;; ;; significant.
;; ;;
;; ;; HIGHLIGHTS is a list of highlight specs, just like in
;; ;; `font-lock-keywords', with these limitations: The face is always
;; ;; overridden (no big disadvantage, since hits in comments etc are
;; ;; filtered anyway), there is no "laxmatch", and an anchored matcher
;; ;; is always a form which must do all the fontification directly.
;; ;; `limit' is a variable bound to the real limit in the context of
;; ;; the anchored matcher forms.
;; ;;
;; ;; This function does not do any hidden buffer changes, but the
;; ;; generated functions will. (They are however used in places
;; ;; covered by the font-lock context.)
;;
;; ;; Note: Replace `byte-compile' with `eval' to debug the generated
;; ;; lambda easier.
;; (byte-compile
;; `(lambda (limit)
;; (let (;; The font-lock package in Emacs is known to clobber
;; ;; `parse-sexp-lookup-properties' (when it exists).
;; (parse-sexp-lookup-properties
;; (cc-eval-when-compile
;; (boundp 'parse-sexp-lookup-properties))))
;; (while (re-search-forward ,regexp limit t)
;; (unless (progn
;; (goto-char (match-beginning 0))
;; (c-skip-comments-and-strings limit))
;; (goto-char (match-end 0))
;; ,@(mapcar
;; (lambda (highlight)
;; (if (integerp (car highlight))
;; (progn
;; (unless (eq (nth 2 highlight) t)
;; (error
;; "The override flag must currently be t in %s"
;; highlight))
;; (when (nth 3 highlight)
;; (error
;; "The laxmatch flag may currently not be set in %s"
;; highlight))
;; `(save-match-data
;; (c-put-font-lock-face
;; (match-beginning ,(car highlight))
;; (match-end ,(car highlight))
;; ,(elt highlight 1))))
;; (when (nth 3 highlight)
;; (error "Match highlights currently not supported in %s"
;; highlight))
;; `(progn
;; ,(nth 1 highlight)
;; (save-match-data ,(car highlight))
;; ,(nth 2 highlight))))
;; highlights))))
;; nil))
;; )
(c-lang-defconst c-basic-matchers-before
csharp `(
;;;; Font-lock the attributes by searching for the
;;;; appropriate regex and marking it as TODO.
;;,`(,(concat "\\(" csharp-attribute-regex "\\)")
;; 0 font-lock-function-name-face)
;; Put a warning face on the opener of unclosed strings that
;; can't span lines. Later font
;; lock packages have a `font-lock-syntactic-face-function' for
;; this, but it doesn't give the control we want since any
;; fontification done inside the function will be
;; unconditionally overridden.
,(c-make-font-lock-search-function
;; Match a char before the string starter to make
;; `c-skip-comments-and-strings' work correctly.
(concat ".\\(" c-string-limit-regexp "\\)")
'((c-font-lock-invalid-string)))
;; Fontify keyword constants.
,@(when (c-lang-const c-constant-kwds)
(let ((re (c-make-keywords-re nil
(c-lang-const c-constant-kwds))))
`((eval . (list ,(concat "\\<\\(" re "\\)\\>")
1 c-constant-face-name)))))
;; Fontify the namespaces that follow using statements.
;; This regex handles the optional alias, but does not fontify it.
,`("\\<\\(using\\)\s+\\(?:[A-Za-z_][[:alnum:]]*\s*=\s*\\)?\\(\\(?:[A-Za-z_][[:alnum:]]*\\.\\)*[A-Za-z_][[:alnum:]]*\\)\s*;"
2 font-lock-constant-face)
;; Fontify all keywords except the primitive types.
,`(,(concat "\\<" (c-lang-const c-regular-keywords-regexp))
1 font-lock-keyword-face)
;; Fontify leading identifiers in fully qualified names like
;; "Foo.Bar".
,@(when (c-lang-const c-opt-identifier-concat-key)
`((,(byte-compile
`(lambda (limit)
(while (re-search-forward
,(concat "\\(\\<" ; 1
"\\(" (c-lang-const c-symbol-key)
"\\)" ; 2
"[ \t\n\r\f\v]*"
(c-lang-const
c-opt-identifier-concat-key)
"[ \t\n\r\f\v]+"
"\\)"
"\\("
(c-lang-const
c-opt-after-id-concat-key)
"\\)")
limit t)
(unless (progn
(goto-char (match-beginning 0))
(c-skip-comments-and-strings limit))
(or (get-text-property (match-beginning 2) 'face)
(c-put-font-lock-face (match-beginning 2)
(match-end 2)
c-reference-face-name))
(goto-char (match-end 1)))))))))
))
(c-lang-defconst c-basic-matchers-after
csharp `(
;; option 1:
;; ,@(when condition
;; `((,(byte-compile
;; `(lambda (limit) ...
;;
;; option 2:
;; ,`((lambda (limit) ...
;;
;; I don't know how to avoid the (when condition ...) in the
;; byte-compiled version.
;;
;; X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+X+
;; Case 1: invocation of constructor + maybe an object
;; initializer. Some possible examples that satisfy:
;;
;; new Foo ();
;;
;; new Foo () { };
;;
;; new Foo { };
;;
;; new Foo { Prop1= 7 };
;;
;; new Foo {
;; Prop1= 7
;; };
;;
;; new Foo {
;; Prop1= 7,
;; Prop2= "Fred"
;; };
;;
;; new Foo {
;; Prop1= new Bar()
;; };
;;
;; new Foo {
;; Prop1= new Bar { PropA = 5.6F }
;; };
;;
,@(when t
`((,(byte-compile
`(lambda (limit)
(let ((parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(while (re-search-forward
,(concat "\\<new"
"[ \t\n\r\f\v]+"
"\\(\\(?:"
(c-lang-const c-symbol-key)
"\\.\\)*"
(c-lang-const c-symbol-key)
"\\)"
)
limit t)
(unless
(progn
(goto-char (match-beginning 0))
(c-skip-comments-and-strings limit))
(csharp-log 3 "ctor candidate at %d" (match-beginning 1))
(save-match-data
;; next thing could be: [] () <> or {} or nothing (semicolon, comma).
;; fontify the typename
(c-put-font-lock-face (match-beginning 1)
(match-end 1)
'font-lock-type-face)
(goto-char (match-end 0))
(c-forward-syntactic-ws)
(if (eq (char-after) ?<) ;; ctor for generic type
(progn
(csharp-log 3 " - generic ctor")
;; skip over <> safely
(c-safe (c-forward-sexp 1) t)
(c-forward-syntactic-ws)))
;; now, could be [] or (..) or {..} or semicolon.
(csharp-log 3 " - looking for sexp")
(if (or
(eq (char-after) ?{) ;; open curly
(and (eq (char-after) 91) ;; open square
(while (eq (char-after) 91)
(c-safe (c-forward-sexp 1)))
(eq (char-before) 93)) ;; close square
(and (eq (char-after) 40) ;; open paren
(c-safe (c-forward-sexp 1) t)))
(progn
;; at this point we've jumped over any intervening s-exp
(c-forward-syntactic-ws)
(csharp-log 3 " - after fwd-syn-ws point(%d)" (point))
(csharp-log 3 " - next char: %c" (char-after))
(if (eq (char-after) ?{)
(let ((start (point))
(end (if (c-safe (c-forward-sexp 1) t)
(point) 0)))
(csharp-log 3 " - put c-decl-id-start on the open-curly at %d" start)
(c-put-char-property start
'c-type
'c-decl-id-start)
(goto-char start)
(if (> end start)
(progn
(forward-char 1) ;; step over open curly
(c-forward-syntactic-ws)
(while (> end (point))
;; now, try to fontify/assign variables to any properties inside the curlies
(csharp-log 3 " - inside open curly point(%d)" (point))
(csharp-log 3 " - next char: %c" (char-after))
;; fontify each property assignment
(if (re-search-forward
(concat "\\(" (c-lang-const c-symbol-key) "\\)\s*=")
end t)
(progn
(csharp-log 3 " - found variable %d-%d"
(match-beginning 1)
(match-end 1))
(c-put-font-lock-face (match-beginning 1)
(match-end 1)
'font-lock-variable-name-face)
(goto-char (match-end 0))
(c-forward-syntactic-ws)
;; advance to the next assignment, if possible
(if (eq (char-after) ?@)
(forward-char 1))
(if (c-safe (c-forward-sexp 1) t)
(progn
(forward-char 1)
(c-forward-syntactic-ws))))
;; else
(csharp-log 3 " - no more assgnmts found")
(goto-char end)))))
)))))
(goto-char (match-end 0))
)))
nil))
)))
;; Case 2: declaration of enum with or without an explicit
;; base type.
;;
;; Examples:
;;
;; public enum Foo { ... }
;;
;; public enum Foo : uint { ... }
;;
,@(when t
`((,(byte-compile
`(lambda (limit)
(let ((parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties))))
(while (re-search-forward
,(concat csharp-enum-decl-re
"[ \t\n\r\f\v]*"
"{")
limit t)
(csharp-log 3 "enum candidate at %d" (match-beginning 0))
(unless
(progn
(goto-char (match-beginning 0))
(c-skip-comments-and-strings limit))
(progn
(save-match-data
(goto-char (match-end 0))
(c-put-char-property (1- (point))
'c-type
'c-decl-id-start)
(c-forward-syntactic-ws))
(save-match-data
(c-font-lock-declarators limit t nil))
(goto-char (match-end 0))
)
)))
nil))
)))
;; Case 3: declaration of constructor
;;
;; Example:
;;
;; private Foo(...) {...}
;;
,@(when t
`((,(byte-compile
`(lambda (limit)
(let ((parse-sexp-lookup-properties
(cc-eval-when-compile
(boundp 'parse-sexp-lookup-properties)))
(found-it nil))
(while (re-search-forward
,(concat
"^[ \t\n\r\f\v]*"
"\\(\\<\\(public\\|private\\|protected\\)\\)?[ \t\n\r\f\v]+"
"\\(@?[[:alpha:]_][[:alnum:]_]*\\)" ;; name of constructor
"[ \t\n\r\f\v]*"
"\\("
"("