Posted 8 years ago by Dominik K
Version: 12.1.0304
Avatar

Hello,

I let the code within the Syntax Editor code go through the .NET compiler and get the necessary Error Message, Line and Column.

Now I want to underline the right part of the code and write the error message in a tooltip which pops up when I hover over the marked text.

How is this possible?

 

I tried it this way:

public bool CompileFormula()
{
  foreach (CompileError error in compileErrors)
  {
    string msg = error.Message;
    int line = error.Line;
    int col = error.Column;

    int startIndex = syntaxEditor.SelectedView.Selection.StartOffset = line + col + 1;
    int endIndex = startIndex + syntaxEditor.Text.Skip(startIndex).TakeWhile(c => !char.isWhiteSpace(c)).Count + 1;
    TextRange errorRange = new TextRange(startIndex, endIndex);

    this.AddSpanIndicator(SpanIndicatorLayer.SyntaxErrorKey, SpanIndicatorLayer.SyntaxErrorDisplayPriority, new WaveLineSpanIndicator(Color.Red), errorRange);
  }
}


    private void AddSpanIndicatorSimple(string layerKey, int layerDisplayPriority, SpanIndicator indicator, TextRange errorRange)
    {
      if (errorRange.Length != 0)
      {
        SpanIndicatorLayer layer = new SpanIndicatorLayer(layerKey, layerDisplayPriority);

        if (layer != null)
        {
          synEditor.Document.SpanIndicatorLayers.Add(layer);
        }

        if (!layer.OverlapsWith(errorRange) && layer.Count != 0)
        {
          layer.Add(indicator, errorRange);
        }
      }
    }

 

Thanks for the help!

[Modified 8 years ago]

Comments (7)

Posted 8 years ago by Dominik K
Avatar

Okay, I get my wave lines (SpanIndicator) now but one question remains:

How can I add a tooltip to this error notification?

I tried it with the Event TokenMouseEnter but well, it doesn't work. Here is the code:

    void synEditor_TokenMouseEnter(object sender, EditorViewMouseEventArgs e)
    {
      switch (e.HitTestResult.Target)
      {
        case SyntaxEditorHitTestTarget.IndicatorMargin:
          // Set the tooltip text for an indicator in the indicator margin if there is one
          if (e.HitTestResult.DisplayLine.IsFirstForDocumentLine)
          {
            Indicator[] indicators = e.HitTestResult.DocumentLine.GetAllVisibleIndicators();
            for (int index = indicators.Length - 1; index >= 0; index--)
            {
              Indicator indicator = indicators[index];
              if (indicator.HasGlyph)
              {
                e.ToolTipText = String.Format("A <b>{0}</b> indicator is under the mouse.", indicator.Name);
                break;
              }
            }
          }
          break;
        case SyntaxEditorHitTestTarget.TextArea:
          if (e.HitTestResult.Offset != -1)
          {
            SpanIndicator[] indicators = synEditor.Document.SpanIndicatorLayers.GetIndicatorsForTextRange(_errorRange, true);
            if ((indicators != null) && (indicators.Length > 0))
            {
              e.ToolTipText = String.Format(_errorMsg, indicators[indicators.Length - 1].Name, indicators[indicators.Length - 1].TextRange);
            }
          }
          break;
      }
    }
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Dominik,

It's hard to say without a simple sample project to debug but part of the original problem might be you using the sample indicator layer key as the one the .NET Languages Add-on already uses.  So the add-on might be clearing and adding its parse errors to it.  Perhaps try using a different key for your layer.

For adding tool tips, you should handle the SyntaxEditor.ViewMouseHover event instead.  That event passes an event args where you can set the e.ToolTipText on, similar to what you are doing.  It should work properly from there.


Actipro Software Support

Posted 8 years ago by Dominik K
Avatar

Thanks a lot!

The tooltips and error signing are working properly now.

/e: Okay, the problem now is that when I use the ViewMouseHover event, I get the tooltip on the whole editor.

And how is it possible to underline more pieces of text in the editor?

