Mono.TextEditor highlight line

2019-08-06 02:22发布

问题:

I am making a pascal code editor in Mono in MonoDevelop. I am using Mono.TextEditor as a code editor widget. However, I cannot find how to highlight a line in the widget. After compilation, I collect line numbers where errors occur, and so I want to highlight them in red. I found

Mono.TextEditor.LineBackgroundMarker

which seems to relate to what I want to do, but I cannot find where and how to use it.

Another option I was looking into was ViBuilder, but I don't even know how to use that. I can think of two ways to solve this problem:

  1. Simply make highlight
  2. Mark a line as error, as default style includes:

    { "name": "Underline(Error)", "color":"invalid-red" }

which also seems to be a possible solution.

回答1:

You can highlight lines in the text editor by adding markers to the underlying document. Use the TextDocument.AddMarker method, as follows:

TextEditor textEditor;
var marker = new Mono.TextEditor.LineBackgroundMarker();     
int lineNumber = ...;
textEditor.Document.AddMarker(lineNumber, marker);
textEditor.QueueDraw();

Also have a look at the Mono.TextEditor.StyleTextMarker class. This class has already the properties "BackgroundColor" / "Color" that you are looking for. Underlining may have to be done manually (for example by inheriting from StyleTextMarker and overriding the Draw method).