-
Notifications
You must be signed in to change notification settings - Fork 0
/
csharp-completion.el
2902 lines (2357 loc) · 104 KB
/
csharp-completion.el
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 <[email protected]>
;; Maintainer: Dino Chiesa <[email protected]>
;; Created: April 2010
;; Modified: January 2011
;; Version: 0.2
;; Keywords: c# languages oop mode
;; X-URL: http://code.google.com/p/csharpmode/
;;
;;
;;; Commentary:
;;
;; Purpose
;;
;; This is a package that performs code-completion or "intellisense"
;; for C# within emacs buffers. It works on GNU Emacs running on
;; Windows and relies on Windows PowerShell in order to work.
;;
;; Out of Scope
;;
;; This module does not do font-lock, indenting, compiling, flymake,
;; or imenu. These C# things are provided by csharp-mode.el , which
;; is a pre-requisite for this module.
;;
;; This module also does not do type-ahead completion, debugging,
;; profiling, or management of C# project files. Those are not
;; available anywhere, as far as I know.
;;
;;
;; -----------------------------------------------------------------------------
;;
;; Packaging:
;;
;; csharp-completion is delivered as a set of files:
;;
;; CscompUtilities.dll - a .NET assembly that performs work in
;; support of code completion. For example it can enumerate the
;; set of types in a namespace imported by a using
;; statement. It can also perform type inference of var types,
;; aiding in completion for such types. Return values are
;; delivered in the form of lisp s-expressions. The DLL is
;; implemented in C#, and is provided with this source code,
;; licensed under the same terms.
;;
;; cscomp-base.el - base function, provides cscomp-log function,
;; which is used by all other elisp modules in this
;; package. cscomp-log is useful primarily for diagnostic
;; purposes.
;;
;; csharp-shell.el - runs the Powershell that loads
;; CscompUtilities.dll and manages input to and output from the
;; shell.
;;
;; csharp-analysis.el - provides code analysis functions, for
;; example the ability to enumerate all the methods in the type
;; being defined in the current buffer; or to enumerate all the
;; local variables that are in scope at a given point in a
;; source code file.
;;
;; csharp-completion.el - this module, which provides executive
;; management of the code-completion function. It provides the
;; two main elisp defuns that perform code completion, as well
;; as supporting functions for doing things like parsing the
;; local code fragment in the buffer to determine what is being
;; requested, popping up menus containing the list of completions,
;; and so on.
;;
;; -----------------------------------------------------------------------------
;;
;; External Dependencies
;;
;; ICSharpCode.NRefactory.dll -
;; a .NET assembly that provides sourcecode-analysis capabilities,
;; shipped as part of SharpDevelop. This DLL is used by
;; CsCompUtilities.dll to do syntax analysis.
;;
;; Windows XP, Vista, or 7
;;
;; PowerShell 2.0 - this is included in Windows 7, free download
;; for prior versions of Windows.
;;
;; GNU Emacs v23.2 or later
;;
;; cc-mode 5.31.? or later - included in Emacs 23.2
;;
;; (optionally) yasnippet, for snippet insertion. If emacs 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. I highly recommend ya-snippet.
;;
;; -----------------------------------------------------------------------------
;;
;; Set up
;;
;; To set up csharp-completion:
;;
;; 1. put all the elisp files into a directory, and put that
;; directory on your load path.
;;
;; 2. put this in your .emacs:
;;
;; (eval-after-load "csharp-completion"
;; '(progn
;; (setq cscomp-assembly-search-paths
;; (list "c:\\.net3.5ra" ;; <<- locations of reference assemblies
;; "c:\\.net3.0ra" ;; <<-
;; ... ;; <<- other assembly directories you use
;; "c:\\.net2.0" ;; <<- location of .NET Framework assemblies
;; "c:\\.net3.5" ;; <<- ditto
;; ))))
;;
;; The `cscomp-assembly-search-paths' should hold a list of
;; directories to search for assemblies that get referenced via using
;; clauses in the modules you edit. This will try default to
;; something reasonable, including the "typical" .NET 2.0 and 3.5
;; directories, as well as the default locations for reference
;; assemblies. If you have non-default locations for these things,
;; you should set them here. Also, if you have other libraries (for
;; example, the WCF Rest Starter kit, or the Windows Automation
;; assemblies) that you reference within your code, you can include
;; the appropriate directory in this list.
;;
;; 3. put this into your csharp-mode-hook:
;;
;; ;; C# code completion
;; (require 'csharp-completion)
;; (csharp-analysis-mode 1)
;; (local-set-key "\M-\\" 'cscomp-complete-at-point)
;; (local-set-key "\M-\." 'cscomp-complete-at-point-menu)
;;
;;
;;
;; -----------------------------------------------------------------------------
;;
;; Usage
;;
;; To use code completion, open a buffer on a C# source file. Place
;; the cursor after a partially-completed statement, and invoke
;; `cscomp-complete-at-point'. (Normally you would bind that fn to a
;; particular keystroke, like M-. or M-\.) Uppn invoking that fn,
;; this module will insert the first completion that matches. If
;; multiple completions are possible, calling the completion function
;; again (or pressing the bound key again) will cycle through the
;; possibilities, similar to the way dabbrev-mode works.
;;
;; You can alternatively call `cscomp-complete-at-point-menu', to get
;; a popup menu of the completion choices.
;;
;; -----------------------------------------------------------------------------
;;
;; How it Works
;;
;; There are 2 complementary sources of information used to generate the
;; completion options: introspection into compiled .NET class libraries
;; (like the base class library), and source code analysis of the
;; currently-being-edited buffer. Both sources are facilitated by an
;; inferior powershell shell running within emacs. The csharp-completion
;; module will start a powershell if it is not already running, then load
;; into that powershell instance a custom library called CscompUtilities.
;; (The powershell instance with the custom library loaded into it, is
;; referred to as the CscompShell within this module.)
;;
;; An example: if you ask for completions on the fragment "System.D",
;; this elisp module will then call into the CscompUtilities library,
;; by sending a command-line to the CscompShell that invokes a static
;; method on the library, to ask the library for possible completions
;; on the fragment. The library builds a list of all available types
;; from the set of referenced assemblies. It then searches the list
;; of types to find all possible completions for the given fragment,
;; and prints the search result, as a string formatted as a lisp
;; s-expression, on its output. This elisp module then gathers the
;; s-expression and evals it, resulting in a structure containing
;; completion options for the given fragment.
;;
;; In this example, starting from "System.D", the list of options
;; returned will include the namespace System.Diagnostics as well as
;; the type System.DateTime; this module will offer those and the
;; other options as possible completions.
;;
;; The latter source of information - source code analysis of the
;; currently-being-edited buffer - is generated via the NRefactory
;; library, which ships as part of the open-source SharpDevelop tool.
;;
;; In the typical case, this module uses both of those sources of
;; information, together, to generate suggestions for completions.
;;
;; To illustrate how, consider another example: suppose your code has
;; a local variable of type System.Xml.XmlDocument, named doc.
;; Suppose the user asks for completion on the fragment "doc.L",
;; implying that the user wants to reference a property, method or
;; field on the doc variable, that begins with the letter L. The
;; module uses the source code analysis 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 a lisp s-expression enumerating the fields, methods and
;; properties for that type. This module then filters the
;; s-expression for members that begin with L, and this filtered list
;; is then used to populate the completion list, which is either
;; displayed in a popup menu, or used to cycle the completion
;; possibilities. The list includes LastChild, LoadXml, the four
;; Load() overloads and LocalName.
;;
;;
;; -----------------------------------------------------------------------------
;;
;; Here's a summary of the situations in which this module can offer
;; completions:
;;
;; a. names of local variables, instance variables, and method arguments.
;;
;; int lengthOfList;
;; void Method1(String lengthyArgumentName)
;; {
;; leng?
;; }
;;
;; 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. M/f/p on local variable declarations with dependencies on var types:
;;
;; var s = "This is a string";
;; var length = s.Length;
;; var radix = length.?
;;
;; e. M/f/p on local variables that are initialized
;; from instance methods and variables.
;;
;; void method1()
;; {
;; var length = this.InstanceMethod();
;; length.?
;; }
;;
;; f. M/f/p on generic types:
;;
;; var x = new List<String>();
;; x.?
;;
;; g. constructor completion, provide template when completing
;;
;; var x = new System.String(?
;;
;; h. constructor completion as above, with unqualified type.
;; This mode does a search in the namespaces from using clauses.
;;
;; var x = new TimeSpan(?
;;
;; i. finding types and namespaces:
;;
;; 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().?
;;
;; -----------------------------------------------------------------------------
;;
;; things that need to be tested or fixed:
;;
;; return new XmlS?
;;
;; The module does not do completion on anonymous (var) types in
;; for loops.
;;
;; detection of presence of ya-snippet.el - how to correctly and
;; dynamically check if ya-snippet is available, and load it if
;; necessary?
;;
;; -----------------------------------------------------------------------------
;;
;;
;; Known bugs/problems :
;;
;;
;; TODO :
;;
;; make an installer.
;;
;; Please send any comments, bugs, enhancements, or suggestions or
;; requests for upgrades to Dino Chiesa ([email protected])
;;
(require 'cscomp-base) ;; cscomp-log, etc. basic stuff.
(require 'csharp-shell) ;; for CscompShell, which does code introspection
(require 'csharp-analysis) ;; for csharp-analysis-* fns
(defvar cscomp-current-p1 nil
"The first part (before any dot) of the current completion.
This is used to determine if re-using previous results is ok.
See also `cscomp-current-list'. ")
(defvar cscomp-current-p2 nil
"The second part (after any dot) of the current completion.
This is used to determine if re-using previous results is ok.
See also `cscomp-current-list'. ")
(defvar cscomp-current-flavor nil
"The second part (after any dot) of the current completion.
This is used to determine if re-using previous results is ok.
See also `cscomp-current-list'. ")
(defvar cscomp-current-list nil
"The list of all the completions for the current request. Each
element of the list is either a string, or a list, in 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)
(defcustom cscomp-assembly-search-paths nil
"a list of strings, each one a directory in which to search for assemblies."
:group 'cscomp
:type 'list)
(defconst cscomp--new-regexp
"^\\([ \t\n\r\f\v]*new[ \t\n\r\f\v]+\\)\\(.+\\)$")
(defun cscomp-referenced-assemblies ()
"Return the list of .NET namespaces, each encoded as a string,
referenced in the current buffer via using statements. The result
is something like this:
(\"System\" \"System.Collections\" \"System.Collections.Generic\" \"System.Reflection\")
"
(interactive)
(let ((result
(csharp-analysis-get-tagnames "import")))
(if (called-interactively-p 'any)
(message "result: %s" (prin1-to-string result)))
result))
(defun cscomp--capitalize (word)
"Capitalize WORD in place, and return it."
(let ((first-char (aref word 0))
result)
(if (> first-char 96)
(format "%c%s" (- first-char 32) (substring word 1))
word)))
(defun cscomp--find-matching-tags (name-fragment nodeset &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', and
`cscomp-matching-instance-members'.
"
(let ((var-label (if local "(Variable) " "(Field/Prop) " ))
result)
(if nodeset
(progn
(while nodeset
(let ((tag (car nodeset)))
(if tag
(let
((member-name (csharp-analysis-tag-name tag))
(member-type (csharp-analysis-tag-type tag))
(member-flavor (csharp-analysis-tag-flavor tag)))
(if (eq 0 (string-match name-fragment member-name))
(let ((descrip
(cond
((string= member-flavor "var")
(concat var-label member-type))
((or (string= member-flavor "property")
(string= member-flavor "field"))
(concat "(" (cscomp--capitalize member-flavor) ") " member-type))
((string= member-flavor "method")
(let* ((arglist (csharp-analysis-method-params tag))
(modifiers (csharp-analysis-tag-modifiers tag))
(arg-descrip
(if (> (length arglist) 0)
(concat "("
(mapconcat
'(lambda (x) (concat (caddr x)
" "
(cadr x)))
arglist ", ")
")")
"()")))
(concat "(Method) "
" " modifiers
" " arg-descrip
" returns "
member-type)))
(t ""))))
(cscomp-log 2 "tags: found %s (%s)"
member-name member-type)
(setq result (cons (list member-name descrip) result)))))))
(setq nodeset (cdr nodeset)))
(cscomp-sort-completion-list result))
nil)))
(defun cscomp-matching-instance-vars (name-fragment)
"Return the list of instance variables in scope, that
match NAME-FRAGMENT. See also, `cscomp-matching-local-vars'.
"
(interactive "sName fragment: ")
(let ((result
(cscomp--find-matching-tags name-fragment (csharp-analysis-instance-variables))))
(if (called-interactively-p 'any)
(message "result: %s" (prin1-to-string result)))
result))
(defun cscomp-matching-instance-members (name-fragment)
"Return the list of instance memebrs in scope in a C# module, that
match NAME-FRAGMENT.
See also, `cscomp-matching-local-vars',
`cscomp-matching-instance-vars'.
"
(interactive "sName fragment: ")
(let ((result
(cscomp--find-matching-tags name-fragment (csharp-analysis-instance-members))))
(if (called-interactively-p 'any)
(message "result: %s" (prin1-to-string result)))
result))
(defun cscomp-matching-local-vars (name-fragment)
"Find the local variables currently in scope that match the given
NAME-FRAGMENT.
For the purposes of this fn, the set of \"local variables\"
includes any parameters for the method, ctor, or setter block.
See also, `cscomp-matching-instance-members',
`cscomp-matching-instance-vars'.
"
(interactive "sName fragment: ")
(let* ((all-vars
(append (csharp-analysis-local-variables) ;; maybe nil
(csharp-analysis-local-arguments))) ;; maybe nil
(result
(cscomp--find-matching-tags name-fragment all-vars t)))
(if (called-interactively-p 'any)
(message "result: %s" (prin1-to-string result)))
result))
(defun cscomp-type-exists (typename)
"Determines if the type named by TYPENAME is known by the
CscompShell. You can provide a short type name, or a
fully-qualified name. The CscompShell must have previously
loaded the containing assembly, for it to be known. See
`cscomp-load-one-assembly'.
"
(interactive "sType name: ")
(csharp-shell-invoke-shell-fn "QualifyType" typename))
(defun cscomp-produce-csharp-arglist-block-from-tag (arglist)
"Produces an argument list block, suitable for framing within
parens in a method declaration, from ARGLIST, a list of local
arguments obtained from
`csharp-analysis-local-arguments'.
When the format of ARGLIST is like this:
((var \"x\" \"System.Int32\" (location (16 29) (16 33)) (id 6))
(var \"enabled\" \"System.Boolean\" (location (16 36) (16 41)) (id 7)))
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 (cadr x))
(var-type (caddr 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)
"Generate a fragment of C# source code that can be compiled to
produce IL, which can then be inspected to determine the type of
the var for which the user is asking completion.
This fn generates dummy code that includes placeholders for all
instance members, method arguments, and local variables. The
generated code 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:
((var \"staticField1\" \"System.Int32\"
(modifier \"private static\")
(location (14 9) (14 20)) (id 17))
(method \"InstanceMethod1\" \"System.String\"
(modifier \"public\")
(params
(var \"x\" \"System.Int32\" (location (16 29) (16 33)) (id 6))
(var \"enabled\" \"System.Boolean\" (location (16 36) (16 41)) (id 7)))
(block ...)
(location (16 9) (18 10)) (id 9))
...
)
The output will look like this:
class foo {
private static int staticField1 = default(int);
string InstanceMethod1(...) { 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 (csharp-analysis-tag-name x))
(member-flavor (csharp-analysis-tag-flavor x))
(member-type (csharp-analysis-tag-type x))
(member-modifiers (csharp-analysis-tag-modifiers x))
one-frag )
(setq one-frag
(cond
((or
(string= member-type "void") ;; the member is a void type
(string= member-type "System.Void"))
"") ;; emit nothing, don't need it.
;; if it turns out I was wrong, and the fn is necessary,
;; we can just emit an empty fn.
((string= member-flavor "method") ;; it's a method
(concat member-modifiers " " member-type " "
member-name "(" (cscomp-produce-csharp-arglist-block-from-tag
(csharp-analysis-method-params x))
") {"
(if member-type
(concat
" return default("
member-type
");")
"")
"} "))
((or
(string= member-flavor "field") ;; it's an instance field
(string= member-flavor "property")) ;; it's an instance property
(concat
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.
"
(let ((start-pos 0))
(while (string-match "\\([^']*\\)'\\(.*\\)" s start-pos)
(setq s (concat
(substring s 0 start-pos)
(substring s start-pos (match-end 1))
"''"
(substring s (+ 1 (match-end 1))))
start-pos (+ 2 (match-end 1)))))
s)
(defun cscomp--infer-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 ((pathlist
(mapconcat 'identity cscomp-assembly-search-paths ",")))
(csharp-shell-invoke-shell-fn "SetAssemblySearchPaths" pathlist))
(let* ((massaged-decl
(cscomp-escape-single-quotes
(cscomp-consolidate-whitespace var-declaration)))
(namespaces (mapconcat 'identity (cscomp-referenced-assemblies) ","))
(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)
"')" )))
(csharp-shell-do-shell-fn 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-one-assembly'."
(interactive "sNamespace: ")
(csharp-shell-invoke-shell-fn "GetCompletionsForNamespace" ns))
(defun cscomp-qualify-type-name (type-name)
"returns fully-qualified type name for a given short TYPE-NAME.
Return value is a string, or nil if the type-name is unknown.
Example: (cscomp-qualify-type-name \"XmlAttribute\")
returns \"System.Xml.XmlAttribute\"
"
(let ((result (cscomp-qualify-name type-name)))
(cond
((string= (nth 0 result) "type")
(nth 1 result))
(t
nil))))
(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: ")
(csharp-shell-invoke-shell-fn "QualifyName" name))
(defun cscomp-get-all-known-matches (fragment)
"returns a list of all possible matches on a given partial name.
The return value is like this:
((\"type\" \"fullNameOfType\")
(\"namespace\" \"FullNameOfNamespace\")
(\"method\" \"NameOfMethod\")
(\"variable\" \"woof\" \"(local) System.Int32\"))
"
(interactive "sfragment to match on: ")
(let* ((fixup-varlist
(lambda (item)
(list "variable" (car item) (cadr item))))
(fixup-instancelist
(lambda (item)
(list "instance" (car item) (cadr item))))
(instance-vars (mapcar fixup-instancelist
(cscomp-matching-instance-vars fragment)))
(locals (mapcar fixup-varlist
(cscomp-matching-local-vars fragment)))
(namespaces (mapconcat 'identity (cscomp-referenced-assemblies) ","))
(matches (csharp-shell-do-shell-fn
(concat "[Ionic.Cscomp.Utilities]::GetMatches('"
fragment
"','"
namespaces
"')"))))
(append instance-vars locals matches)))
(defun cscomp-load-one-assembly (lib)
"Loads a assembly into the csharp-shell, which then allows
Cscomp to do completion (etc) on the types in that library. The
assembly should be a namespace of an assy that is in the GAC, or
the full path of the assembly file (usually a DLL).
"
(interactive "sLibrary: ")
(csharp-shell-invoke-shell-fn "LoadOneAssembly" lib))
(defun cscomp-load-imported-namespaces ()
"Loads assemblies for the imported namespaces into the CscompShell."
(mapcar 'cscomp-load-one-assembly
(cscomp-referenced-assemblies)))
(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 2 "get-qualified-name (%s)" name)
(let ((result (cscomp-type-exists name)))
(if result result
;; else
(let ((usinglist (cscomp-referenced-assemblies))
fullname namespace )
;; usinglist is like this:
;; ("System" "System.Collections" "System.Collections.Generic" ...)
(setq result nil)
(while usinglist
(setq namespace (car usinglist))
(cscomp-load-one-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) ;; end the loop
(setq usinglist (cdr usinglist)))))
(cscomp-log 2 "get-qualified-name (%s): '%s'" name result)
result)))
(defun cscomp-reset-typeinfo-cache ()
"Clears all entries in the type information cache."
(interactive)
(setq cscomp-typeinfo-cache 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 "typeinfo cache is full"))
;;else
;; nconc?
(setq cscomp-typeinfo-cache
(append
cscomp-typeinfo-cache
(list (list name typeinfo)))))))
(defun cscomp--get-typeinfo-from-cache (name usinglist)
"Gets type information for the type with NAME, from the cache."
(if (string= name "var")
nil ;; unknown
(let ((temp (nth 0 cscomp-typeinfo-cache))
(index -1)
(found nil))
(while (and temp (not found))
(incf index)
(cscomp-log 4 "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 "-get-typeinfo-from-cache: HIT name(%s) r(%s)"
name (prin1-to-string (nth 1 temp)))
(nth 1 temp))
(cscomp-log 3 "-get-typeinfo-from-cache: MISS name(%s)" name)
nil))))
(defun cscomp-get-typeinfo (name)
"Return the class info list for the class NAME. This function first
checks to see if the class info is cached. If so, it returns the
cached class info. Otherwise, it creates the type info list. Each
element of the list returned by this function is itself a list whose
car is a possible completion and whose cdr gives additional
informations on the completion - the property type, or the param
list and return type for a method, etc."
(interactive "sTypename: ")
(if (string= name "var")
(progn
(cscomp-log 2 "ENTER get-typeinfo name(%s)...result:nil" name)
nil) ;; unknown
(cscomp-log 2 "ENTER get-typeinfo name(%s)...trying cache..." name)
(let* ((usinglist (cscomp-referenced-assemblies))
(type-info (cscomp--get-typeinfo-from-cache name usinglist))
(ulist (mapconcat 'identity usinglist ", "))
namespace
qualified-type )
;; load all the assemblies mentioned in the using clauses
(while usinglist
(setq namespace (car usinglist))
(cscomp-load-one-assembly namespace)
(setq usinglist (cdr usinglist)))
(if (null type-info)
(progn
(setq qualified-type
(csharp-shell-exec-and-eval-result
(concat
"[Ionic.Cscomp.Utilities]::QualifyType('"
;;(cscomp-escape-string-for-powershell name)
;; dont need to escape if using single quotes
name
"','"
ulist
"')")))
(cscomp-log 2 "get-typeinfo...(%s)..." (prin1-to-string qualified-type))
(if qualified-type
(setq type-info
(csharp-shell-exec-and-eval-result
(concat "[Ionic.Cscomp.Utilities]::GetTypeInfo('"
(car qualified-type) ; type name
"', '"
(cadr qualified-type) ; assembly name
"')" ))))
(if type-info
(cscomp-add-to-typeinfo-cache name type-info))))
type-info)))
(defun cscomp-get-type-ctors (type-name)
"Retrieve constructors from CscompShell for the type named by TYPE-NAME.
"
(interactive "sType name: ")
(csharp-shell-invoke-shell-fn "GetConstructors" type-name))
(defun cscomp-split-by-dots (s)
"When the string contains a dot, this fn returns a 2-element
list (TOKEN1 TOKEN2). TOKEN1 is the substring of s that precedes
the last dot. TOKEN2 is the substring that follows the last dot.
When the string does not contain a dot, this fn returns a
2-element list in which the first element is nil and the 2nd
element is the entire string.
"
(cscomp-log 2 "split-by-dots: s[%s]" s)
(if (string-match "\\(.*\\)\\.\\(.*\\)" s)