Posted 7 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 7 years ago]

Comments (5)

Posted 7 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 7 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 7 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 7 years ago]

Posted 7 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 6 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 6 years ago]

The latest build of this product (v2018.1 build 0341) was released 9 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.