Move functions from IDEProcs to LazFileUtils. Remove unused function MiniMizeName.

git-svn-id: trunk@62444 -
This commit is contained in:
juha 2019-12-25 11:26:25 +00:00
parent 7d87865122
commit d0ca74aa06
4 changed files with 49 additions and 117 deletions

View File

@ -68,6 +68,9 @@ function TryCreateRelativePath(const Dest, Source: String; UsePointDirectory: bo
function CreateRelativePath(const Filename, BaseDirectory: string;
UsePointDirectory: boolean = false; AlwaysRequireSharedBaseFolder: Boolean = True): string;
function FileIsInPath(const Filename, Path: string): boolean;
function PathIsInPath(const Path, Directory: string): boolean;
// Storten a file name for display.
function ShortDisplayFilename(const aFileName: string; aLimit: Integer = 80): string;
type
TPathDelimSwitch = (
@ -735,10 +738,7 @@ var
ExpPath: String;
l: integer;
begin
if Path='' then begin
Result:=false;
exit;
end;
if Path='' then exit(false);
ExpFile:=ResolveDots(Filename);
ExpPath:=AppendPathDelim(ResolveDots(Path));
l:=length(ExpPath);
@ -746,6 +746,48 @@ begin
and (CompareFilenames(ExpPath,LeftStr(ExpFile,l))=0);
end;
function PathIsInPath(const Path, Directory: string): boolean;
// Note: Under Windows this treats C: as C:\
var
ExpPath: String;
ExpDir: String;
l: integer;
begin
if Path='' then exit(false);
ExpPath:=AppendPathDelim(ResolveDots(Path));
ExpDir:=AppendPathDelim(ResolveDots(Directory));
l:=length(ExpDir);
Result:=(l>0) and (length(ExpPath)>=l) and (ExpPath[l]=PathDelim)
and (CompareFilenames(ExpDir,LeftStr(ExpPath,l))=0);
end;
function ShortDisplayFilename(const aFileName: string; aLimit: Integer): string;
// Shorten a long filename for display.
// Add '...' after the 2. path delimiter, then the end part of filename.
var
StartLen, EndLen, SepCnt: Integer;
begin
if Length(aFileName) > aLimit then
begin
StartLen := 1;
SepCnt := 0;
while StartLen < Length(aFileName) - (aLimit div 2) do
begin
if aFileName[StartLen] in AllowDirectorySeparators then
begin
Inc(SepCnt);
if SepCnt = 2 then Break;
end;
Inc(StartLen);
end;
EndLen := aLimit - StartLen - 3;
Result := Copy(aFileName, 1, StartLen) + '...'
+ Copy(aFileName, Length(aFileName)-EndLen+1, EndLen);
end
else
Result := aFileName;
end;
// Path delimiters

View File

@ -60,8 +60,6 @@ function CreateEmptyFile(const Filename: string): boolean;
// file names
function FilenameIsPascalSource(const Filename: string): boolean;
function ChompEndNumber(const s: string): string;
function ShortDisplayFilename(const aFileName: string): string;
function PathIsInPath(const Path, Directory: string): boolean;
// find file
function FindFilesCaseInsensitive(const Directory,
@ -719,50 +717,6 @@ begin
Result:=copy(Result,1,NewLen);
end;
function ShortDisplayFilename(const aFileName: string): string;
// Shorten a long filename for display.
// Add '...' after the 2. path delimiter, then the end part of filename.
const
Limit = 80;
var
StartLen, EndLen, SepCnt: Integer;
begin
if Length(aFileName) > Limit then
begin
StartLen := 1;
SepCnt := 0;
while StartLen < Length(aFileName) - (Limit div 2) do
begin
if aFileName[StartLen] in AllowDirectorySeparators then
begin
Inc(SepCnt);
if SepCnt = 2 then Break;
end;
Inc(StartLen);
end;
EndLen := Limit - StartLen - 3;
Result := Copy(aFileName, 1, StartLen) + '...'
+ Copy(aFileName, Length(aFileName)-EndLen+1, EndLen);
end
else
Result := aFileName;
end;
function PathIsInPath(const Path, Directory: string): boolean;
// Note: Under Windows this treats C: as C:\
var
ExpPath: String;
ExpDir: String;
l: integer;
begin
if Path='' then exit(false);
ExpPath:=AppendPathDelim(ResolveDots(Path));
ExpDir:=AppendPathDelim(ResolveDots(Directory));
l:=length(ExpDir);
Result:=(l>0) and (length(ExpPath)>=l) and (ExpPath[l]=PathDelim)
and (CompareFilenames(ExpDir,LeftStr(ExpPath,l))=0);
end;
function FindFirstFileWithExt(const Directory, Ext: string): string;
var
FileInfo: TSearchRec;

View File

@ -70,10 +70,9 @@ uses
IDECommands, IDEWindowIntf, ProjectIntf, ToolBarIntf, ObjectInspector,
PropEdits, IDEDialogs, IDEUtils, EditorSyntaxHighlighterDef,
// IDE
LazConf, LazarusIDEStrConsts, Project, BuildManager, IDEProcs,
EnvironmentOpts, EditorOptions, CompilerOptions, SourceEditor, SourceSynEditor,
FindInFilesDlg, DesktopManager, Splash, MainBar, MainIntf, Designer, Debugger,
RunParamsOpts;
LazConf, LazarusIDEStrConsts, Project, BuildManager, EnvironmentOpts,
EditorOptions, CompilerOptions, SourceEditor, SourceSynEditor, FindInFilesDlg,
DesktopManager, Splash, MainBar, MainIntf, Designer, Debugger, RunParamsOpts;
type
TResetToolFlag = (

View File

@ -222,74 +222,11 @@ Type
property OnUTF8KeyPress;
end;
function MiniMizeName(FileName: String; Canvas: TCanvas; MaxWidth: Integer): String;
procedure Register;
implementation
function MiniMizeName(FileName: String; Canvas: TCanvas; MaxWidth: Integer): String;
{
This function will return a shortened version of FileName, so that it fits
on the given Canvas, with a given MaxWidth.
eg. C:\Documents and Settings\User\Application Data\Microsoft\Word\custom.dic
would become something like: C:\...\Word\custom.dic
}
procedure RemoveFirstDir(var Dir: String);
{
This procedure will remove the first directory from Dir
and will set ADelim to the Delimiter that separated the first Dir
eg. In: Dir: 'Dir1\Dir2\Dir3'
}
var p: Integer;
begin
p:= Pos(PathDelim,Dir);
if (p > 0) then
begin
Dir := Copy(Dir,p+1,Length(Dir)-p);
end;
end;
var Drive, Dir, Fn: String;
ComposedName: String;
TWidth: Integer;
begin
Result := FileName;
//if FileName does not contain any (sub)dir then return FileName
if Pos(PathDelim, FileName) = 0 then Exit;
//if FileName fits, no need to do anyhing
if Canvas.TextWidth(FileName) <= MaxWidth then Exit;
Drive := ExtractFileDrive(FileName);
Fn := ExtractFileName(FileName);
Dir := ExtractFilePath(FileName);
//Remove Drive from Dir
if (Length(Drive) > 0) then System.Delete(Dir, 1, Length(Drive));
//Transfer all PathDelimiters at the start of Dir to Drive
While (Length(Dir) > 0) and (Dir[1] in ['/','\']) do
begin
Drive := Drive + Dir[1];
System.Delete(Dir,1,1);
end;
//if Dir is empty then we cannot shorten it,
//and we know at this point that Drive+FileName is too long, so we return only filename
if (Length(Dir) = 0) then
begin
Result := Fn;
Exit;
end;
repeat
//at this point we know that Dir ends with PathDelim (otherwise we exited before this point,
//so RemoveFirstDir will return a truncated Dir or an empty string
RemoveFirstDir(Dir);
ComposedName := Drive+'...'+PathDelim+Dir+Fn;
TWidth := Canvas.TextWidth(ComposedName);
until (Length(Dir) = 0) or (TWidth <= MaxWidth);
if (TWidth <= MaxWidth) then Result := ComposedName else Result := Fn;
end;
{ TCustomFileListBox }
procedure TCustomFileListBox.UpdateFileList;