Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line by line parsing (simple change) #30

Open
icedman opened this issue Mar 4, 2022 · 4 comments
Open

line by line parsing (simple change) #30

icedman opened this issue Mar 4, 2022 · 4 comments

Comments

@icedman
Copy link

icedman commented Mar 4, 2022

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
Copy link
Author

icedman commented Mar 7, 2022

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 icedman closed this as completed Mar 7, 2022
@icedman icedman reopened this Mar 7, 2022
@icedman
Copy link
Author

icedman commented Mar 7, 2022

I made a PR.

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

@Dev-Owl
Copy link

Dev-Owl commented Apr 5, 2022

@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.

@icedman
Copy link
Author

icedman commented Apr 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants