1

I want to fetch the entire line string (UTF8) and want to do operation on the line string. I have tried following code. but if we are having multibyte characters am not able to do this.

J:=1;
  CurrentRowStr :='';
  while True do
  begin
    //detect end of line
    Buffer.EditPosition.Move(Changes[I].FLine,J);
    CurrentRowStr := CurrentRowStr + Buffer.EditPosition.Character ;
    J := J+1;
  end;
  CurrentRowStr := Buffer.EditPosition.Read(J-1);

if anyone can help me to get particular line string using OpenToolsAPI, it would be great help.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Sumit Chourasia
  • 2,394
  • 7
  • 30
  • 57

1 Answers1

2

You can use a IOTAEditReader to get entire lines. The following code is from my Conversion Helper Package. Most of this revolves around the GetCurrentLineParams function:

function GetEditor: IOTASourceEditor;
var
  ModuleServices: IOTAModuleServices;
  Module: IOTAModule;
  I: Integer;
begin
  ModuleServices := BorlandIDEServices as IOTAModuleServices;
  Module := ModuleServices.CurrentModule;
  for I := 0 to Module.GetModuleFileCount - 1 do
    if Supports(Module.GetModuleFileEditor(I), IOTASourceEditor, Result) then
      Break;
end;

function GetLineAtCharPos(const Editor: IOTASourceEditor;
  const EditView: IOTAEditView; CharPos: TOTACharPos): string;
var
  EditReader: IOTAEditReader;
  Start, Len: Integer;
  Res: AnsiString;
begin
  CharPos.CharIndex := 0;
  Start := EditView.CharPosToPos(CharPos);
  Inc(CharPos.Line);
  Len := EditView.CharPosToPos(CharPos) - Start;
  if Len > 0 then
  begin
    SetLength(Res, Len);
    EditReader := Editor.CreateReader;
    EditReader.GetText(Start, PAnsiChar(Res), Len);
    Result := string(PAnsiChar(Res));
  end;
end;

function GetCurrentLine(const Editor: IOTASourceEditor;
  var BufferStart, Index: LongInt): string;
var
  BufferLength: LongInt;
  EditReader: IOTAEditReader;
  Res: AnsiString;
begin
  GetCurrentLineParams(Editor, BufferStart, BufferLength, Index);
  SetLength(Res, BufferLength);
  EditReader := Editor.CreateReader;
  EditReader.GetText(BufferStart, PAnsiChar(Res), BufferLength);
  Result := string(PAnsiChar(Res)); // just to be sure.
end;

function GetCurrentCharPos(const Editor: IOTASourceEditor; out EditView:
  IOTAEditView): TOTACharPos;
var
  CursorPos: TOTAEditPos;
begin
  EditView := Editor.GetEditView(0);
  CursorPos := EditView.CursorPos;
  EditView.ConvertPos(True, CursorPos, Result);
end;

procedure GetCurrentLineParams(const Editor: IOTASourceEditor;
  var Start, Length, Index: Integer);
var
  EditView: IOTAEditView;
  CharPos: TOTACharPos;
begin
  CharPos := GetCurrentCharPos(Editor, EditView);
  Index := CharPos.CharIndex + 1;
  CharPos.CharIndex := 0;
  Start := EditView.CharPosToPos(CharPos);
  Inc(CharPos.Line);
  Length := EditView.CharPosToPos(CharPos) - Start;
end;

function GetCurrentLineStart(const Editor: IOTASourceEditor): Integer;
var
  L, I: Integer;
begin
  GetCurrentLineParams(Editor, Result, L, I);
end;

function GetCurrentLineLength(const Editor: IOTASourceEditor): Integer;
var
  S, I: Integer;
begin
  GetCurrentLineParams(Editor, S, Result, I);
end;
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Am able to get the string now. my issue now is am not able to properly decode multibyte characters for example TC噂閲厭院運閏雲犾硺礼涖猪犱猤獷 is japanese characters. how can I decode it to UTF8 String, I have tried UTF8Encode method but that is not able to decode the string as well. – Sumit Chourasia Oct 15 '17 at 06:47
  • 1
    I can't test this right now, but the characters should be multibyte already. Just assign it to an UTF8String instead of an AnsiString. – Rudy Velthuis Oct 15 '17 at 11:55
  • It is working fine. but i'm getting following popup now. myfile's time/date on disk has changed. In-memory changes to this module have been detected as well. Reloading the module from disk will overwrite these changes. Reload? is there a way we can overcome this issue.? – Sumit Chourasia Oct 15 '17 at 13:10
  • That is a totally different matter. I guess the file must have changed (but not through the code above). It is up to you how you react. – Rudy Velthuis Oct 15 '17 at 14:14