tliron / glsp

Language Server Protocol SDK for Go
Apache License 2.0
183 stars 24 forks source link

Support textDocument/inlineCompletion request from 3.18 #36

Open rcjsuen opened 1 month ago

rcjsuen commented 1 month ago

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
}