Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGDK.ToolsAPI.ProjectMatch.pas
More file actions
Latest commit
59 lines (45 loc) · 1.7 KB
/
Copy pathGDK.ToolsAPI.ProjectMatch.pas
File metadata and controls
59 lines (45 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
unit GDK.ToolsAPI.ProjectMatch;
// Pure project-selection logic, kept free of any ToolsAPI dependency so it can
// be unit-tested. TToolsApiHelper.FindProject feeds it the project-group file
// names and a user-supplied query (a project name, base name or path fragment).
interface
type
TProjectMatch = record
// Returns the index in FileNames of the project that best matches Query, or
// -1 when nothing matches or a substring match is ambiguous. Precedence:
// exact full path, then exact base name (no extension), then a unique
// case-insensitive substring of the file name. All comparisons ignore case.
classfunctionIndexOf(const FileNames: TArray<string>; const Query: string): Integer; static;
end;
implementation
uses
System.SysUtils,
System.IOUtils;
classfunctionTProjectMatch.IndexOf(const FileNames: TArray<string>; const Query: string): Integer;
begin
Result := -1;
const Trimmed = Query.Trim;
if Trimmed = ''then
Exit;
const NormQuery = Trimmed.ToLower;
const QueryBase = TPath.GetFileNameWithoutExtension(Trimmed).ToLower;
// 1. Exact full-path match.
forvar Index := 0to High(FileNames) do
if FileNames[Index].ToLower = NormQuery then
Exit(Index);
// 2. Exact project name (base file name without extension).
forvar Index := 0to High(FileNames) do
if TPath.GetFileNameWithoutExtension(FileNames[Index]).ToLower = QueryBase then
Exit(Index);
// 3. Unique case-insensitive substring of the file name.
var Match := -1;
forvar Index := 0to High(FileNames) do
if FileNames[Index].ToLower.Contains(NormQuery) then
begin
if Match <> -1then
Exit(-1);
Match := Index;
end;
Result := Match;
end;
end.