Skip to content

Commit

Permalink
feat(lsp): add initialization options about codelenses (#1176)
Browse files Browse the repository at this point in the history
* feat(lsp): add initialization options about codelenses

- `enableDebugCodelens`: to enable/disable debug codelens
- `evalCodelensDisplayInline`: to select result output methods of evaluate codelens

Signed-off-by: Rintaro Okamura <[email protected]>

* Address linter issue, explain init options

Signed-off-by: Charlie Egan <[email protected]>

---------

Signed-off-by: Rintaro Okamura <[email protected]>
Signed-off-by: Charlie Egan <[email protected]>
Co-authored-by: Charlie Egan <[email protected]>
  • Loading branch information
rinx and charlieegan3 authored Oct 7, 2024
1 parent ed287dc commit 9503967
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
30 changes: 22 additions & 8 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,6 @@ func (l *LanguageServer) StartCommandWorker(ctx context.Context) { // nolint:mai
// handle this ourselves as it's a rename and not a content edit
fixed = false
case "regal.debug":
if l.clientIdentifier != clients.IdentifierVSCode {
l.logError(errors.New("regal.debug command is only supported in VSCode"))

break
}

if len(params.Arguments) != 3 {
l.logError(fmt.Errorf("expected three arguments, got %d", len(params.Arguments)))

Expand Down Expand Up @@ -854,7 +848,8 @@ func (l *LanguageServer) StartCommandWorker(ctx context.Context) { // nolint:mai
target = strings.TrimPrefix(path, currentModule.Package.Path.String()+".")
}

if l.clientIdentifier == clients.IdentifierVSCode {
if l.clientInitializationOptions.EvalCodelensDisplayInline != nil &&
*l.clientInitializationOptions.EvalCodelensDisplayInline {
responseParams := map[string]any{
"result": result,
"line": line,
Expand Down Expand Up @@ -1654,7 +1649,26 @@ func (l *LanguageServer) handleTextDocumentCodeLens(
return nil, nil // return a null response, as per the spec
}

return rego.CodeLenses(ctx, params.TextDocument.URI, contents, module) //nolint:wrapcheck
lenses, err := rego.CodeLenses(ctx, params.TextDocument.URI, contents, module)
if err != nil {
return nil, fmt.Errorf("failed to get code lenses: %w", err)
}

if l.clientInitializationOptions.EnableDebugCodelens != nil &&
*l.clientInitializationOptions.EnableDebugCodelens {
return lenses, nil
}

// filter out `regal.debug` codelens
filteredLenses := make([]types.CodeLens, 0, len(lenses))

for _, lens := range lenses {
if lens.Command.Command != "regal.debug" {
lenses = append(lenses, lens)
}
}

return filteredLenses, nil
}

func (l *LanguageServer) handleTextDocumentCompletion(
Expand Down
8 changes: 8 additions & 0 deletions internal/lsp/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ type FileEvent struct {
}

type InitializationOptions struct {
// Formatter specifies the formatter to use. Options: 'opa fmt' (default),
// 'opa fmt --rego-v1' or 'regal fix'.
Formatter *string `json:"formatter,omitempty"`
// EnableDebugCodelens, if set, will enable debug codelens
// when clients request code lenses for a file.
EnableDebugCodelens *bool `json:"enableDebugCodelens,omitempty"`
// EvalCodelensDisplayInline, if set, will show evaluation results natively
// in the calling editor, rather than in an output file.
EvalCodelensDisplayInline *bool `json:"evalCodelensDisplayInline,omitempty"`
}

type InitializeParams struct {
Expand Down

0 comments on commit 9503967

Please sign in to comment.