Skip to content

Commit

Permalink
Language server refactoring (StyraInc#1349)
Browse files Browse the repository at this point in the history
While there's more to do here, we've got to start somewhere.

- Simplify handlers by extracting shared logic
- Use "global" vars for static slices to avoid needless allocations
- No named return values
- Add GetContentAndModule convenience function to reduce boilerplate
- Various fixes

This should not change behavior of anything, just nake server.go
a little more manageable.

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Jan 20, 2025
1 parent d7f522e commit a5d1742
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 377 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- uses: golangci/golangci-lint-action@ec5d18412c0aeab7936cb16880d708ba2a64e1ae # v6.2.0
if: matrix.os.name == 'linux'
with:
version: v1.63.1
version: v1.63.4
- uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
with:
name: regal-${{ matrix.os.name }}
Expand Down
14 changes: 14 additions & 0 deletions internal/lsp/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func (c *Cache) SetModule(fileURI string, module *ast.Module) {
c.modules.Set(fileURI, module)
}

func (c *Cache) GetContentAndModule(fileURI string) (string, *ast.Module, bool) {
content, ok := c.GetFileContents(fileURI)
if !ok {
return "", nil, false
}

module, ok := c.GetModule(fileURI)
if !ok {
return "", nil, false
}

return content, module, true
}

func (c *Cache) Rename(oldKey, newKey string) {
if content, ok := c.fileContents.Get(oldKey); ok {
c.fileContents.Set(newKey, content)
Expand Down
44 changes: 44 additions & 0 deletions internal/lsp/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package handler

import (
"context"

"github.com/anderseknert/roast/pkg/encoding"
"github.com/sourcegraph/jsonrpc2"
)

type handlerFunc[T any] func(T) (any, error)

type handlerContextFunc[T any] func(context.Context, T) (any, error)

var ErrInvalidParams = &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams}

func Decode[T any](req *jsonrpc2.Request, params *T) error {
if req.Params == nil {
return ErrInvalidParams
}

if err := encoding.JSON().Unmarshal(*req.Params, &params); err != nil {
return &jsonrpc2.Error{Code: jsonrpc2.CodeInvalidParams, Message: err.Error()}
}

return nil
}

func WithParams[T any](req *jsonrpc2.Request, h handlerFunc[T]) (any, error) {
var params T
if err := Decode(req, &params); err != nil {
return nil, err
}

return h(params)
}

func WithContextAndParams[T any](ctx context.Context, req *jsonrpc2.Request, h handlerContextFunc[T]) (any, error) {
var params T
if err := Decode(req, &params); err != nil {
return nil, err
}

return h(ctx, params)
}
Loading

0 comments on commit a5d1742

Please sign in to comment.