Skip to content

Commit

Permalink
Fixed publish diags for TCP and added debug log option for jrpc2
Browse files Browse the repository at this point in the history
  • Loading branch information
juliosueiras committed Feb 26, 2020
1 parent 75d62a2 commit 98502e3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 25 deletions.
10 changes: 4 additions & 6 deletions langserver/did_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ func TextDocumentDidChange(ctx context.Context, vs lsp.DidChangeTextDocumentPara

DiagsFiles[fileURL] = tfstructs.GetDiagnostics(tempFile.Name(), fileURL)

if !isTCP {
TextDocumentPublishDiagnostics(StdioServer, ctx, lsp.PublishDiagnosticsParams{
URI: vs.TextDocument.URI,
Diagnostics: DiagsFiles[fileURL],
})
}
TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{
URI: vs.TextDocument.URI,
Diagnostics: DiagsFiles[fileURL],
})
return nil
}
10 changes: 4 additions & 6 deletions langserver/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ func TextDocumentDidOpen(ctx context.Context, vs lsp.DidOpenTextDocumentParams)

DiagsFiles[fileURL] = tfstructs.GetDiagnostics(fileURL, fileURL)

if !isTCP {
TextDocumentPublishDiagnostics(StdioServer, ctx, lsp.PublishDiagnosticsParams{
URI: vs.TextDocument.URI,
Diagnostics: DiagsFiles[fileURL],
})
}
TextDocumentPublishDiagnostics(ctx, lsp.PublishDiagnosticsParams{
URI: vs.TextDocument.URI,
Diagnostics: DiagsFiles[fileURL],
})
tempFile.Write([]byte(vs.TextDocument.Text))
return nil
}
17 changes: 8 additions & 9 deletions langserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
"os"
)

func RunStdioServer() {
isTCP = false
func RunStdioServer(oldLogInstance *oldLog.Logger) {
isTCP = false

StdioServer = jrpc2.NewServer(ServiceMap, &jrpc2.ServerOptions{
AllowPush: true,
Logger: oldLogInstance,
})

StdioServer.Start(channel.Header("")(os.Stdin, os.Stdout))
Expand All @@ -32,10 +33,10 @@ func RunStdioServer() {
log.Info("Server Finish")
}

func RunTCPServer(port int) {
isTCP = true
func RunTCPServer(address string, port int, oldLogInstance *oldLog.Logger) {
isTCP = true

lst, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
lst, err := net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
if err != nil {
log.Fatalf("Listen: %v", err)
}
Expand All @@ -46,15 +47,13 @@ func RunTCPServer(port int) {

ctx, cancelFunc := context.WithCancel(ctx)

oldLogInstance := oldLog.New(os.Stdout, "", 0)

go func() {
if err := server.Loop(lst, ServiceMap, &server.LoopOptions{
Framing: newChan,
ServerOptions: &jrpc2.ServerOptions{
AllowPush: true,
AllowPush: true,
Logger: oldLogInstance,
},
},
}); err != nil {
log.Errorf("Loop: unexpected failure: %v", err)
cancelFunc()
Expand Down
12 changes: 10 additions & 2 deletions langserver/publish_diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import (
lsp "github.com/sourcegraph/go-lsp"
)

func TextDocumentPublishDiagnostics(server *jrpc2.Server, ctx context.Context, vs lsp.PublishDiagnosticsParams) error {
func TextDocumentPublishDiagnostics(ctx context.Context, vs lsp.PublishDiagnosticsParams) error {

return server.Push(ctx, "textDocument/publishDiagnostics", vs)
var resultedError error

if isTCP {
resultedError = jrpc2.ServerPush(ctx, "textDocument/publishDiagnostics", vs)
} else {
resultedError = StdioServer.Push(ctx, "textDocument/publishDiagnostics", vs)
}

return resultedError
}
29 changes: 27 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ import (

var tcp = flag.Bool("tcp", false, "Use TCP instead of Stdio(which is default)")
var port = flag.Int("port", 9900, "Port for TCP Server")
var address = flag.String("address", "127.0.0.1", "Address for TCP Server")

var location = flag.String("log-location", "", "Location of the lsp log")
var locationJRPC2 = flag.String("log-jrpc2-location", "", "Location of the lsp log for jrpc2")

var debug = flag.Bool("debug", false, "Enable debug output")
var debugJRPC2 = flag.Bool("debug-jrpc2", false, "Enable debug output for jrpc2")

var enableLogFile = flag.Bool("enable-log-file", false, "Enable log file")
var enableLogFileJRPC2 = flag.Bool("enable-log-jrpc2-file", false, "Enable log file for JRPC2")

var Version string
var GitCommit string
Expand Down Expand Up @@ -52,11 +59,29 @@ func main() {
log.SetOutput(f)
}

var oldLogInstance *oldLog.Logger

if *debugJRPC2 {
if !*tcp && !*enableLogFileJRPC2 {
log.Fatal("Debug for JRPC2 has to be set for log file location if is set to use stdio")
}

oldLogInstance = oldLog.New(os.Stdout, "", 0)
if *enableLogFileJRPC2 {
f, err := os.OpenFile(fmt.Sprintf("%stf-lsp-jrpc2.log", *locationJRPC2), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
oldLogInstance.SetOutput(f)
}
}

langserver.InitializeServiceMap()

if *tcp {
langserver.RunTCPServer(*port)
langserver.RunTCPServer(*address, *port, oldLogInstance)
} else {
langserver.RunStdioServer()
langserver.RunStdioServer(oldLogInstance)
}
}

0 comments on commit 98502e3

Please sign in to comment.