Skip to content

Conversation

@rcjsuen
Copy link

@rcjsuen rcjsuen commented Oct 17, 2024

I used #30 as an example for adding support for the new textDocument/inlineCompletion request introduced in 3.18. @tliron I know copy/pasting isn't the best of ideas but it can help get people unblocked for now. What do you think?

I tested it by having Visual Studio Code connect to the language server below and triggering the textDocument/inlineCompletion request from the editor.

package main

import (
	"github.com/tliron/commonlog"
	"github.com/tliron/glsp"
	protocol316 "github.com/tliron/glsp/protocol_3_16"
	protocol317 "github.com/tliron/glsp/protocol_3_17"
	protocol318 "github.com/tliron/glsp/protocol_3_18"
	"github.com/tliron/glsp/server"
)

const lsName = "my language"

var (
	version string = "0.0.1"
	handler protocol318.Handler
)

func main() {
	// This increases logging verbosity (optional)
	commonlog.Configure(1, nil)

	handler = protocol318.Handler{
		Initialize: initialize,
		TextDocumentInlineCompletion: func(context *glsp.Context, params *protocol318.InlineCompletionParams) (any, error) {
			return []protocol318.InlineCompletionItem{
				{
					InsertText: "hello world",
					Range: &protocol316.Range{
						Start: protocol316.Position{
							Line:      0,
							Character: 0,
						},
						End: protocol316.Position{
							Line:      0,
							Character: 0,
						},
					},
				},
			}, nil
		},
		Handler: protocol317.Handler{
			Handler: protocol316.Handler{
				Initialized: initialized,
				Shutdown:    shutdown,
				SetTrace:    setTrace,
			},
		},
	}

	server := server.NewServer(&handler, lsName, false)

	server.RunStdio()
}

func initialize(context *glsp.Context, params *protocol318.InitializeParams) (any, error) {
	capabilities := handler.CreateServerCapabilities()

	return protocol318.InitializeResult{
		Capabilities: capabilities,
		ServerInfo: &protocol316.InitializeResultServerInfo{
			Name:    lsName,
			Version: &version,
		},
	}, nil
}

func initialized(context *glsp.Context, params *protocol316.InitializedParams) error {
	return nil
}

func shutdown(context *glsp.Context) error {
	protocol316.SetTraceValue(protocol316.TraceValueOff)
	return nil
}

func setTrace(context *glsp.Context, params *protocol316.SetTraceParams) error {
	protocol316.SetTraceValue(params.Value)
	return nil
}

Signed-off-by: Remy Suen <remy.suen@docker.com>
@tris203
Copy link
Collaborator

tris203 commented Jun 28, 2025

Hi @rcjsuen,
Thanks for your contribution!
I've recently taken over maintenance of this library and I'm excited about this. I'd love to bring full support for all LSP 3.18 methods.
I'm currently weighing up whether to merge this as a shortcut, or to implement a protocol factory or builder to reduce duplication.

@rcjsuen
Copy link
Author

rcjsuen commented Jun 28, 2025

@tris203 Do whatever you think is best for the project. :)

In our case, we ended up copying the code into our repository (also under Apache v2) and merged the versions together instead of trying to deal with the version differences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants