Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protocol): Add custom request handling #32

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions protocol_3_16/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package protocol

import (
"encoding/json"

"github.com/tliron/glsp"
)

type CustomRequestHandler struct {
Method string
Func CustomRequestFunc
// This field should be private however it is used in both versions of the protocol
Params json.RawMessage
}

type CustomRequestFunc func(context *glsp.Context, params json.RawMessage) (any, error)
24 changes: 24 additions & 0 deletions protocol_3_16/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ type Handler struct {
TextDocumentLinkedEditingRange TextDocumentLinkedEditingRangeFunc
TextDocumentMoniker TextDocumentMonikerFunc

// Custom Request/Notification
CustomRequest []CustomRequestHandler

initialized bool
lock sync.Mutex
}
Expand Down Expand Up @@ -709,11 +712,32 @@ func (self *Handler) Handle(context *glsp.Context) (r any, validMethod bool, val
r, err = self.TextDocumentMoniker(context, &params)
}
}

default:
if self.CustomRequest != nil {
if handler, ok := self.FindCustomRequestHandler(context.Method); ok {
validMethod = true
if err = json.Unmarshal(context.Params, &handler.Params); err == nil {
validParams = true
r, err = handler.Func(context, handler.Params)
}
}
}

}

return
}

func (self *Handler) FindCustomRequestHandler(method string) (*CustomRequestHandler, bool) {
for _, handler := range self.CustomRequest {
if handler.Method == method {
return &handler, true
}
}
return nil, false
}

func (self *Handler) IsInitialized() bool {
self.lock.Lock()
defer self.lock.Unlock()
Expand Down
20 changes: 20 additions & 0 deletions protocol_3_17/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,30 @@ func (self *Handler) Handle(context *glsp.Context) (r any, validMethod bool, val
r, err = self.TextDocumentDiagnostic(context, &params)
}
}

default:
if self.CustomRequest != nil {
if handler, ok := self.FindCustomRequestHandler(context.Method); ok {
validMethod = true
if err = json.Unmarshal(context.Params, &handler.Params); err == nil {
validParams = true
r, err = handler.Func(context, handler.Params)
}
}
}

}

return
}

func (self *Handler) FindCustomRequestHandler(method string) (*protocol316.CustomRequestHandler, bool) {
for _, handler := range self.CustomRequest {
if handler.Method == method {
return &handler, true
}
}
return nil, false
}

func (self *Handler) IsInitialized() bool {
Expand Down