glsp icon indicating copy to clipboard operation
glsp copied to clipboard

feat(protocol): Add custom request handling

Open tris203 opened this issue 1 year ago • 2 comments

This commit introduces the ability to handle custom requests in the protocol package. A new struct, CustomRequestHandler, has been added which contains a Method and a Func. The Func is a function that takes a context and params and returns an error.

The Handler struct now includes a slice of CustomRequestHandlers, and the Handle method has been updated to handle custom requests by unmarshalling the params into the handler's params and then calling the handler's Func.

A new method, FindCustomRequestHandler, has been added to the Handler struct. This method takes a method string and returns the corresponding CustomRequestHandler and a boolean indicating whether the handler was found.

The server's handle method has also been updated to handle errors from the Handler's Handle method.

Usage example:

	CustomRequests := []protocol.CustomRequestHandler{
		{
			Func:   server.TestHandler,
			Method: "test/test",
		},
	}

	handler = protocol.Handler{
		Initialize:            server.initialize,
		Initialized:           server.initialized,
		Shutdown:              server.shutdown,
		SetTrace:              server.setTrace,
		TextDocumentDidOpen:   server.didOpen,
		TextDocumentDidChange: server.didChange,
		TextDocumentDidClose:  server.didClose,
		CustomRequest:         CustomRequests,
	}

then server.TestHandler as below:


func (s *Server) TestHandler(context *glsp.Context, params json.RawMessage) error {
	type TestParams struct {
		Nah string `json:"nah"`
	}

	unmarshaledParams := TestParams{}
	unmarshalErr := json.Unmarshal(params, &unmarshaledParams)
	if unmarshalErr != nil {
		fmt.Println("Error unmarshaling params")
	}
	fmt.Println("TestHandler")
	fmt.Printf("Params: %v", unmarshaledParams.Nah)
	return nil
}

Closes #31

tris203 avatar Jun 28 '24 21:06 tris203