highlight.dart icon indicating copy to clipboard operation
highlight.dart copied to clipboard

line by line parsing (simple change)

Open icedman opened this issue 3 years ago • 4 comments

Please add line by line parsing ... Below is currently my workaround (I saved the latest continuation and pass it back on the next line parse). The block comments work correctly. But I'm not sure if this is the right way to do this.

index fdab3dc..6cf7c6e 100644
--- a/highlight/lib/src/highlight.dart
+++ b/highlight/lib/src/highlight.dart
@@ -251,7 +251,9 @@ class Highlight {
   /// [autoDetect]: The default value is `false`. Pass `true` to enable language auto detection.
   /// Notice that **this may cause performance issue** because it will try to parse source with
   /// all registered languages and use the most relevant one.
-  Result parse(String source, {String? language, bool autoDetection = false}) {
+  
+  Mode? lastContinuation;
+  Result parse(String source, {String? language, bool autoDetection = false, Mode? continuation}) {
     if (language == null) {
       if (autoDetection) {
         return _parseAuto(source);
@@ -259,7 +261,7 @@ class Highlight {
         throw ArgumentError.notNull('language');
       }
     }
-    return _parse(source, language: language);
+    return _parse(source, language: language, continuation: continuation);
   }
 
   Result _parse(
@@ -369,6 +371,7 @@ class Highlight {
       if (explicit) {
         continuations[top!.subLanguage!.first] = result.top;
       }
+
       return _buildSpan(result.language, result.nodes, noPrefix: true);
     }
 
@@ -378,6 +381,7 @@ class Highlight {
               ? _processSubLanguage()!
               : _processKeywords(),
           currentChildren);
+      lastContinuation = top;
       mode_buffer = '';
     }
 
@@ -404,6 +408,7 @@ class Highlight {
           }
         }
         _startNewMode(new_mode);
+
         return new_mode.returnBegin == true ? 0 : lexeme.length;
       }
 

icedman avatar Mar 04 '22 03:03 icedman

I realized result.top is what I was looking for and that's already existing. We just need this change:

-  Result parse(String source, {String? language, bool autoDetection = false}) {
+  Result parse(String source, {String? language, bool autoDetection = false, Mode? continuation}) {

icedman avatar Mar 07 '22 00:03 icedman

I made a PR.

I think the change is simple enough and it works for my project (https://github.com/icedman/flutter_editor)

icedman avatar Mar 07 '22 00:03 icedman

@icedman Would be interested how you made the line by line syntax highlighting working, currently trying that as well. My goal is to have syntax highlighting even with wrong syntax in between and highlight the part that is wrong on my own.

For me, it's not clear what I need to set in the Mode parameter to do that. Any advice would be appreciated.

Dev-Owl avatar Apr 05 '22 20:04 Dev-Owl

See my PR request or my project at https://github.com/icedman/flutter_editor

If you read the code for this plugin.. A document is actually highlighted line-by-line. Mode .. is the last parser state. You need to keep track of the mode of the previous line to continue the parsing.

icedman avatar Apr 14 '22 06:04 icedman