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

Support textDocument/inlineCompletion request from 3.18 #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
}

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.

1 participant