-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.go
More file actions
404 lines (368 loc) · 12.9 KB
/
hover.go
File metadata and controls
404 lines (368 loc) · 12.9 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package lsp
import (
"fmt"
"sort"
"strings"
"github.com/GoCodeAlone/workflow/schema"
protocol "github.com/tliron/glsp/protocol_3_16"
)
// Hover returns markdown hover content for the given position context, or nil
// if there is nothing to show.
func Hover(reg *Registry, doc *Document, ctx PositionContext) *protocol.Hover {
if ctx.InTemplate {
return hoverTemplateExpr(reg, doc, ctx)
}
switch ctx.Section {
case SectionModules:
if ctx.FieldName == "type" && ctx.ModuleType != "" {
return hoverModuleType(reg, ctx.ModuleType)
}
if ctx.ModuleType != "" && ctx.FieldName != "" {
return hoverConfigField(reg, ctx.ModuleType, ctx.FieldName)
}
if ctx.ModuleType != "" {
return hoverModuleType(reg, ctx.ModuleType)
}
case SectionTriggers:
if ctx.FieldName != "" {
return hoverTriggerType(reg, ctx.FieldName)
}
case SectionPipeline:
if ctx.FieldName == "type" && ctx.StepType != "" {
return hoverStepType(reg, ctx.StepType)
}
if ctx.StepType != "" && ctx.FieldName != "" {
return hoverStepConfigField(reg, ctx.StepType, ctx.FieldName)
}
if ctx.StepType != "" {
return hoverStepType(reg, ctx.StepType)
}
}
return nil
}
// hoverModuleType generates hover markdown for a module type.
func hoverModuleType(reg *Registry, moduleType string) *protocol.Hover {
info, ok := reg.ModuleTypes[moduleType]
if !ok {
return nil
}
var sb strings.Builder
sb.WriteString("**")
sb.WriteString(moduleType)
sb.WriteString("**")
if info.Label != "" && info.Label != moduleType {
sb.WriteString(" — ")
sb.WriteString(info.Label)
}
sb.WriteString("\n\n")
if info.Description != "" {
sb.WriteString(info.Description)
sb.WriteString("\n\n")
}
if info.Category != "" {
fmt.Fprintf(&sb, "**Category:** %s\n\n", info.Category)
}
if len(info.ConfigKeys) > 0 {
sb.WriteString("**Config keys:** `")
sb.WriteString(strings.Join(info.ConfigKeys, "`, `"))
sb.WriteString("`\n")
}
return markdownHover(sb.String())
}
// hoverConfigField generates hover markdown for a module config field.
func hoverConfigField(reg *Registry, moduleType, field string) *protocol.Hover {
info, ok := reg.ModuleTypes[moduleType]
if !ok {
return nil
}
for _, k := range info.ConfigKeys {
if k == field {
return markdownHover(fmt.Sprintf("**%s** — config key for `%s`", field, moduleType))
}
}
return nil
}
// hoverTriggerType generates hover markdown for a trigger type.
func hoverTriggerType(reg *Registry, triggerType string) *protocol.Hover {
info, ok := reg.TriggerTypes[triggerType]
if !ok {
return nil
}
return markdownHover(fmt.Sprintf("**%s** trigger\n\n%s", info.Type, info.Description))
}
// hoverStepType generates hover markdown for a pipeline step type.
func hoverStepType(reg *Registry, stepType string) *protocol.Hover {
info, ok := reg.StepTypes[stepType]
if !ok {
return nil
}
var sb strings.Builder
sb.WriteString("**")
sb.WriteString(stepType)
sb.WriteString("**\n\n")
if info.Description != "" {
sb.WriteString(info.Description)
sb.WriteString("\n")
}
if len(info.ConfigDefs) > 0 {
sb.WriteString("\n**Config:**\n")
for i := range info.ConfigDefs {
cf := &info.ConfigDefs[i]
req := ""
if cf.Required {
req = " *(required)*"
}
fmt.Fprintf(&sb, "- `%s` (%s): %s%s\n", cf.Key, cf.Type, cf.Description, req)
}
}
if len(info.Outputs) > 0 {
sb.WriteString("\n**Outputs:**\n")
for _, o := range info.Outputs {
fmt.Fprintf(&sb, "- `%s` (%s): %s\n", o.Key, o.Type, o.Description)
}
}
return markdownHover(sb.String())
}
// hoverStepConfigField generates hover markdown for a step config field.
func hoverStepConfigField(reg *Registry, stepType, field string) *protocol.Hover {
info, ok := reg.StepTypes[stepType]
if !ok {
return nil
}
for i := range info.ConfigDefs {
cf := &info.ConfigDefs[i]
if cf.Key == field {
var sb strings.Builder
fmt.Fprintf(&sb, "**%s** — config key for `%s`\n\n", field, stepType)
fmt.Fprintf(&sb, "**Type:** %s\n\n", cf.Type)
if cf.Description != "" {
sb.WriteString(cf.Description)
sb.WriteString("\n")
}
if cf.Required {
sb.WriteString("\n*Required*\n")
}
if cf.DefaultValue != nil {
fmt.Fprintf(&sb, "\n**Default:** `%v`\n", cf.DefaultValue)
}
if len(cf.Options) > 0 {
sb.WriteString("\n**Options:** `")
sb.WriteString(strings.Join(cf.Options, "`, `"))
sb.WriteString("`\n")
}
return markdownHover(sb.String())
}
}
// Fallback for config keys without rich metadata.
for _, k := range info.ConfigKeys {
if k == field {
return markdownHover(fmt.Sprintf("**%s** — config key for `%s`", field, stepType))
}
}
return nil
}
// hoverTemplateExpr provides hover documentation for template expressions.
func hoverTemplateExpr(reg *Registry, doc *Document, ctx PositionContext) *protocol.Hover {
tp := ctx.TemplatePath
if tp == nil {
return hoverTemplateFunction(ctx.FieldName)
}
// Non-dot expression with no namespace — check if it's a function name.
if tp.Namespace == "" && tp.Raw != "" && tp.Raw != "." {
return hoverTemplateFunction(tp.Raw)
}
switch tp.Namespace {
case "steps":
return hoverTemplateStepOutput(reg, doc, ctx, tp)
case "trigger":
return hoverTemplateTrigger(ctx, tp)
case "body":
return hoverTemplateBody(tp)
case "meta":
return hoverTemplateMeta(tp)
case "":
return hoverTemplateNamespaces()
}
return nil
}
// hoverTemplateStepOutput shows docs for .steps.stepName.field.
func hoverTemplateStepOutput(reg *Registry, doc *Document, ctx PositionContext, tp *TemplateExprPath) *protocol.Hover {
if tp.StepName == "" {
return markdownHover("**Steps namespace**\n\nAccess step outputs via `.steps.<step-name>.<field>`")
}
if doc == nil {
return markdownHover(fmt.Sprintf("**Step:** `%s`\n\nStep output data.", tp.StepName))
}
pctx := BuildPipelineContext(reg, doc, ctx.Line)
stepCtx := pctx.Steps[tp.StepName]
if stepCtx == nil {
return markdownHover(fmt.Sprintf("**Step:** `%s`\n\nNo output info available (step may not precede current position).", tp.StepName))
}
// Show a specific field if one is targeted.
fieldName := tp.FieldPrefix
if fieldName == "" {
fieldName = tp.SubField
}
if fieldName != "" {
for _, f := range stepCtx.Fields {
if f.Key == fieldName {
var sb strings.Builder
fmt.Fprintf(&sb, "**`.steps.%s.%s`** (`%s`)\n\n", tp.StepName, fieldName, f.Type)
if f.Description != "" {
sb.WriteString(f.Description)
sb.WriteString("\n")
}
fmt.Fprintf(&sb, "\n*Step type:* `%s`", stepCtx.StepType)
return markdownHover(sb.String())
}
}
}
// Show all outputs for the step.
var sb strings.Builder
fmt.Fprintf(&sb, "**Step outputs:** `%s` (`%s`)\n\n", tp.StepName, stepCtx.StepType)
if len(stepCtx.Fields) == 0 {
sb.WriteString("No outputs defined.\n")
} else {
sb.WriteString("**Available fields:**\n\n")
fields := make([]schema.InferredOutput, len(stepCtx.Fields))
copy(fields, stepCtx.Fields)
sort.Slice(fields, func(i, j int) bool { return fields[i].Key < fields[j].Key })
for _, f := range fields {
if f.Description != "" {
fmt.Fprintf(&sb, "- `%s` (%s): %s\n", f.Key, f.Type, f.Description)
} else {
fmt.Fprintf(&sb, "- `%s` (%s)\n", f.Key, f.Type)
}
}
}
return markdownHover(sb.String())
}
// hoverTemplateTrigger shows docs for .trigger.* expressions.
func hoverTemplateTrigger(ctx PositionContext, tp *TemplateExprPath) *protocol.Hover {
var sb strings.Builder
sb.WriteString("**Trigger context** — `.trigger.*`\n\n")
if tp.SubField != "" {
fmt.Fprintf(&sb, "**Sub-namespace:** `.trigger.%s`\n\n", tp.SubField)
switch tp.SubField {
case "path_params":
sb.WriteString("URL path parameters (e.g. `/users/{id}` → `.trigger.path_params.id`)\n")
case "query":
sb.WriteString("URL query parameters (e.g. `?page=1` → `.trigger.query.page`)\n")
case "headers":
sb.WriteString("HTTP request headers (e.g. `.trigger.headers.Authorization`)\n")
case "body":
sb.WriteString("Request body fields (accessible via `.trigger.body.<field>`)\n")
}
} else {
sb.WriteString("**Available sub-namespaces:**\n\n")
sb.WriteString("- `.trigger.path_params` — URL path parameters\n")
sb.WriteString("- `.trigger.query` — Query string parameters\n")
sb.WriteString("- `.trigger.headers` — HTTP headers\n")
sb.WriteString("- `.trigger.body` — Request body fields\n")
}
// Suppress unused parameter warning.
_ = ctx
return markdownHover(sb.String())
}
// hoverTemplateBody shows docs for .body.* expressions.
func hoverTemplateBody(tp *TemplateExprPath) *protocol.Hover {
var sb strings.Builder
sb.WriteString("**Body context** — `.body.*`\n\n")
switch {
case tp.SubField != "":
fmt.Fprintf(&sb, "**Field:** `.body.%s`\n\nNested field within the request body.\n", tp.SubField)
case tp.FieldPrefix != "":
fmt.Fprintf(&sb, "**Field:** `.body.%s` — request body field\n", tp.FieldPrefix)
default:
sb.WriteString("Request body data. Access individual fields via `.body.<field-name>`.\n")
}
return markdownHover(sb.String())
}
// hoverTemplateMeta shows docs for .meta.* expressions.
func hoverTemplateMeta(tp *TemplateExprPath) *protocol.Hover {
var sb strings.Builder
sb.WriteString("**Meta context** — `.meta.*`\n\n")
fieldName := tp.FieldPrefix
if fieldName == "" {
fieldName = tp.SubField
}
metaDocs := map[string]string{
"pipeline_name": "Name of the currently executing pipeline.",
"trigger_type": "Type of trigger that started this pipeline (e.g. 'http').",
"timestamp": "ISO 8601 timestamp of when the pipeline was triggered.",
}
if fieldName != "" {
if doc, ok := metaDocs[fieldName]; ok {
fmt.Fprintf(&sb, "**`.meta.%s`**\n\n%s\n", fieldName, doc)
} else {
fmt.Fprintf(&sb, "**`.meta.%s`** — pipeline metadata field\n", fieldName)
}
} else {
sb.WriteString("**Available fields:**\n\n")
sb.WriteString("- `.meta.pipeline_name` — Name of the current pipeline\n")
sb.WriteString("- `.meta.trigger_type` — Trigger type that started this pipeline\n")
sb.WriteString("- `.meta.timestamp` — Pipeline start timestamp\n")
}
return markdownHover(sb.String())
}
// hoverTemplateNamespaces shows the top-level template namespaces.
func hoverTemplateNamespaces() *protocol.Hover {
md := "**Template context namespaces**\n\n" +
"- `.steps.<name>.<field>` — Output fields from a completed pipeline step\n" +
"- `.trigger.path_params.*` — URL path parameters from the trigger\n" +
"- `.trigger.query.*` — Query string parameters from the trigger\n" +
"- `.trigger.headers.*` — HTTP headers from the trigger\n" +
"- `.trigger.body.*` — Request body fields from the trigger\n" +
"- `.body.*` — Request body shorthand\n" +
"- `.meta.*` — Pipeline metadata (name, trigger_type, timestamp)\n"
return markdownHover(md)
}
// hoverTemplateFunction generates hover for template function names.
func hoverTemplateFunction(name string) *protocol.Hover {
docs := map[string]string{
"uuidv4": "Generates a new UUID v4 string.",
"uuid": "Generates a new UUID v4 string (alias for uuidv4).",
"now": "Returns the current UTC time. Accepts an optional Go time layout string.",
"lower": "Converts a string to lower case.",
"upper": "Converts a string to upper case.",
"title": "Converts a string to title case.",
"default": "Returns the fallback value if the primary value is empty or nil.",
"trimPrefix": "Removes the given prefix from a string if present.",
"trimSuffix": "Removes the given suffix from a string if present.",
"json": "Marshals a value to a JSON string.",
"step": "Accesses step output by step name and optional nested keys.",
"trigger": "Accesses trigger data by nested keys.",
"replace": "Replaces occurrences of a substring. Usage: replace old new str",
"contains": "Tests whether a string contains a substring.",
"hasPrefix": "Tests whether a string starts with a prefix.",
"hasSuffix": "Tests whether a string ends with a suffix.",
"split": "Splits a string by a separator into a list.",
"join": "Joins a list of strings with a separator.",
"trimSpace": "Removes leading and trailing whitespace.",
"urlEncode": "URL-encodes a string.",
"add": "Adds two numbers.",
"sub": "Subtracts the second number from the first.",
"mul": "Multiplies two numbers.",
"div": "Divides the first number by the second.",
"toInt": "Converts a value to an integer.",
"toFloat": "Converts a value to a float.",
"toString": "Converts a value to a string.",
"length": "Returns the length of a string, array, or map.",
"coalesce": "Returns the first non-empty value from arguments.",
"config": "Reads a value from the config provider by key.",
}
doc, ok := docs[name]
if !ok {
return nil
}
return markdownHover(fmt.Sprintf("**%s** — %s", name, doc))
}
// markdownHover wraps a markdown string in a Hover response.
func markdownHover(md string) *protocol.Hover {
return &protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: md,
},
}
}