PavelTorgashov / FastColoredTextBox

Fast Colored TextBox for Syntax Highlighting. The text editor component for .NET.
Other
1.21k stars 463 forks source link

Refactor SyntaxHighlighter class into one class for each language #256

Open tvidvei opened 1 year ago

tvidvei commented 1 year ago

Hi

Thanks a lot for an excellent component! I have started to use it in one of my projects, and it works great! But I still have one wish - and proposal:

I want to implement syntax highlighting for additional languages, or modify the highlighting for some of the existing languages. The solution offered, as I understand it, is one of the following - provided that we will not modify FastColoredTextBox itself: a) Use Language.Custom with a description file specifying the syntax highlighting b) Write an event handler for the TextChanged event in FTBC with the new highlighting functionality. (See SimplestSyntaxHighlightingSample in Tester project.)

I think, however, that we could get more flexibility and a cleaner solution if we instead could define replacements for the built-in SyntaxHighlighter class, which contains all the syntax highlighting for all the buildt-in languages + the support for custom highlighting by means of a description file. We can achieve this while preserving all the present functionality in FastColoredTextBox (ftbc) by doing a refactoring on the SyntaxHighlighter class along these lines:

  1. Define an interface ISyntaxHighlighter which defines the functionality (methods an properties) to be delivered to the ftbc by a syntax highlighter implementation
  2. Define a base class SyntaxHighlighterBase : ISyntaxHighlighter which should contain the stuff common to all languages implemented in todays SyntaxHighlighter.
  3. Implement a subclass of SyntaxHighlighterBase for each language
  4. Define a SyntaxHighlighterAttribute with at least a Name property to give names (typically language names) to the ISyntaxHighlighter classes. (The Name attribute replaces todays enum Language in some respects, but has the advantage that new names can be added without needing to do modifications to ftbc.)
  5. Modify the functionality of the Language property in ftbc so that an instance of the right class of Syntaxhighlighters is chosen. It should be able to look for SyntaxHighlighters both in the application using the ftbc, an external assembly as specified, and of course among the built-in highlighters in FastColoredTextBox.

I have already implemented this functionality in my local ftbc repo, since I needed it in my project. But I wanted to present the idea and listen to the reactions before I make a pull request out of it.

Regards, Tor Vidvei