added example for adding a new codetools function to the IDE via a package

git-svn-id: trunk@8231 -
This commit is contained in:
mattias 2005-11-28 19:42:16 +00:00
parent b35c4de7a4
commit 73b6c0cdd6
8 changed files with 215 additions and 13 deletions

3
.gitattributes vendored
View File

@ -662,6 +662,9 @@ examples/codepageconverter/mainunit.lrt svneol=native#text/plain
examples/codepageconverter/mainunit.pas svneol=native#text/pascal
examples/codepageconverter/mainunit.po svneol=native#text/plain
examples/codepageconverter/mainunit.ru_RU.UTF-8.po svneol=native#text/plain
examples/codetools/jumptoimplementation/codetoolsexample1.pas svneol=native#text/plain
examples/codetools/jumptoimplementation/jumptoimplementation.lpk svneol=native#text/plain
examples/codetools/jumptoimplementation/jumptoimplementation.pas svneol=native#text/plain
examples/combobox.lpi svneol=native#text/plain
examples/combobox.pp svneol=native#text/pascal
examples/comdialogs.lpi svneol=native#text/plain

View File

@ -63,6 +63,7 @@ type
);
const
CleanCodeXYPosition: TCodeXYPosition = (X:0; Y:0; Code:nil);
AllCommonAtomWords = [cafWord, cafEnd, cafRecord, cafBegin];
CommonAtomFlagNames: array[TCommonAtomFlag] of shortstring = (
'None',

View File

@ -111,13 +111,6 @@ type
const AFilename: string): string;
function FindCodeOfMainUnitHint(Code: TCodeBuffer): TCodeBuffer;
procedure CreateScanner(Code: TCodeBuffer);
procedure ClearError;
procedure ClearCurCodeTool;
function InitCurCodeTool(Code: TCodeBuffer): boolean;
function InitResourceTool: boolean;
procedure ClearPositions;
function GetCodeToolForSource(Code: TCodeBuffer;
GoToMainCode, ExceptionOnError: boolean): TCustomCodeTool;
procedure SetAbortable(const AValue: boolean);
procedure SetAddInheritedCodeToOverrideMethod(const AValue: boolean);
procedure SetCheckFilesOnDisk(NewValue: boolean);
@ -129,7 +122,6 @@ type
procedure SetCursorBeyondEOL(NewValue: boolean);
procedure BeforeApplyingChanges(var Abort: boolean);
procedure AfterApplyingChanges;
function HandleException(AnException: Exception): boolean;
procedure AdjustErrorTopLine;
procedure WriteError;
function OnGetCodeToolForBuffer(Sender: TObject;
@ -173,11 +165,22 @@ type
function GetIncludeCodeChain(Code: TCodeBuffer;
RemoveFirstCodesWithoutTool: boolean;
out ListOfCodeBuffer: TFPList): boolean;
function FindCodeToolForSource(Code: TCodeBuffer): TCustomCodeTool;
property OnSearchUsedUnit: TOnSearchUsedUnit
read FOnSearchUsedUnit write FOnSearchUsedUnit;
// initializing single codetools
function FindCodeToolForSource(Code: TCodeBuffer): TCustomCodeTool;
property CurCodeTool: TEventsCodeTool read FCurCodeTool;
procedure ClearCurCodeTool;
function InitCurCodeTool(Code: TCodeBuffer): boolean;
function InitResourceTool: boolean;
procedure ClearPositions;
function GetCodeToolForSource(Code: TCodeBuffer;
GoToMainCode, ExceptionOnError: boolean): TCustomCodeTool;
// exception handling
procedure ClearError;
function HandleException(AnException: Exception): boolean;
procedure SetError(Code: TCodeBuffer; Line, Column: integer;
const TheMessage: string);
property CatchExceptions: boolean

View File

@ -0,0 +1,92 @@
{ Copyright (C) 2005 Mattias Gaertner
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
Abstract:
Demonstrates how to add a new menu item to the IDE:
Search -> Jump to Implementation
}
unit CodeToolsExample1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, MenuIntf, LazIDEIntf, SrcEditorIntf, CodeToolManager,
CodeTree, CodeCache, CodeAtom, CustomCodeTool, FindDeclarationTool;
procedure JumpIDEToImplementationKeyword(Sender: TObject);
procedure Register;
implementation
procedure JumpIDEToImplementationKeyword(Sender: TObject);
var
SrcEditor: TSourceEditorInterface;
CodeBuffer: TCodeBuffer;
CurCodeTool: TCustomCodeTool;
Node: TCodeTreeNode;
Tool: TFindDeclarationTool;
NewCodePos: TCodeXYPosition;
NewTopLine: Integer;
begin
if Sender=nil then ;
// commit editor changes to codetools
if not LazarusIDE.BeginCodeTools then exit;
// get active source editor
SrcEditor:=SourceEditorWindow.ActiveEditor;
if SrcEditor=nil then exit;
CodeBuffer:=SrcEditor.CodeToolsBuffer as TCodeBuffer;
try
// init codetool for the source
if CodeToolBoss.InitCurCodeTool(CodeBuffer) then begin
CurCodeTool:=CodeToolBoss.CurCodeTool;
if CurCodeTool is TFindDeclarationTool then begin
// search imlementation node
Tool:=TFindDeclarationTool(CurCodeTool);
Node:=Tool.FindImplementationNode;
if Node<>nil then begin
// convert text position to editor postion
NewTopLine:=0;
NewCodePos:=CleanCodeXYPosition;
if Tool.CleanPosToCaretAndTopLine(Node.StartPos,
NewCodePos,NewTopLine)
then begin
// jump
LazarusIDE.DoOpenFileAndJumpToPos(NewCodePos.Code.Filename,
Point(NewCodePos.X,NewCodePos.Y),NewTopLine,-1,
[ofRegularFile,ofUseCache]);
end;
end;
end;
end;
except
LazarusIDE.DoJumpToCodeToolBossError;
end;
end;
procedure Register;
begin
RegisterIDEMenuCommand(itmCodeToolSearches,'JumpToImplementation',
'Jump to implementation keyword',nil,@JumpIDEToImplementationKeyword);
end;
end.

View File

@ -0,0 +1,43 @@
<?xml version="1.0"?>
<CONFIG>
<Package Version="2">
<Name Value="JumpToImplementation"/>
<CompilerOptions>
<Version Value="5"/>
<SearchPaths>
<OtherUnitFiles Value="$(LazarusDir)/components/codetools/units/$(TargetCPU)-$(TargetOS)/"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)/"/>
</SearchPaths>
<CodeGeneration>
<Generate Value="Faster"/>
</CodeGeneration>
<Other>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
<Files Count="1">
<Item1>
<Filename Value="codetoolsexample1.pas"/>
<HasRegisterProc Value="True"/>
<UnitName Value="codetoolsexample1"/>
</Item1>
</Files>
<Type Value="RunAndDesignTime"/>
<RequiredPkgs Count="2">
<Item1>
<PackageName Value="IDEIntf"/>
</Item1>
<Item2>
<PackageName Value="FCL"/>
<MinVersion Major="1" Valid="True"/>
</Item2>
</RequiredPkgs>
<UsageOptions>
<UnitPath Value="$(PkgOutDir)"/>
</UsageOptions>
<PublishOptions>
<Version Value="2"/>
<IgnoreBinaries Value="False"/>
</PublishOptions>
</Package>
</CONFIG>

