Skip to content

Commit

Permalink
improve active parameter detection
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Oct 14, 2022
1 parent b0360c3 commit df26a96
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 4 additions & 1 deletion decoder/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ func (d *PathDecoder) hoverDataForExpr(expr hcl.Expression, constraints ExprCons
f, ok := d.pathCtx.Functions[e.Name]
if ok {
return &lang.HoverData{
Content: lang.Markdown(fmt.Sprintf("```hcl\n%s(%s) %s\n```\n\n%s", e.Name, f.ParameterSignature(), f.ReturnTypes.FriendlyName(), f.Description)),
// We could use a code block here, but the highlighting of type names
// conflicts with the naming of function parameters
// e.g. a parameter named list
Content: lang.Markdown(fmt.Sprintf("`%s(%s) %s`\n\n%s", e.Name, f.ParameterSignature(), f.ReturnTypes.FriendlyName(), f.Description)),
Range: expr.Range(),
}, nil
}
Expand Down
27 changes: 22 additions & 5 deletions decoder/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,34 @@ func (d *PathDecoder) SignatureAtPos(filename string, pos hcl.Pos) (*lang.FuncSi
return nil // Unknown function
}

// TODO this should be based on `pos` and not the count of parameters
// a new entry is added to `Args` only after a user adds content, like ""
// not after a user types a `,`
// TODO this need to account for variadic arguments (VarParam)
activePar := max(len(fNode.Args)-1, 0)
activePar := 0 // default to first parameter
foundActivePar := false
// TODO check paren rage
for i, v := range fNode.Args {
if v.Range().ContainsPos(pos) {
activePar = i
foundActivePar = true
break
}
}

if !foundActivePar {
// TODO how to account for spaces and commas?
// use f.Bytes
}

paramsLen := len(f.Params)
if f.VarParam != nil {
paramsLen += 1
}

if activePar > paramsLen {
// there are multiple variadic parameters defined, so
// we want to highlight the variadic parameter in the
// function signature
activePar = paramsLen - 1
}

parameters := make([]lang.FuncParameter, 0, paramsLen)
for _, p := range f.Params {
parameters = append(parameters, lang.FuncParameter{
Expand Down

0 comments on commit df26a96

Please sign in to comment.