This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
/
index.ts
183 lines (157 loc) · 3.85 KB
/
index.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import languageClient = require("vscode-languageclient");
import path = require("path");
import vscode = require("vscode");
import fs = require("fs");
import os = require("os");
let client: languageClient.LanguageClient;
function crawl(root: string): Iterable<string> {
return {
[Symbol.iterator]() {
return {
next() {
const value = root;
root = path.dirname(value);
return {
value,
done: root === "." || root === "/",
};
},
};
},
};
}
async function tryChain(
root: string,
suffix: string,
): Promise<undefined | string> {
for (const dir of crawl(root)) {
const possible = path.join(dir, suffix);
try {
await fs.promises.access(possible);
return possible;
} catch (err) {
}
}
return undefined;
}
async function tryManifest(root: string): Promise<undefined | string> {
for (const dir of crawl(root)) {
const manifestLoc = path.join(dir, "package.json");
try {
const content = await fs.promises.readFile(manifestLoc, "utf8");
const json = JSON.parse(content);
if (json.romeLSPBin) {
return String(path.resolve(dir, json.romeLSPBin));
}
} catch (err) {
if (!(err instanceof SyntaxError || err.code === "ENOENT")) {
throw err;
}
}
}
return undefined;
}
async function getRomeLocation(): Promise<
| undefined
| {
path: string;
env: {
[key: string]: string;
};
}
> {
const {workspaceFolders} = vscode.workspace;
if (workspaceFolders === undefined) {
return undefined;
}
// Find relative to workspace directories
for (const {uri} of workspaceFolders) {
if (uri.scheme === "file") {
const manifest = await tryManifest(uri.path);
if (manifest !== undefined) {
return {path: manifest, env: {}};
}
const nodeModules = await tryChain(
uri.path,
"node_modules/rome/bin/rome/index.js",
);
if (nodeModules !== undefined) {
return {path: nodeModules, env: {}};
}
}
}
// Find development build
try {
const possible = path.join(os.tmpdir(), "rome-dev", "index.js");
await fs.promises.access(possible);
return {path: possible, env: {ROME_DEV: "1"}};
} catch (err) {
}
return undefined;
}
export async function activate() {
const outputChannel = vscode.window.createOutputChannel("Rome");
function log(message: string) {
outputChannel.appendLine(message);
}
let res = await getRomeLocation();
// If no romePath was found then watch workspace folders until we find one
if (res === undefined) {
log(
"No Rome path found. Waiting for workspace folder changes before trying again",
);
await new Promise((resolve) => {
const event = vscode.workspace.onDidChangeWorkspaceFolders(() => {
getRomeLocation().then((_res) => {
if (_res !== undefined) {
res = _res;
event.dispose();
resolve();
}
});
});
});
}
if (res === undefined) {
throw new Error("Should have been defined");
}
const {path: romePath, env} = res;
log(`Discovered Rome path ${romePath}`);
let serverOptions: languageClient.ServerOptions = {
module: romePath,
args: ["lsp"],
transport: languageClient.TransportKind.stdio,
options: {
env,
},
};
let clientOptions: languageClient.LanguageClientOptions = {
outputChannel,
documentSelector: [
{scheme: "file", language: "javascript"},
{scheme: "file", language: "javascriptreact"},
{scheme: "file", language: "typescript"},
{scheme: "file", language: "typescriptreact"},
{scheme: "file", language: "json"},
],
};
client = new languageClient.LanguageClient(
"rome",
"Rome",
serverOptions,
clientOptions,
);
client.start();
}
export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
}