|
| 1 | +// modified based on https://github.com/microsoft/typescript-go/blob/cedc0cbe6c188f9bfe6a51af00c79be48c9ab74d/cmd/tsgo/lsp.go#L1 |
1 | 2 | package main
|
2 | 3 |
|
3 | 4 | import (
|
4 |
| - "context" |
5 |
| - "io" |
6 |
| - "log" |
7 | 5 | "os"
|
| 6 | + "runtime" |
8 | 7 |
|
9 |
| - "github.com/sourcegraph/jsonrpc2" |
| 8 | + "github.com/microsoft/typescript-go/shim/bundled" |
| 9 | + "github.com/microsoft/typescript-go/shim/core" |
| 10 | + "github.com/microsoft/typescript-go/shim/tspath" |
| 11 | + "github.com/microsoft/typescript-go/shim/vfs/osvfs" |
10 | 12 | "github.com/web-infra-dev/rslint/internal/lsp"
|
| 13 | + "github.com/web-infra-dev/rslint/internal/utils" |
11 | 14 | )
|
12 | 15 |
|
13 |
| -func runLSP() int { |
14 |
| - log.SetOutput(os.Stderr) // Send logs to stderr so they don't interfere with LSP communication |
| 16 | +func runLSP(args []string) int { |
15 | 17 |
|
16 |
| - server := lsp.NewLSPServer() |
| 18 | + fs := bundled.WrapFS(osvfs.FS()) |
| 19 | + defaultLibraryPath := bundled.LibPath() |
| 20 | + typingsLocation := getGlobalTypingsCacheLocation() |
17 | 21 |
|
18 |
| - // Create a simple ReadWriteCloser from stdin/stdout |
19 |
| - stream := &struct { |
20 |
| - io.Reader |
21 |
| - io.Writer |
22 |
| - io.Closer |
23 |
| - }{ |
24 |
| - Reader: os.Stdin, |
25 |
| - Writer: os.Stdout, |
26 |
| - Closer: os.Stdin, |
| 22 | + s := lsp.NewServer(&lsp.ServerOptions{ |
| 23 | + In: lsp.ToReader(os.Stdin), |
| 24 | + Out: lsp.ToWriter(os.Stdout), |
| 25 | + Err: os.Stderr, |
| 26 | + Cwd: utils.Must(os.Getwd()), |
| 27 | + FS: fs, |
| 28 | + DefaultLibraryPath: defaultLibraryPath, |
| 29 | + TypingsLocation: typingsLocation, |
| 30 | + }) |
| 31 | + |
| 32 | + if err := s.Run(); err != nil { |
| 33 | + return 1 |
27 | 34 | }
|
| 35 | + return 0 |
| 36 | +} |
28 | 37 |
|
29 |
| - // Create connection using stdin/stdout |
30 |
| - conn := jsonrpc2.NewConn( |
31 |
| - context.Background(), |
32 |
| - jsonrpc2.NewBufferedStream(stream, jsonrpc2.VSCodeObjectCodec{}), |
33 |
| - jsonrpc2.HandlerWithError(server.Handle), |
34 |
| - ) |
| 38 | +func getGlobalTypingsCacheLocation() string { |
| 39 | + switch runtime.GOOS { |
| 40 | + case "windows": |
| 41 | + return tspath.CombinePaths(tspath.CombinePaths(getWindowsCacheLocation(), "Microsoft/TypeScript"), core.VersionMajorMinor()) |
| 42 | + case "openbsd", "freebsd", "netbsd", "darwin", "linux", "android": |
| 43 | + return tspath.CombinePaths(tspath.CombinePaths(getNonWindowsCacheLocation(), "typescript"), core.VersionMajorMinor()) |
| 44 | + default: |
| 45 | + panic("unsupported platform: " + runtime.GOOS) |
| 46 | + } |
| 47 | +} |
35 | 48 |
|
36 |
| - // Wait for connection to close |
37 |
| - <-conn.DisconnectNotify() |
| 49 | +func getWindowsCacheLocation() string { |
| 50 | + basePath, err := os.UserCacheDir() |
| 51 | + if err != nil { |
| 52 | + if basePath, err = os.UserConfigDir(); err != nil { |
| 53 | + if basePath, err = os.UserHomeDir(); err != nil { |
| 54 | + if userProfile := os.Getenv("USERPROFILE"); userProfile != "" { |
| 55 | + basePath = userProfile |
| 56 | + } else if homeDrive, homePath := os.Getenv("HOMEDRIVE"), os.Getenv("HOMEPATH"); homeDrive != "" && homePath != "" { |
| 57 | + basePath = homeDrive + homePath |
| 58 | + } else { |
| 59 | + basePath = os.TempDir() |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return basePath |
| 65 | +} |
38 | 66 |
|
39 |
| - return 0 |
| 67 | +func getNonWindowsCacheLocation() string { |
| 68 | + if xdgCacheHome := os.Getenv("XDG_CACHE_HOME"); xdgCacheHome != "" { |
| 69 | + return xdgCacheHome |
| 70 | + } |
| 71 | + const platformIsDarwin = runtime.GOOS == "darwin" |
| 72 | + var usersDir string |
| 73 | + if platformIsDarwin { |
| 74 | + usersDir = "Users" |
| 75 | + } else { |
| 76 | + usersDir = "home" |
| 77 | + } |
| 78 | + homePath, err := os.UserHomeDir() |
| 79 | + if err != nil { |
| 80 | + if home := os.Getenv("HOME"); home != "" { |
| 81 | + homePath = home |
| 82 | + } else { |
| 83 | + var userName string |
| 84 | + if logName := os.Getenv("LOGNAME"); logName != "" { |
| 85 | + userName = logName |
| 86 | + } else if user := os.Getenv("USER"); user != "" { |
| 87 | + userName = user |
| 88 | + } |
| 89 | + if userName != "" { |
| 90 | + homePath = "/" + usersDir + "/" + userName |
| 91 | + } else { |
| 92 | + homePath = os.TempDir() |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + var cacheFolder string |
| 97 | + if platformIsDarwin { |
| 98 | + cacheFolder = "Library/Caches" |
| 99 | + } else { |
| 100 | + cacheFolder = ".cache" |
| 101 | + } |
| 102 | + return tspath.CombinePaths(homePath, cacheFolder) |
40 | 103 | }
|
0 commit comments