summaryrefslogtreecommitdiff
path: root/search/search.go
blob: e79498550613f94046db87c314c70f3fdf03173d (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package search

import (
	"git.ewy.one/pik.git/model"
	"slices"
)

// Result is a struct containing information about the search and search results
type Result struct {
	// Target is the target selected by the search
	Target model.Target
	// Source is the source belonging to the selected Target
	Source *model.Source
	// NeedsConfirmation is true when there are discrepancies between expected and actual invocation
	NeedsConfirmation bool
	// Overridden is whether it was overridden by a .override target
	Overridden bool
	// Sub is the subcategory or -folder
	Sub []string
	// Args are the remaining arguments which we should pass to the target
	Args []string
}

// Search is the meat of pik
// since there are a ton of different ways to invoke targets, leave a unit test
// when you change this
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 i, arg := range args {
		for _, src := range s.Sources {

			if targetSource == nil {
				if src.Is(arg) {
					targetSource = src

					// only try to find the default target if this is the last argument
					if len(args)-1 != i {
						continue args_loop
					}

					// 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) {
						if slices.Equal(t.Sub(), subdir) {
							target = t
							targetSource = src
						} else {
							suspect = t
							suspectSource = 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 && suspect == nil {
			subdir = append(subdir, arg)
			continue args_loop
		} else if targetSource != nil || suspect != nil {
			forward = append(forward, arg)
			continue args_loop
		}
	}

	if suspect != nil && target == nil {
		target = suspect
		targetSource = suspectSource

		if !(suspect.Sub() != nil && subdir == nil) {
			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,
	}
}