View File

@ -0,0 +1,21 @@
{ This file was automatically created by Lazarus. Do not edit!
This source is only used to compile and install the package.
}
unit JumpToImplementation;
interface
uses
CodeToolsExample1, LazarusPackageIntf;
implementation
procedure Register;
begin
RegisterUnit('CodeToolsExample1', @CodeToolsExample1.Register);
end;
initialization
RegisterPackage('JumpToImplementation', @Register);
end.

View File

@ -594,6 +594,9 @@ type
function DoOpenFileAtCursor(Sender: TObject): TModalResult;
function DoOpenFileAndJumpToIdentifier(const AFilename, AnIdentifier: string;
PageIndex: integer; Flags: TOpenFlags): TModalResult; override;
function DoOpenFileAndJumpToPos(const AFilename: string;
const CursorPosition: TPoint; TopLine: integer;
PageIndex: integer; Flags: TOpenFlags): TModalResult; override;
function DoSaveAll(Flags: TSaveFlags): TModalResult;
procedure DoRestart;
function DoOpenMainUnit(ProjectLoading: boolean): TModalResult;
@ -720,6 +723,7 @@ type
procedure RescanCompilerDefines(OnlyIfCompilerChanged: boolean);
procedure UpdateEnglishErrorMsgFilename;
procedure ActivateCodeToolAbortableMode;
function BeginCodeTools: boolean; override;
function BeginCodeTool(var ActiveSrcEdit: TSourceEditor;
var ActiveUnitInfo: TUnitInfo;
Flags: TCodeToolsFlags): boolean;
@ -6143,6 +6147,7 @@ begin
Result:=mrOk;
InputHistories.FileDialogSettings.InitialDir:=ExtractFilePath(FName);
if DoOpenEditorFile(FName,-1,[ofAddToRecent])=mrOk then begin
// success
end;
end;
end;
@ -6169,6 +6174,28 @@ begin
DoJumpToCodeToolBossError;
end;
function TMainIDE.DoOpenFileAndJumpToPos(const AFilename: string;
const CursorPosition: TPoint; TopLine: integer; PageIndex: integer;
Flags: TOpenFlags): TModalResult;
var
ActiveUnitInfo, OldActiveUnitInfo: TUnitInfo;
ActiveSrcEdit, OldActiveSrcEdit: TSourceEditor;
NewSource: TCodeBuffer;
begin
GetCurrentUnit(OldActiveSrcEdit,OldActiveUnitInfo);
Result:=DoOpenEditorFile(AFilename, PageIndex, Flags);
if Result<>mrOk then exit;
GetCurrentUnit(ActiveSrcEdit,ActiveUnitInfo);
if ActiveUnitInfo<>nil then begin
DoJumpToCodePos(OldActiveSrcEdit, OldActiveUnitInfo,
ActiveUnitInfo.Source,
CursorPosition.X, CursorPosition.Y, TopLine, true);
Result:=mrOk;
end else begin
Result:=mrCancel;
end;
end;
function TMainIDE.DoNewProject(ProjectDesc: TProjectDescriptor):TModalResult;
var i:integer;
Begin
@ -10120,6 +10147,14 @@ begin
CodeToolBoss.Abortable:=true;
end;
function TMainIDE.BeginCodeTools: boolean;
var
ActiveSrcEdit: TSourceEditor;
ActiveUnitInfo: TUnitInfo;
begin
Result:=BeginCodeTool(nil,ActiveSrcEdit,ActiveUnitInfo,[]);
end;
procedure TMainIDE.OnBeforeCodeToolBossApplyChanges(Manager: TCodeToolManager;
var Abort: boolean);
// the CodeToolBoss built a list of Sources that will be modified

