-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
118 lines (102 loc) · 2.91 KB
/
extension.ts
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
import * as vscode from "vscode";
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
} from "vscode-languageclient/node";
import * as path from "path";
import * as os from "os";
import * as fs from "fs";
let client: LanguageClient;
function verifyPath(filepath: string): string | undefined {
try {
fs.accessSync(filepath);
return filepath;
} catch {
return undefined;
}
}
function getServerPath(): string {
// Check configuration first
const config = vscode.workspace.getConfiguration("fe-analyzer");
const customPath = config.get<string>("binaryPath");
if (customPath && verifyPath(customPath)) {
return customPath;
}
// Development path
if (process.env.NODE_ENV === "development") {
const devPath = path.join(
__dirname,
"..",
"..",
"..",
"..",
"..",
"target",
"debug",
"fe-language-server",
);
const verifiedDevPath = verifyPath(devPath);
if (verifiedDevPath) {
return verifiedDevPath;
}
}
// Production paths
const platform = os.platform();
const binariesDir = path.join(__dirname, "..", "server");
const executable =
platform === "win32" ? "fe-language-server.exe" : "fe-language-server";
// Explicitly type the platform paths
const platformPaths: Record<string, string> = {
win32: path.join(binariesDir, "windows", executable),
darwin: path.join(binariesDir, "mac", executable),
linux: path.join(binariesDir, "x86_64-unknown-linux-gnu", executable),
};
const platformPath = platformPaths[platform];
if (platformPath) {
const verifiedPlatformPath = verifyPath(platformPath);
if (verifiedPlatformPath) {
return verifiedPlatformPath;
}
}
// Fallback to PATH lookup
const pathEnv = process.env.PATH || "";
const pathSeparator = platform === "win32" ? ";" : ":";
const paths = pathEnv.split(pathSeparator);
for (const p of paths) {
const fullPath = path.join(p, executable);
if (verifyPath(fullPath)) {
return fullPath;
}
}
throw new Error("Could not find fe-language-server executable");
}
export async function activate(
context: vscode.ExtensionContext,
): Promise<void> {
try {
const serverPath = getServerPath();
const serverOptions: ServerOptions = {
run: { command: serverPath },
debug: { command: serverPath },
};
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: "file", language: "fe" }],
};
client = new LanguageClient(
"fe-language-server",
"Fe Language Server",
serverOptions,
clientOptions,
);
await client.start();
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
vscode.window.showErrorMessage(
`Failed to start Fe Language Server: ${errorMessage}`,
);
}
}
export function deactivate(): Thenable<void> | undefined {
return client?.stop();
}