Skip to content

Commit

Permalink
internal/lsp: fix CompatibleCodeActions
Browse files Browse the repository at this point in the history
CompatibleCodeActions was not expecting map[string]any,
but that's the type passed by the call in acmelsp.CodeActionAndFormat,
because InitializeResult.Capabilities is in fact a map[string]any.

The result was that CodeActionAndFormat never thought it could
use source.organizeImports, so Put was not adding new Go imports.

Now Put can add imports (again?).
  • Loading branch information
rsc committed Apr 5, 2024
1 parent b7279d8 commit 016b2d6
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions internal/lsp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,45 @@ func ServerProvidesCodeAction(cap *protocol.ServerCapabilities, kind protocol.Co
}

func CompatibleCodeActions(cap *protocol.ServerCapabilities, kinds []protocol.CodeActionKind) []protocol.CodeActionKind {
var allowed []protocol.CodeActionKind
switch ap := cap.CodeActionProvider.(type) {
default:
log.Printf("CompatibleCodeActions: unexpected CodeActionProvider type %T", ap)
case bool:
if ap {
return kinds
allowed = kinds
}
return nil
case protocol.CodeActionOptions:
var compat []protocol.CodeActionKind
for _, k := range kinds {
found := false
for _, kk := range ap.CodeActionKinds {
if k == kk {
found = true
break
}
allowed = ap.CodeActionKinds
case map[string]any:
as, ok := ap["codeActionKinds"].([]any)
if !ok {
log.Printf("codeActionKinds is %T", ap["codeActionKinds"])
break
}
for i, a := range as {
b, ok := a.(string)
if !ok {
log.Printf("codeActionKinds[%d] is %T", i, b)
}
if found {
allowed = append(allowed, protocol.CodeActionKind(b))
}
}

var compat []protocol.CodeActionKind
Kinds:
for _, k := range kinds {
for _, allow := range allowed {
if k == allow {
compat = append(compat, k)
} else {
log.Printf("code action %v is not compatible with server", k)
continue Kinds
}
}
return compat
log.Printf("code action %v is not compatible with server kinds %v", k, allowed)
}
return nil
return compat

}

func LocationLink(l *protocol.Location, basedir string) string {
Expand Down

0 comments on commit 016b2d6

Please sign in to comment.