From ad7c2022b0f193cf19c05b8f926a7ed2041eac95 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 13 Jun 2025 17:32:08 +0200 Subject: [PATCH] IDE: update name of editor-macro in source-editor when renaming in macro list. Issue #41699 --- components/codetools/codecache.pas | 6 ++++- ide/editormacrolistviewer.pas | 43 +++++++++++++++++++++++++----- ide/packages/ideproject/project.pp | 10 +++++-- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/components/codetools/codecache.pas b/components/codetools/codecache.pas index d9aa0724fc..907cfcd645 100644 --- a/components/codetools/codecache.pas +++ b/components/codetools/codecache.pas @@ -957,8 +957,12 @@ end; procedure TCodeCache.OnBufferSetFileName(Sender: TCodeBuffer; const OldFilename: string); +var + OldFile: TCodeBuffer; begin - FItems.Delete(FItems.Find(FindFile(OldFilename))); + OldFile := FindFile(OldFilename); + if OldFile <> nil then + FItems.Delete(FItems.Find(OldFile)); if FindFile(Sender.Filename)=nil then FItems.Add(Sender); end; diff --git a/ide/editormacrolistviewer.pas b/ide/editormacrolistviewer.pas index c176b5536c..2c2a4b42d2 100644 --- a/ide/editormacrolistviewer.pas +++ b/ide/editormacrolistviewer.pas @@ -41,7 +41,7 @@ uses LazIDEIntf, IDEDialogs, // IDE LazarusIDEStrConsts, ProjectDefs, LazConf, Project, KeyMapping, - KeyMapShortCutDlg, MainIntf, ToolBarIntf; + KeyMapShortCutDlg, MainIntf, ToolBarIntf, ProjectIntf; type TSynEditorMacro = class(TSynMacroRecorder) @@ -299,6 +299,8 @@ procedure SaveGlobalInfo; implementation +uses SourceFileManager; + {$R *.lfm} var @@ -349,6 +351,11 @@ begin else if AName = 'Ide' then Result := EditorMacroListGlob; end; +function InternalFilenameForMacro(AnOwnerList: TEditorMacroList; AMacro: TEditorMacro): string; +begin + Result := EditorMacroVirtualDrive+MacroListToName(AnOwnerList)+'|'+AMacro.MacroName; +end; + procedure DoEditorMacroStateChanged; begin if EditorMacroForRecording= nil then exit; @@ -1099,6 +1106,8 @@ procedure TMacroListViewer.btnRenameClick(Sender: TObject); var s: String; M: TEditorMacro; + UnitInfo: TUnitInfo; + i: Integer; begin if lbMacroView.ItemIndex < 0 then exit; M := CurrentEditorMacroList.Macros[lbMacroView.ItemIndex]; @@ -1115,8 +1124,14 @@ begin s := ''; end; end; - if s <> '' then + if s <> '' then begin + UnitInfo := Project1.UnitInfoWithFilename(InternalFilenameForMacro(CurrentEditorMacroList, M), [pfsfOnlyVirtualFiles]); M.MacroName := s; + if UnitInfo <> nil then begin + UnitInfo.Filename := InternalFilenameForMacro(CurrentEditorMacroList, M); + UpdateSourceNames; + end; + end; UpdateDisplay; end; end; @@ -1180,7 +1195,7 @@ begin M := CurrentEditorMacroList.Macros[lbMacroView.ItemIndex]; if M = nil then exit; LazarusIDE.DoOpenEditorFile( - EditorMacroVirtualDrive+MacroListToName(CurrentEditorMacroList)+'|'+M.MacroName, + InternalFilenameForMacro(CurrentEditorMacroList, M), -1, -1, [ofVirtualFile, ofInternalFile]); end; @@ -1231,7 +1246,7 @@ begin CurrentEditorMacroList.Add(M); Assert(not FIsPlaying, 'TMacroListViewer.btnAddEditNewClick: IsPlaying'); LazarusIDE.DoOpenEditorFile( - EditorMacroVirtualDrive+MacroListToName(CurrentEditorMacroList)+'|'+M.MacroName, + InternalFilenameForMacro(CurrentEditorMacroList, M), -1, -1, [ofVirtualFile, ofInternalFile]); UpdateDisplay; @@ -1351,26 +1366,42 @@ end; procedure TMacroListViewer.tbMoveIDEClick(Sender: TObject); var i: Integer; + M: TEditorMacro; + UnitInfo: TUnitInfo; begin if (lbMacroView.ItemIndex < 0) or (CurrentEditorMacroList = EditorMacroListGlob) then exit; i := lbMacroView.ItemIndex; - EditorMacroListGlob.Add(CurrentEditorMacroList.Macros[i]); + M := CurrentEditorMacroList.Macros[i]; + UnitInfo := Project1.UnitInfoWithFilename(InternalFilenameForMacro(CurrentEditorMacroList, M), [pfsfOnlyVirtualFiles]); + EditorMacroListGlob.Add(M); CurrentEditorMacroList.Delete(i); if CurrentEditorMacroList = EditorMacroListProj then Project1.Modified := True; MainIDEInterface.SaveEnvironment(False); + if UnitInfo <> nil then begin + UnitInfo.Filename := InternalFilenameForMacro(EditorMacroListGlob, M); + UpdateSourceNames; + end; UpdateDisplay; end; procedure TMacroListViewer.tbMoveProjectClick(Sender: TObject); var i: Integer; + M: TEditorMacro; + UnitInfo: TUnitInfo; begin if (lbMacroView.ItemIndex < 0) or (CurrentEditorMacroList = EditorMacroListProj) then exit; i := lbMacroView.ItemIndex; - EditorMacroListProj.Add(CurrentEditorMacroList.Macros[i]); + M := CurrentEditorMacroList.Macros[i]; + UnitInfo := Project1.UnitInfoWithFilename(InternalFilenameForMacro(CurrentEditorMacroList, M), [pfsfOnlyVirtualFiles]); + EditorMacroListProj.Add(M); CurrentEditorMacroList.Delete(i); Project1.Modified := True; if CurrentEditorMacroList = EditorMacroListGlob then MainIDEInterface.SaveEnvironment(False); + if UnitInfo <> nil then begin + UnitInfo.Filename := InternalFilenameForMacro(EditorMacroListProj, M); + UpdateSourceNames; + end; UpdateDisplay; end; diff --git a/ide/packages/ideproject/project.pp b/ide/packages/ideproject/project.pp index 18190f2691..ae7754c4f6 100644 --- a/ide/packages/ideproject/project.pp +++ b/ide/packages/ideproject/project.pp @@ -2081,8 +2081,14 @@ end; procedure TUnitInfo.SetFilename(const AValue: string); begin - if fSource<>nil then - LazTracer.RaiseGDBException('TUnitInfo.SetFilename Source<>nil') + if fSource<>nil then begin + if InternalFile then begin + fSource.Filename := AValue; + fFileName:=AValue + end + else + LazTracer.RaiseGDBException('TUnitInfo.SetFilename Source<>nil'); + end else SetInternalFilename(AValue); end;