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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
package search
import (
"pik/model"
"slices"
)
type Result struct {
Target model.Target
Source *model.Source
NeedsConfirmation bool
Overridden bool
Sub []string
Args []string
}
// Search is the meat of pik
func Search(s *model.State, args ...string) *Result {
var target model.Target
var targetSource *model.Source
var confirm bool
var overridden bool
var subdir []string
var forward []string
var suspect model.Target
var suspectSource *model.Source
args_loop:
for _, arg := range args {
for _, src := range s.Sources {
if targetSource == nil {
if src.Is(arg) {
targetSource = src
// try to look for arg target with the same name as the source
// "default target" of sorts
for _, t := range targetSource.Targets {
if t.Matches(arg) {
target = t
continue args_loop
}
}
continue args_loop
}
}
if target == nil && targetSource == nil {
// uncertain about source, check ours to see if any match
for _, t := range src.Targets {
if t.Matches(arg) {
target = t
targetSource = src
continue args_loop
}
}
} else if target == nil { // && targetSource == nil (but it is always true)
// source located,
for _, t := range targetSource.Targets {
if t.Matches(arg) {
target = t
continue args_loop
}
}
// if we find the right target
for _, t := range src.Targets {
if t.Matches(arg) {
confirm = true
suspect = t
suspectSource = src
continue args_loop
}
}
}
}
if target == nil {
subdir = append(subdir, arg)
continue args_loop
} else if targetSource != nil {
forward = append(forward, arg)
continue args_loop
}
}
if suspect != nil && target == nil {
target = suspect
targetSource = suspectSource
confirm = true
}
if target != nil && target.Sub() != nil && subdir != nil && !slices.Equal(target.Sub(), subdir) {
confirm = true
}
if target == nil {
forward = args
}
if target != nil && targetSource != nil {
for _, t := range targetSource.Targets {
if slices.Equal(t.Invocation(targetSource), target.Invocation(targetSource)) {
if t.Tags().Has(model.Override) {
overridden = true
target = t
}
}
}
}
return &Result{
Target: target,
Source: targetSource,
NeedsConfirmation: confirm,
Overridden: overridden,
Sub: subdir,
Args: forward,
}
}
|