Recent

Author Topic: Deprecated function  (Read 2028 times)

Kaller

  • Jr. Member
  • **
  • Posts: 73
Deprecated function
« on: August 26, 2023, 08:55:08 am »
SynEdit.RowColToCharIndex(Pt) says it is deprecated.  It  works Ok but should I use an alternative?

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2247
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Deprecated function
« Reply #1 on: August 26, 2023, 10:05:03 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   i, charIndex: Integer;
  3. begin
  4.   charIndex := 0;
  5.   for i := 0 to SynEdit1.CaretXY.Y - 2 do
  6.     charIndex := charIndex + Length(SynEdit1.Lines[i]) + Length(LineEnding);
  7.   charIndex := charIndex + SynEdit1.CaretXY.X;
charIndex will got same value as RowColToCharIndex was given.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9991
  • Debugger - SynEdit - and more
    • wiki
Re: Deprecated function
« Reply #2 on: August 26, 2023, 10:52:35 am »
There is no alternative with the same functionality.

The idea is to use a different concept, and use x/y.


The way SynEdit operates, using "char index" can be very slow.
It will be fine for files with a 1000 chars. Though even then, if you use lots of calls, it may be slow.

"RowColToCharIndex" has to run over all lines each times it is invoked. Like the example by KodeZwerg.

The question is what are you trying to do?
In most cases it can be done without RowColToCharIndex.

Do you want to store the current caret or selection, and then insert new text?
Use SynEdit.TextBetweenPoint[p1,p1] :=
This will automatically adjust the caret and selection.

Also note, that in **most cases** you don't want to operate on SynEdit.Lines.  SynEdit.Lines is to load the initial text (and only that).
Operating an the lines removes all undo/redo info.
Again TextBetweenPoint is the solution.

Kaller

  • Jr. Member
  • **
  • Posts: 73
Re: Deprecated function
« Reply #3 on: August 26, 2023, 11:51:47 am »
Actually I have a custom script language I wrote and as I run it from the editor I display the next line to run highlighted as it runs. So fast is good and flaky is not.  textbetweenpoints sounds like it may do it. I see there is a selection mode called smLine. 
« Last Edit: August 26, 2023, 12:04:24 pm by Kaller »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9991
  • Debugger - SynEdit - and more
    • wiki
Re: Deprecated function
« Reply #4 on: August 26, 2023, 11:57:43 am »
I display the next line to run highlighted as it runs.
Have you seen "OnSpecialLineColors" ?

Code: Pascal  [Select][+][-]
  1. procedure(Sender: TObject; Line: integer; var Special: boolean; var FG, BG: TColor) of object;

