public static SyntaxMode Read(XmlReader reader) { SyntaxMode result = new SyntaxMode(); List <Match> matches = new List <Match> (); List <Span> spanList = new List <Span> (); List <Marker> prevMarkerList = new List <Marker> (); XmlReadHelper.ReadList(reader, Node, delegate() { switch (reader.LocalName) { case Node: string extends = reader.GetAttribute("extends"); if (!String.IsNullOrEmpty(extends)) { result = (SyntaxMode)SyntaxModeService.GetSyntaxMode(extends).MemberwiseClone(); } result.Name = reader.GetAttribute("name"); result.MimeType = reader.GetAttribute(MimeTypesAttribute); if (!String.IsNullOrEmpty(reader.GetAttribute("ignorecase"))) { result.IgnoreCase = Boolean.Parse(reader.GetAttribute("ignorecase")); } return(true); case Rule.Node: result.rules.Add(Rule.Read(result, reader, result.IgnoreCase)); return(true); } return(result.ReadNode(reader, matches, spanList, prevMarkerList)); }); result.spans = spanList.ToArray(); result.prevMarker = prevMarkerList.ToArray(); result.matches = matches.ToArray(); return(result); }
public void UpdateDocumentHighlighting() { if (doc == null || doc.SuppressHighlightUpdate) { return; } SyntaxModeService.StartUpdate(doc, this, 0, doc.TextLength); }
void HandleTextReplaced(object sender, DocumentChangeEventArgs e) { if (doc == null || doc.SuppressHighlightUpdate || doc.CurrentAtomicUndoOperationType == OperationType.Format) { return; } SyntaxModeService.StartUpdate(doc, this, e.Offset, e.Offset + e.InsertionLength); }
void HandleTextSet(object sender, EventArgs e) { if (doc == null || doc.SuppressHighlightUpdate) { return; } SyntaxModeService.StartUpdate(doc, this, 0, doc.TextLength); }
static SyntaxModeService() { StartUpdateThread(); LoadStylesAndModes(typeof(SyntaxModeService).Assembly); SyntaxModeService.AddSemanticRule("text/x-csharp", "Comment", new HighlightUrlSemanticRule("Comment(Line)")); SyntaxModeService.AddSemanticRule("text/x-csharp", "XmlDocumentation", new HighlightUrlSemanticRule("Comment(Doc)")); SyntaxModeService.AddSemanticRule("text/x-csharp", "String", new HighlightUrlSemanticRule("String")); InstallSyntaxMode("text/x-jay", new SyntaxModeProvider(doc => new JaySyntaxMode(doc))); }
static SyntaxModeService() { StartUpdateThread(); LoadStylesAndModes(typeof(SyntaxModeService).Assembly); SyntaxModeService.GetSyntaxMode("text/x-csharp").AddSemanticRule("Comment", new HighlightUrlSemanticRule("comment")); SyntaxModeService.GetSyntaxMode("text/x-csharp").AddSemanticRule("XmlDocumentation", new HighlightUrlSemanticRule("comment")); SyntaxModeService.GetSyntaxMode("text/x-csharp").AddSemanticRule("String", new HighlightUrlSemanticRule("string")); InstallSyntaxMode("text/x-jay", new JaySyntaxMode()); }
public virtual Chunk GetChunks(int offset, int length) { SyntaxModeService.ScanSpans(doc, mode, spanParser.CurRule, spanParser.SpanStack, line.Offset, offset); length = System.Math.Min(doc.Length - offset, length); str = length > 0 ? doc.GetTextAt(offset, length) : null; curChunk = new Chunk(offset, 0, GetSpanStyle()); spanParser.ParseSpans(offset, length); curChunk.Length = offset + length - curChunk.Offset; if (curChunk.Length > 0) { curChunk.Style = GetStyle(curChunk) ?? GetSpanStyle(); AddRealChunk(curChunk); } return(startChunk); }
public override Rule GetRule(TextDocument document, string name) { if (name == null || name == "<root>") { return(this); } if (name.StartsWith("mode:", StringComparison.Ordinal)) { return(SyntaxModeService.GetSyntaxMode(document, name.Substring("mode:".Length))); } foreach (Rule rule in rules) { if (rule.Name == name) { return(rule); } } return(this); }
public virtual Chunk GetChunks(int offset, int length) { if (lineOffset < offset) { SyntaxModeService.ScanSpans(doc, mode, spanParser.CurRule, spanParser.SpanStack, lineOffset, offset); } length = System.Math.Min(doc.TextLength - offset, length); var minOffset = curChunk == null ? 0 : curChunk.Offset + curChunk.Length; curChunk = new Chunk(offset, 0, GetSpanStyle()); spanParser.ParseSpans(offset, length); curChunk.SpanStack = spanParser.SpanStack; curChunk.Length = offset + length - curChunk.Offset; if (curChunk.Length > 0 && curChunk.Offset >= minOffset) { curChunk.Style = GetStyle(curChunk) ?? GetSpanStyle(); AddRealChunk(curChunk); } return(startChunk); }
public override Rule GetRule(string name) { if (name == null || name == "<root>") { return(this); } if (name.StartsWith("mode:")) { return(SyntaxModeService.GetSyntaxMode(name.Substring("mode:".Length))); } foreach (Rule rule in rules) { if (rule.Name == name) { return(rule); } } return(this); }
public static ColorScheme LoadFrom(Stream stream) { var result = new ColorScheme(); var reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(stream, new System.Xml.XmlDictionaryReaderQuotas()); var root = XElement.Load(reader); // The fields we'd like to extract result.Name = root.XPathSelectElement("name").Value; if (result.Name != "Default") { result.CopyValues(SyntaxModeService.DefaultColorStyle); } var version = Version.Parse(root.XPathSelectElement("version").Value); if (version.Major != 1) { return(null); } var el = root.XPathSelectElement("description"); if (el != null) { result.Description = el.Value; } el = root.XPathSelectElement("originator"); if (el != null) { result.Originator = el.Value; } el = root.XPathSelectElement("baseScheme"); if (el != null) { result.BaseScheme = el.Value; } if (result.BaseScheme != null) { var baseScheme = SyntaxModeService.GetColorStyle(result.BaseScheme); if (baseScheme != null) { result.CopyValues(baseScheme); } } var palette = new Dictionary <string, Cairo.Color> (); foreach (var color in root.XPathSelectElements("palette/*")) { var name = color.XPathSelectElement("name").Value; if (palette.ContainsKey(name)) { throw new InvalidDataException("Duplicate palette color definition for: " + name); } palette.Add( name, ParseColor(color.XPathSelectElement("value").Value) ); } foreach (var colorElement in root.XPathSelectElements("//colors/*")) { var color = AmbientColor.Create(colorElement, palette); PropertyDecsription info; if (!ambientColors.TryGetValue(color.Name, out info)) { Console.WriteLine("Ambient color:" + color.Name + " not found."); continue; } info.Info.SetValue(result, color, null); } foreach (var textColorElement in root.XPathSelectElements("//text/*")) { var color = ChunkStyle.Create(textColorElement, palette); PropertyDecsription info; if (!textColors.TryGetValue(color.Name, out info)) { Console.WriteLine("Text color:" + color.Name + " not found."); continue; } info.Info.SetValue(result, color, null); } // Check scheme bool valid = true; foreach (var color in textColors.Values) { if (color.Info.GetValue(result, null) == null) { Console.WriteLine(color.Attribute.Name + " == null"); valid = false; } } foreach (var color in ambientColors.Values) { if (color.Info.GetValue(result, null) == null) { Console.WriteLine(color.Attribute.Name + " == null"); valid = false; } } if (!valid) { throw new InvalidDataException("Scheme " + result.Name + " is not valid."); } return(result); }
public static ColorScheme Import(string fileName, Stream stream) { var result = new ColorScheme(); result.Name = Path.GetFileNameWithoutExtension(fileName); result.Description = "Imported color scheme"; result.Originator = "Imported from " + fileName; var colors = new Dictionary <string, VSSettingColor> (); using (var reader = XmlReader.Create(stream)) { while (reader.Read()) { if (reader.LocalName == "Item") { var color = VSSettingColor.Create(reader); if (colors.ContainsKey(color.Name)) { Console.WriteLine("Warning: {0} is defined twice in vssettings.", color.Name); continue; } colors[color.Name] = color; } } } HashSet <string> importedAmbientColors = new HashSet <string> (); // convert ambient colors foreach (var ambient in ambientColors.Values) { if (!string.IsNullOrEmpty(ambient.Attribute.VSSetting)) { var import = AmbientColor.Import(colors, ambient.Attribute.VSSetting); if (import != null) { importedAmbientColors.Add(import.Name); ambient.Info.SetValue(result, import, null); continue; } } } // convert text colors foreach (var vsc in colors.Values) { bool found = false; foreach (var color in textColors) { if (color.Value.Attribute.VSSetting == null) { continue; } var split = color.Value.Attribute.VSSetting.Split('?'); foreach (var s in split) { if (s == vsc.Name) { /* if (vsc.Foreground == "0x02000000" && vsc.Background == "0x02000000") { * color.Value.Info.SetValue (result, result.PlainText, null); * found = true; * continue; * }*/ var textColor = ChunkStyle.Import(color.Value.Attribute.Name, vsc); if (textColor != null) { color.Value.Info.SetValue(result, textColor, null); found = true; } } } } if (!found && !importedAmbientColors.Contains(vsc.Name)) { Console.WriteLine(vsc.Name + " not imported!"); } } result.IndentationGuide = new AmbientColor(); result.IndentationGuide.Colors.Add(Tuple.Create("color", AlphaBlend(result.PlainText.Foreground, result.PlainText.Background, 0.3))); result.TooltipText = result.PlainText.Clone(); var h = (HslColor)result.TooltipText.Background; h.L += 0.01; result.TooltipText.Background = h; result.TooltipPagerTop = new AmbientColor(); result.TooltipPagerTop.Colors.Add(Tuple.Create("color", result.TooltipText.Background)); result.TooltipPagerBottom = new AmbientColor(); result.TooltipPagerBottom.Colors.Add(Tuple.Create("color", result.TooltipText.Background)); result.TooltipPagerTriangle = new AmbientColor(); result.TooltipPagerTriangle.Colors.Add(Tuple.Create("color", AlphaBlend(result.PlainText.Foreground, result.PlainText.Background, 0.8))); result.TooltipBorder = new AmbientColor(); result.TooltipBorder.Colors.Add(Tuple.Create("color", AlphaBlend(result.PlainText.Foreground, result.PlainText.Background, 0.5))); var defaultStyle = SyntaxModeService.GetColorStyle(HslColor.Brightness(result.PlainText.Background) < 0.5 ? "Monokai" : "Default"); foreach (var color in textColors.Values) { if (color.Info.GetValue(result, null) == null) { color.Info.SetValue(result, color.Info.GetValue(defaultStyle, null), null); } } foreach (var color in ambientColors.Values) { if (color.Info.GetValue(result, null) == null) { color.Info.SetValue(result, color.Info.GetValue(defaultStyle, null), null); } } if (result.PlainText.TransparentForeground) { result.PlainText.Foreground = new Cairo.Color(0, 0, 0); } return(result); }
public void Save(string fileName) { using (var writer = new StreamWriter(fileName)) { writer.WriteLine("{"); writer.WriteLine("\t\"name\":\"{0}\",", Name); writer.WriteLine("\t\"version\":\"1.0\","); if (!string.IsNullOrEmpty(Description)) { writer.WriteLine("\t\"description\":\"{0}\",", Description); } if (!string.IsNullOrEmpty(Originator)) { writer.WriteLine("\t\"originator\":\"{0}\",", Originator); } if (!string.IsNullOrEmpty(BaseScheme)) { writer.WriteLine("\t\"baseScheme\":\"{0}\",", BaseScheme); } var baseStyle = SyntaxModeService.GetColorStyle(BaseScheme ?? "Default"); writer.WriteLine("\t\"colors\":["); bool first = true; foreach (var ambient in ambientColors) { var thisValue = ambient.Value.Info.GetValue(this, null) as AmbientColor; if (thisValue == null) { continue; } var baseValue = ambient.Value.Info.GetValue(baseStyle, null) as AmbientColor; if (thisValue.Equals(baseValue)) { continue; } var colorString = new StringBuilder(); foreach (var color in thisValue.Colors) { if (colorString.Length > 0) { colorString.Append(", "); } colorString.Append(string.Format("\"{0}\":\"{1}\"", color.Item1, ColorToMarkup(color.Item2))); } if (colorString.Length == 0) { Console.WriteLine("Invalid ambient color :" + thisValue); continue; } if (!first) { writer.WriteLine(","); } else { first = false; } writer.Write("\t\t{"); writer.Write("\"name\": \"{0}\", {1}", ambient.Value.Attribute.Name, colorString); writer.Write(" }"); } writer.WriteLine("\t],"); first = true; writer.WriteLine("\t\"text\":["); foreach (var textColor in textColors) { var thisValue = textColor.Value.Info.GetValue(this, null) as ChunkStyle; if (thisValue == null) { continue; } var baseValue = textColor.Value.Info.GetValue(baseStyle, null) as ChunkStyle; if (thisValue.Equals(baseValue)) { continue; } var colorString = new StringBuilder(); if (!thisValue.TransparentForeground) { colorString.Append(string.Format("\"fore\":\"{0}\"", ColorToMarkup(thisValue.Foreground))); } if (!thisValue.TransparentBackground) { if (colorString.Length > 0) { colorString.Append(", "); } colorString.Append(string.Format("\"back\":\"{0}\"", ColorToMarkup(thisValue.Background))); } if (thisValue.FontWeight != FontWeight.Normal) { if (colorString.Length > 0) { colorString.Append(", "); } colorString.Append(string.Format("\"weight\":\"{0}\"", thisValue.FontWeight)); } if (thisValue.FontStyle != FontStyle.Normal) { if (colorString.Length > 0) { colorString.Append(", "); } colorString.Append(string.Format("\"style\":\"{0}\"", thisValue.FontStyle)); } if (!first) { writer.WriteLine(","); } else { first = false; } writer.Write("\t\t{"); if (colorString.Length == 0) { writer.Write("\"name\": \"{0}\"", textColor.Value.Attribute.Name); } else { writer.Write("\"name\": \"{0}\", {1}", textColor.Value.Attribute.Name, colorString); } writer.Write(" }"); } writer.WriteLine(); writer.WriteLine("\t]"); writer.WriteLine("}"); } }