mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-05-18 09:02:51 +02:00
cody: insert file at cursor
git-svn-id: trunk@30678 -
This commit is contained in:
parent
f1cd1cf62e
commit
d9366ecdb7
@ -27,27 +27,40 @@ unit CodyFrm;
|
|||||||
|
|
||||||
{$mode objfpc}{$H+}
|
{$mode objfpc}{$H+}
|
||||||
|
|
||||||
|
{$R *.lfm}
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
||||||
// codetools
|
// codetools
|
||||||
CodeToolManager, CodeCache,
|
FileProcs, CodeToolManager, SourceLog, CodeCache,
|
||||||
// IDEIntf
|
// IDEIntf
|
||||||
LazIDEIntf, SrcEditorIntf, IDEDialogs,
|
LazIDEIntf, SrcEditorIntf, IDEDialogs,
|
||||||
// cody
|
// cody
|
||||||
CodyStrConsts;
|
CodyStrConsts;
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
|
{ TCody }
|
||||||
|
|
||||||
|
TCody = class
|
||||||
|
public
|
||||||
|
procedure DecodeLoaded(Sender: TSourceLog; const Filename: string;
|
||||||
|
var Source, DiskEncoding, MemEncoding: string);
|
||||||
|
end;
|
||||||
|
|
||||||
TCodyWindow = class(TForm)
|
TCodyWindow = class(TForm)
|
||||||
private
|
private
|
||||||
public
|
public
|
||||||
end;
|
end;
|
||||||
|
|
||||||
var
|
var
|
||||||
|
Cody: TCody;
|
||||||
CodyWindow: TCodyWindow;
|
CodyWindow: TCodyWindow;
|
||||||
|
|
||||||
procedure RemoveWithBlockCmd(Sender: TObject);
|
procedure RemoveWithBlockCmd(Sender: TObject);
|
||||||
|
procedure InsertFileAtCursor(Sender: TObject);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
@ -83,7 +96,65 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{$R *.lfm}
|
procedure InsertFileAtCursor(Sender: TObject);
|
||||||
|
var
|
||||||
|
OpenDialog: TOpenDialog;
|
||||||
|
Filter: String;
|
||||||
|
Filename: String;
|
||||||
|
Code: TCodeBuffer;
|
||||||
|
SrcEdit: TSourceEditorInterface;
|
||||||
|
begin
|
||||||
|
SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
|
||||||
|
if SrcEdit=nil then exit;
|
||||||
|
|
||||||
|
OpenDialog:=TOpenDialog.Create(nil);
|
||||||
|
Code:=nil;
|
||||||
|
try
|
||||||
|
InitIDEFileDialog(OpenDialog);
|
||||||
|
OpenDialog.Title:='Select file to insert at cursor';
|
||||||
|
OpenDialog.Options:=OpenDialog.Options+[ofFileMustExist];
|
||||||
|
Filter:='Pascal' + ' (*.pas;*.pp)|*.pas;*.pp';
|
||||||
|
Filter:=Filter+'|'+'All files' + ' (' + GetAllFilesMask + ')|' + GetAllFilesMask;
|
||||||
|
OpenDialog.Filter:=Filter;
|
||||||
|
if not OpenDialog.Execute then exit;
|
||||||
|
Filename:=OpenDialog.FileName;
|
||||||
|
if not FileIsText(Filename) then begin
|
||||||
|
if IDEMessageDialog('Warning','The file seems to be a binary. Proceed?',
|
||||||
|
mtConfirmation,[mbOk,mbCancel])<>mrOK then exit;
|
||||||
|
end;
|
||||||
|
Code:=TCodeBuffer.Create;
|
||||||
|
Code.Filename:=Filename;
|
||||||
|
Code.OnDecodeLoaded:=@Cody.DecodeLoaded;
|
||||||
|
if not Code.LoadFromFile(Filename) then begin
|
||||||
|
IDEMessageDialog('Error','Unable to load file "'+Filename+'"'#13
|
||||||
|
+Code.LastError,
|
||||||
|
mtError,[mbCancel]);
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
SrcEdit.Selection:=Code.Source;
|
||||||
|
finally
|
||||||
|
OpenDialog.Free;
|
||||||
|
Code.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TCody }
|
||||||
|
|
||||||
|
procedure TCody.DecodeLoaded(Sender: TSourceLog; const Filename: string;
|
||||||
|
var Source, DiskEncoding, MemEncoding: string);
|
||||||
|
begin
|
||||||
|
//debugln(['TCody.DecodeLoaded ',Filename]);
|
||||||
|
if (Sender is TCodeBuffer)
|
||||||
|
and Assigned(CodeToolBoss.SourceCache.OnDecodeLoaded) then
|
||||||
|
CodeToolBoss.SourceCache.OnDecodeLoaded(TCodeBuffer(Sender),Filename,
|
||||||
|
Source,DiskEncoding,MemEncoding);
|
||||||
|
end;
|
||||||
|
|
||||||
|
initialization
|
||||||
|
Cody:=TCody.Create;
|
||||||
|
finalization
|
||||||
|
FreeAndNil(Cody);
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -40,32 +40,47 @@ implementation
|
|||||||
|
|
||||||
procedure Register;
|
procedure Register;
|
||||||
var
|
var
|
||||||
CmdCategory: TIDECommandCategory;
|
CmdCatProjectMenu: TIDECommandCategory;
|
||||||
|
CmdCatCodeTools: TIDECommandCategory;
|
||||||
|
CmdCatFileMenu: TIDECommandCategory;
|
||||||
PPUListCommand: TIDECommand;
|
PPUListCommand: TIDECommand;
|
||||||
AddAssignMethodCommand: TIDECommand;
|
AddAssignMethodCommand: TIDECommand;
|
||||||
RemoveWithBlockCommand: TIDECommand;
|
RemoveWithBlockCommand: TIDECommand;
|
||||||
|
InsertFileAtCursorCommand: TIDECommand;
|
||||||
begin
|
begin
|
||||||
CmdCategory:=IDECommandList.FindCategoryByName('ProjectMenu');
|
CmdCatFileMenu:=IDECommandList.FindCategoryByName('FileMenu');
|
||||||
if CmdCategory=nil then
|
if CmdCatFileMenu=nil then
|
||||||
raise Exception.Create('cody: PPUListDlg.Register: command category ProjectMenu not found');
|
raise Exception.Create('cody: command category FileMenu not found');
|
||||||
|
CmdCatProjectMenu:=IDECommandList.FindCategoryByName('ProjectMenu');
|
||||||
|
if CmdCatProjectMenu=nil then
|
||||||
|
raise Exception.Create('cody: command category ProjectMenu not found');
|
||||||
|
CmdCatCodeTools:=IDECommandList.FindCategoryByName('CodeTools');
|
||||||
|
if CmdCatCodeTools=nil then
|
||||||
|
raise Exception.Create('cody: command category CodeTools not found');
|
||||||
|
|
||||||
|
// insert file at cursor
|
||||||
|
InsertFileAtCursorCommand:=RegisterIDECommand(CmdCatFileMenu,
|
||||||
|
'InsertFileAtCursor',crsInsertFileAtCursor,
|
||||||
|
CleanIDEShortCut,CleanIDEShortCut,nil,@InsertFileAtCursor);
|
||||||
|
RegisterIDEMenuCommand(SrcEditSubMenuSource,'InsertFileAtCursor',
|
||||||
|
crsInsertFileAtCursor,nil,nil,InsertFileAtCursorCommand);
|
||||||
|
|
||||||
// show ppu list of project
|
// show ppu list of project
|
||||||
PPUListCommand:=RegisterIDECommand(CmdCategory, 'ShowPPUList',
|
PPUListCommand:=RegisterIDECommand(CmdCatProjectMenu, 'ShowPPUList',
|
||||||
crsShowUsedPpuFiles,
|
crsShowUsedPpuFiles,
|
||||||
CleanIDEShortCut,CleanIDEShortCut,nil,@ShowPPUList);
|
CleanIDEShortCut,CleanIDEShortCut,nil,@ShowPPUList);
|
||||||
RegisterIDEMenuCommand(itmProjectWindowSection,'PPUList',crsShowUsedPpuFiles,
|
RegisterIDEMenuCommand(itmProjectWindowSection,'PPUList',crsShowUsedPpuFiles,
|
||||||
nil,nil,PPUListCommand);
|
nil,nil,PPUListCommand);
|
||||||
|
|
||||||
CmdCategory:=IDECommandList.FindCategoryByName('CodeTools');
|
|
||||||
if CmdCategory=nil then
|
|
||||||
raise Exception.Create('cody: AddAssignMethodDlg.Register: command category CodeTools not found');
|
|
||||||
// add Assign method
|
// add Assign method
|
||||||
AddAssignMethodCommand:=RegisterIDECommand(CmdCategory, 'AddAssignMethod',
|
AddAssignMethodCommand:=RegisterIDECommand(CmdCatCodeTools, 'AddAssignMethod',
|
||||||
crsAddAssignMethod,
|
crsAddAssignMethod,
|
||||||
CleanIDEShortCut,CleanIDEShortCut,nil,@ShowAddAssignMethodDialog);
|
CleanIDEShortCut,CleanIDEShortCut,nil,@ShowAddAssignMethodDialog);
|
||||||
RegisterIDEMenuCommand(SrcEditSubMenuSource, 'AddAssignMethod',
|
RegisterIDEMenuCommand(SrcEditSubMenuSource, 'AddAssignMethod',
|
||||||
crsAddAssignMethod2,nil,nil,AddAssignMethodCommand);
|
crsAddAssignMethod2,nil,nil,AddAssignMethodCommand);
|
||||||
|
|
||||||
// remove With block
|
// remove With block
|
||||||
RemoveWithBlockCommand:=RegisterIDECommand(CmdCategory, 'RemoveWithBlock',
|
RemoveWithBlockCommand:=RegisterIDECommand(CmdCatCodeTools, 'RemoveWithBlock',
|
||||||
crsRemoveWithBlock,
|
crsRemoveWithBlock,
|
||||||
CleanIDEShortCut,CleanIDEShortCut,nil,@RemoveWithBlockCmd);
|
CleanIDEShortCut,CleanIDEShortCut,nil,@RemoveWithBlockCmd);
|
||||||
RegisterIDEMenuCommand(SrcEditSubMenuRefactor, 'RemoveWithBlock',
|
RegisterIDEMenuCommand(SrcEditSubMenuRefactor, 'RemoveWithBlock',
|
||||||
|
@ -55,6 +55,7 @@ resourcestring
|
|||||||
crsSource = 'Source: %s';
|
crsSource = 'Source: %s';
|
||||||
crsPPU = 'PPU: %s';
|
crsPPU = 'PPU: %s';
|
||||||
crsShowUsedPpuFiles = 'Show used .ppu files ...';
|
crsShowUsedPpuFiles = 'Show used .ppu files ...';
|
||||||
|
crsInsertFileAtCursor = 'Insert file at cursor ...';
|
||||||
crsVirtualUnit = 'Virtual unit';
|
crsVirtualUnit = 'Virtual unit';
|
||||||
crsProjectOutput = 'Project output';
|
crsProjectOutput = 'Project output';
|
||||||
crsClose = '&Close';
|
crsClose = '&Close';
|
||||||
|
@ -142,6 +142,10 @@ msgstr ""
|
|||||||
msgid "Gbytes"
|
msgid "Gbytes"
|
||||||
msgstr "Gbyte"
|
msgstr "Gbyte"
|
||||||
|
|
||||||
|
#: codystrconsts.crsinsertfileatcursor
|
||||||
|
msgid "Insert file at cursor ..."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: codystrconsts.crskbytes
|
#: codystrconsts.crskbytes
|
||||||
msgid "kbytes"
|
msgid "kbytes"
|
||||||
msgstr "Kbyte"
|
msgstr "Kbyte"
|
||||||
|
@ -133,6 +133,10 @@ msgstr ""
|
|||||||
msgid "Gbytes"
|
msgid "Gbytes"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: codystrconsts.crsinsertfileatcursor
|
||||||
|
msgid "Insert file at cursor ..."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: codystrconsts.crskbytes
|
#: codystrconsts.crskbytes
|
||||||
msgid "kbytes"
|
msgid "kbytes"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
@ -141,6 +141,10 @@ msgstr ""
|
|||||||
msgid "Gbytes"
|
msgid "Gbytes"
|
||||||
msgstr "Gbytes"
|
msgstr "Gbytes"
|
||||||
|
|
||||||
|
#: codystrconsts.crsinsertfileatcursor
|
||||||
|
msgid "Insert file at cursor ..."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: codystrconsts.crskbytes
|
#: codystrconsts.crskbytes
|
||||||
msgid "kbytes"
|
msgid "kbytes"
|
||||||
msgstr "kbytes"
|
msgstr "kbytes"
|
||||||
|
@ -141,6 +141,10 @@ msgstr "Расположите курсор редактора исходног
|
|||||||
msgid "Gbytes"
|
msgid "Gbytes"
|
||||||
msgstr "ГБ"
|
msgstr "ГБ"
|
||||||
|
|
||||||
|
#: codystrconsts.crsinsertfileatcursor
|
||||||
|
msgid "Insert file at cursor ..."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: codystrconsts.crskbytes
|
#: codystrconsts.crskbytes
|
||||||
msgid "kbytes"
|
msgid "kbytes"
|
||||||
msgstr "кБ"
|
msgstr "кБ"
|
||||||
|
@ -129,6 +129,7 @@ type
|
|||||||
procedure EncodeSaving(const AFilename: string; var ASource: string); virtual;
|
procedure EncodeSaving(const AFilename: string; var ASource: string); virtual;
|
||||||
public
|
public
|
||||||
Data: Pointer;
|
Data: Pointer;
|
||||||
|
LastError: string;
|
||||||
function LineCount: integer;
|
function LineCount: integer;
|
||||||
function GetLine(Index: integer): string; // 0-based
|
function GetLine(Index: integer): string; // 0-based
|
||||||
function GetLineLength(Index: integer): integer; // 0-based
|
function GetLineLength(Index: integer): integer; // 0-based
|
||||||
@ -789,7 +790,8 @@ var
|
|||||||
fs: TFileStream;
|
fs: TFileStream;
|
||||||
p: Integer;
|
p: Integer;
|
||||||
begin
|
begin
|
||||||
Result := True;
|
Result := False;
|
||||||
|
LastError:='';
|
||||||
try
|
try
|
||||||
fs := TFileStream.Create(UTF8ToSys(Filename), fmOpenRead or fmShareDenyNone);
|
fs := TFileStream.Create(UTF8ToSys(Filename), fmOpenRead or fmShareDenyNone);
|
||||||
try
|
try
|
||||||
@ -817,8 +819,10 @@ begin
|
|||||||
finally
|
finally
|
||||||
fs.Free;
|
fs.Free;
|
||||||
end;
|
end;
|
||||||
|
Result := True;
|
||||||
except
|
except
|
||||||
Result := False;
|
on E: Exception do
|
||||||
|
LastError:=E.Message;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -841,17 +845,16 @@ begin
|
|||||||
DebugLn(['TSourceLog.SaveToFile Self=',DbgS(Self),' ',Filename,' Size=',length(Source)]);
|
DebugLn(['TSourceLog.SaveToFile Self=',DbgS(Self),' ',Filename,' Size=',length(Source)]);
|
||||||
CTDumpStack;
|
CTDumpStack;
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
Result := True;
|
Result := False;
|
||||||
|
LastError:='';
|
||||||
try
|
try
|
||||||
// keep filename case on disk
|
// keep filename case on disk
|
||||||
TheFilename := FindDiskFilename(Filename);
|
TheFilename := FindDiskFilename(Filename);
|
||||||
if FileExistsUTF8(TheFilename) then
|
if FileExistsUTF8(TheFilename) then begin
|
||||||
begin
|
|
||||||
InvalidateFileStateCache(TheFilename);
|
InvalidateFileStateCache(TheFilename);
|
||||||
fs := TFileStream.Create(UTF8ToSys(TheFilename), fmOpenWrite or fmShareDenyNone);
|
fs := TFileStream.Create(UTF8ToSys(TheFilename), fmOpenWrite or fmShareDenyNone);
|
||||||
fs.Size := 0;
|
fs.Size := 0;
|
||||||
end
|
end else begin
|
||||||
else begin
|
|
||||||
InvalidateFileStateCache; // invalidate all (samba shares)
|
InvalidateFileStateCache; // invalidate all (samba shares)
|
||||||
fs := TFileStream.Create(UTF8ToSys(TheFilename), fmCreate);
|
fs := TFileStream.Create(UTF8ToSys(TheFilename), fmCreate);
|
||||||
end;
|
end;
|
||||||
@ -867,8 +870,10 @@ begin
|
|||||||
finally
|
finally
|
||||||
fs.Free;
|
fs.Free;
|
||||||
end;
|
end;
|
||||||
|
Result := True;
|
||||||
except
|
except
|
||||||
Result := False;
|
on E: Exception do
|
||||||
|
LastError:=E.Message;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user