Skip to content

Use a different temp folder to preprocess/generate compiler_command.json #63

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

Merged
merged 2 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Better cleanup...
  • Loading branch information
cmaglie committed Feb 10, 2021
commit 397053cc4827f32eb874d953b3b31c19d49b1f15
39 changes: 27 additions & 12 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type InoHandler struct {
clangdNotificationCount int64
progressHandler *ProgressProxyHandler

closing chan bool
clangdStarted *sync.Cond
dataMux sync.RWMutex
lspInitializeParams *lsp.InitializeParams
Expand Down Expand Up @@ -119,6 +120,7 @@ func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
handler := &InoHandler{
docs: map[string]*lsp.TextDocumentItem{},
inoDocsWithDiagnostics: map[lsp.DocumentURI]bool{},
closing: make(chan bool),
config: lsp.BoardConfig{
SelectedBoard: board,
},
Expand Down Expand Up @@ -158,15 +160,27 @@ type FileData struct {
version int
}

// StopClangd closes the connection to the clangd process.
func (handler *InoHandler) StopClangd() {
handler.ClangdConn.Close()
handler.ClangdConn = nil
// Close closes all the json-rpc connections.
func (handler *InoHandler) Close() {
if handler.ClangdConn != nil {
handler.ClangdConn.Close()
handler.ClangdConn = nil
}
if handler.closing != nil {
close(handler.closing)
handler.closing = nil
}
}

// CloseNotify returns a channel that is closed when the InoHandler is closed
func (handler *InoHandler) CloseNotify() <-chan bool {
return handler.closing
}

// CleanUp performs cleanup of the workspace and temp files create by the language server
func (handler *InoHandler) CleanUp() {
if handler.buildPath != nil {
log.Printf("removing buildpath")
handler.buildPath.RemoveAll()
handler.buildPath = nil
}
Expand Down Expand Up @@ -489,12 +503,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
// Exit the process and trigger a restart by the client in case of a severe error
if err.Error() == "context deadline exceeded" {
log.Println(prefix + "Timeout exceeded while waiting for a reply from clangd.")
handler.exit()
log.Println(prefix + "Please restart the language server.")
handler.Close()
}
if strings.Contains(err.Error(), "non-added document") || strings.Contains(err.Error(), "non-added file") {
log.Printf(prefix + "The clangd process has lost track of the open document.")
log.Printf(prefix+" %s", err)
handler.exit()
log.Println(prefix + "Please restart the language server.")
handler.Close()
}
}

Expand All @@ -505,12 +521,6 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
return result, err
}

func (handler *InoHandler) exit() {
log.Println("Please restart the language server.")
handler.StopClangd()
os.Exit(1)
}

func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.InitializeParams) error {
currCppTextVersion := 0
if params != nil {
Expand Down Expand Up @@ -579,6 +589,11 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
handler.ClangdConn = jsonrpc2.NewConn(context.Background(), clangdStream, clangdHandler,
jsonrpc2.OnRecv(streams.JSONRPCConnLogOnRecv("IDE LS <-- CL:")),
jsonrpc2.OnSend(streams.JSONRPCConnLogOnSend("IDE LS --> CL:")))
go func() {
<-handler.ClangdConn.DisconnectNotify()
log.Printf("Lost connection with clangd!")
handler.Close()
}()

// Send initialization command to clangd
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
Expand Down
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"os"
"os/signal"

"github.com/arduino/go-paths-helper"
"github.com/bcmi-labs/arduino-language-server/handler"
Expand Down Expand Up @@ -53,7 +54,16 @@ func main() {
}

inoHandler := handler.NewInoHandler(stdio, initialBoard)
defer inoHandler.StopClangd()
defer inoHandler.CleanUp()
<-inoHandler.StdioConn.DisconnectNotify()

// Intercept kill signal
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, os.Kill)

select {
case <-inoHandler.CloseNotify():
case <-c:
log.Println("INTERRUPTED")
}
inoHandler.CleanUp()
inoHandler.Close()
}