From e3830cda1d4ea133c53adc7df3ff4175a9bdd260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Pen=CC=83alba?= Date: Wed, 31 Jul 2024 17:43:13 +0200 Subject: [PATCH] Emit a ModelTokensChangedEvent for all the lines when setting a null grammar --- src/TextMateSharp.Tests/Model/TMModelTests.cs | 42 +++++++++++++++++++ src/TextMateSharp/Model/Range.cs | 6 +++ src/TextMateSharp/Model/TMModel.cs | 4 ++ 3 files changed, 52 insertions(+) diff --git a/src/TextMateSharp.Tests/Model/TMModelTests.cs b/src/TextMateSharp.Tests/Model/TMModelTests.cs index 7b27cf5..e02e1c8 100644 --- a/src/TextMateSharp.Tests/Model/TMModelTests.cs +++ b/src/TextMateSharp.Tests/Model/TMModelTests.cs @@ -66,6 +66,48 @@ public void TMModel_Should_Not_Parse_Setting_A_Null_Grammar() It.IsAny()), Times.Never()); } + [Test] + public void TMModel_Should_Emit_ModelTokensChangedEvent_To_Clean_Highlighted_Lines_When_Setting_A_Null_Grammar_After_Having_Another_Grammar() + { + ModelLinesMock modelLines = new ModelLinesMock(new string[] { "line 1", "line 2", "line 3" }); + + TMModel tmModel = new TMModel(modelLines); + + Mock changesListenerMock = new Mock( + MockBehavior.Strict); + changesListenerMock.Setup( + c => c.ModelTokensChanged(It.IsAny())); + + RegistryOptions options = new RegistryOptions(ThemeName.DarkPlus); + Registry.Registry registry = new Registry.Registry(options); + IGrammar grammar = registry.LoadGrammar("source.cs"); + + tmModel.SetGrammar(grammar); + + tmModel.AddModelTokensChangedListener(changesListenerMock.Object); + tmModel.SetGrammar(null); + + // verify the three lines were invalidated + changesListenerMock.Verify(c => c.ModelTokensChanged( + It.Is(e => IsRangeValid(e, 0, 2))), + Times.Once()); + + } + + static bool IsRangeValid(ModelTokensChangedEvent e, int fromLine, int toLine) + { + if (e.Ranges.Count != 1) + return false; + + if (e.Ranges[0].FromLineNumber != fromLine) + return false; + + if (e.Ranges[0].ToLineNumber != toLine) + return false; + + return true; + } + class ModelLinesMock : AbstractLineList { string[] _lines; diff --git a/src/TextMateSharp/Model/Range.cs b/src/TextMateSharp/Model/Range.cs index b5aa8f1..2db8ed3 100644 --- a/src/TextMateSharp/Model/Range.cs +++ b/src/TextMateSharp/Model/Range.cs @@ -10,5 +10,11 @@ public Range(int lineNumber) FromLineNumber = lineNumber; ToLineNumber = lineNumber; } + + public Range(int fromLineNumber, int toLineNumber) + { + FromLineNumber = fromLineNumber; + ToLineNumber = toLineNumber; + } } } \ No newline at end of file diff --git a/src/TextMateSharp/Model/TMModel.cs b/src/TextMateSharp/Model/TMModel.cs index 10eaabd..896c8fb 100644 --- a/src/TextMateSharp/Model/TMModel.cs +++ b/src/TextMateSharp/Model/TMModel.cs @@ -291,6 +291,10 @@ public void SetGrammar(IGrammar grammar) Start(); InvalidateLine(0); } + else + { + Emit(new ModelTokensChangedEvent(new Range(0, _lines.GetNumberOfLines() - 1), this)); + } } }