I need to extract a selected word delimited by text (start delimiter '[' end delimiter ']'), for example:
"Sample data speed: [CustomerTable.MyField] km/h".
I want to display a "friendly" caption of the selected delimited field.
If a letter of MyField is selected, TrichRichViewEdit.GetCurrentWord will return 'MyField', but I need to get 'CustomerTable.MyField'.
I was thinking of removing '[', ']', '.' from TCustomRichView.delimiters before calling TrichRichViewEdit.GetCurrentWord, but this approach seems to be above all since I am using TrichRichViewEdit.OnSelect event.
I haven't found a solution in the Trichview documentation, so I have come up with the following code.
Unfortunately, it does not work in some cases. Do you have a better solution?
Code: Select all
function GetWordUnderCursorWithDelimiters( ARichView: TRichViewEdit; APrefix, ASuffix: string ): string;
var
LItemIndex, LOffset, LStartPos, LEndPos: Integer;
LText: string;
begin
Result := '';
// Get index of item focused
LItemIndex := ARichView.CurItemNo;
LOffset := ARichView.OffsetInCurItem;
// Check active item
if (LItemIndex >= 0) and (ARichView.GetItemStyle(LItemIndex) >= 0) then
begin
// get texy
LText := ARichView.GetItemText(LItemIndex);
// find start delimiter
LStartPos := LOffset;
while (LStartPos > 1) and (Copy(LText, LStartPos - Length(APrefix), Length(APrefix)) <> APrefix) do
Dec(LStartPos);
// find end delimiter
LEndPos := LOffset;
while (LEndPos <= Length(LText)) and (Copy(LText, LEndPos, Length(ASuffix)) <> ASuffix) do
Inc(LEndPos);
// Adjust position to exclude delimiters
if (Copy(LText, LStartPos - Length(APrefix), Length(APrefix)) = APrefix) and
(Copy(LText, LEndPos, Length(ASuffix)) = ASuffix) then
begin
// Exclure le préfixe et le suffixe de l'extraction
Result := Copy(LText, LStartPos, LEndPos - LStartPos);
end;
end;
end;