Skip to content

Commit 7d8203a

Browse files
zthGabriel Nordeborn
authored and
Gabriel Nordeborn
committed
temporary command for extracting docs
1 parent 33b9f5c commit 7d8203a

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

client/src/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "./commands/code_analysis";
77

88
export { createInterface } from "./commands/create_interface";
9+
export { extractDocs } from "./commands/extract_docs";
910
export { openCompiled } from "./commands/open_compiled";
1011
export { switchImplIntf } from "./commands/switch_impl_intf";
1112

client/src/commands/extract_docs.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as p from "vscode-languageserver-protocol";
2+
import { LanguageClient, RequestType } from "vscode-languageclient/node";
3+
import { Position, Uri, window, workspace, WorkspaceEdit } from "vscode";
4+
import path = require("path");
5+
6+
export const extractDocsRequest = new RequestType<
7+
p.TextDocumentIdentifier,
8+
string,
9+
void
10+
>("textDocument/extractDocs");
11+
12+
export const extractDocs = async (client: LanguageClient) => {
13+
if (!client) {
14+
return window.showInformationMessage("Language server not running");
15+
}
16+
17+
const editor = window.activeTextEditor;
18+
19+
if (!editor) {
20+
return window.showInformationMessage("No active editor");
21+
}
22+
23+
try {
24+
const docUri = editor.document.uri.toString();
25+
const res = await client.sendRequest(extractDocsRequest, {
26+
uri: docUri,
27+
});
28+
29+
const newFile = Uri.parse(
30+
"untitled:" +
31+
path.join(
32+
workspace.workspaceFolders[0].uri.fsPath,
33+
`${path.basename(docUri)}.json`
34+
)
35+
);
36+
workspace.openTextDocument(newFile).then((document) => {
37+
const edit = new WorkspaceEdit();
38+
edit.insert(newFile, new Position(0, 0), JSON.stringify(res, null, 2));
39+
return workspace.applyEdit(edit).then((success) => {
40+
if (success) {
41+
window.showTextDocument(document);
42+
} else {
43+
window.showInformationMessage("Error!");
44+
}
45+
});
46+
});
47+
} catch (e) {
48+
console.error("failed", e);
49+
}
50+
};

client/src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ export function activate(context: ExtensionContext) {
201201
customCommands.openCompiled(client);
202202
});
203203

204+
commands.registerCommand("rescript-vscode.extract_docs", () => {
205+
customCommands.extractDocs(client);
206+
});
207+
204208
commands.registerCommand(
205209
"rescript-vscode.go_to_location",
206210
async (fileUri: string, startLine: number, startCol: number) => {

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
"command": "rescript-vscode.create_interface",
5050
"title": "ReScript: Create an interface file for this implementation file"
5151
},
52+
{
53+
"command": "rescript-vscode.extract_docs",
54+
"title": "ReScript: Extract documentation as JSON for file."
55+
},
5256
{
5357
"command": "rescript-vscode.open_compiled",
5458
"category": "ReScript",

server/src/server.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ let openCompiledFileRequest = new v.RequestType<
152152
void
153153
>("textDocument/openCompiled");
154154

155+
let extractDocsRequest = new v.RequestType<
156+
p.TextDocumentIdentifier,
157+
p.TextDocumentIdentifier,
158+
void
159+
>("textDocument/extractDocs");
160+
155161
let getCurrentCompilerDiagnosticsForFile = (
156162
fileUri: string
157163
): p.Diagnostic[] => {
@@ -965,6 +971,24 @@ function createInterface(msg: p.RequestMessage): p.Message {
965971
}
966972
}
967973

974+
function extractDocs(msg: p.RequestMessage): p.Message {
975+
let params = msg.params as p.TextDocumentIdentifier;
976+
let filePath = fileURLToPath(params.uri);
977+
978+
let response = utils.runAnalysisCommand(
979+
filePath,
980+
["extractDocs", filePath],
981+
msg
982+
);
983+
984+
let res: p.ResponseMessage = {
985+
jsonrpc: c.jsonrpcVersion,
986+
id: msg.id,
987+
result: response.result,
988+
};
989+
return res;
990+
}
991+
968992
function openCompiledFile(msg: p.RequestMessage): p.Message {
969993
let params = msg.params as p.TextDocumentIdentifier;
970994
let filePath = fileURLToPath(params.uri);
@@ -1230,6 +1254,8 @@ function onMessage(msg: p.Message) {
12301254
send(createInterface(msg));
12311255
} else if (msg.method === openCompiledFileRequest.method) {
12321256
send(openCompiledFile(msg));
1257+
} else if (msg.method === extractDocsRequest.method) {
1258+
send(extractDocs(msg));
12331259
} else if (msg.method === p.InlayHintRequest.method) {
12341260
let params = msg.params as InlayHintParams;
12351261
let extName = path.extname(params.textDocument.uri);

0 commit comments

Comments
 (0)