| // 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 main |
| |
| import ( |
| "context" |
| "log" |
| "os" |
| |
| "github.com/sourcegraph/jsonrpc2" |
| |
| "fidl-lsp/analysis" |
| "fidl-lsp/langserver" |
| ) |
| |
| var ( |
| fuchsiaDir = os.Getenv("FUCHSIA_DIR") |
| outDir = fuchsiaDir + "/out/default/" |
| fidlProjectPath = outDir + "fidl_project.json" |
| toolsPath = fuchsiaDir + "/out/default.zircon/tools/" |
| fidlcPath = toolsPath + "fidlc" |
| fidlLintPath = toolsPath + "fidl-lint" |
| fidlFormatPath = toolsPath + "fidl-format" |
| ) |
| |
| type stdrwc struct{} |
| |
| func (stdrwc) Read(p []byte) (int, error) { |
| return os.Stdin.Read(p) |
| } |
| |
| func (stdrwc) Write(p []byte) (int, error) { |
| return os.Stdout.Write(p) |
| } |
| |
| func (stdrwc) Close() error { |
| if err := os.Stdin.Close(); err != nil { |
| return err |
| } |
| return os.Stdout.Close() |
| } |
| |
| func main() { |
| ctx, cancel := context.WithCancel(context.Background()) |
| defer cancel() |
| |
| trace := log.New(os.Stderr, "[LSP] ", log.Lshortfile) |
| logOpt := jsonrpc2.LogMessages(trace) |
| |
| // fidl_project.json is generated by running |
| // `python3 scripts/gen_fidl_project.py path/to/fidl_project.json` |
| fidlProject, err := analysis.LoadFidlProject(fidlProjectPath) |
| if err != nil { |
| trace.Fatalf("Failed to parse fidl_project.json at `%s`: %s\n", fidlProjectPath, err) |
| } |
| analyzer := analysis.NewAnalyzer( |
| analysis.Config{ |
| BuildRootDir: fuchsiaDir, |
| FidlcPath: fidlcPath, |
| FidlLintPath: fidlLintPath, |
| FidlFormatPath: fidlFormatPath, |
| }, |
| fidlProject, |
| ) |
| |
| handler := langserver.NewLangHandler(langserver.NewDefaultConfig(), trace, analyzer) |
| server := langserver.LspServer{handler} |
| |
| // Setup a new Conn over stdio |
| trace.Println("setting up a new connection") |
| conn := jsonrpc2.NewConn( |
| ctx, |
| jsonrpc2.NewBufferedStream(stdrwc{}, jsonrpc2.VSCodeObjectCodec{}), |
| server, |
| logOpt, |
| ) |
| defer conn.Close() |
| |
| <-conn.DisconnectNotify() |
| } |