-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcsharp-completion.el
More file actions
2794 lines (2224 loc) · 95.1 KB
/
csharp-completion.el
File metadata and controls
2794 lines (2224 loc) · 95.1 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-completion.el -- Smart code completion for C#
;;
;; Author: Dino Chiesa <dpchiesa@hotmail.com>
;; Maintainer: Dino Chiesa <dpchiesa@hotmail.com>
;; Created: April 2010
;; Modified: April 2010
;; Version: 0.1.5
;; Keywords: c# languages oop mode
;; X-URL: http://code.google.com/p/csharpmode/
;;
;;
;;; Commentary:
;;
;; This is a code-completion or "intellisense" package for C#. The
;; scope of this module is much smaller that a full "development
;; envvironment". It does smart code completion for C#, in
;; emacs. It does not do font-lock, indenting, debugging,
;; compiling, profiling, and so on.
;;
;; To use it, place the cursor after a partially-completed
;; statement, and invoke `cscomp-complete-at-point'. Normally this
;; would be bound to a particular keystroke, like M-. This module
;; will insert the first completion that matches. If multiple
;; completions are possible, calling the completion function again
;; will cycle through the possibilities, similar to the way
;; dabbrev-mode works.
;;
;; You can also call `cscomp-complete-at-point-menu', and get a popup
;; menu of the completion choices.
;;
;; There are 2 complementary sources of information for the
;; completions: introspection into compiled .NET class libraries
;; (like the base class library), and parse analysis from the
;; semantic.el package, which is part of CEDET. In the typical
;; case, this module uses both of those sources of information,
;; together.
;;
;; The reflection is done by an inferior powershell shell running
;; within emacs, that has loaded a custom .NET assembly. The
;; library exposes static methods that perform type reflection,
;; using the capabilities of the System.Reflection namespace. These
;; methods then return strings which are lisp s-expressions that
;; can be eval'd, resulting in structures containing information
;; for a given type - including the available methods, fields and
;; properties, and the attributes on same. This piece of the
;; puzzle is called the "CscompShell".
;;
;; As an example, suppose your code has a local variable of type
;; System.Xml.XmlDocument, named doc. Suppose the user asks for
;; completion on that variable. The module uses the semantic parse
;; data to identify the name and type of the local variable. It then
;; sends a "GetTypeInfo" command to the CscompShell, passing
;; System.Xml.XmlDocument. The CscompShell returns an s-expression
;; enumerating the fields, methods and properties for that type.
;; This s-expression is then used to populate the completion list.
;;
;;
;; Here's a survey of the situations in which this module can offer
;; completions:
;;
;; a. names of local variables, instance variables, and method arguments.
;;
;; void Method1(String longArgumentName)
;; {
;; long?
;; }
;;
;; b. Methods, fields and properties (m/f/p) on a local variable
;; with known type, on method arguments of known type, or on
;; instance variables of known type:
;;
;; String x = "this is a string";
;; x.?
;;
;; c. M/f/p on local variables with var type:
;;
;; var x = "this is a string";
;; x.?
;;
;; d. Cascading local variable declarations of var type:
;;
;; var s = "This is a string";
;; var length = s.Length;
;; var radix = length.?
;;
;; e. completion on generic types:
;;
;; var x = new List<String>();
;; x.?
;;
;; f. completion on local variables that are initialized
;; from instance methods and variables.
;;
;; void method1()
;; {
;; var length = this.InstanceMethod();
;; length.?
;; }
;;
;; g. constructor completion, provide template when completing
;;
;; var x = new System.String(?
;;
;; h. constructor completion as above, with unqualified type.
;;
;; var x = new TimeSpan(?
;;
;; i. finding constructors among qualified and unqualified types
;;
;; var x = new TimeS?
;;
;; j. complete static methods on known types,
;; whether fully qualified or unqualified.
;;
;; String.Jo?
;;
;; k. present template fpr static methods on known types,
;; whether fully qualified or unqualified.
;;
;; String.Join(?
;;
;; l. Immediate values.
;;
;; 7.C?
;;
;; m. other compound Expressions:
;;
;; Path.GetRandomFileName().Normalize().?
;;
;;
;;
;; =======================================================
;;
;; Dependencies:
;;
;; cc-mode 5.31.?
;;
;; semantic.el 1.0pre7
;;
;; optionally, yasnippet, for snippet insertion. If the user has
;; yasnippet loaded, then this completion module will insert
;; a snippet (template) when the user selects a Method from the
;; completion list menu. The method snippet will have all the
;; method parameters as placeholders; the developer then types over
;; those placeholders to supply the actual method parameters.
;; If yasnippet is not loaded, then the completion is just
;; the method name, and the developer has to fill in the
;; param-list himself.
;;
;; PowerShell, and a separate DLL that must run in Powershell. That
;; DLL is implemented in C#.
;;
;;
;;
;;
;; Known bugs/problems :
;;
;; 1. The module does not do completion on anonymous (var) types in
;; for loops.
;;
;;
;;
;; TODO :
;;
;; make an installer.
;;
;; Please send any comments, bugs, or upgrade requests to
;; Dino Chiesa (dpchiesa@hotmail.com)
;;
(require 'csharp-shell)
(require 'semantic-idle) ;; for ... reparsing a buffer
;; Design notes:
;;
;; Tue, 04 May 2010 10:47
;;
;; This completion depends on the semantic package for parsing a C#
;; buffer. That gives the module a way to interrogate the names and
;; types of local and instance variables, in order to do completion on
;; them.
;;
;; Semantic also provides the list of using statements for a C# module,
;; which tells us which assemblies we need to search in, for
;; completions. These are then loaded into CscompShell for interrogation.
;;
(defvar cscomp-current-list nil
"The list of all the completion. Each element of the list is
either a string, or a list which the car is the possible completion,
and the cadr is an additional information about this completion.")
(defvar cscomp-current-list-index nil
"An index to an element in cscomp-current-list. This is used to
cycle the list.")
(defvar cscomp-current-fragment nil
"The current fragment we're trying to complete. This is used to trim the thing that gets inserted.")
(defvar cscomp-current-beginning (make-marker)
"The beginning of the region where the last completion was inserted.")
(defvar cscomp-current-end (make-marker)
"The end of the region where the last completion was inserted.")
(defvar cscomp-typeinfo-cache nil)
(defcustom cscomp-typeinfo-cache-size 150
"The max size of completion buffer cache, in entries."
:group 'cscomp
:type 'integer)
(defun cscomp-referenced-assemblies-list ()
"Return the list of .NET namespaces referenced in the
current buffer via using statements. It uses the semantic parser
table to find the 'using' statements. "
(interactive)
(if (not (semantic-active-p)) (semantic-new-buffer-fcn))
(let* ((tokens (semantic-fetch-tags))
;;(usings (semantic-find-nonterminal-by-token (quote include) tokens))
(usings (semantic-brute-find-tag-by-class 'include tokens)))
(cscomp-log 3 "cscomp-referenced-assemblies-list: found using statements: '%s'" usings)
(mapcar 'car usings)))
(defun cscomp-instance-vars ()
"Return the list of instance variables in a C# module.
This uses the semantic parser table to find the variable
declarations.
The return value is a list of semantic tags. Looks like:
\((\"flavor\" variable
(:type \"int\")
(reparse-symbol class_member_declaration) #<overlay from 580 to 595 in a.cs>)
(\"flicky\" variable
(:type \"String\")
(reparse-symbol class_member_declaration) #<overlay from 604 to 636 in a.cs>)
(\"label\" variable
(:type \"String\")
(reparse-symbol class_member_declaration) #<overlay from 645 to 669 in a.cs>))
"
(if (not (semantic-active-p)) (semantic-new-buffer-fcn))
(semantic-fetch-tags)
(let* ((class (cscomp-find-enclosing-csharp-class)) ;; the enclosing class
(tokens (semantic-tag-type-members class)) ;; the members of that class
(vars (semantic-brute-find-tag-by-class 'variable tokens))) ;; the members that are variables
(cscomp-log 3 "cscomp-instance-vars: found instance vars: '%s'" vars)
vars))
(defun cscomp-instance-members ()
"Return the list of instance members in a C# module.
This uses the semantic parser table to find the memebr
declarations.
The return value is a list of semantic tags. Looks like:
\((\"flavor\" variable
(:type \"int\")
(reparse-symbol class_member_declaration) #<overlay from 580 to 595 in a.cs>)
(\"Hello\" function
(:type \"String\")
(reparse-symbol class_member_declaration) #<overlay from 604 to 636 in a.cs>)
(\"label\" variable
(:type \"String\")
(reparse-symbol class_member_declaration) #<overlay from 645 to 669 in a.cs>))
"
(if (not (semantic-active-p)) (semantic-new-buffer-fcn))
(semantic-fetch-tags)
(let ((class (cscomp-find-enclosing-csharp-class))) ;; the enclosing class
(semantic-tag-type-members class))) ;; the members of that class
(defun cscomp--find-matching-tags (name-fragment tagset &optional local)
"Return the list of tags from a given set, that
match NAME-FRAGMENT. This is used by `cscomp-matching-local-vars',
`cscomp-matching-instance-vars',
`cscomp-matching-instance-members'.
"
(let ((var-label (if local "(Variable) " "(Field) " ))
result)
(if tagset
(progn
(while tagset
(let* ((tag (car tagset))
(member-name (semantic-tag-name tag))
(member-type (semantic-tag-type tag))
(member-clazz (semantic-tag-class tag))
)
(if (eq 0 (string-match name-fragment member-name))
(let ((descrip
(cond
((string= member-clazz "variable")
(concat var-label member-type))
((string= member-clazz "function")
(let* ((arglist (semantic-tag-get-attribute tag :arguments))
(modifiers (semantic-tag-modifiers tag))
(arg-descrip
(if (> (length arglist) 0)
(concat "("
(mapconcat
'(lambda (x) (concat (semantic-tag-type x)
" "
(semantic-tag-name x)))
arglist ", ")
")")
"()")))
(concat "(Method) "
" " (mapconcat 'identity modifiers " ") " "
arg-descrip
" returns "
member-type)))
(t ""))))
(cscomp-log 2 "cscomp-matching-tags: found %s (%s)"
member-name member-type)
(setq result (cons (list member-name descrip) result)))))
(setq tagset (cdr tagset)))
(cscomp-sort-completion-list result))
nil)))
(defun cscomp-matching-instance-vars (name-fragment)
"Return the list of instance variables in a C# module, that
match NAME-FRAGMENT. See also, `cscomp-matching-local-vars'.
"
(cscomp--find-matching-tags name-fragment (cscomp-instance-vars)))
(defun cscomp-matching-instance-members (name-fragment)
"Return the list of instance memebrs in a C# module, that
match NAME-FRAGMENT.
See also, `cscomp-matching-local-vars',
`cscomp-matching-instance-vars'.
"
(cscomp--find-matching-tags name-fragment (cscomp-instance-members)))
(defun cscomp-matching-local-vars (name-fragment)
"Use the semantic lex/analysis results to find local variables
that match the given NAME-FRAGMENT.
See also, `cscomp-matching-instance-members',
`cscomp-matching-instance-vars'.
"
(cscomp-start-stripped-semantic)
(cscomp--find-matching-tags name-fragment (semantic-get-all-local-variables) t))
(defun cscomp-find-enclosing-csharp-class (&optional posn)
"returns a tag of type 'type (in other words, a c# class or struct) that
encloses POSN, or point if POSN is nil. If there is no enclosing 'type,
then return nil.
"
;; This isn't quite correct. At this time, the C# namespace defn gets
;; parsed as a type. I couldn't figure how to get namespace to get
;; parsed as a new 'namespace tag. Therefore, this fn can sometimes
;; return a tag corresponding to a namespace, as opposed to a tag
;; corresponding to a bonafide type.
(let ((nar (semantic-current-tag-of-class 'type)))
nar))
;; (defun cscomp-find-semantic-things (tag-class)
;; "Search for tags in semantic parse state."
;; (interactive "sTag type: ")
;; (if (not (semantic-active-p)) (semantic-new-buffer-fcn))
;; (let* ((tokens (semantic-fetch-tags))
;; (matches (cscomp-find-by-class tag-class tokens t)))
;; (if matches
;; (while matches
;; (let ((one (car matches)))
;; (setq matches (cdr matches))))
;; nil
;; )))
(defun cscomp-find-by-class (cls streamorbuffer &optional search-parts search-includes)
"Find all tags with class CLS within STREAMORBUFFER.
CLS is a string which is the name of the class of the tags returned, such as
include, variable, type.
See `semantic-tag-class'.
Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to
`semantic-brute-find-tag-by-function'."
(semantic-brute-find-tag-by-function
(lambda (tag)
(let ((class (semantic-tag-class tag)))
(string= class cls)))
streamorbuffer search-parts search-includes))
;; (lambda (tag)
;; (let ((class (semantic-tag-class tag)))
;; (if (and (listp ts)
;; (or (= (length ts) 1)
;; (string= (semantic-tag-class ts) type)))
;;
;; (setq ts (semantic-tag-name ts)))
;; (equal type ts)))
;; streamorbuffer search-parts search-includes))
(defun cscomp-debugonly-list-all-local-variables ()
"fiddle with local variables and semantic."
(interactive)
(cscomp-start-stripped-semantic)
(semantic-lex (point-min) (point-max) 100)
(let ((locals (semantic-get-all-local-variables)))
(mapcar '(lambda (x)
(cscomp-log 3 "local var: name(%s) type(%s) pos(%s)"
(car x) (cadr (nth 2 x))
(nth 4 x)))
locals)))
;; (defun cscomp-variables-in-scope ()
;; "Return the list of variables currently in scope.
;; It uses the semantic parser table to find them."
;; (interactive)
;; (if (not (semantic-active-p)) (semantic-new-buffer-fcn))
;; (let* ((tokens (semantic-fetch-tags))
;; (vars (semantic-brute-find-tag-by-class 'variable tokens)))
;; (cscomp-log 3 "cscomp-variables-in-scope: found variables: '%s'" vars)
;; (mapcar 'car vars)))
;; (defun cscomp-valid-csharp-declaration-at (point varname)
;; "Verify that a POINT starts a valid csharp declaration
;; for the VARNAME variable."
;; (save-excursion
;; (goto-char point)
;; (if (looking-at
;; (concat "\\([A-Za-z0-9_.\177-\377]+\\)[ \t\n\r]+"
;; (cscomp-double-backquotes varname)
;; "[ \t\n\r]*[;=]"))
;; (match-string 1)
;; nil)))
;; (defun cscomp-double-backslashes (varname)
;; "Build a new string identical to VARNAME, except that every backslash
;; `\' is doubled, so that it can be used in a regex expression.
;; "
;; (let (result (idx 0) (len (length varname)) curcar)
;; (while (< idx len)
;; (setq curcar (elt varname idx))
;; (setq result (concat result (if (eq curcar ?\\)
;; "\\\\"
;; (make-string 1 curcar))))
;; (setq idx (1+ idx)))
;; result))
;; (defun cscomp-declared-type-of (name)
;; "Find in the current buffer the csharp type of the variable NAME. The
;; function returns a string containing the name of the class, or nil
;; otherwise. This function does not give the fully-qualified csharp class
;; name, it just returns the type as it is declared."
;; (save-excursion
;; (let (found res pos orgpt resname)
;; (while (and (not found)
;; (search-backward name nil t))
;; (setq pos (point))
;; (backward-word 1)
;; (setq resname (cscomp-valid-csharp-declaration-at (point) name))
;; (goto-char pos)
;; (forward-char -1)
;; (if resname
;; (progn (setq res resname)
;; (setq found t))))
;; res)))
;; (defun cscomp-filter-fqn (importlist)
;; "Filter all the fully-qualified classnames in the import list. It uses
;; the knowledge that those classnames are at the beginning of the list,
;; so that it can stops at the first package import (with a star `*' at
;; the end of the declaration)."
;; (if importlist
;; (if (string= "*" (car (cdr (car importlist))))
;; importlist
;; (cscomp-filter-fqn (cdr importlist)))))
(defun cscomp-escape-string-for-powershell (arg)
"Powershell uses the backquote for an escape char. This fn
escapes the backquote for a string that will eventually be sent
to powershell (CscompShell).
I think this is only necessary when the arg is submitted to
powershell within double-quotes. If the arg is within single
quotes, backquotes do not need to be escaped.
"
(let ((matcho (string-match "\\(.+\\)`\\(.+\\)" arg)))
(if matcho
(concat
(substring arg (match-beginning 1) (match-end 1))
"``"
(substring arg (match-beginning 2) (match-end 2)))
arg)))
(defun cscomp-send-string-to-shell (command-string)
"sends a string to cscompshell function, returns the result."
(let ((result (csharp-shell-exec-and-eval-result command-string)))
(cscomp-log 2 "send-string-to-shell (%s) result(%s)" command-string (prin1-to-string result))
;;(if (and result (listp result)) result nil)
result
))
(defun cscomp-invoke-shell-fn (fn arg)
"invokes a 1-arg CscompShell function, returns the result."
;; need to use single-quotes here around the arg, because in
;; some cases the arg can have double-quotes in it.
(let* ((escaped-arg (cscomp-escape-string-for-powershell arg)))
(cscomp-send-string-to-shell (concat "[Ionic.Cscomp.Utilities]::" fn "(\'" escaped-arg "\')"))))
(defun cscomp-type-exists (typename)
"Determines if the given type is known by the CscompShell. You can provide a short type name, or a fully-qualified name. You must have loaded the assembly into the shell, for it to be known. See `cscomp-load-assembly'."
(interactive "sType name: ")
(cscomp-invoke-shell-fn "QualifyType" typename))
(defun cscomp-get-members-of-class (semantic-tag)
"Gets the members of the class denoted by the SEMANTIC-TAG.
If SEMANTIC-TAG is nil, then this function gets the closest type
containing point, and gets the members of that.
"
(if (null semantic-tag) (setq semantic-tag (cscomp-get-current-class)))
(if (eq (cadr semantic-tag) 'type)
(semantic-tag-type-members semantic-tag)
nil))
(defun cscomp-get-current-class (&optional posn)
"Return the semantic tag for the current class or type in scope at POSN.
"
(interactive)
(save-excursion
(if posn (goto-char posn))
(semantic-fetch-tags)
(let ((containing-type (semantic-current-tag-of-class 'type)))
containing-type)))
(defun cscomp-produce-csharp-arglist-block-from-dbrecord (arglist)
"Produces an argument list block, suitable for framing within parens
in a method declaration, from ARGLIST, a list of local arguments obtained from
`semantic-get-local-arguments'.
When the format of ARGLIST is like this:
((\"count\"
variable
(:type \"int\")
(:filename \"c:/dinoch/dev/dotnet/CsharpCompletion.cs\"
reparse-symbol formal_parameters)
[621 630])
(\"melvin\"
variable
(:type \"string\")
(:filename \"c:/dinoch/dev/dotnet/CsharpCompletion.cs\"
reparse-symbol formal_parameters)
[631 645]))
The return value is like this:
int count, string melvin
When the arglist is empty, the return value is a string of zero length.
"
(let ((fragment ""))
(while arglist
(let* ((x (car arglist))
(var-name (car x))
(var-type (cadr (nth 2 x))))
(setq fragment (concat fragment var-type " " var-name)
arglist (cdr arglist))
(if arglist
(setq fragment (concat fragment ", ")))))
fragment))
(defun cscomp-produce-instance-member-code-fragment (member-list)
"Produce a C# fragment that defines placeholder instance members,
to be inserted into a class template which is then compiled, so that
Cscomp can inspect the resulting IL to determine the type of a local var
on which the user is asking for completion.
Cscomp uses the compiler to determine the type of the var. It
dynamically generates and compiles a class with the same variable
declaration as the code being edited.
The initialization of the var may depend on instance members. If
so, the compiled class must provide instance members of the
correct type to satisfy those dependencies. This function
generates C# code to provide those instance members.
For input that looks like this:
((\"staticField1\" variable
(:typemodifiers (\"private\" \"static\") :type \"int\")
(reparse-symbol class_member_declaration)
#<overlay from 605 to 642 in CsharpCompletion.cs>)
(\"InstanceMethod1\" function
(:arguments (...) :type \"string\")
(reparse-symbol class_member_declaration)
#<overlay from 652 to 741 in CsharpCompletion.cs>)
(\"Run\" function
(:typemodifiers (\"public\") :arguments (...) :type \"void\")
(reparse-symbol class_member_declaration)
#<overlay from 752 to 1487 in CsharpCompletion.cs>)
(\"Main\" function
(:typemodifiers (\"public\" \"static\") :arguments (...) :type \"void\")
(reparse-symbol class_member_declaration)
#<overlay from 1497 to 1806 in CsharpCompletion.cs>)
)
The output will look like this:
private static int staticField1 = default(int);
string InstanceMethid(...) { return default(string); }
Any void methods will not be emitted because they cannot affect the
types of local variables declared in methods.
"
(let ((synthetic-code ""))
(while member-list
(let* ((x (car member-list))
(member-name (car x))
(member-flavor (cadr x))
(member-type (semantic-tag-get-attribute x :type))
(member-modifiers (semantic-tag-get-attribute x :typemodifiers))
one-frag
)
;; (message "n(%s) f(%s) t(%s)"
;; member-name
;; member-flavor
;; member-type)
(setq one-frag
(cond
((string= member-type "void") ;; the member is a void type
"") ;; emit nothing, don't need it.
;; it's possible we might need it, in which case,
;; we can just emit an empty fn.
((eq member-flavor 'function) ;; it's a method
(concat
(mapconcat 'identity member-modifiers " ")
" "
member-type
" "
member-name
"("
(cscomp-produce-csharp-arglist-block-from-dbrecord
(semantic-tag-get-attribute x :arguments))
") {"
(if member-type
(concat
" return default("
member-type
");")
"")
"} "))
((eq member-flavor 'variable) ;; it's an instance variable
(concat
(mapconcat 'identity member-modifiers " ")
" "
member-type
" "
member-name
" = default("
member-type
"); "))
(t
"")))
(setq synthetic-code (concat synthetic-code one-frag)))
(setq member-list (cdr member-list)))
synthetic-code))
(defun cscomp-consolidate-whitespace (s)
"Collapse consecutive whitespace characters in the given string S
to a single space.
"
;; trim leading spaces
(if (string-match "^\\([ \t\n\r\f\v]+\\)" s)
(setq s (substring s (match-end 1))))
;; collapse multiple whitespace into one
(while (string-match "\\([ \t\n\r\f\v]\\{2,\\}\\)" s)
(setq s
(concat
(substring s 0 (match-beginning 1))
" "
(substring s (match-end 1)))))
s)
(defun cscomp-escape-single-quotes (s)
"Escape single-quotes in the given string S. This is for use within
powershell.
"
;; escape single-quotes
(while (string-match "\\(.+\\)'\\(.+\\)" s)
(setq s
(concat
(substring s 0 (match-beginning 1))
"`'"
(substring s (match-end 1)))))
s)
;;(setq cscomp-log-level 2)
(defun cscomp-get-var-type-given-decl (var-declaration
var-index
classname
arglist
instance-members)
"Determines the type of the var declared in the given declaration.
VAR-DECLARATION is the C# code that declares all the local vars up to
and including the local var of interest.
VAR-INDEX is the zero-based index of the local arg in that list,
that is of interest. The initialization of that local var may
depend on the prior local vars, which is why we need the entire
var declaration list.
CLASSNAME is the name of the class in which the local vars
appear. This is used in the generated (synthetic) code, in case
there is a reference to a class-static member.
ARGLIST is a string with the arglist for the method that contains the
local variable in question. This will satisfy dependencies on
local arguments in the initializer for the var, if any.
INSTANCE-MEMBERS is a C# code fragment defining instance members for the
synthetic class. This will satisfy dependencies on instance members in
the initializer for the var, if any.
"
(let* ((massaged-decl
(cscomp-escape-single-quotes
(cscomp-consolidate-whitespace var-declaration)))
(namespaces (mapconcat 'identity (cscomp-referenced-assemblies-list) ","))
(command-string
(concat "[Ionic.Cscomp.Utilities]::GetTypeGivenVarDecl('"
massaged-decl
"','"
namespaces
"','" ;; for now, no additional assembly references
"',"
(number-to-string var-index)
",'"
classname
"','"
arglist
"','"
(cscomp-consolidate-whitespace instance-members)
"')" )))
(cscomp-send-string-to-shell command-string)))
(defun cscomp-get-completions-for-namespace (ns)
"Gets a list of known completions (types and child namespaces)
for the given namespace. You must have loaded an assembly containing
types from the namespace, into the shell, for it to be known.
See `cscomp-load-assembly'."
(interactive "sNamespace: ")
(cscomp-invoke-shell-fn "GetCompletionsForNamespace" ns))
(defun cscomp-qualify-name (name)
"determines if the thing is a type, namespace, or ...?
Result is
(list \"type\" name)
or
(list \"namespace\" name)
or
(list \"unknown\" name)
"
(interactive "sname to qualify: ")
(cscomp-invoke-shell-fn "QualifyName" name))
(defun cscomp-get-matches (fragment)
"returns a list of all possible matches on a given partial name.
The return value is like this:
(list (list \"type\" \"fullNameOfType\")
(list \"namespace\" \"FullNameOfNamespace\"))
"
(interactive "sfragment to match on: ")
(let ((namespaces (mapconcat 'identity (cscomp-referenced-assemblies-list) ",")))
(cscomp-send-string-to-shell
(concat "[Ionic.Cscomp.Utilities]::GetMatches('"
fragment
"','"
namespaces
"')"))))
(defun cscomp-load-additional-assemblies (lib-list)
"Loads a set of assemblies into the csharp-shell, which then allows Cscomp
to do completion (etc) on the types in those libraries."
(mapcar 'cscomp-load-assembly lib-list))
(defun cscomp-load-assembly (lib)
"Loads a assembly into the csharp-shell, which then allows Cscomp
to do completion (etc) on the types in that library."
(interactive "sLibrary: ")
(cscomp-invoke-shell-fn "LoadOneAssembly" lib))
(defun cscomp-get-qualified-name (name)
"Guess the fully qualified name of the class NAME, using the
list of referenced assemblies. It returns a string if the fqn
was found, or null otherwise."
(interactive "sType name: ")
(cscomp-log 1 "get-qualified-name (%s)" name)
(let ((result (cscomp-type-exists name)))
(if result result
;; else
(let ((usinglist (cscomp-referenced-assemblies-list))
fullname namespace )
;; usinglist is like this:
;; ("System" "System.Collections" "System.Collections.Generic" "System.Reflection")
(setq result nil)
(while usinglist
(setq namespace (car usinglist))
(cscomp-load-assembly namespace)
(setq fullname (concat namespace "." name))
(cscomp-log 2 "checking this type: '%s'" fullname)
(if (cscomp-type-exists fullname)
(setq result fullname
usinglist nil)
(setq usinglist (cdr usinglist))))
(cscomp-log 1 "get-qualified-name rtns: '%s'" result)
result))))
;; (defun cscomp-flush-typeinfo-cache ()
;; "Flushes all entries in the completion cache"
;; (interactive)
;; (setq cscomp-typeinfo-cache nil))
;;
;;
;; (defun cscomp-flush-classes-in-cache (class-list)
;; "Flushes all the classes in CLASS-LIST as entries of cache."
;; (let ((temp (nth 0 cscomp-typeinfo-cache))
;; (index -1)
;; (found nil)
;; (class (car class-list)))
;; (while class
;; (while (and temp (not found))
;; (setq index (1+ index))
;; (setq temp (nth index cscomp-typeinfo-cache))
;; (if (string= (car temp) class)
;; (setq found t)))
;; (if found
;; (setq cscomp-typeinfo-cache
;; (nthcdr (1+ index) cscomp-typeinfo-cache)))
;; (setq class-list (cdr class-list))
;; (setq class (car class-list))
;; (setq found nil))))
(defun cscomp-add-to-typeinfo-cache (name typeinfo)
(let (new-entry new-list)
(if (nth cscomp-typeinfo-cache-size cscomp-typeinfo-cache)
(progn
(setq new-entry (list name typeinfo))
(setq new-list (list new-entry nil))
(setcdr new-list (cdr cscomp-typeinfo-cache))
(setq cscomp-typeinfo-cache new-list)
(cscomp-log 1 "cache is full") )
;;else
(setq cscomp-typeinfo-cache
(append
cscomp-typeinfo-cache
(list (list name typeinfo)))))))
(defun cscomp-get-typeinfo-from-cache (name)
(let ((temp (nth 0 cscomp-typeinfo-cache)) (index -1) (found nil))
(while (and temp (not found))
(setq index (1+ index))
(cscomp-log 2 "looking at cache item %d" index)
(setq temp (nth index cscomp-typeinfo-cache))
(if (string= (car temp) name)
(setq found t)))
(if found
(progn
(cscomp-log 3 "cscomp-get-typeinfo-from-cache: HIT name(%s) r(%s)"
name (prin1-to-string (nth 1 temp)))
(nth 1 temp))
(cscomp-log 1 "cscomp-get-typeinfo-from-cache: MISS name(%s)" name)
nil)))
(defun cscomp-get-typeinfo (name)