|
| 1 | +{******************************************************************************} |
| 2 | +{ Unit Note: } |
| 3 | +{ This file is partly derived from CnPack For Delphi/C++Builder } |
| 4 | +{ } |
| 5 | +{ Original author: } |
| 6 | +{ CnPack For Delphi/C++Builder } |
| 7 | +{ 中国人自己的开放源码第三方开发包 } |
| 8 | +{ (C)Copyright 2001-2023 CnPack 开发组 } |
| 9 | +{ 网站地址:http://www.cnpack.org } |
| 10 | +{ 电子邮件:master@cnpack.org } |
| 11 | +{******************************************************************************} |
| 12 | + |
| 13 | +unit DelphiAIDev.Utils.CnWizard; |
| 14 | + |
| 15 | +interface |
| 16 | + |
| 17 | +uses |
| 18 | + System.SysUtils, |
| 19 | + System.Classes, |
| 20 | + System.Variants, |
| 21 | + Winapi.Windows, |
| 22 | + ToolsAPI; |
| 23 | + |
| 24 | +function QuerySvcs(const Instance: IUnknown; const Intf: TGUID; out Inst): Boolean; |
| 25 | +function CnOtaGetEditBuffer: IOTAEditBuffer; |
| 26 | +function CnOtaGetTopMostEditView: IOTAEditView; |
| 27 | +function CnOtaEditPosToLinePos(EditPos: TOTAEditPos; EditView: IOTAEditView = nil): Integer; |
| 28 | +function OTAEditPos(Col: SmallInt; Line: Longint): TOTAEditPos; |
| 29 | +function CnOtaGetCurrLineText(var Text: string; var LineNo: Integer; var CharIndex: Integer; View: IOTAEditView = nil): Boolean; |
| 30 | +function GetCurrentLine(const AEditView: IOTAEditView): string; |
| 31 | + |
| 32 | +implementation |
| 33 | + |
| 34 | +function QuerySvcs(const Instance: IUnknown; const Intf: TGUID; out Inst): Boolean; |
| 35 | +begin |
| 36 | + Result := (Instance <> nil) and Supports(Instance, Intf, Inst); |
| 37 | +end; |
| 38 | + |
| 39 | +function CnOtaGetEditBuffer: IOTAEditBuffer; |
| 40 | +var |
| 41 | + iEditorServices: IOTAEditorServices; |
| 42 | +begin |
| 43 | + QuerySvcs(BorlandIDEServices, IOTAEditorServices, iEditorServices); |
| 44 | + if iEditorServices <> nil then |
| 45 | + begin |
| 46 | + Result := iEditorServices.GetTopBuffer; |
| 47 | + Exit; |
| 48 | + end; |
| 49 | + Result := nil; |
| 50 | +end; |
| 51 | + |
| 52 | +function CnOtaGetTopMostEditView: IOTAEditView; |
| 53 | +var |
| 54 | + iEditBuffer: IOTAEditBuffer; |
| 55 | +begin |
| 56 | + iEditBuffer := CnOtaGetEditBuffer; |
| 57 | + if iEditBuffer <> nil then |
| 58 | + begin |
| 59 | + Result := iEditBuffer.GetTopView; |
| 60 | + Exit; |
| 61 | + end; |
| 62 | + Result := nil; |
| 63 | +end; |
| 64 | + |
| 65 | +function CnOtaEditPosToLinePos(EditPos: TOTAEditPos; EditView: IOTAEditView = nil): Integer; |
| 66 | +var |
| 67 | + CharPos: TOTACharPos; |
| 68 | +begin |
| 69 | + if EditView = nil then |
| 70 | + EditView := CnOtaGetTopMostEditView; |
| 71 | + if Assigned(EditView) then |
| 72 | + begin |
| 73 | + EditView.ConvertPos(True, EditPos, CharPos); |
| 74 | + Result := EditView.CharPosToPos(CharPos); |
| 75 | + end |
| 76 | + else |
| 77 | + Result := 0; |
| 78 | +end; |
| 79 | + |
| 80 | +function OTAEditPos(Col: SmallInt; Line: Longint): TOTAEditPos; |
| 81 | +begin |
| 82 | + Result.Col := Col; |
| 83 | + Result.Line := Line; |
| 84 | +end; |
| 85 | + |
| 86 | +function ConvertEditorTextToTextW(const Text: AnsiString): string; |
| 87 | +begin |
| 88 | + Result := UTF8ToUnicodeString(Text); |
| 89 | +end; |
| 90 | + |
| 91 | +function CnOtaGetCurrLineText(var Text: string; var LineNo: Integer; var CharIndex: Integer; View: IOTAEditView = nil): Boolean; |
| 92 | +var |
| 93 | + L1, L2: Integer; |
| 94 | + Reader: IOTAEditReader; |
| 95 | + EditBuffer: IOTAEditBuffer; |
| 96 | + EditPos: TOTAEditPos; |
| 97 | + CharPos: TOTACharPos; |
| 98 | + OutStr: AnsiString; |
| 99 | +begin |
| 100 | + Result := False; |
| 101 | + //teste |
| 102 | + if not Assigned(View)then |
| 103 | + View := CnOtaGetTopMostEditView; |
| 104 | + if not Assigned(View) then |
| 105 | + Exit ; |
| 106 | + |
| 107 | + EditPos := View.CursorPos; |
| 108 | + View.ConvertPos(True, EditPos, CharPos); |
| 109 | + LineNo := CharPos.Line; |
| 110 | + CharIndex := CharPos.CharIndex; |
| 111 | + |
| 112 | + EditBuffer := View.Buffer; |
| 113 | + L1 := CnOtaEditPosToLinePos(OTAEditPos(1, LineNo), EditBuffer.TopView); |
| 114 | + if (LineNo >= View.Buffer.GetLinesInBuffer) then |
| 115 | + L2 := CnOtaEditPosToLinePos(OTAEditPos(High(SmallInt), LineNo + 1), EditBuffer.TopView) |
| 116 | + else |
| 117 | + L2 := CnOtaEditPosToLinePos(OTAEditPos(1, LineNo + 1), EditBuffer.TopView) - 2; |
| 118 | + SetLength(OutStr, L2 - L1); |
| 119 | + Reader := EditBuffer.CreateReader; |
| 120 | + try |
| 121 | + Reader.GetText(L1, PAnsiChar(OutStr), L2 - L1); |
| 122 | + finally |
| 123 | + Reader := nil; |
| 124 | + end; |
| 125 | + {$IFDEF UNICODE} |
| 126 | + Text := TrimRight(ConvertEditorTextToTextW(OutStr)); |
| 127 | + {$ELSE} |
| 128 | + Text := TrimRight(string(ConvertEditorTextToText(OutStr))); |
| 129 | + {$ENDIF} |
| 130 | + Result := True; |
| 131 | +end; |
| 132 | + |
| 133 | +function GetCurrentLine(const AEditView: IOTAEditView): string; |
| 134 | +var |
| 135 | + TextLen: Integer; |
| 136 | + StartPos: Integer; |
| 137 | + EndPos: Integer; |
| 138 | + LineNo: Integer; |
| 139 | + CharIndex: Integer; |
| 140 | + LineText: String; |
| 141 | +begin |
| 142 | + Result := ''; |
| 143 | +// if IsEditControl(Screen.ActiveControl) and Assigned(AEditView) then |
| 144 | +// if not (IsEditControl(Screen.ActiveControl))then |
| 145 | +// Exit; |
| 146 | + |
| 147 | + if not(Assigned(AEditView)) then |
| 148 | + Exit; |
| 149 | + |
| 150 | + if AEditView.Block.IsValid then |
| 151 | + begin |
| 152 | +// StartPos := CnOtaEditPosToLinePos(OTAEditPos(AEditView.Block.StartingColumn, |
| 153 | +// AEditView.Block.StartingRow), AEditView); |
| 154 | +// EndPos := CnOtaEditPosToLinePos(OTAEditPos(AEditView.Block.EndingColumn, |
| 155 | +// AEditView.Block.EndingRow), AEditView); |
| 156 | +// TextLen := AEditView.Block.Size; |
| 157 | +// |
| 158 | +// {$IFDEF UNICODE} |
| 159 | +// CnOtaInsertTextIntoEditorAtPosW(AEditView.Block.Text, StartPos, AEditView.Buffer); |
| 160 | +// {$ELSE} |
| 161 | +// CnOtaInsertTextIntoEditorAtPos(ConvertEditorTextToText(AEditView.Block.Text), StartPos, AEditView.Buffer); |
| 162 | +// {$ENDIF} |
| 163 | +// AEditView.CursorPos := CnOtaLinePosToEditPos(StartPos + TextLen); |
| 164 | +// AEditView.Block.BeginBlock; |
| 165 | +// AEditView.CursorPos := CnOtaLinePosToEditPos(EndPos + TextLen); |
| 166 | +// AEditView.Block.EndBlock; |
| 167 | +// |
| 168 | +// AEditView.Paint; |
| 169 | + end |
| 170 | + else |
| 171 | + begin |
| 172 | + CnOtaGetCurrLineText(LineText, LineNo, CharIndex); |
| 173 | + Result := LineText; |
| 174 | + |
| 175 | + //Inc(LineNo); |
| 176 | + //CnOtaInsertSingleLine(LineNo, LineText, AEditView); |
| 177 | + end; |
| 178 | +end; |
| 179 | + |
| 180 | +end. |
0 commit comments