-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserFocus.go
More file actions
126 lines (112 loc) · 2.56 KB
/
parserFocus.go
File metadata and controls
126 lines (112 loc) · 2.56 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
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
package parser
import (
"fmt"
"regexp"
)
// FocusPackagePath will tell the parser to look for a
// specific package.
//
// The given string will be directly forwarded to a
// regexp.MatchString call, so it should represent a
// go regular expression.
//
// Note that the packagePath argument refers to the import
// path to the target package, not the package name
func FocusPackagePath(packagePath string) *Focus {
return &Focus{
&packagePath,
nil,
nil,
}
}
// FocusFilePath will tell the parser to look for a
// specific file, based on it's ABSOLUTE path.
//
// The given string will be directly forwarded to a
// regexp.MatchString call, so it should represent a
// go regular expression.
func FocusFilePath(filePath string) *Focus {
return &Focus{
nil,
&filePath,
nil,
}
}
// FocusTypeName will tell the parser to look for a
// specific GO typename.
//
// The given string will be directly forwarded to a
// regexp.MatchString call, so it should represent a
// go regular expression.
func FocusTypeName(typeName string) *Focus {
return &Focus{
nil,
nil,
&typeName,
}
}
func MergeFocus(f1 *Focus, f2 *Focus) *Focus {
f := *f1
if f2.packagePath != nil {
f.packagePath = f2.packagePath
}
if f2.filePath != nil {
f.filePath = f2.filePath
}
if f2.typeName != nil {
f.typeName = f2.typeName
}
return &f
}
// -----
// Focus tells to the parser
// what it needs to focus on
type Focus struct {
// packagePath is the import
// path to focused package.
packagePath *string
// filePath is the file system absolute
// path to the focused file
filePath *string
// typeName is the name of the
// focused GO typename
typeName *string
}
// is is used to check if the parser focus is equal to the given one
func (f *Focus) is(lvl focusLevel, value string) bool {
if f == nil {
// If it's nil, there's no focus
return true
}
// If the focus lvl equivalent is nil, return true
// because the focus is in something else
switch lvl {
case focusPackagePath:
if f.packagePath == nil {
return true
}
b, _ := regexp.MatchString(*f.packagePath, value)
return b
case focusFilePath:
if f.filePath == nil {
return true
}
b, _ := regexp.MatchString(*f.filePath, value)
return b
case focusTypeName:
if f.typeName == nil {
return true
}
b, _ := regexp.MatchString(*f.typeName, value)
return b
default:
panic(fmt.Errorf("unrecognizable focus: %s", lvl))
}
}
type focusLevel string
// focusLevel options
const (
focusPackagePath focusLevel = "packagePath"
focusFilePath focusLevel = "filePath"
focusTypeName focusLevel = "typeName"
)