我想获取整个行字符串(UTF8)并希望对行字符串进行操作。我试过以下代码。但如果我们有多字节字符,我就无法做到这一点。
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);
如果有人可以帮助我使用OpenToolsAPI获取特定的字符串,那将会很有帮助。
答案 0 :(得分:2)
您可以使用IOTAEditReader
获取整行。以下代码来自我的Conversion Helper Package。其中大部分内容围绕GetCurrentLineParams
函数:
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;