|
@@ -4,10 +4,11 @@ using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
using System.Threading.Tasks;
|
|
|
using Terminal.Gui;
|
|
|
using static UICatalog.Scenario;
|
|
|
-
|
|
|
+using Attribute = Terminal.Gui.Attribute;
|
|
|
|
|
|
namespace UICatalog.Scenarios {
|
|
|
[ScenarioMetadata (Name: "Syntax Highlighting", Description: "Text editor with keyword highlighting")]
|
|
@@ -35,7 +36,10 @@ namespace UICatalog.Scenarios {
|
|
|
Height = Dim.Fill (1),
|
|
|
};
|
|
|
|
|
|
+ textView.Init();
|
|
|
|
|
|
+ textView.Text = "SELECT TOP 100 * \nfrom\n MyDb.dbo.Biochemistry;";
|
|
|
+
|
|
|
Win.Add (textView);
|
|
|
|
|
|
var statusBar = new StatusBar (new StatusItem [] {
|
|
@@ -55,11 +59,69 @@ namespace UICatalog.Scenarios {
|
|
|
|
|
|
private class SqlTextView : TextView{
|
|
|
|
|
|
+ private HashSet<string> keywords = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase);
|
|
|
+ private Attribute blue;
|
|
|
+ private Attribute white;
|
|
|
+
|
|
|
+
|
|
|
+ public void Init()
|
|
|
+ {
|
|
|
+ keywords.Add("select");
|
|
|
+ keywords.Add("distinct");
|
|
|
+ keywords.Add("top");
|
|
|
+ keywords.Add("from");
|
|
|
+
|
|
|
+ blue = Driver.MakeAttribute (Color.Cyan, Color.Black);
|
|
|
+ white = Driver.MakeAttribute (Color.White, Color.Black);
|
|
|
+ }
|
|
|
|
|
|
+ protected override void ColorNormal ()
|
|
|
+ {
|
|
|
+ Driver.SetAttribute (white);
|
|
|
+ }
|
|
|
|
|
|
protected override void ColorNormal (List<System.Rune> line, int idx)
|
|
|
{
|
|
|
- Driver.SetAttribute (Driver.MakeAttribute (Color.Green, Color.Black));
|
|
|
+ if(IsKeyword(line,idx))
|
|
|
+ {
|
|
|
+ Driver.SetAttribute (blue);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ Driver.SetAttribute (white);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool IsKeyword(List<System.Rune> line, int idx)
|
|
|
+ {
|
|
|
+ var word = IdxToWord(line,idx);
|
|
|
+
|
|
|
+ if(string.IsNullOrWhiteSpace(word)){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return keywords.Contains(word,StringComparer.CurrentCultureIgnoreCase);
|
|
|
+ }
|
|
|
+
|
|
|
+ private string IdxToWord(List<System.Rune> line, int idx)
|
|
|
+ {
|
|
|
+ var words = Regex.Split(
|
|
|
+ new string(line.Select(r=>(char)r).ToArray()),
|
|
|
+ "\b");
|
|
|
+
|
|
|
+
|
|
|
+ int count = 0;
|
|
|
+ string current = null;
|
|
|
+
|
|
|
+ foreach(var word in words)
|
|
|
+ {
|
|
|
+ current = word;
|
|
|
+ count+= word.Length;
|
|
|
+ if(count >= idx){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return current;
|
|
|
}
|
|
|
}
|
|
|
}
|