View File

@ -22,7 +22,7 @@ unit LazIDEIntf;
interface
uses
Classes, SysUtils, Forms, ProjectIntf;
Classes, SysUtils, Forms, ProjectIntf, SrcEditorIntf;
type
// open file flags
@ -108,6 +108,9 @@ type
Flags: TOpenFlags): TModalResult; virtual; abstract;
function DoOpenFileAndJumpToIdentifier(const AFilename, AnIdentifier: string;
PageIndex: integer; Flags: TOpenFlags): TModalResult; virtual; abstract;
function DoOpenFileAndJumpToPos(const AFilename: string;
const CursorPosition: TPoint; TopLine: integer;
PageIndex: integer; Flags: TOpenFlags): TModalResult; virtual; abstract;
function DoNewProject(ProjectDesc: TProjectDescriptor): TModalResult; virtual; abstract;
function DoSaveProject(Flags: TSaveFlags): TModalResult; virtual; abstract;
@ -122,11 +125,12 @@ type
procedure CopySecondaryConfigFile(const AFilename: String); virtual; abstract;
function CreateNewUniqueFilename(const Prefix, Ext: string;
NewOwner: TObject; Flags: TSearchIDEFileFlags;
TryWithoutNumber: boolean): string; virtual; abstract;
NewOwner: TObject; Flags: TSearchIDEFileFlags;
TryWithoutNumber: boolean): string; virtual; abstract;
function SubstituteMakros(var s: string): boolean; virtual; abstract;
function BeginCodeTools: boolean; virtual; abstract;
procedure DoJumpToCodeToolBossError; virtual; abstract;
procedure SaveSourceEditorChangesToCodeCache(PageIndex: integer); virtual; abstract;