summaryrefslogtreecommitdiffhomepage
path: root/js/src/lsp/index.ts
blob: e3344a934b792d5e6e0174c648f0087b9fdac276 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { App } from "../app/app";
import { Log } from "../util/log";
import { LSPClient } from "./client";
import path from "path";

export namespace LSP {
  const log = Log.create({ service: "lsp" });

  const state = App.state(
    "lsp",
    async () => {
      log.info("initializing");
      const clients = new Map<string, LSPClient.Info>();

      return {
        clients,
      };
    },
    async (state) => {
      for (const client of state.clients.values()) {
        await client.shutdown();
      }
    },
  );

  export async function file(input: string) {
    const extension = path.parse(input).ext;
    const s = await state();
    const matches = AUTO.filter((x) => x.extensions.includes(extension));
    for (const match of matches) {
      const existing = s.clients.get(match.id);
      if (existing) continue;
      const client = await LSPClient.create({
        cmd: match.command,
        serverID: match.id,
      });
      s.clients.set(match.id, client);
    }
    await run(async (client) => {
      const wait = client.waitForDiagnostics({ path: input });
      await client.notify.open({ path: input });
      return wait;
    });
  }

  export async function diagnostics() {
    const results: Record<string, LSPClient.Diagnostic[]> = {};
    for (const result of await run(async (client) => client.diagnostics)) {
      for (const [path, diagnostics] of result.entries()) {
        const arr = results[path] || [];
        arr.push(...diagnostics);
        results[path] = arr;
      }
    }
    return results;
  }

  export async function hover(input: {
    file: string;
    line: number;
    character: number;
  }) {
    return run((client) => {
      return client.connection.sendRequest("textDocument/hover", {
        textDocument: {
          uri: `file://${input.file}`,
        },
        position: {
          line: input.line,
          character: input.character,
        },
      });
    });
  }

  async function run<T>(
    input: (client: LSPClient.Info) => Promise<T>,
  ): Promise<T[]> {
    const clients = await state().then((x) => [...x.clients.values()]);
    const tasks = clients.map((x) => input(x));
    return Promise.all(tasks);
  }

  const AUTO: {
    id: string;
    command: string[];
    extensions: string[];
    install?: () => Promise<void>;
  }[] = [
    {
      id: "typescript",
      command: ["bun", "x", "typescript-language-server", "--stdio"],
      extensions: [
        ".ts",
        ".tsx",
        ".js",
        ".jsx",
        ".mjs",
        ".cjs",
        ".mts",
        ".cts",
        ".mtsx",
        ".ctsx",
      ],
    },
    /*
    {
      id: "golang",
      command: ["gopls"],
      extensions: [".go"],
    },
    */
  ];

  export namespace Diagnostic {
    export function pretty(diagnostic: LSPClient.Diagnostic) {
      const severityMap = {
        1: "ERROR",
        2: "WARN",
        3: "INFO",
        4: "HINT",
      };

      const severity = severityMap[diagnostic.severity || 1];
      const line = diagnostic.range.start.line + 1;
      const col = diagnostic.range.start.character + 1;

      return `${severity} [${line}:${col}] ${diagnostic.message}`;
    }
  }
}