Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 5851bb9

Browse files
committed
[ide] add new endpoints for tools
1 parent f90b1b2 commit 5851bb9

File tree

6 files changed

+172
-11
lines changed

6 files changed

+172
-11
lines changed

extensions/codestory/src/extension.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export async function activate(context: ExtensionContext) {
5151
const newAddress: AddressInfo = serverAddress as AddressInfo;
5252
port = newAddress.port;
5353
}
54+
console.log(serverAddress);
5455
console.log(`Server for talking to sidecar is running at http://localhost:${server.address()}/`);
5556
}); // Use 0 to let the OS pick an available port
5657
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as vscode from 'vscode';
7+
8+
export function getDiagnosticsFromEditor(filePath: string, interestedRange: vscode.Range): vscode.Diagnostic[] {
9+
const fileUri = vscode.Uri.file(filePath);
10+
const diagnostics = vscode.languages.getDiagnostics(fileUri);
11+
diagnostics.filter((diagnostic) => {
12+
return interestedRange.contains(diagnostic.range);
13+
});
14+
return diagnostics;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as vscode from 'vscode';
7+
import { SidecarGoToDefinitionRequest, SidecarGoToDefinitionResponse } from './types';
8+
import { shouldTrackFile } from '../utilities/openTabs';
9+
10+
export async function goToDefinition(request: SidecarGoToDefinitionRequest): Promise<SidecarGoToDefinitionResponse> {
11+
const locations: vscode.LocationLink[] = await vscode.commands.executeCommand(
12+
'vscode.executeTypeDefinitionProvider',
13+
request.fs_file_path,
14+
request.position
15+
);
16+
const definitons = await Promise.all(locations.map(async (location) => {
17+
const uri = location.targetUri;
18+
const range = location.targetRange;
19+
// we have to always open the text document first, this ends up sending
20+
// it over to the sidecar as a side-effect but that is fine
21+
22+
// No need to await on this
23+
if (shouldTrackFile(uri)) {
24+
console.log('we are tracking this uri');
25+
console.log(uri);
26+
// sidecarClient.documentOpen(textDocument.uri.fsPath, textDocument.getText(), textDocument.languageId);
27+
}
28+
29+
// return the value as we would normally
30+
return {
31+
fs_file_path: uri.fsPath,
32+
range: {
33+
startPosition: {
34+
line: range.start.line,
35+
character: range.start.character,
36+
},
37+
endPosition: {
38+
line: range.end.line,
39+
character: range.end.character,
40+
}
41+
},
42+
};
43+
}));
44+
// lets return all of them over here
45+
return {
46+
symbols: definitons,
47+
};
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as vscode from 'vscode';
7+
import { SidecarOpenFileToolRequest, SidecarOpenFileToolResponse } from './types';
8+
9+
export async function openFileEditor(request: SidecarOpenFileToolRequest): Promise<SidecarOpenFileToolResponse> {
10+
const filePath = request.fs_file_path;
11+
const textDocument = await vscode.workspace.openTextDocument(filePath);
12+
// we get back the text document over here
13+
const contents = textDocument.getText();
14+
return {
15+
fs_file_path: filePath,
16+
file_contents: contents,
17+
};
18+
}

extensions/codestory/src/server/requestHandler.ts

+35-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55
import * as http from 'http';
6+
import { SidecarDiagnosticsRequest, SidecarGoToDefinitionRequest, SidecarOpenFileToolRequest } from './types';
7+
import { Position, Range } from 'vscode';
8+
import { getDiagnosticsFromEditor } from './diagnostics';
9+
import { openFileEditor } from './openFile';
10+
import { goToDefinition } from './goToDefinition';
611

712
// Helper function to read the request body
813
function readRequestBody(req: http.IncomingMessage): Promise<string> {
@@ -23,18 +28,37 @@ function readRequestBody(req: http.IncomingMessage): Promise<string> {
2328
// Async handler function to handle incoming requests
2429
export async function handleRequest(req: http.IncomingMessage, res: http.ServerResponse) {
2530
try {
26-
const body = await readRequestBody(req);
27-
console.log(body);
28-
// const request = JSON.parse(body);
29-
// console.log(request);
31+
if (req.method === 'POST' && req.url === '/diagnostics') {
32+
const body = await readRequestBody(req);
33+
console.log('body from post request for diagnostics');
34+
console.log(body);
35+
console.log('log after post for diagnostics');
36+
const diagnosticsBody: SidecarDiagnosticsRequest = JSON.parse(body);
37+
const selectionRange = new Range(new Position(diagnosticsBody.range.startPosition.line, diagnosticsBody.range.startPosition.character), new Position(diagnosticsBody.range.endPosition.line, diagnosticsBody.range.endPosition.character));
38+
const diagnosticsFromEditor = getDiagnosticsFromEditor(diagnosticsBody.fs_file_path, selectionRange);
39+
// Process the diagnostics request asynchronously
40+
const response = {
41+
'diagnostics': diagnosticsFromEditor,
42+
};
3043

31-
// Process the request asynchronously
32-
const response = {
33-
'reply': 'gg from vscode',
34-
};
35-
36-
res.writeHead(200, { 'Content-Type': 'application/json' });
37-
res.end(JSON.stringify(response));
44+
res.writeHead(200, { 'Content-Type': 'application/json' });
45+
res.end(JSON.stringify(response));
46+
} else if (req.method === 'POST' && req.url === '/file_open') {
47+
const body = await readRequestBody(req);
48+
const openFileRequest: SidecarOpenFileToolRequest = JSON.parse(body);
49+
const response = await openFileEditor(openFileRequest);
50+
res.writeHead(200, { 'Content-Type': 'application/json' });
51+
res.end(JSON.stringify(response));
52+
} else if (req.method === 'POST' && req.url === 'go_to_definition') {
53+
const body = await readRequestBody(req);
54+
const request: SidecarGoToDefinitionRequest = JSON.parse(body);
55+
const response = await goToDefinition(request);
56+
res.writeHead(200, { 'Content-Type': 'application/json' });
57+
res.end(JSON.stringify(response));
58+
} else {
59+
res.writeHead(200, { 'Content-Type': 'application/json' });
60+
res.end(JSON.stringify({ reply: 'gg' }));
61+
}
3862
} catch (err) {
3963
console.error(err);
4064
res.writeHead(500, { 'Content-Type': 'application/json' });
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
// We store the various types of requests we are getting from the sidecar over here
7+
8+
export type SidecarDiagnosticsRequest = {
9+
fs_file_path: string;
10+
range: {
11+
startPosition: {
12+
line: number;
13+
character: number;
14+
};
15+
endPosition: {
16+
line: number;
17+
character: number;
18+
};
19+
};
20+
};
21+
22+
export type SidecarGoToDefinitionRequest = {
23+
fs_file_path: string;
24+
position: {
25+
line: number;
26+
character: number;
27+
};
28+
};
29+
30+
export type SidecarGoToDefinitionResponse = {
31+
symbols: FileAndRange[];
32+
};
33+
34+
export type FileAndRange = {
35+
fs_file_path: string;
36+
range: {
37+
startPosition: {
38+
line: number;
39+
character: number;
40+
};
41+
endPosition: {
42+
line: number;
43+
character: number;
44+
};
45+
};
46+
};
47+
48+
export type SidecarOpenFileToolRequest = {
49+
fs_file_path: string;
50+
};
51+
52+
export type SidecarOpenFileToolResponse = {
53+
fs_file_path: string;
54+
file_contents: string;
55+
};

0 commit comments

Comments
 (0)