diff --git a/components/codetools/finddeclarationtool.pas b/components/codetools/finddeclarationtool.pas index f9909e7843..d1ecba8b56 100644 --- a/components/codetools/finddeclarationtool.pas +++ b/components/codetools/finddeclarationtool.pas @@ -7375,7 +7375,7 @@ var ExprP:=PChar(Expr); for i:=0 to length(UseNames)-1 do begin UseNameP:=PChar(UseNames[i]); - if CompareIdentifiers(UseNameP,ExprP)=0 then begin + if CompareDottedIdentifiers(UseNameP,ExprP)=0 then begin DotCount:=GetDotCountInIdentifier(UseNameP); if DotCount>BestDotCount then begin BestDotCount:=DotCount; diff --git a/components/codetools/tests/testrefactoring.pas b/components/codetools/tests/testrefactoring.pas index dabcfa8096..7d0f091d86 100644 --- a/components/codetools/tests/testrefactoring.pas +++ b/components/codetools/tests/testrefactoring.pas @@ -73,6 +73,7 @@ type procedure TestRenameUsedUnit_Impl; procedure TestRenameUsedUnit_FN_KeepShort; procedure TestRenameUsedUnit_InFilename; + procedure TestRenameUsedUnit_LongestUnitnameWins; end; implementation @@ -1656,6 +1657,64 @@ begin end; end; +procedure TTestRefactoring.TestRenameUsedUnit_LongestUnitnameWins; +var + RedUnit, RedGreenUnit, RedGreenBlueUnit: TCodeBuffer; +begin + RedUnit:=CodeToolBoss.CreateFile('red.pas'); + RedGreenUnit:=CodeToolBoss.CreateFile('red.green.pas'); + RedGreenBlueUnit:=CodeToolBoss.CreateFile('red.green.blue.pas'); + try + RedUnit.Source:='unit Red;'+LineEnding + +'interface'+LineEnding + +'var'+LineEnding + +' Red, Green: word;'+LineEnding + +'implementation'+LineEnding + +'end.'; + + RedGreenUnit.Source:='unit Red.Green;'+LineEnding + +'interface'+LineEnding + +'var'+LineEnding + +' Green, Blue: word;'+LineEnding + +'implementation'+LineEnding + +'end.'; + + RedGreenBlueUnit.Source:='unit Red.Green.Blue;'+LineEnding + +'interface'+LineEnding + +'var'+LineEnding + +' Blue: word;'+LineEnding + +'implementation'+LineEnding + +'end.'; + + Add([ + 'program test1;', + '{$mode objfpc}{$H+}', + 'uses Red, Red.Green, Red.Green.Blue;', + 'begin', + ' red.red:=1;', + ' red.green.green:=2;', + ' red.green.blue.blue:=3;', + 'end.', + '']); + RenameUsedUnitRefs(RedGreenUnit,'&End','end.pas'); + CheckDiff(Code,[ + 'program test1;', + '{$mode objfpc}{$H+}', + 'uses Red, &End, Red.Green.Blue;', + 'begin', + ' red.red:=1;', + ' &End.green:=2;', + ' red.green.blue.blue:=3;', + 'end.', + '']); + + finally + RedUnit.IsDeleted:=true; + RedGreenUnit.IsDeleted:=true; + RedGreenBlueUnit.IsDeleted:=true; + end; +end; + initialization RegisterTests([TTestRefactoring]); end.