From 0cafa8f7286e26681746ee934d180d7b1989de82 Mon Sep 17 00:00:00 2001 From: JHubi1 Date: Fri, 29 Aug 2025 10:41:54 +0200 Subject: [PATCH 1/2] Implement autoDetection for language parsing Added autoDetection parameter to HighlightView for language detection. --- flutter_highlight/lib/flutter_highlight.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/flutter_highlight/lib/flutter_highlight.dart b/flutter_highlight/lib/flutter_highlight.dart index 9afbc47..62f9308 100644 --- a/flutter_highlight/lib/flutter_highlight.dart +++ b/flutter_highlight/lib/flutter_highlight.dart @@ -14,6 +14,12 @@ class HighlightView extends StatelessWidget { /// [All available languages](https://github.com/pd4d10/highlight/tree/master/highlight/lib/languages) final String? language; + /// Automatically detect the most likely language if [language] is null. + /// + /// Notice that **this may cause performance issue** because it will try to parse source with + /// all registered languages and use the most relevant one. + final bool? autoDetection; + /// Highlight theme /// /// [All available themes](https://github.com/pd4d10/highlight/blob/master/flutter_highlight/lib/themes) @@ -30,6 +36,7 @@ class HighlightView extends StatelessWidget { HighlightView( String input, { this.language, + this.autoDetection = false, this.theme = const {}, this.padding, this.textStyle, @@ -93,7 +100,7 @@ class HighlightView extends StatelessWidget { child: RichText( text: TextSpan( style: _textStyle, - children: _convert(highlight.parse(source, language: language).nodes!), + children: _convert(highlight.parse(source, language: language, autoDetection: autoDetection).nodes!), ), ), ); From 5179638b4c3a05528e1860f50ea64a95f393f2be Mon Sep 17 00:00:00 2001 From: JHubi1 Date: Fri, 29 Aug 2025 10:45:08 +0200 Subject: [PATCH 2/2] Improve formatting and autoDetection handling --- flutter_highlight/lib/flutter_highlight.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/flutter_highlight/lib/flutter_highlight.dart b/flutter_highlight/lib/flutter_highlight.dart index 62f9308..76598aa 100644 --- a/flutter_highlight/lib/flutter_highlight.dart +++ b/flutter_highlight/lib/flutter_highlight.dart @@ -16,8 +16,8 @@ class HighlightView extends StatelessWidget { /// Automatically detect the most likely language if [language] is null. /// - /// Notice that **this may cause performance issue** because it will try to parse source with - /// all registered languages and use the most relevant one. + /// Notice that **this may cause performance issue** because it will try to + /// parse source with all registered languages and use the most relevant one. final bool? autoDetection; /// Highlight theme @@ -100,7 +100,10 @@ class HighlightView extends StatelessWidget { child: RichText( text: TextSpan( style: _textStyle, - children: _convert(highlight.parse(source, language: language, autoDetection: autoDetection).nodes!), + children: _convert(highlight + .parse(source, + language: language, autoDetection: autoDetection ?? false) + .nodes!), ), ), );