Forum > LCL

SynEdit highlight Matching text

(1/2) > >>

Tony Stone:
In the Lazarus Code editor I can see text "highlighted" or boxed out that matches exactly what my text selection is.  I am trying to do this with a TSynEdit control on a form.  Is this standard behavior I can implement in my own SynEdit control on my form using standard properties of the synedit?  Or is it much more involved using highlighters etc?  I just need a little nudge in the right direction.  Currently I am poking through the unit SynEditMarkupHighAll. 

Tony Stone:
Ok so this was a quick and dirty way that almost does what I want.  It will make all text that matches seltext have a green foreground which is great but if the selected text starts with a number it will not set the foreground.  Or if you select text not broken up with spaces it also does not set the foreground color.  So this experiment is a failure but I wonder if using the TSynAnySyn highlighter is still the right approach?





--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TfrmMain.hlSynEditMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);var  SynEdit: TSynEdit;  SelectedText: string;  Highlighter: TSynAnySyn;begin  SynEdit := TSynEdit(Sender);  SelectedText := SynEdit.SelText;    Highlighter := TSynAnySyn(SynEdit.Highlighter);  Highlighter.Constants.Clear;  Highlighter.ConstantAttri.Foreground:=clGreen;    if Length(SelectedText) <= 1 then Exit;    Highlighter.Constants.Add(SelectedText);  SynEdit.Refresh;end;


Thaddy:
Did you investigate the humongous amount of options that TSynAnySyn has? I will have a look. You are definitely on the rght way.

Martin_fr:
It is actually build-in, even in the SynEdit on your form, but not well exposed.

You can access it via:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    MarkCaret := TSynEditMarkupHighlightAllCaret(ASynEdit.MarkupByClass[TSynEditMarkupHighlightAllCaret]);
And then set colors (.MarkupInfo.Background) and other options (search the class, and look at properties).

You can also create your own instance, and add it to the MarkupManager (SynEdit.MarkupManager.AddMarkUp()).
Then you got 2 highlgihts.

There are other similar classes.


As for the suggestion of using a HL => this would work, but isn't really how it is meant to be.

HL are meant to scan the text by some grammar, and work context sensitive (even so SynAnySyn is not). HL are not meant to follow the caret, or react to selection.

Markup are meant for any colors that do not depend on grammar or the language/format of the text.

Martin_fr:

--- Quote from: Tony Stone on October 16, 2024, 03:19:59 am ---  Currently I am poking through the unit SynEditMarkupHighAll.

--- End quote ---

Really close. Correct unit.

Navigation

[0] Message Index

[#] Next page

Go to full version