@@ -5,15 +5,24 @@ import (
5
5
"strings"
6
6
7
7
lib "github.com/nedpals/errgoengine"
8
+ "github.com/nedpals/errgoengine/utils/numbers"
9
+ )
10
+
11
+ type cannotBeAppliedErrorKind int
12
+
13
+ const (
14
+ cannotBeAppliedMismatchedArgType cannotBeAppliedErrorKind = 0
15
+ cannotBeAppliedMismatchedArgCount cannotBeAppliedErrorKind = iota
8
16
)
9
17
10
18
type cannotBeAppliedErrorCtx struct {
11
- rawRequiredTypes []string
12
- rawFoundTypes []string
13
- requiredTypes []lib.Symbol
14
- foundTypes []lib.Symbol
15
- callExprNode lib.SyntaxNode
16
- firstUnmatchedIdx int
19
+ rawRequiredTypes []string
20
+ rawFoundTypes []string
21
+ requiredTypes []lib.Symbol
22
+ foundTypes []lib.Symbol
23
+ callExprNode lib.SyntaxNode
24
+ kind cannotBeAppliedErrorKind
25
+ invalidIdx int
17
26
}
18
27
19
28
var CannotBeAppliedError = lib.ErrorTemplate {
@@ -43,15 +52,22 @@ var CannotBeAppliedError = lib.ErrorTemplate{
43
52
cCtx .foundTypes = append (cCtx .foundTypes , sym )
44
53
}
45
54
46
- // get first unmatched idx
47
- for i := 0 ; i < len (cCtx .requiredTypes ); i ++ {
48
- if i >= len (cCtx .foundTypes ) {
49
- break
50
- }
55
+ // get invalid idx
56
+ if len (cCtx .rawFoundTypes ) > len (cCtx .rawRequiredTypes ) {
57
+ cCtx .kind = cannotBeAppliedMismatchedArgCount
58
+ cCtx .invalidIdx = len (cCtx .rawFoundTypes ) - 1
59
+ } else {
60
+ cCtx .kind = cannotBeAppliedMismatchedArgType
61
+
62
+ for i := 0 ; i < len (cCtx .requiredTypes ); i ++ {
63
+ if i >= len (cCtx .foundTypes ) {
64
+ break
65
+ }
51
66
52
- if cCtx .requiredTypes [i ] != cCtx .foundTypes [i ] {
53
- cCtx .firstUnmatchedIdx = i
54
- break
67
+ if cCtx .requiredTypes [i ] != cCtx .foundTypes [i ] {
68
+ cCtx .invalidIdx = i
69
+ break
70
+ }
55
71
}
56
72
}
57
73
@@ -77,7 +93,7 @@ var CannotBeAppliedError = lib.ErrorTemplate{
77
93
node := lib .WrapNode (m .Document , c .Node )
78
94
fmt .Println (node .Text ())
79
95
cCtx .callExprNode = node
80
- argNode := node .ChildByFieldName ("arguments" ).NamedChild (cCtx .firstUnmatchedIdx )
96
+ argNode := node .ChildByFieldName ("arguments" ).NamedChild (cCtx .invalidIdx )
81
97
m .Nearest = argNode
82
98
return false
83
99
}
@@ -87,18 +103,40 @@ var CannotBeAppliedError = lib.ErrorTemplate{
87
103
m .Context = cCtx
88
104
},
89
105
OnGenExplainFn : func (cd * lib.ContextData , gen * lib.ExplainGenerator ) {
90
- gen .Add ("This error occurs when there is an attempt to apply a method with arguments that do not match the method signature." )
106
+ ctx := cd .MainError .Context .(cannotBeAppliedErrorCtx )
107
+
108
+ switch ctx .kind {
109
+ case cannotBeAppliedMismatchedArgCount :
110
+ gen .Add ("This error occurs when there is an attempt to apply a method with an incorrect number of arguments." )
111
+ case cannotBeAppliedMismatchedArgType :
112
+ gen .Add ("This error occurs when there is an attempt to apply a method with arguments that do not match the method signature." )
113
+ default :
114
+ gen .Add ("unable to determine." )
115
+ }
91
116
},
92
117
OnGenBugFixFn : func (cd * lib.ContextData , gen * lib.BugFixGenerator ) {
93
118
ctx := cd .MainError .Context .(cannotBeAppliedErrorCtx )
94
119
95
- gen .Add ("Use the correct argument types" , func (s * lib.BugFixSuggestion ) {
96
- s .AddStep ("Provide the correct argument types when calling the `%s` method" , cd .Variables ["method" ]).
97
- AddFix (lib.FixSuggestion {
98
- NewText : castValueNode (cd .MainError .Nearest , ctx .requiredTypes [ctx .firstUnmatchedIdx ]),
99
- StartPosition : cd .MainError .Nearest .StartPosition (),
100
- EndPosition : cd .MainError .Nearest .EndPosition (),
101
- })
102
- })
120
+ switch ctx .kind {
121
+ case cannotBeAppliedMismatchedArgCount :
122
+ gen .Add ("Use the correct number of arguments" , func (s * lib.BugFixSuggestion ) {
123
+ s .AddStep ("Modify the `%s` method call to use only %s argument." , cd .Variables ["method" ], numbers .ToWords (len (ctx .rawRequiredTypes ))).
124
+ AddFix (lib.FixSuggestion {
125
+ NewText : "" ,
126
+ StartPosition : cd .MainError .Nearest .PrevSibling ().StartPosition (),
127
+ EndPosition : cd .MainError .Nearest .EndPosition (),
128
+ })
129
+ })
130
+ case cannotBeAppliedMismatchedArgType :
131
+ gen .Add ("Use the correct argument types" , func (s * lib.BugFixSuggestion ) {
132
+ s .AddStep ("Provide the correct argument types when calling the `%s` method" , cd .Variables ["method" ]).
133
+ AddFix (lib.FixSuggestion {
134
+ NewText : castValueNode (cd .MainError .Nearest , ctx .requiredTypes [ctx .invalidIdx ]),
135
+ StartPosition : cd .MainError .Nearest .StartPosition (),
136
+ EndPosition : cd .MainError .Nearest .EndPosition (),
137
+ })
138
+ })
139
+ }
140
+
103
141
},
104
142
}
0 commit comments