Skip to content

Commit

Permalink
Emit a ModelTokensChangedEvent for all the lines when setting a null …
Browse files Browse the repository at this point in the history
…grammar
  • Loading branch information
danipen committed Jul 31, 2024
1 parent 5f8da79 commit e3830cd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/TextMateSharp.Tests/Model/TMModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,48 @@ public void TMModel_Should_Not_Parse_Setting_A_Null_Grammar()
It.IsAny<ModelTokensChangedEvent>()), 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<IModelTokensChangedListener> changesListenerMock = new Mock<IModelTokensChangedListener>(
MockBehavior.Strict);
changesListenerMock.Setup(
c => c.ModelTokensChanged(It.IsAny<ModelTokensChangedEvent>()));

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<ModelTokensChangedEvent>(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;
Expand Down
6 changes: 6 additions & 0 deletions src/TextMateSharp/Model/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ public Range(int lineNumber)
FromLineNumber = lineNumber;
ToLineNumber = lineNumber;
}

public Range(int fromLineNumber, int toLineNumber)
{
FromLineNumber = fromLineNumber;
ToLineNumber = toLineNumber;
}
}
}
4 changes: 4 additions & 0 deletions src/TextMateSharp/Model/TMModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ public void SetGrammar(IGrammar grammar)
Start();
InvalidateLine(0);
}
else
{
Emit(new ModelTokensChangedEvent(new Range(0, _lines.GetNumberOfLines() - 1), this));
}
}
}

Expand Down

0 comments on commit e3830cd

Please sign in to comment.