/// <summary> /// Attempts to load a syntax highlighting guide for the grammar. /// </summary> /// <param name="grammar">The grammar to use.</param> /// <returns>A new <see cref="Tuple{ISyntaxHighlightingGuide, SyntaxHighlightingGuideReference}"/> or <see langword="null"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="grammar"/> is <see langword="null"/>.</exception> /// <exception cref="T:System.IO.FileLoadException">A file that was found could not be loaded.</exception> /// <exception cref="T:System.IO.FileNotFoundException">The assembly path is an empty string ("") or does not exist.</exception> /// <exception cref="T:System.BadImageFormatException">The assembly path is not a valid assembly.</exception> public static Tuple <SyntaxHighlightingGuideReference, ISyntaxHighlightingGuide> LoadSyntaxHighlightingGuide([NotNull] this GrammarReference grammar) { if (grammar == null) { throw new ArgumentNullException(nameof(grammar)); } var scanner = new Scanner(); var loader = new Loader(); // First try loading a guide from the target assembly's directory var pathRoot = Path.GetDirectoryName(grammar.AssemblyPath); Tuple <SyntaxHighlightingGuideReference, ISyntaxHighlightingGuide> guideResult = null; if (Directory.Exists(pathRoot)) { guideResult = SyntaxHighlighting.LoadSyntaxGuideFromPath(grammar, scanner, loader, pathRoot); if (guideResult != null) { return(guideResult); } } // Now try loading a guide from the Grun.Net Guides folder pathRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); // ReSharper disable once AssignNullToNotNullAttribute pathRoot = Path.Combine(pathRoot, "Guides"); if (Directory.Exists(pathRoot)) { guideResult = SyntaxHighlighting.LoadSyntaxGuideFromPath(grammar, scanner, loader, pathRoot); } return(guideResult); }
async Task <HighlightedLine> ISyntaxHighlighting.GetHighlightedLineAsync(IDocumentLine line, CancellationToken cancellationToken) { if (line == null) { throw new ArgumentNullException(nameof(line)); } if (!DefaultSourceEditorOptions.Instance.EnableSemanticHighlighting) { return(await syntaxMode.GetHighlightedLineAsync(line, cancellationToken)); } var syntaxLine = await syntaxMode.GetHighlightedLineAsync(line, cancellationToken).ConfigureAwait(false); if (syntaxLine.Segments.Count == 0) { return(syntaxLine); } lock (lineSegments) { var segments = new List <ColoredSegment> (syntaxLine.Segments); int endOffset = segments [segments.Count - 1].EndOffset; try { var tree = lineSegments.FirstOrDefault(t => t.Item1 == line); int lineOffset = line.Offset; if (tree == null) { tree = Tuple.Create(line, new HighlightingSegmentTree()); tree.Item2.InstallListener(editor.Document); foreach (var seg2 in semanticHighlighting.GetColoredSegments(new TextSegment(lineOffset, line.Length))) { tree.Item2.AddStyle(seg2, seg2.ColorStyleKey); } while (lineSegments.Count > MaximumCachedLineSegments) { var removed = lineSegments.Dequeue(); try { removed.Item2.RemoveListener(); } catch (Exception) { } } lineSegments.Enqueue(tree); } foreach (var treeseg in tree.Item2.GetSegmentsOverlapping(line)) { var inLineStartOffset = Math.Max(0, treeseg.Offset - lineOffset); var inLineEndOffset = Math.Min(line.Length, treeseg.EndOffset - lineOffset); if (inLineEndOffset <= inLineStartOffset) { continue; } var semanticSegment = new ColoredSegment(inLineStartOffset, inLineEndOffset - inLineStartOffset, syntaxLine.Segments [0].ScopeStack.Push(treeseg.Style)); SyntaxHighlighting.ReplaceSegment(segments, semanticSegment); } } catch (Exception e) { LoggingService.LogError("Error in semantic highlighting: " + e); return(syntaxLine); } return(new HighlightedLine(line, segments)); } }
static void Main(string[] args) { SyntaxHighlighting.EnableVirtualTerminalProcessing(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); var j = new Json(File.ReadAllText("in.json")); //File.WriteAllText(@"test.html", SyntaxHighlighting.ToHtml(j)); //File.WriteAllText(@"test.json", j.ToFormatString(), Encoding.UTF8); //foreach (Json json in j["response"]["items"].Value as JsonObjectArray) //{ // Console.WriteLine("\r\n" + json["conversation_message_id"].Value + ((int)json["from_id"].Value == 272611387 ? " Egor" : " Vlas")); // Console.WriteLine(SyntaxHighlighting.ToAnsiWithEscapeSequences(json["text"].Value)); // if (json.IndexByKey("fwd_messages") != -1) // foreach (Json jsonfwd in json["fwd_messages"].Value as JsonObjectArray) // Console.WriteLine("\t" + SyntaxHighlighting.ToAnsiWithEscapeSequences(jsonfwd["text"].Value)); //} var f = File.OpenWrite("testst.txt"); for (var i = 0; i < 200000; i++) { var bytes = Encoding.UTF8.GetBytes($"{i}: &#{i};\r\n"); f.Write(bytes, 0, bytes.Length); } f.Close(); //Console.WriteLine(SyntaxHighlighting.ToAnsiWithEscapeSequences(j)); stopWatch.Stop(); Console.WriteLine($"{stopWatch.ElapsedMilliseconds} ms"); //Thread.Sleep(0); //Process.Start("cmd", "/c start test.html"); //Process.Start("cmd", "/c start test.rtf"); //Console.WriteLine(@"\u0072\u0079\u0020y\n performa\\nce TEST".UnescapeString().EscapeString().ToUnicodeString()); //Stopwatch stopWatch = new Stopwatch(); //stopWatch.Start(); //var j = new JsonObjectArray(File.ReadAllText(@"detectable.json")); //var s = Json.FromStructure(test, true); //stopWatch.Stop(); //Console.WriteLine($"{stopWatch.ElapsedMilliseconds} ms"); //File.WriteAllText("test.json", s.ToFormatString()); //stopWatch.Start(); //var file = new Json(File.ReadAllText("test.json")); //var testout = Json.ToStructure<Test>(new Json(file)); //stopWatch.Stop(); //Console.WriteLine($"{stopWatch.ElapsedMilliseconds} ms"); //var ss = Json.FromStructure(testout, true); //Console.WriteLine(ss.ToFormatString() + "\r\n\r\n"); //Main2(args); Console.ReadLine(); }
public void TestMethod1() { IHighlightingDefinition SyntaxHighlighting; FountainGame fountainGame = new FountainGame(GetText("ScreenPlay1.fountain")); using (XmlReader reader = XmlReader.Create("Fountain-Mode.xshd")) { SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance); } foreach (SceneHeading sceneHeading in fountainGame.Blocks.SceneHeadings) { Regex regex = SyntaxHighlighting.GetNamedRuleSet("SceneHeadings").Rules[0].Regex; string text = sceneHeading.Spans.Literals[0].Text; Assert.IsTrue(regex.IsMatch(text), regex.ToString() + "\r\n" + text); } foreach (Character character in fountainGame.Blocks.Characters) { Regex regex = SyntaxHighlighting.GetNamedRuleSet("").Spans[0].StartExpression; string text = character.Spans.Literals[0].Text.Trim(); Assert.IsTrue(regex.IsMatch(text), regex.ToString() + "\r\n" + text); } foreach (Boneyard boneyard in fountainGame.Blocks.Boneyards) { Regex regex = SyntaxHighlighting.GetNamedRuleSet("Boneyard").Rules[0].Regex; string text = boneyard.Text; Assert.IsTrue(regex.IsMatch(text), regex.ToString() + "\r\n" + text); } foreach (Parenthetical parenthetical in fountainGame.Blocks.Parentheticals) { Regex regex = SyntaxHighlighting.GetNamedRuleSet("Parenthetical").Rules[0].Regex; string text = fountainGame.GetText(parenthetical.Range).Trim(); Assert.IsTrue(regex.IsMatch(text), regex.ToString() + "\r\n" + text); } }
async Task <HighlightedLine> ISyntaxHighlighting.GetHighlightedLineAsync(IDocumentLine line, CancellationToken cancellationToken) { if (line == null) { throw new ArgumentNullException(nameof(line)); } if (!DefaultSourceEditorOptions.Instance.EnableSemanticHighlighting) { return(await syntaxMode.GetHighlightedLineAsync(line, cancellationToken)); } var syntaxLine = await syntaxMode.GetHighlightedLineAsync(line, cancellationToken).ConfigureAwait(false); if (syntaxLine.Segments.Count == 0) { return(syntaxLine); } var segments = new List <ColoredSegment> (syntaxLine.Segments); int endOffset = segments [segments.Count - 1].EndOffset; try { // This code should not have any lambda capture linq, as it is a hot loop. int lineOffset = line.Offset; foreach (var treeseg in semanticHighlighting.GetColoredSegments(new TextSegment(lineOffset, line.Length))) { var inLineStartOffset = Math.Max(0, treeseg.Offset - lineOffset); var inLineEndOffset = Math.Min(line.Length, treeseg.EndOffset - lineOffset); if (inLineEndOffset <= inLineStartOffset) { continue; } var semanticSegment = new ColoredSegment(inLineStartOffset, inLineEndOffset - inLineStartOffset, treeseg.ScopeStack); SyntaxHighlighting.ReplaceSegment(segments, semanticSegment); } } catch (Exception e) { LoggingService.LogError("Error in semantic highlighting: ", e); return(syntaxLine); } return(new HighlightedLine(line, segments)); }