From: Thomas M. <tw...@us...> - 2006-05-27 17:55:03
|
Update of /cvsroot/gexperts/gexperts/unstable/Src/Formatter In directory sc8-pr-cvs11.sourceforge.net:/tmp/cvs-serv29931/unstable/Src/Formatter Modified Files: GX_CodeFormatterConfig.pas GX_Formatter.pas GX_eCodeFormatter.pas Log Message: * moved the formatter expert code to yet another object which is called by the plain expert and the editor expert, so the code isn't duplicated. * added a help string to the editor expert * formatter engine now takes a string list Index: GX_Formatter.pas =================================================================== RCS file: /cvsroot/gexperts/gexperts/unstable/Src/Formatter/GX_Formatter.pas,v retrieving revision 1.24 retrieving revision 1.25 diff -u -d -r1.24 -r1.25 --- GX_Formatter.pas 27 May 2006 14:38:40 -0000 1.24 +++ GX_Formatter.pas 27 May 2006 17:54:50 -0000 1.25 @@ -1,4 +1,5 @@ -// the code formatter expert +// the code formatter expert as a regular expert +// (only use one of GX_Formatter or GXeCodeFormatter) // Original Author: Thomas Mueller (http://www.dummzeuch.de) unit GX_Formatter; @@ -13,31 +14,15 @@ {$ENDIF} SysUtils, Classes, - Dialogs, Menus, - ToolsApi, - GX_OtaUtils, - GX_GenericUtils, GX_Experts, - GX_GExperts, GX_ConfigurationInfo, - GX_CodeFormatterGXConfigWrapper, - GX_CodeFormatterTypes, - GX_CodeFormatterConfig, - GX_CodeFormatterEngine, - GX_CodeFormatterBookmarks, - GX_CodeFormatterBreakpoints, - GX_CodeFormatterDefaultSettings, - GX_CodeFormatterConfigHandler, - GX_CodeFormatterDone, - GX_CodeFormatterSettings, - GX_CodeFormatterSaveList; + GX_CodeFormatterExpert; type - TCodeFormatterExpert = class(TGX_Expert) + TGxCodeFormatterExpert = class(TGX_Expert) private - FFormatter: TCodeFormatterEngine; - procedure ShowDoneDialog(out ShowDone: Boolean); + FExpert: TCodeFormatterExpert; protected procedure InternalLoadSettings(Settings: TGExpertsSettings); override; procedure InternalSaveSettings(Settings: TGExpertsSettings); override; @@ -54,157 +39,66 @@ { TCodeFormatterExpert } -procedure TCodeFormatterExpert.Click(Sender: TObject); -resourcestring - str_NoEditor = 'No source editor'; - str_UnsupportedFileTypeS = 'Unsupported file type: %s'; - str_UnableToGetContentsS = 'Unable to get contents of %s'; -var - SourceEditor: IOTASourceEditor; - FileName: string; - FullText: TStringList; - ShowDone: Boolean; - Bookmarks: ICodeFormatterSaveList; - Breakpoints: ICodeFormatterSaveList; - WasBinary: Boolean; - i: integer; - TempSettings: TCodeFormatterSettings; - OrigSettings: TCodeFormatterEngineSettings; - FirstLine: string; +procedure TGxCodeFormatterExpert.Click(Sender: TObject); begin - SourceEditor := GxOtaGetCurrentSourceEditor; - if not Assigned(SourceEditor) then - raise ECodeFormatter.Create(str_NoEditor); - FileName := SourceEditor.FileName; - if not (IsPascalSourceFile(FileName) or IsDelphiPackage(FileName)) then - raise ECodeFormatter.CreateFmt(str_UnsupportedFileTypeS, [ExtractFileName(FileName)]); - - TempSettings := nil; - FullText := TStringList.Create; - try - if not GxOtaGetFileAsText(Filename, FullText, WasBinary) then - raise ECodeFormatter.CreateFmt(str_UnableToGetContentsS, [FileName]); - if FullText.Count = 0 then - exit; - - Bookmarks := TBookmarkHandler.Create; - Breakpoints := TBreakpointHandler.Create; - - FirstLine := FullText[0]; - if SameText(Copy(FirstLine, 1, 20), '{GXFormatter.config=') then begin - FirstLine := Trim(Copy(FirstLine, 21, Length(FirstLine) - 21)); - TempSettings := TCodeFormatterSettings.Create; - if TCodeFormatterConfigHandler.GetDefaultConfig(FirstLine, TempSettings) then begin - OrigSettings := FFormatter.Settings.Settings; - FFormatter.Settings.Settings := TempSettings.Settings; - end else - FreeAndNil(TempSettings); - end; - - FFormatter.Sourcecode := FullText.Text; - if FFormatter.Execute then begin - if FullText.Text <> FFormatter.Sourcecode then begin - GxOtaReplaceEditorText(SourceEditor, FFormatter.Sourcecode); - for i := 0 to SourceEditor.EditViewCount - 1 do - SourceEditor.EditViews[i].Paint; - end; - if FFormatter.Settings.ShowDoneDialog then begin - ShowDoneDialog(ShowDone); - if Showdone <> FFormatter.Settings.ShowDoneDialog then begin - FFormatter.Settings.ShowDoneDialog := ShowDone; - end; - end; - end; - - finally - FreeAndNil(FullText); - if Assigned(TempSettings) then begin - FreeAndNil(TempSettings); - FFormatter.Settings.Settings := OrigSettings; - end; - end; + FExpert.Execute; end; -procedure TCodeFormatterExpert.Configure; +procedure TGxCodeFormatterExpert.Configure; begin - TfmCodeFormatterConfig.Execute(FFormatter.Settings); + FExpert.Configure; end; -constructor TCodeFormatterExpert.Create; -var - Settings: TCodeFormatterEngineSettings; +constructor TGxCodeFormatterExpert.Create; begin inherited Create; - Settings := BorlandDefaults; - FFormatter := TCodeFormatterEngine.Create; - FFormatter.Settings.Settings := Settings; + FExpert := TCodeFormatterExpert.Create; ShortCut := Menus.ShortCut(Word('F'), [ssCtrl, ssAlt]); end; -destructor TCodeFormatterExpert.Destroy; +destructor TGxCodeFormatterExpert.Destroy; begin - FreeAndNil(FFormatter); + FreeAndNil(FExpert); inherited; end; -function TCodeFormatterExpert.GetActionCaption: string; +function TGxCodeFormatterExpert.GetActionCaption: string; resourcestring SMenuCaption = 'Code &Formatter'; begin Result := SMenuCaption; end; -class function TCodeFormatterExpert.GetName: string; +class function TGxCodeFormatterExpert.GetName: string; begin Result := 'CodeFormatter'; end; -function TCodeFormatterExpert.HasConfigOptions: Boolean; +function TGxCodeFormatterExpert.HasConfigOptions: Boolean; begin Result := True; end; -function TCodeFormatterExpert.HasMenuItem: Boolean; +function TGxCodeFormatterExpert.HasMenuItem: Boolean; begin Result := True; end; -procedure TCodeFormatterExpert.InternalLoadSettings(Settings: TGExpertsSettings); -var - Reader: IConfigReader; +procedure TGxCodeFormatterExpert.InternalLoadSettings(Settings: TGExpertsSettings); begin inherited; - - Reader := TGxConfigWrapper.Create(Settings, ConfigurationKey); - TCodeFormatterConfigHandler.ReadSettings(Reader, FFormatter.Settings); + FExpert.InternalLoadSettings(ConfigurationKey, Settings); end; -procedure TCodeFormatterExpert.InternalSaveSettings(Settings: TGExpertsSettings); -var - Writer: IConfigWriter; +procedure TGxCodeFormatterExpert.InternalSaveSettings(Settings: TGExpertsSettings); begin inherited; - - Writer := TGxConfigWrapper.Create(Settings, ConfigurationKey); - TCodeFormatterConfigHandler.WriteSettings(Writer, FFormatter.Settings); -end; - -procedure TCodeFormatterExpert.ShowDoneDialog(out ShowDone: Boolean); -var - DoneForm: TfmCodeFormatterDone; -begin - DoneForm := TfmCodeFormatterDone.Create(nil); - try - DoneForm.ShowModal; - ShowDone := not DoneForm.chk_DontShowAgain.Checked; - finally - DoneForm.Free; - end; + FExpert.InternalSaveSettings(ConfigurationKey, Settings); end; initialization - RegisterGX_Expert(TCodeFormatterExpert); +// RegisterGX_Expert(TGxCodeFormatterExpert); end. Index: GX_CodeFormatterConfig.pas =================================================================== RCS file: /cvsroot/gexperts/gexperts/unstable/Src/Formatter/GX_CodeFormatterConfig.pas,v retrieving revision 1.14 retrieving revision 1.15 diff -u -d -r1.14 -r1.15 --- GX_CodeFormatterConfig.pas 19 Feb 2006 21:26:49 -0000 1.14 +++ GX_CodeFormatterConfig.pas 27 May 2006 17:54:50 -0000 1.15 @@ -459,20 +459,24 @@ procedure TfmCodeFormatterConfig.ts_PreviewShow(Sender: TObject); var Formatter: TCodeFormatterEngine; + st: TStringList; begin + st := nil; Formatter := TCodeFormatterEngine.Create; try + // this temporary string list is necessary to prevent an infinite loop (whose reason I don't really understand :-( ) + st := TStringList.Create; + st.Assign(m_PreviewAfter.Lines); FormToSettings(Formatter.Settings); - Formatter.Sourcecode := m_PreviewBefore.Lines.Text; - if Formatter.Execute then begin + if Formatter.Execute(st) then begin m_PreviewAfter.Lines.BeginUpdate; - m_PreviewAfter.Lines.Clear; - m_PreviewAfter.Lines.Text := Formatter.Sourcecode; + m_PreviewAfter.Lines.Assign(st); m_PreviewAfter.Lines.EndUpdate; m_PreviewBeforeClick(nil); end; finally Formatter.Free; + st.Free; end; end; Index: GX_eCodeFormatter.pas =================================================================== RCS file: /cvsroot/gexperts/gexperts/unstable/Src/Formatter/GX_eCodeFormatter.pas,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- GX_eCodeFormatter.pas 27 May 2006 14:38:40 -0000 1.1 +++ GX_eCodeFormatter.pas 27 May 2006 17:54:50 -0000 1.2 @@ -1,3 +1,6 @@ +// the code formatter expert as an editor expert +// (only use one of GX_Formatter or GXeCodeFormatter) +// Original Author: Thomas Mueller (http://www.dummzeuch.de) unit GX_eCodeFormatter; {$I GX_CondDefine.inc} @@ -11,34 +14,20 @@ {$ENDIF} SysUtils, Classes, - Dialogs, Menus, - ToolsAPI, - GX_OtaUtils, - GX_GenericUtils, GX_EditorExpert, GX_ConfigurationInfo, - GX_CodeFormatterGXConfigWrapper, - GX_CodeFormatterTypes, - GX_CodeFormatterConfig, - GX_CodeFormatterEngine, - GX_CodeFormatterBookmarks, - GX_CodeFormatterBreakpoints, - GX_CodeFormatterDefaultSettings, - GX_CodeFormatterConfigHandler, - GX_CodeFormatterDone, - GX_CodeFormatterSettings, - GX_CodeFormatterSaveList; + GX_CodeFormatterExpert; type TeCodeFormatterExpert = class(TEditorExpert) private - FFormatter: TCodeFormatterEngine; - procedure ShowDoneDialog(out ShowDone: Boolean); + FExpert: TCodeFormatterExpert; protected function GetDisplayName: string; override; class function GetName: string; override; function GetBitmapFileName: string; override; + procedure GetHelpString(List: TStrings); override; procedure InternalLoadSettings(Settings: TGExpertsSettings); override; procedure InternalSaveSettings(Settings: TGExpertsSettings); override; public @@ -53,97 +42,27 @@ procedure TeCodeFormatterExpert.Configure; begin - TfmCodeFormatterConfig.Execute(FFormatter.Settings); + FExpert.Configure; end; constructor TeCodeFormatterExpert.Create; -var - Settings: TCodeFormatterEngineSettings; begin inherited Create; - Settings := BorlandDefaults; - FFormatter := TCodeFormatterEngine.Create; - FFormatter.Settings.Settings := Settings; + FExpert := TCodeFormatterExpert.Create; ShortCut := Menus.ShortCut(Word('F'), [ssCtrl, ssAlt]); end; destructor TeCodeFormatterExpert.Destroy; begin - FreeAndNil(FFormatter); + FreeAndNil(FExpert); inherited; end; procedure TeCodeFormatterExpert.Execute(Sender: TObject); -resourcestring - str_NoEditor = 'No source editor'; - str_UnsupportedFileTypeS = 'Unsupported file type: %s'; - str_UnableToGetContentsS = 'Unable to get contents of %s'; -var - SourceEditor: IOTASourceEditor; - FileName: string; - FullText: TStringList; - ShowDone: Boolean; - Bookmarks: ICodeFormatterSaveList; - Breakpoints: ICodeFormatterSaveList; - WasBinary: Boolean; - i: integer; - TempSettings: TCodeFormatterSettings; - OrigSettings: TCodeFormatterEngineSettings; - FirstLine: string; begin - SourceEditor := GxOtaGetCurrentSourceEditor; - if not Assigned(SourceEditor) then - raise ECodeFormatter.Create(str_NoEditor); - FileName := SourceEditor.FileName; - if not (IsPascalSourceFile(FileName) or IsDelphiPackage(FileName)) then - raise ECodeFormatter.CreateFmt(str_UnsupportedFileTypeS, [ExtractFileName(FileName)]); - - TempSettings := nil; - FullText := TStringList.Create; - try - if not GxOtaGetFileAsText(Filename, FullText, WasBinary) then - raise ECodeFormatter.CreateFmt(str_UnableToGetContentsS, [FileName]); - if FullText.Count = 0 then - exit; - - Bookmarks := TBookmarkHandler.Create; - Breakpoints := TBreakpointHandler.Create; - - FirstLine := FullText[0]; - if SameText(Copy(FirstLine, 1, 20), '{GXFormatter.config=') then begin - FirstLine := Trim(Copy(FirstLine, 21, Length(FirstLine) - 21)); - TempSettings := TCodeFormatterSettings.Create; - if TCodeFormatterConfigHandler.GetDefaultConfig(FirstLine, TempSettings) then begin - OrigSettings := FFormatter.Settings.Settings; - FFormatter.Settings.Settings := TempSettings.Settings; - end else - FreeAndNil(TempSettings); - end; - - FFormatter.Sourcecode := FullText.Text; - if FFormatter.Execute then begin - if FullText.Text <> FFormatter.Sourcecode then begin - GxOtaReplaceEditorText(SourceEditor, FFormatter.Sourcecode); - for i := 0 to SourceEditor.EditViewCount - 1 do - SourceEditor.EditViews[i].Paint; - end; - if FFormatter.Settings.ShowDoneDialog then begin - ShowDoneDialog(ShowDone); - if Showdone <> FFormatter.Settings.ShowDoneDialog then begin - FFormatter.Settings.ShowDoneDialog := ShowDone; - end; - end; - end; - - finally - FreeAndNil(FullText); - if Assigned(TempSettings) then begin - FreeAndNil(TempSettings); - FFormatter.Settings.Settings := OrigSettings; - end; - end; + FExpert.Execute; end; function TeCodeFormatterExpert.GetBitmapFileName: string; @@ -156,9 +75,22 @@ Result := 'Code Formatter'; end; +procedure TeCodeFormatterExpert.GetHelpString(List: TStrings); +resourcestring + SFormatterHelp = + ' This expert is the source code formatter formerly known as' + + ' DelForEx. It can handle different configuration sets between which' + + ' you can switch using the tools button.' + sLineBreak + + ' To force a configuration set for a particular unit, add' + sLineBreak + sLineBreak + + ' {GXFormatter.config=<name>}' + sLineBreak + sLineBreak + + ' as the first line to the unit.'; +begin + List.Text := SFormatterHelp; +end; + class function TeCodeFormatterExpert.GetName: string; begin - Result := self.ClassName; + Result := 'CodeFormatter'; end; function TeCodeFormatterExpert.HasConfigOptions: Boolean; @@ -167,36 +99,15 @@ end; procedure TeCodeFormatterExpert.InternalLoadSettings(Settings: TGExpertsSettings); -var - Reader: IConfigReader; begin inherited; - - Reader := TGxConfigWrapper.Create(Settings, ConfigurationKey); - TCodeFormatterConfigHandler.ReadSettings(Reader, FFormatter.Settings); + FExpert.InternalLoadSettings(ConfigurationKey, Settings); end; procedure TeCodeFormatterExpert.InternalSaveSettings(Settings: TGExpertsSettings); -var - Writer: IConfigWriter; begin inherited; - - Writer := TGxConfigWrapper.Create(Settings, ConfigurationKey); - TCodeFormatterConfigHandler.WriteSettings(Writer, FFormatter.Settings); -end; - -procedure TeCodeFormatterExpert.ShowDoneDialog(out ShowDone: Boolean); -var - DoneForm: TfmCodeFormatterDone; -begin - DoneForm := TfmCodeFormatterDone.Create(nil); - try - DoneForm.ShowModal; - ShowDone := not DoneForm.chk_DontShowAgain.Checked; - finally - DoneForm.Free; - end; + FExpert.InternalSaveSettings(ConfigurationKey, Settings); end; initialization |