Will be called for each line, and you can give it a fore/background color (clNone for "not changed", if you want to change only one.

"OnSpecialLineMarkup" for more complex colors (border, blend/fade color ...)

-----
Mind those are called in the paint event:
- they must be fast.
- the must NOT call "invalidate" or other paint related code

"must be fast" => calculate which line to color in other code (e.g. KeyDown / StateChanged / ....). Store the line and color.
Then in OnSpecialLineColors, just check if the call is for the stored line, and return the stored color.




I am not sure if that needs  a highlighter assigned.
If it does, and if you don't have one, use a dummy like TSynPosSyn
« Last Edit: August 26, 2023, 12:01:45 pm by Martin_fr »

Kaller

  • Jr. Member
  • **
  • Posts: 73
Re: Deprecated function
« Reply #5 on: August 26, 2023, 01:05:48 pm »
i see. Well the answer is obvious now I will just keep the cumulative char count myself!  Since the lines come up consecutively.  Then just   
SelectedColor.Background := clYellow;
SelectedColor.Foreground := clBlack;
SelStart  := StartPos;
SelEnd    := EndPos;



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9991
  • Debugger - SynEdit - and more
    • wiki
Re: Deprecated function
« Reply #6 on: August 26, 2023, 01:21:32 pm »
i see. Well the answer is obvious now I will just keep the cumulative char count myself!  Since the lines come up consecutively.  Then just   
SelectedColor.Background := clYellow;
SelectedColor.Foreground := clBlack;
SelStart  := StartPos;
SelEnd    := EndPos;

StartPos/EndPos are points (x/y) too. So where to you need the Position in the text?

Also, note that SynEdit does NOT keep the original LineEnding. So if your text does have LineEndings that differ from the default for the OS, then  RowColToCharIndex  gives the wrong result.
Of course in your own code you may be able to correct for that.

Still, I don't see where you need the "CharIndex"?
Do you read a 2nd copy of the text from a file?
If so, then maybe go the opposite way, while you read the file, notice and count the LineEndings that you passed (the y pos). Keep the CharIndex of the last LineEnding, so you can get the diff to the current pos (and use that as the x pos).

But anyway, yes keeping counting (cumulative) will solve the speed issue.
Besides, in your case you are running only ONE count for each user input (each step instruction by the user). So that won't get you into to much trouble.
Some people used that in search/replace or similar, running hundreds of calls to RowColToCharIndex for a single user command. And then a 1000 lines of text multiplied by several hundred invocations....

Kaller

  • Jr. Member
  • **
  • Posts: 73
Re: Deprecated function
« Reply #7 on: August 27, 2023, 02:21:56 pm »
Here's my quick and dirty solution
to  highlighted the text in synedit during the processing loop...
i dumped it all the text into a stringlist and looped it to accumulate a charcounter as I processed the lines. Don't forget to add in the end of line chars. I think there is a property called sLines or something.

SynEditSelectedColor.Background := clYellow;  // just Toggle it back to reset any background color.
synedit.selstart := charcounter + 1;   
synedit.selend =  synedit.selstart  + length(s) + 1;  where s is the stringlist string

works like a charm :)







« Last Edit: August 27, 2023, 02:36:19 pm by Kaller »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9991
  • Debugger - SynEdit - and more
    • wiki
Re: Deprecated function
« Reply #8 on: August 27, 2023, 02:41:55 pm »
synedit.selend =  synedit.selstart  + length(s) + 1;  where s is the stringlist string

Ahhh, now I get it...

Well, "SelStart" should probably get the same warning. It has the same flaw.
(Don't worry, it is not going to be removed.).

The Method you may need is
Code: Pascal  [Select][+][-]
  1. SynEdit.BlockBegin := Point(1, Line);
  2. SynEdit.BlockEnd := Point(length(SynEdit.Lines[Line-1])+1, Line); // IIRC SynEdit.Lines is 0-based

But as I said, you can also do:
Code: Pascal  [Select][+][-]
  1. SynEdit.OnSpecialLineColors := @Form1.MySynLineColor;
  2. /////////
  3. procedure TForm1.MySynLineColor(Sender: TObject; Line: integer; var Special: boolean; var FG, BG: TColor);
  4. begin
  5.   Special  := Line = FMyStoredLineToHighlight;
  6.   // if Special then
  7.   FG := clNone;
  8.   BG := clYellow;
  9. end;
« Last Edit: August 27, 2023, 02:48:45 pm by Martin_fr »

Kaller

  • Jr. Member
  • **
  • Posts: 73
Re: Deprecated function
« Reply #9 on: August 28, 2023, 12:52:44 am »
OnSpecialLineColors is also marked as deprecated.  Synedit doesn't appear to be that well documented, so I kind of stumble my way through it. For example the Clearall function results in a single line remaining, line 1, with a CR LF entry in windows so the line count is 1, not zero after clearing the page and the char count is 2.  Inserting text can carry line endings with it so I was finding the files growing during save load cycles when no edits were occurring.  Anyways it seems like it is hard to avoid the indexing by character as it might be baked in to everything, but making my own indexer was one idea. Fortunately the text is not so long. I might still do something like dump the synedit text into a string list, and then use the associated object feature of the stringlist to maintain a line counter and a byte counter, at least the compiler might stop nagging me about deprecated functions. LOl.
« Last Edit: September 02, 2023, 07:22:20 am by Kaller »

 

TinyPortal © 2005-2018