forked from ljw1004/csharp2colorized
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csharp2colorized.cs
769 lines (706 loc) · 38.6 KB
/
csharp2colorized.cs
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
namespace CSharp2Colorized
{
public class ColorizedWord
{
public string Text;
public int Red;
public int Green;
public int Blue;
public bool IsItalic;
public override string ToString() => Text;
}
public class ColorizedLine
{
public List<ColorizedWord> Words = new List<ColorizedWord>();
public override string ToString() => string.Join("", Words.Select(w => w.Text));
}
public static class Colorize
{
private static int Main(string[] args0)
{
var args = new List<string>(args0);
Action<IEnumerable<string>> converter = null;
if (args.Count == 1 && new[] { "-vb", "--vb" }.Contains(args[0]))
{
converter = lines => Console.WriteLine(Lines2Html(VB(string.Join("",lines)))); args.RemoveAt(0);
}
else if (args.Count == 1 && new[] { "-csharp", "--csharp", "-c#", "--c#", "-cs", "--cs" }.Contains(args[0]))
{
converter = lines => Console.WriteLine(Lines2Html(CSharp(string.Join("",lines)))); args.RemoveAt(0);
}
else if (args.Count == 1 && new[] { "-md", "--md" }.Contains(args[0]))
{
converter = ColorizeMd; args.RemoveAt(0);
}
else if (args.Any(a => a.StartsWith("-")) || (converter != null && args.Any()))
{
Console.WriteLine("usage: csharp2colorized [-cs|-vb|-md] | [files]");
return 0;
}
if (args.Count == 0)
{
// pipe
if (converter == null) converter = lines => Console.WriteLine(Lines2Html(CSharp(string.Join("",lines))));
converter(Console2Enumerable());
return 0;
}
// otherwise, assume command-line arguments tell us filenames
// (and sume that converter == null)
var fns = new List<string>();
foreach (var fn in args)
{
if (!fn.Contains("*") && !fn.Contains("?"))
{
if (!File.Exists(fn)) { Console.Error.WriteLine($"Not found - {fn}"); return 1; }
fns.Add(fn);
}
else
{
// Windows command-shell doesn't do globbing, so we have to do it ourselves
string dir = Path.GetDirectoryName(fn), filename = Path.GetFileName(fn);
if (dir.Contains("*") || dir.Contains("?")) { Console.Error.WriteLine("Can't match wildcard directory names"); return 1; }
if (dir == "") dir = Directory.GetCurrentDirectory();
if (!Directory.Exists(dir)) { Console.Error.WriteLine($"Not found - \"{dir}\""); return 1; }
var fns2 = Directory.GetFiles(dir, filename);
if (fns2.Length == 0) { Console.Error.WriteLine($"Not found - \"{fn}\""); return 1; }
fns.AddRange(fns2);
}
}
foreach (var fn in fns)
{
if (fns.Count > 1) Console.WriteLine($"<h1>{WebUtility.HtmlEncode(Path.GetFileName(fn))}</h1>");
var code = File.ReadAllText(fn);
var ext = Path.GetExtension(fn).ToLower();
if (ext == ".vb" || ext == ".vbx" || ext == "bas") Console.WriteLine(Lines2Html(VB(code)));
else if (ext == ".cs" || ext == ".csx") Console.WriteLine(Lines2Html(CSharp(code)));
else if (ext == ".md") ColorizeMd(String2Enumerable(code));
else { Console.Error.WriteLine($"Don't know how to colorize - \"{fn}\""); continue; }
}
return 0;
}
private static IEnumerable<string> Console2Enumerable()
{
while (true)
{
var s = Console.ReadLine();
if (s == null) yield break;
yield return s + Environment.NewLine;
}
}
private static IEnumerable<string> String2Enumerable(string s)
{
int istart = 0; string b = null;
for (int i=0; i<s.Length; i++)
{
if (s[i] == '\r' && i + 1 < s.Length && s[i + 1] == '\n') b = "\r\n";
else if (s[i] == '\r') b = "\r";
else if (s[i] == '\n') b = "\n";
else continue;
yield return s.Substring(istart, i + b.Length - istart);
istart = i + b.Length;
i = istart-1;
}
if (istart < s.Length) yield return s.Substring(istart);
}
private static void ColorizeMd(IEnumerable<string> lines)
{
string lang = null, fence = null, indent = null, terminator = "";
StringBuilder fb = null, cb = null;
foreach (var line in lines)
{
if (cb == null) // non-codeblock
{
MdIsFenceStart(line, out lang, out fence, out indent, out terminator);
if (lang == null) { Console.Write(string.IsNullOrWhiteSpace(line) ? " "+line : line); }
else { fb = new StringBuilder(); fb.Append(line); cb = new StringBuilder(); }
// the IsNullOrWhitespace is to workaround this: https://github.com/dotnet/corefx/issues/7409
}
else // codeblock
{
fb.Append(line);
var line2 = MdRemoveFenceIndent(line.TrimEnd("\r\n".ToCharArray()), indent);
if (!MdIsFenceEnd(line2, fence)) { cb.AppendLine(line2); continue; }
//
Func<string, IEnumerable<ColorizedLine>> converter = null;
if (lang == "csharp" || lang == "cs" || lang == "c#" || lang == "csx") converter = CSharp;
else if (lang == "vb" || lang == "vb.net" || lang == "vbnet") converter = VB;
else { cb = null; Console.Write(fb.ToString()); continue; }
//
var code = cb.ToString();
var cols = converter(code);
var html = Lines2Html(cols);
var pre = html.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
var ss = pre.Take(1).Select(s => s.Replace("<pre>", "<pre>" + indent))
.Concat(pre.Skip(1).Select(s => indent + s));
var tt = string.Join(terminator, ss);
Console.Write(tt);
cb = null;
}
}
}
private static Regex MdFenceStart = new Regex("^( *)((?<back>````*)|(?<tilde>~~~~*)) *([^ \r\n]*)");
private static bool MdIsFenceStart(string line, out string lang, out string fence, out string indent, out string terminator)
{
lang = null; fence = null; indent = null; terminator = null;
var m = MdFenceStart.Match(line);
if (!m.Success) return false;
indent = m.Groups[1].Value;
fence = m.Groups[2].Value;
lang = m.Groups[3].Value;
if (line.EndsWith("\r\n")) terminator = "\r\n";
else if (line.EndsWith("\r")) terminator = "\r";
else if (line.EndsWith("\n")) terminator = "\n";
else terminator = "\r\n";
return true;
}
private static string MdRemoveFenceIndent(string line, string indent)
{
while (indent.StartsWith(" ") && line.StartsWith(" "))
{
line = line.Substring(1); indent = indent.Substring(1);
}
return line;
}
private static bool MdIsFenceEnd(string line, string fence)
{
if (!line.StartsWith(fence)) return false;
fence = fence.Substring(0, 1);
while (line.StartsWith(fence)) line = line.Substring(1);
while (line.StartsWith(" ")) line = line.Substring(1);
return (line == "");
}
/// <summary>
/// Colorizes a fragment of C# code
/// </summary>
public static IEnumerable<ColorizedLine> CSharp(string code) =>
Words2Lines(CSharpSpecific.CSharpColorizingWalker.ColorizeInternal(code));
public static IEnumerable<ColorizedLine> VB(string code) =>
Words2Lines(VBSpecific.VBColorizingWalker.ColorizeInternal(code));
public static IEnumerable<ColorizedLine> PlainText(string src) =>
Words2Lines(ColorizePlainTextInternal(src));
public static string Lines2Html(IEnumerable<ColorizedLine> lines)
{
var sb = new StringBuilder();
sb.Append("<pre style=\"font-family: Consolas, 'Lucida Console', Courier, 'Courier New', monospace\">");
foreach (var line in lines)
{
foreach (var word in line.Words)
{
var color = $"color:#{word.Red:x2}{word.Green:x2}{word.Blue:x2}";
if (color == "color:#000000") color = null;
if (word.IsItalic) sb.Append("<em>");
if (color != null) sb.Append($"<span style=\"{color}\">");
sb.Append(WebUtility.HtmlEncode(word.Text));
if (color != null) sb.Append("</span>");
if (word.IsItalic) sb.Append("</em>");
}
// workaround for https://github.com/dotnet/corefx/issues/7409
sb.AppendLine(line.Words.Count>0 ? "" : " ");
}
sb.AppendLine("</pre>");
return sb.ToString();
}
/// <summary>
/// Helper function: takes an load of words (with "null" representing a linebreak) and turns them into lines,
/// minimizing along the way by combining adjacent words of the same color
/// </summary>
public static IEnumerable<ColorizedLine> Words2Lines(IEnumerable<ColorizedWord> words)
{
var encounteredFirstLinebreak = false;
var currentLine = new ColorizedLine();
var currentWord = null as ColorizedWord;
foreach (var nextWord in words)
{
if (nextWord == null) // linebreak
{
if (currentWord != null)
{
currentWord.Text = currentWord.Text.TrimEnd();
if (!string.IsNullOrWhiteSpace(currentWord.Text)) currentLine.Words.Add(currentWord);
}
if (encounteredFirstLinebreak || currentLine.Words.Count > 0) yield return currentLine;
encounteredFirstLinebreak = true;
currentLine = new ColorizedLine();
currentWord = null;
}
else if (currentWord == null) // first word on line
{
currentWord = nextWord;
}
else if (string.IsNullOrWhiteSpace(currentWord.Text)) // merge the currentWord into the new one
{
nextWord.Text = currentWord.Text + nextWord.Text;
currentWord = nextWord;
}
else if (string.IsNullOrWhiteSpace(nextWord.Text)) // merge the word into the previous one
{
currentWord.Text = currentWord.Text + nextWord.Text;
}
else if (currentWord.Red == nextWord.Red && currentWord.Green == nextWord.Green && currentWord.Blue == nextWord.Blue && currentWord.IsItalic == nextWord.IsItalic)
{
currentWord.Text = currentWord.Text + nextWord.Text;
}
else
{
currentLine.Words.Add(currentWord);
currentWord = nextWord;
}
}
if (currentWord != null)
{
currentWord.Text = currentWord.Text.TrimEnd();
if (!string.IsNullOrWhiteSpace(currentWord.Text)) currentLine.Words.Add(currentWord);
}
if (currentLine.Words.Count > 0) yield return currentLine;
}
private static IEnumerable<ColorizedWord> ColorizePlainTextInternal(string src)
{
var lines = src.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
if (lines.Last() == "") lines.RemoveAt(lines.Count - 1);
foreach (var line in lines)
{
if (line != "") yield return new ColorizedWord { Text = line };
yield return null;
}
}
}
namespace CSharpSpecific
{
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
internal class CSharpColorizingWalker : CSharpSyntaxWalker
{
// This code is based on that of Shiv Kumar at http://www.matlus.com/c-to-html-syntax-highlighter-using-roslyn/
internal static IEnumerable<ColorizedWord> ColorizeInternal(string code)
{
code = code.Replace("...", "___threedots___"); // because ... is unusually hard to parse
code = code.Replace("from *", "from ___linq_transparent_asterisk___"); // transparent identifier doesn't parse
var ref_mscorlib = MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location);
var ref_system = MetadataReference.CreateFromFile(typeof(Uri).GetTypeInfo().Assembly.Location);
var ref_systemcore = MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location);
var ref_systemcollectionsimmutable = MetadataReference.CreateFromFile(typeof(ImmutableArray<>).GetTypeInfo().Assembly.Location);
var parse_options = new CSharpParseOptions(kind: SourceCodeKind.Script);
var compile_options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: new[] { "System", "System.Collections", "System.Collections.Generic" });
var compilationUnit = SyntaxFactory.ParseCompilationUnit(code, options: parse_options);
var syntaxTree = compilationUnit.SyntaxTree;
var compilation = CSharpCompilation.Create("dummyAssemblyName", new[] { syntaxTree }, new[] { ref_mscorlib, ref_system, ref_systemcore, ref_systemcollectionsimmutable }, compile_options);
var semanticModel = compilation.GetSemanticModel(syntaxTree, true);
var w = new CSharpColorizingWalker() { sm = semanticModel };
w.Visit(syntaxTree.GetRoot());
//
foreach (var word in w.Words)
{
if (word == null) yield return word;
else if (word.Text == "___linq_transparent_asterisk___") yield return new ColorizedWord { Text = "*" };
else if (word.Text == "___threedots___") yield return new ColorizedWord { Text = "..." };
else
{
word.Text = word.Text.Replace("___linq_transparent_asterisk___", "*");
word.Text = word.Text.Replace("___threedots___", "...");
yield return word;
}
}
}
private List<ColorizedWord> Words = new List<ColorizedWord>();
private SemanticModel sm;
private CSharpColorizingWalker() : base(SyntaxWalkerDepth.StructuredTrivia) { }
public override void VisitToken(SyntaxToken token)
{
VisitTriviaList(token.LeadingTrivia);
IEnumerable<ColorizedWord> r = null;
if (token.IsKeyword())
{
r = Col(token.Text, "Keyword");
}
else if (token.IsKind(SyntaxKind.StringLiteralToken))
{
r = Col(token.Text, "StringLiteral");
}
else if (token.IsKind(SyntaxKind.CharacterLiteralToken))
{
r = Col(token.Text, "StringLiteral");
}
else if (token.IsKind(SyntaxKind.IdentifierToken) && token.Parent is TypeParameterSyntax)
{
r = Col(token.Text, "UserType");
}
else if (token.IsKind(SyntaxKind.IdentifierToken) && token.Parent is SimpleNameSyntax)
{
var name = token.Parent as SimpleNameSyntax;
ISymbol symbol = null;
try { symbol = sm?.GetSymbolInfo(name).Symbol; }
catch (NullReferenceException) { }
// https://github.com/dotnet/roslyn/issues/10023 this might throw NRE, even though it shouldn't...
if (symbol?.Kind == SymbolKind.NamedType
|| symbol?.Kind == SymbolKind.TypeParameter)
{
r = Col(token.Text, "UserType");
}
else if (symbol?.Kind == SymbolKind.DynamicType)
{
r = Col(token.Text, "Keyword");
}
else if (symbol?.Kind == SymbolKind.Namespace
|| symbol?.Kind == SymbolKind.Parameter
|| symbol?.Kind == SymbolKind.Local
|| symbol?.Kind == SymbolKind.Field
|| symbol?.Kind == SymbolKind.Property)
{
r = Col(token.Text, "PlainText");
}
else if (name.Identifier.Text == "var")
{
r = Col(token.Text, "Keyword");
}
else if (new[] { "C", "T", "U", "V" }.Contains(name.Identifier.Text))
{
r = Col(token.Text, "UserType");
}
}
else if (token.IsKind(SyntaxKind.IdentifierToken) && token.Parent is TypeDeclarationSyntax)
{
var name = token.Parent as TypeDeclarationSyntax;
var symbol = sm.GetDeclaredSymbol(name);
if (symbol?.Kind == SymbolKind.NamedType)
{
r = Col(token.Text, "UserType");
}
}
if (r == null)
{
if ((token.Parent as EnumDeclarationSyntax)?.Identifier == token)
{
r = Col(token.Text, "UserType");
}
else if ((token.Parent as GenericNameSyntax)?.Identifier == token)
{
if ((token.Parent.Parent as InvocationExpressionSyntax)?.Expression == token.Parent // e.g. "G<X>(1)"
|| (token.Parent.Parent.Parent as InvocationExpressionSyntax)?.Expression == token.Parent.Parent) // e.g. e.G<X>(1)
{
r = Col(token.Text, "PlainText");
}
else // all other G<A> will assume that G is a type
{
r = Col(token.Text, "UserType");
}
}
else if (token.Parent.IsKind(SyntaxKind.GenericName))
{
if (token.Parent.Parent.IsKind(SyntaxKind.VariableDeclaration) // e.g. "private static readonly HashSet patternHashSet = New HashSet();" the first HashSet in this case
|| token.Parent.Parent.IsKind(SyntaxKind.ObjectCreationExpression)) // e.g. "private static readonly HashSet patternHashSet = New HashSet();" the second HashSet in this case
{
r = Col(token.Text, "UserType");
}
}
else if (token.Parent.IsKind(SyntaxKind.IdentifierName))
{
if (token.Parent.Parent.IsKind(SyntaxKind.Parameter)
|| token.Parent.Parent.IsKind(SyntaxKind.Attribute)
|| token.Parent.Parent.IsKind(SyntaxKind.CatchDeclaration)
|| token.Parent.Parent.IsKind(SyntaxKind.ObjectCreationExpression)
|| token.Parent.Parent.IsKind(SyntaxKind.MethodDeclaration)
|| token.Parent.Parent.IsKind(SyntaxKind.BaseList) // e.g. "public sealed class BuilderRouteHandler IRouteHandler" IRouteHandler in this case
|| token.Parent.Parent.Parent.IsKind(SyntaxKind.TypeOfExpression) // e.g. "Type baseBuilderType = TypeOf(BaseBuilder);" BaseBuilder in this case
|| token.Parent.Parent.IsKind(SyntaxKind.VariableDeclaration) // e.g. "private DbProviderFactory dbProviderFactory;" Or "DbConnection connection = dbProviderFactory.CreateConnection();"
|| token.Parent.Parent.IsKind(SyntaxKind.TypeArgumentList)) // e.g. "DbTypes = New Dictionary();" DbType in this case
{
r = Col(token.Text, "UserType");
}
else if ((token.Parent.Parent as CastExpressionSyntax)?.Type == token.Parent // e.g. "(Foo)x" the Foo
|| (token.Parent.Parent as TypeConstraintSyntax)?.Type == token.Parent // e.g. "where T:Foo" the Foo
|| (token.Parent.Parent as ArrayTypeSyntax)?.ElementType == token.Parent) // e.g. "Foo[]" the Foo
{
r = Col(token.Text, "UserType");
}
else if ((token.Parent.Parent.IsKind(SyntaxKind.ForEachStatement) && token.GetNextToken().Kind() != SyntaxKind.CloseParenToken)
|| (token.Parent.Parent.Parent.IsKind(SyntaxKind.CaseSwitchLabel) && token.GetPreviousToken().Kind() != SyntaxKind.DotToken)
|| (token.Parent.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression) && token.Parent.Parent.Parent.IsKind(SyntaxKind.Argument) && !token.GetPreviousToken().IsKind(SyntaxKind.DotToken) && token.Text.Length > 0 && !char.IsLower(token.Text[0])) // e.g. "DbTypes.Add("int", DbType.Int32);" DbType in this case
|| (token.Parent.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression) && !token.GetPreviousToken().IsKind(SyntaxKind.DotToken) && token.Text.Length > 0 && !char.IsLower(token.Text[0])))
{
r = Col(token.Text, "UserType");
}
}
}
if (r == null && !string.IsNullOrEmpty(token.Text)) // Empty comes from EndOfFile, OmmittedToken, ...
{
r = Col(token.Text, "PlainText");
}
if (r != null) Words.AddRange(r);
VisitTriviaList(token.TrailingTrivia);
}
void VisitTriviaList(SyntaxTriviaList trivias)
{
foreach (var trivia in trivias)
{
var txt = trivia.ToFullString();
if (trivia.IsKind(SyntaxKind.EndOfLineTrivia))
{
Words.Add(null);
}
else if (trivia.IsKind(SyntaxKind.MultiLineCommentTrivia)
|| trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)
|| trivia.IsKind(SyntaxKind.MultiLineDocumentationCommentTrivia)
|| trivia.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia))
{
Words.AddRange(Col(txt, "Comment"));
}
else if (trivia.IsKind(SyntaxKind.DisabledTextTrivia))
{
Words.AddRange(Col(txt, "ExcludedCode"));
}
else if (trivia.IsDirective)
{
Words.AddRange(Col(txt, "Preprocessor"));
}
else
{
Words.AddRange(Col(txt, "PlainText"));
}
}
}
private IEnumerable<ColorizedWord> Col(string token, string color)
{
var isFirstLine = true;
foreach (var txt in token.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
{
if (isFirstLine) isFirstLine = false; else yield return null;
//
if (txt == "") continue;
else if (color == "PlainText") yield return new ColorizedWord { Text = txt };
else if (color == "Keyword") yield return new ColorizedWord { Text = txt, Blue = 255 };
else if (color == "UserType") yield return new ColorizedWord { Text = txt, Red = 43, Green = 145, Blue = 175 };
else if (color == "StringLiteral") yield return new ColorizedWord { Text = txt, Red = 163, Green = 21, Blue = 21 };
else if (color == "Comment") yield return new ColorizedWord { Text = txt, Green = 128 };
else if (color == "ExcludedCode") yield return new ColorizedWord { Text = txt, Red = 128, Green = 128, Blue = 128 };
else if (color == "Preprocessor") yield return new ColorizedWord { Text = txt, Red = 163, Green = 21, Blue = 128 };
else throw new Exception("bad color name");
}
}
}
}
namespace VBSpecific
{
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
internal class VBColorizingWalker : VisualBasicSyntaxWalker
{
// This code is based on that of Shiv Kumar at http://www.matlus.com/c-to-html-syntax-highlighter-using-roslyn/
private static Regex re = new Regex("^From [a-zA-Z0-9_]+ ((?<in>In)|(?<as>As)) ");
internal static IEnumerable<ColorizedWord> ColorizeInternal(string code)
{
if (re.IsMatch(code)) code = "__dummyfrom__=" + code; // because From is contextual
code = code.Replace("...", "___threedots___"); // because ... is unusually hard to parse
var ref_mscorlib = MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location);
var ref_system = MetadataReference.CreateFromFile(typeof(Uri).GetTypeInfo().Assembly.Location);
var ref_systemcore = MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location);
var ref_systemcollectionsimmutable = MetadataReference.CreateFromFile(typeof(ImmutableArray<>).GetTypeInfo().Assembly.Location);
var parse_options = new VisualBasicParseOptions(kind: SourceCodeKind.Script);
var compile_options = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, globalImports: new[] { GlobalImport.Parse("System"), GlobalImport.Parse("System.Collections"), GlobalImport.Parse("System.Collections.Generic") });
var compilationUnit = SyntaxFactory.ParseCompilationUnit(code, options: parse_options);
var syntaxTree = compilationUnit.SyntaxTree;
var compilation = VisualBasicCompilation.Create("dummyAssemblyName", new[] { syntaxTree }, new[] { ref_mscorlib, ref_system, ref_systemcore, ref_systemcollectionsimmutable }, compile_options);
var semanticModel = compilation.GetSemanticModel(syntaxTree, true);
var w = new VBColorizingWalker() { sm = semanticModel };
w.Visit(syntaxTree.GetRoot());
//
var suppressNextEquals = false;
foreach (var word in w.Words)
{
if (word == null) yield return word;
else if (word.Text == "___threedots___") yield return new ColorizedWord { Text = "..." };
else if (word.Text == "__dummyfrom__") { suppressNextEquals = true; continue; }
else if (word.Text == "=" && suppressNextEquals) { }
else
{
word.Text = word.Text.Replace("___threedots___", "...").Replace("__dummyfrom__", "");
yield return word;
}
}
}
private List<ColorizedWord> Words = new List<ColorizedWord>();
private SemanticModel sm;
private VBColorizingWalker() : base(SyntaxWalkerDepth.StructuredTrivia) { }
public override void VisitToken(SyntaxToken token)
{
VisitTriviaList(token.LeadingTrivia);
if (token.Text == "Foo")
{
Console.WriteLine();
}
IEnumerable<ColorizedWord> r = null;
if (token.IsKeyword())
{
r = Col(token.Text, "Keyword");
}
else if (token.IsKind(SyntaxKind.StringLiteralToken))
{
r = Col(token.Text, "StringLiteral");
}
else if (token.IsKind(SyntaxKind.CharacterLiteralToken))
{
r = Col(token.Text, "StringLiteral");
}
else if (token.IsKind(SyntaxKind.IdentifierToken) && token.Parent is TypeParameterSyntax)
{
r = Col(token.Text, "UserType");
}
else if (token.IsKind(SyntaxKind.IdentifierToken) && token.Parent is SimpleNameSyntax)
{
var name = token.Parent as SimpleNameSyntax;
ISymbol symbol = null;
try { symbol = sm?.GetSymbolInfo(name).Symbol; }
catch (NullReferenceException) { }
// https://github.com/dotnet/roslyn/issues/10023 this might throw NRE, even though it shouldn't...
if (symbol?.Kind == SymbolKind.NamedType
|| symbol?.Kind == SymbolKind.TypeParameter)
{
r = Col(token.Text, "UserType");
}
else if (symbol?.Kind == SymbolKind.Namespace
|| symbol?.Kind == SymbolKind.Parameter
|| symbol?.Kind == SymbolKind.Local
|| symbol?.Kind == SymbolKind.Field
|| symbol?.Kind == SymbolKind.Property)
{
r = Col(token.Text, "PlainText");
}
else if (name.Identifier.Text == "var")
{
r = Col(token.Text, "Keyword");
}
else if (new[] { "C", "T", "U", "V" }.Contains(name.Identifier.Text))
{
r = Col(token.Text, "UserType");
}
}
else if (token.IsKind(SyntaxKind.IdentifierToken) && token.Parent is DeclarationStatementSyntax)
{
var name = token.Parent as DeclarationStatementSyntax;
var symbol = sm.GetDeclaredSymbol(name);
if (symbol?.Kind == SymbolKind.NamedType)
{
r = Col(token.Text, "UserType");
}
}
if (r == null)
{
if ((token.Parent as EnumStatementSyntax)?.Identifier == token)
{
r = Col(token.Text, "UserType");
}
else if ((token.Parent as GenericNameSyntax)?.Identifier == token)
{
if ((token.Parent.Parent as InvocationExpressionSyntax)?.Expression == token.Parent // e.g. "G<X>(1)"
|| (token.Parent.Parent.Parent as InvocationExpressionSyntax)?.Expression == token.Parent.Parent) // e.g. e.G<X>(1)
{
r = Col(token.Text, "PlainText");
}
else // all other G<A> will assume that G is a type
{
r = Col(token.Text, "UserType");
}
}
else if (token.Parent.IsKind(SyntaxKind.GenericName))
{
if (token.Parent.Parent.IsKind(SyntaxKind.SimpleAsClause) // e.g. "dim x As HashSet" the HashSet
|| token.Parent.Parent.IsKind(SyntaxKind.ObjectCreationExpression)) // e.g. "dim x As New HashSet" or "dim x = New HashSet"
{
r = Col(token.Text, "UserType");
}
}
else if (token.Parent.IsKind(SyntaxKind.IdentifierName))
{
if (token.Parent.Parent.IsKind(SyntaxKind.Parameter)
|| token.Parent.Parent.IsKind(SyntaxKind.Attribute)
|| token.Parent.Parent.IsKind(SyntaxKind.CatchStatement)
|| token.Parent.Parent.IsKind(SyntaxKind.ObjectCreationExpression)
|| token.Parent.Parent.IsKind(SyntaxKind.SubStatement)
|| token.Parent.Parent.IsKind(SyntaxKind.FunctionStatement)
|| token.Parent.Parent.IsKind(SyntaxKind.InheritsStatement) // e.g. "public sealed class BuilderRouteHandler IRouteHandler" IRouteHandler in this case
|| token.Parent.Parent.IsKind(SyntaxKind.GetTypeExpression) // e.g. "GetType(BaseBuilder);" BaseBuilder in this case
|| token.Parent.Parent.IsKind(SyntaxKind.SimpleAsClause) // e.g. "private DbProviderFactory dbProviderFactory;" Or "DbConnection connection = dbProviderFactory.CreateConnection();"
|| token.Parent.Parent.IsKind(SyntaxKind.TypeArgumentList)) // e.g. "DbTypes = New Dictionary();" DbType in this case
{
r = Col(token.Text, "UserType");
}
else if ((token.Parent.Parent as CastExpressionSyntax)?.Type == token.Parent // e.g. "(Foo)x" the Foo
|| (token.Parent.Parent as TypeConstraintSyntax)?.Type == token.Parent // e.g. "where T:Foo" the Foo
|| (token.Parent.Parent as ArrayTypeSyntax)?.ElementType == token.Parent) // e.g. "Foo[]" the Foo
{
r = Col(token.Text, "UserType");
}
else if ((token.Parent.Parent.IsKind(SyntaxKind.ForEachStatement) && token.GetNextToken().Kind() != SyntaxKind.CloseParenToken)
//|| (token.Parent.Parent.Parent.IsKind(SyntaxKind.CaseSwitchLabel) && token.GetPreviousToken().Kind() != SyntaxKind.DotToken)
//|| (token.Parent.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression) && token.Parent.Parent.Parent.IsKind(SyntaxKind.Argument) && !token.GetPreviousToken().IsKind(SyntaxKind.DotToken) && token.Text.Length > 0 && !char.IsLower(token.Text[0])) // e.g. "DbTypes.Add("int", DbType.Int32);" DbType in this case
|| (token.Parent.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression) && !token.GetPreviousToken().IsKind(SyntaxKind.DotToken) && token.Text.Length > 0 && !char.IsLower(token.Text[0])))
{
r = Col(token.Text, "UserType");
}
}
}
if (r == null && !string.IsNullOrEmpty(token.Text)) // Empty comes from EndOfFile, OmmittedToken, ...
{
r = Col(token.Text, "PlainText");
}
if (r != null) Words.AddRange(r);
VisitTriviaList(token.TrailingTrivia);
}
void VisitTriviaList(SyntaxTriviaList trivias)
{
foreach (var trivia in trivias)
{
var txt = trivia.ToFullString();
if (trivia.IsKind(SyntaxKind.EndOfLineTrivia))
{
Words.Add(null);
}
else if (trivia.IsKind(SyntaxKind.CommentTrivia)
|| trivia.IsKind(SyntaxKind.DocumentationCommentTrivia))
{
Words.AddRange(Col(txt, "Comment"));
}
else if (trivia.IsKind(SyntaxKind.DisabledTextTrivia))
{
Words.AddRange(Col(txt, "ExcludedCode"));
}
else if (trivia.IsDirective)
{
Words.AddRange(Col(txt, "Preprocessor"));
}
else
{
Words.AddRange(Col(txt, "PlainText"));
}
}
}
private IEnumerable<ColorizedWord> Col(string token, string color)
{
var isFirstLine = true;
foreach (var txt in token.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
{
if (isFirstLine) isFirstLine = false; else yield return null;
//
if (txt == "") continue;
else if (color == "PlainText") yield return new ColorizedWord { Text = txt };
else if (color == "Keyword") yield return new ColorizedWord { Text = txt, Blue = 255 };
else if (color == "UserType") yield return new ColorizedWord { Text = txt, Red = 43, Green = 145, Blue = 175 };
else if (color == "StringLiteral") yield return new ColorizedWord { Text = txt, Red = 163, Green = 21, Blue = 21 };
else if (color == "Comment") yield return new ColorizedWord { Text = txt, Green = 128 };
else if (color == "ExcludedCode") yield return new ColorizedWord { Text = txt, Red = 128, Green = 128, Blue = 128 };
else if (color == "Preprocessor") yield return new ColorizedWord { Text = txt, Red = 163, Green = 21, Blue = 128 };
else throw new Exception("bad color name");
}
}
}
}
}