Skip to content

handle 'exit' message #50

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

Open
wants to merge 1 commit 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
47 changes: 30 additions & 17 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ import (
"github.com/sourcegraph/jsonrpc2"
)

func NewHandler(logger logger, noLinterName bool) jsonrpc2.Handler {
type HandlerConfig struct {
Logger logger
NoLinterName bool
Close func()
}

func NewHandler(cfg *HandlerConfig) jsonrpc2.Handler {
handler := &langHandler{
logger: logger,
logger: cfg.Logger,
request: make(chan DocumentURI),
noLinterName: noLinterName,
noLinterName: cfg.NoLinterName,
close: cfg.Close,
}

go handler.linter()

return jsonrpc2.HandlerWithError(handler.handle)
Expand All @@ -28,6 +36,7 @@ type langHandler struct {
request chan DocumentURI
command []string
noLinterName bool
close func()

rootURI string
rootDir string
Expand Down Expand Up @@ -110,13 +119,6 @@ func (h *langHandler) lint(uri DocumentURI) ([]Diagnostic, error) {
return diagnostics, nil
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func (h *langHandler) diagnosticMessage(issue *Issue) string {
if h.noLinterName {
return issue.Text
Expand All @@ -126,12 +128,7 @@ func (h *langHandler) diagnosticMessage(issue *Issue) string {
}

func (h *langHandler) linter() {
for {
uri, ok := <-h.request
if !ok {
break
}

for uri := range h.request {
diagnostics, err := h.lint(uri)
if err != nil {
h.logger.Printf("%s", err)
Expand Down Expand Up @@ -161,6 +158,8 @@ func (h *langHandler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *json
return
case "shutdown":
return h.handleShutdown(ctx, conn, req)
case "exit":
return h.handleExit(ctx, conn, req)
case "textDocument/didOpen":
return h.handleTextDocumentDidOpen(ctx, conn, req)
case "textDocument/didClose":
Expand Down Expand Up @@ -198,8 +197,22 @@ func (h *langHandler) handleInitialize(_ context.Context, conn *jsonrpc2.Conn, r
}, nil
}

func (h *langHandler) shutdown() {
if h.request != nil {
close(h.request)
h.request = nil
}
}

func (h *langHandler) handleShutdown(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
close(h.request)
h.shutdown()

return nil, nil
}

func (h *langHandler) handleExit(_ context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request) (result interface{}, err error) {
h.shutdown()
h.close()

return nil, nil
}
Expand Down
31 changes: 24 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,37 @@ func main() {

logger := newStdLogger(*debug)

handler := NewHandler(logger, *noLinterName)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

var connOpt []jsonrpc2.ConnOpt
handler := NewHandler(&HandlerConfig{
Logger: logger,
NoLinterName: *noLinterName,
Close: cancel,
})

logger.Printf("golangci-lint-langserver: connections opened")
var connOpt []jsonrpc2.ConnOpt
if *debug {
connOpt = append(connOpt, jsonrpc2.LogMessages(logger))
}

<-jsonrpc2.NewConn(
context.Background(),
conn := jsonrpc2.NewConn(
ctx,
jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}),
handler,
connOpt...,
).DisconnectNotify()
)

logger.Printf("golangci-lint-langserver: connections closed")
defer conn.Close()

logger.Printf("golangci-lint-langserver: connection opened")
defer logger.Printf("golangci-lint-langserver: connection closed")

select {
case <-ctx.Done():
case <-conn.DisconnectNotify():
}
}

type stdrwc struct{}
Expand Down