re-editor icon indicating copy to clipboard operation
re-editor copied to clipboard

would like to show syntax errors inline with the text

Open csells opened this issue 1 year ago • 5 comments

When there are syntax errors, I'd like to be able to show the red squiggles and provide a tooltip over the code with the error message.

This editor is so close to being useful for read code; this feature puts it over the edge!

csells avatar Dec 04 '24 23:12 csells

The CodeLineEditingController has a spanBuilder parameter that allow you to do just that.

You just have to build your syntax checker, and using the span builder you can add the red squiggles.

https://pub.dev/documentation/re_editor/latest/re_editor/CodeLineSpanBuilder.html

hommes-doutant avatar Mar 19 '25 13:03 hommes-doutant

I tried to do it this way, but its not showing the TextSpan inside the code editor:

Image

LUK3D avatar Mar 19 '25 19:03 LUK3D

It's because it is not used in that way. Here's a small guide :

  1. You set your function as the parameter of the CodeLineEditingController when creating it. And it runs it everytime something changes.
final controller = CodeLineEditingController(
        codeLines: isEmpty ? 
            CodeLines.fromText('') : 
            CodeLines.fromText(content),
        spanBuilder: _buildSpan, // Add span builder here
      );
  1. You create your buildspan function with the same signature as the doc and return a textspan
TextSpan _buildSpan({
    required CodeLine codeLine,
    required BuildContext context,
    required int index,
    required TextStyle style,
    required TextSpan textSpan,
  }) {
    final spans = <TextSpan>[];
        spans.add(TextSpan(
          text: text[highlightIndex],
          style: spanStyle.copyWith(
            backgroundColor: Colors.yellow.withOpacity(0.3),
            fontWeight: FontWeight.bold,
          ),
        ));
    return TextSpan(children: spans.isNotEmpty ? spans : [textSpan], style: style);
  }

P.S. I haven't tested it for syntax error, I've stripped a function from my code since there was a lot of other functions involved. But it should highlight every line in yellow, while keeping syntax highlighting intact.

If you want to implement the method is to check the position of the characters you want to modify, iterate over the whole text to find them, split their current textspan, change the style, and put everything back together.

hommes-doutant avatar Mar 19 '25 21:03 hommes-doutant

Do you have an example with a syntax checker using LSP?

csells avatar Mar 23 '25 01:03 csells

Do you have an example with a syntax checker using LSP?

Sorry, there is no need for code syntax analysis in my project. I think you can launch a LSP server, input the code, and build a custom TextSpan after getting the result, ref to https://github.com/reqable/re-editor/issues/56#issuecomment-2738188595

MegatronKing avatar Mar 23 '25 02:03 MegatronKing