[Modified 8 years ago]

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Dominik,

I'm not quite sure what you mean.  If you are handling ViewMouseHover and you are only getting indicators that cover the target offset, it should only show them where appropriate.  Perhaps you can explain in more detail what you see.

Also we'd need more detail about the underline question as well.  In general though, underlining can be done via the styles assigned to tokens.


Actipro Software Support

Posted 8 years ago by Dominik K
Avatar

Hello,

I try to explain in more detail. I'm using the ViewMouseHover event right now and addet following lines:

void synEditor_ViewMouseHover(object sender, EditorViewMouseEventArgs e)
    {
      if (e.HitTestResult.Target == SyntaxEditorHitTestTarget.TextArea)
      {
        SpanIndicator[] ind = synEditor.Document.SpanIndicatorLayers.GetIndicatorsForTextRange(_errorRange, true);
        if ((ind != null) && (ind.Length > 0))
        {
          e.ToolTipText = _errorMsg;
        }
      }
    }

 Now I get the tooltip when I am in the control and not only when I am over the TextArea with the error.

The problem with the underlining is now solved, thanks.

 

Okay, I found my mistake... I didn't use the HitTestResult.Token, so my following codes works as I want:

    void synEditor_ViewMouseHover(object sender, EditorViewMouseEventArgs e)
    {
      if (e.HitTestResult.Target == SyntaxEditorHitTestTarget.TextArea && e.HitTestResult.Token != null)
      {
        //----------------------------------------------------
        //TextRange from the Token
        //----------------------------------------------------
        TextRange codeRange = new TextRange(e.HitTestResult.Token.StartOffset, e.HitTestResult.Token.EndOffset);
        //----------------------------------------------------
        // If the current Token is the same as the TextRange from the error, show tooltip
        //----------------------------------------------------
        if (codeRange.OverlapsWith(_errorRange))
        {
          SpanIndicator[] ind = synEditor.Document.SpanIndicatorLayers.GetIndicatorsForTextRange(_errorRange, true);
          if ((ind != null) && (ind.Length > 0))
          {
            e.ToolTipText = _errorMsg;
          }
        }
      }
    }

[Modified 8 years ago]

Posted 8 months ago by Tajko
Avatar

Few years later.. xd

Hello Dominik, 

can you tell me where you get CompileError class and what is it 'AddSpanIndicator'? im trying make custom error messages and i just started working with this control and im green with this -.-, don't know how use and what to do with this.

[Modified 8 months ago]

Posted 8 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

The squiggle line API in the WinForms SyntaxEditor 2020.1 version is much different than in the past versions, and now is as the WPF version's squiggle line API has always been.  I believe you have downloaded the WPF version of SyntaxEditor, which never had the API listed above in this thread.

Squiggle lines (like compile errors) are now created by using a tagger to provide ISquiggleTag instances over text ranges.  The AdornmentsSquigglesIntro QuickStart shows one way to add a tagger (inherting TaggerBase<ISquiggleTag>), where it scans text on the fly and tags ranges of text with ISquiggleTag of a certain color.  Another way is to make a tagger that inherits CollectionTagger<ISquiggleTag>, which is better if you have a fixed set of ranges you know need to be tagged.  The ReadOnlyRegions QuickStart shows an example of working with that but with CollectionTagger<IReadOnlyTag> instead.  The same concepts work with a CollectionTagger<ISquiggleTag>.

All the above being said, the most common way to implement squiggle tags is have your parser language service return an IParseData that implements IParseErrorProvider, since IParseErrorProvider has an Errors collection.  Then if your language has these two language services registered, it will show the squiggle tags for those IParseError instances and will support mouse hover tooltips too.

// Register a tagger provider for showing parse errors
this.RegisterService(new CodeDocumentTaggerProvider<ParseErrorTagger>(typeof(ParseErrorTagger)));

// Register a squiggle tag quick info provider
this.RegisterService(new SquiggleTagQuickInfoProvider());


Actipro Software Support

The latest build of this product (v2020.1 build 0402) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.