SyntaxHighlighting.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using Terminal.Gui;
  10. using static UICatalog.Scenario;
  11. using Attribute = Terminal.Gui.Attribute;
  12. namespace UICatalog.Scenarios {
  13. [ScenarioMetadata (Name: "Syntax Highlighting", Description: "Text editor with keyword highlighting")]
  14. [ScenarioCategory ("Controls")]
  15. class SyntaxHighlighting : Scenario {
  16. public override void Setup ()
  17. {
  18. Win.Title = this.GetName ();
  19. Win.Y = 1; // menu
  20. Win.Height = Dim.Fill (1); // status bar
  21. Top.LayoutSubviews ();
  22. var menu = new MenuBar (new MenuBarItem [] {
  23. new MenuBarItem ("_File", new MenuItem [] {
  24. new MenuItem ("_Quit", "", () => Quit()),
  25. })
  26. });
  27. Top.Add (menu);
  28. var textView = new SqlTextView () {
  29. X = 0,
  30. Y = 0,
  31. Width = Dim.Fill (),
  32. Height = Dim.Fill (1),
  33. };
  34. textView.Init();
  35. textView.Text = "SELECT TOP 100 * \nfrom\n MyDb.dbo.Biochemistry;";
  36. Win.Add (textView);
  37. var statusBar = new StatusBar (new StatusItem [] {
  38. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
  39. });
  40. Top.Add (statusBar);
  41. }
  42. private void Quit ()
  43. {
  44. Application.RequestStop ();
  45. }
  46. private class SqlTextView : TextView{
  47. private HashSet<string> keywords = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase);
  48. private Attribute blue;
  49. private Attribute white;
  50. public void Init()
  51. {
  52. keywords.Add("select");
  53. keywords.Add("distinct");
  54. keywords.Add("top");
  55. keywords.Add("from");
  56. blue = Driver.MakeAttribute (Color.Cyan, Color.Black);
  57. white = Driver.MakeAttribute (Color.White, Color.Black);
  58. }
  59. protected override void ColorNormal ()
  60. {
  61. Driver.SetAttribute (white);
  62. }
  63. protected override void ColorNormal (List<System.Rune> line, int idx)
  64. {
  65. if(IsKeyword(line,idx))
  66. {
  67. Driver.SetAttribute (blue);
  68. }
  69. else{
  70. Driver.SetAttribute (white);
  71. }
  72. }
  73. private bool IsKeyword(List<System.Rune> line, int idx)
  74. {
  75. var word = IdxToWord(line,idx);
  76. if(string.IsNullOrWhiteSpace(word)){
  77. return false;
  78. }
  79. return keywords.Contains(word,StringComparer.CurrentCultureIgnoreCase);
  80. }
  81. private string IdxToWord(List<System.Rune> line, int idx)
  82. {
  83. var words = Regex.Split(
  84. new string(line.Select(r=>(char)r).ToArray()),
  85. "\b");
  86. int count = 0;
  87. string current = null;
  88. foreach(var word in words)
  89. {
  90. current = word;
  91. count+= word.Length;
  92. if(count >= idx){
  93. break;
  94. }
  95. }
  96. return current;
  97. }
  98. }
  99. }
  100. }