blob: 56662fd14f65853694e2f972f8a3155cae5864ed [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package langserver
import (
"context"
"path/filepath"
"github.com/sourcegraph/go-lsp"
"github.com/sourcegraph/jsonrpc2"
"fidl-lsp/analysis"
)
// clientConfig are the configuration settings from the LSP client. These are
// relevant to the Analyzer. If they are left unset, the Analyzer uses its
// defaults.
type clientConfig struct {
FidlProject string `json:"fidlProject,omitempty"`
BuildRoot string `json:"buildRoot,omitempty"`
Tools toolsConfig `json:"tools,omitempty"`
}
type toolsConfig struct {
Compiler string `json:"compiler,omitempty"`
Linter string `json:"linter,omitempty"`
Formatter string `json:"formatter,omitempty"`
}
func (h *LangHandler) requestConfiguration(ctx context.Context, conn *jsonrpc2.Conn) {
go func() {
var cfg []clientConfig
conn.Call(
ctx,
"workspace/configuration",
lsp.ConfigurationParams{
Items: []lsp.ConfigurationItem{{Section: "fidl"}},
},
&cfg,
)
if len(cfg) < 1 {
h.log.Printf("received invalid configuration settings. unable to update\n")
return
}
h.updateConfig(cfg[0])
}()
}
func (h *LangHandler) handleDidChangeConfiguration(params lsp.DidChangeConfigurationParams) {
h.log.Printf("updating with new config settings: %#v", params.Settings)
cfg, ok := params.Settings.([]clientConfig)
if !ok {
h.log.Printf("received invalid configuration settings. unable to update\n")
return
}
if len(cfg) < 1 {
h.log.Printf("received invalid configuration settings. unable to update\n")
return
}
h.updateConfig(cfg[0])
}
func (h *LangHandler) updateConfig(cfg clientConfig) {
if cfg.FidlProject != "" {
fidlProject, err := analysis.LoadFidlProject(cfg.FidlProject)
if err != nil {
h.log.Printf("failed to parse fidl_project.json at `%s`: %s\n", cfg.FidlProject, err)
}
// TODO: remove the currently compiled libraries?
h.analyzer.ImportCompiledLibraries(fidlProject)
// If the BuildRoot is unset, set it to the directory containing
// `FidlProject`
if cfg.BuildRoot == "" {
cfg.BuildRoot = filepath.Dir(cfg.FidlProject)
}
}
h.analyzer.SetConfig(analysis.Config{
BuildRootDir: cfg.BuildRoot,
FidlcPath: cfg.Tools.Compiler,
FidlLintPath: cfg.Tools.Linter,
FidlFormatPath: cfg.Tools.Formatter,
})
}