mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-05-15 19:42:49 +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+}
|
||||
|
||||
{$R *.lfm}
|
||||
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
||||
// codetools
|
||||
CodeToolManager, CodeCache,
|
||||
FileProcs, CodeToolManager, SourceLog, CodeCache,
|
||||
// IDEIntf
|
||||
LazIDEIntf, SrcEditorIntf, IDEDialogs,
|
||||
// cody
|
||||
CodyStrConsts;
|
||||
|
||||
type
|
||||
|
||||
{ TCody }
|
||||
|
||||
TCody = class
|
||||
public
|
||||
procedure DecodeLoaded(Sender: TSourceLog; const Filename: string;
|
||||
var Source, DiskEncoding, MemEncoding: string);
|
||||
end;
|
||||
|
||||
TCodyWindow = class(TForm)
|
||||
private
|
||||
public
|
||||
end;
|
||||
|
||||
var
|
||||
Cody: TCody;
|
||||
CodyWindow: TCodyWindow;
|
||||
|
||||
procedure RemoveWithBlockCmd(Sender: TObject);
|
||||
procedure InsertFileAtCursor(Sender: TObject);
|
||||
|
||||
implementation
|
||||
|
||||
@ -83,7 +96,65 @@ begin
|
||||
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.
|
||||
|
||||
|
@ -40,32 +40,47 @@ implementation
|
||||
|
||||
procedure Register;
|
||||
var
|
||||
CmdCategory: TIDECommandCategory;
|
||||
CmdCatProjectMenu: TIDECommandCategory;
|
||||
CmdCatCodeTools: TIDECommandCategory;
|
||||
CmdCatFileMenu: TIDECommandCategory;
|
||||
PPUListCommand: TIDECommand;
|
||||
AddAssignMethodCommand: TIDECommand;
|
||||
RemoveWithBlockCommand: TIDECommand;
|
||||
InsertFileAtCursorCommand: TIDECommand;
|
||||
begin
|
||||
CmdCategory:=IDECommandList.FindCategoryByName('ProjectMenu');
|
||||
if CmdCategory=nil then
|
||||
raise Exception.Create('cody: PPUListDlg.Register: command category ProjectMenu not found');
|
||||
CmdCatFileMenu:=IDECommandList.FindCategoryByName('FileMenu');
|
||||
if CmdCatFileMenu=nil then
|
||||
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
|
||||
PPUListCommand:=RegisterIDECommand(CmdCategory, 'ShowPPUList',
|
||||
PPUListCommand:=RegisterIDECommand(CmdCatProjectMenu, 'ShowPPUList',
|
||||
crsShowUsedPpuFiles,
|
||||
CleanIDEShortCut,CleanIDEShortCut,nil,@ShowPPUList);
|
||||
RegisterIDEMenuCommand(itmProjectWindowSection,'PPUList',crsShowUsedPpuFiles,
|
||||
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
|
||||
AddAssignMethodCommand:=RegisterIDECommand(CmdCategory, 'AddAssignMethod',
|
||||
AddAssignMethodCommand:=RegisterIDECommand(CmdCatCodeTools, 'AddAssignMethod',
|
||||
crsAddAssignMethod,
|
||||
CleanIDEShortCut,CleanIDEShortCut,nil,@ShowAddAssignMethodDialog);
|
||||
RegisterIDEMenuCommand(SrcEditSubMenuSource, 'AddAssignMethod',
|
||||
crsAddAssignMethod2,nil,nil,AddAssignMethodCommand);
|
||||
|
||||
// remove With block
|
||||
RemoveWithBlockCommand:=RegisterIDECommand(CmdCategory, 'RemoveWithBlock',
|
||||
RemoveWithBlockCommand:=RegisterIDECommand(CmdCatCodeTools, 'RemoveWithBlock',
|
||||
crsRemoveWithBlock,
|
||||
CleanIDEShortCut,CleanIDEShortCut,nil,@RemoveWithBlockCmd);
|
||||
RegisterIDEMenuCommand(SrcEditSubMenuRefactor, 'RemoveWithBlock',
|
||||
|
@ -55,6 +55,7 @@ resourcestring
|
||||
crsSource = 'Source: %s';
|
||||
crsPPU = 'PPU: %s';
|
||||
crsShowUsedPpuFiles = 'Show used .ppu files ...';
|
||||
crsInsertFileAtCursor = 'Insert file at cursor ...';
|
||||
crsVirtualUnit = 'Virtual unit';
|
||||
crsProjectOutput = 'Project output';
|
||||
crsClose = '&Close';
|
||||
|
@ -142,6 +142,10 @@ msgstr ""
|
||||
msgid "Gbytes"
|
||||
msgstr "Gbyte"
|
||||
|
||||
#: codystrconsts.crsinsertfileatcursor
|
||||
msgid "Insert file at cursor ..."
|
||||
msgstr ""
|
||||
|
||||
#: codystrconsts.crskbytes
|
||||
msgid "kbytes"
|
||||
msgstr "Kbyte"
|
||||
|
@ -133,6 +133,10 @@ msgstr ""
|
||||
msgid "Gbytes"
|
||||
msgstr ""
|
||||
|
||||
#: codystrconsts.crsinsertfileatcursor
|
||||
msgid "Insert file at cursor ..."
|
||||
msgstr ""
|
||||
|
||||
#: codystrconsts.crskbytes
|
||||
msgid "kbytes"
|
||||
msgstr ""
|
||||
|
@ -141,6 +141,10 @@ msgstr ""
|
||||
msgid "Gbytes"
|
||||
msgstr "Gbytes"
|
||||
|
||||
#: codystrconsts.crsinsertfileatcursor
|
||||
msgid "Insert file at cursor ..."
|
||||
msgstr ""
|
||||
|
||||
#: codystrconsts.crskbytes
|
||||
msgid "kbytes"
|
||||
msgstr "kbytes"
|
||||
|
@ -141,6 +141,10 @@ msgstr "Расположите курсор редактора исходног
|
||||
msgid "Gbytes"
|
||||
msgstr "ГБ"
|
||||
|
||||
#: codystrconsts.crsinsertfileatcursor
|
||||
msgid "Insert file at cursor ..."
|
||||
msgstr ""
|
||||
|
||||
#: codystrconsts.crskbytes
|
||||
msgid "kbytes"
|
||||
msgstr "кБ"
|
||||
|
@ -129,6 +129,7 @@ type
|
||||
procedure EncodeSaving(const AFilename: string; var ASource: string); virtual;
|
||||
public
|
||||
Data: Pointer;
|
||||
LastError: string;
|
||||
function LineCount: integer;
|
||||
function GetLine(Index: integer): string; // 0-based
|
||||
function GetLineLength(Index: integer): integer; // 0-based
|
||||
@ -789,7 +790,8 @@ var
|
||||
fs: TFileStream;
|
||||
p: Integer;
|
||||
begin
|
||||
Result := True;
|
||||
Result := False;
|
||||
LastError:='';
|
||||
try
|
||||
fs := TFileStream.Create(UTF8ToSys(Filename), fmOpenRead or fmShareDenyNone);
|
||||
try
|
||||
@ -817,8 +819,10 @@ begin
|
||||
finally
|
||||
fs.Free;
|
||||
end;
|
||||
Result := True;
|
||||
except
|
||||
Result := False;
|
||||
on E: Exception do
|
||||
LastError:=E.Message;
|
||||
end;
|
||||
end;
|
||||
|
||||
@ -841,17 +845,16 @@ begin
|
||||
DebugLn(['TSourceLog.SaveToFile Self=',DbgS(Self),' ',Filename,' Size=',length(Source)]);
|
||||
CTDumpStack;
|
||||
{$ENDIF}
|
||||
Result := True;
|
||||
Result := False;
|
||||
LastError:='';
|
||||
try
|
||||
// keep filename case on disk
|
||||
TheFilename := FindDiskFilename(Filename);
|
||||
if FileExistsUTF8(TheFilename) then
|
||||
begin
|
||||
if FileExistsUTF8(TheFilename) then begin
|
||||
InvalidateFileStateCache(TheFilename);
|
||||
fs := TFileStream.Create(UTF8ToSys(TheFilename), fmOpenWrite or fmShareDenyNone);
|
||||
fs.Size := 0;
|
||||
end
|
||||
else begin
|
||||
end else begin
|
||||
InvalidateFileStateCache; // invalidate all (samba shares)
|
||||
fs := TFileStream.Create(UTF8ToSys(TheFilename), fmCreate);
|
||||
end;
|
||||
@ -867,8 +870,10 @@ begin
|
||||
finally
|
||||
fs.Free;
|
||||
end;
|
||||
Result := True;
|
||||
except
|
||||
Result := False;
|
||||
on E: Exception do
|
||||
LastError:=E.Message;
|
||||
end;
|
||||
end;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user