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

Commit 9eef4dd

Browse files
authored
Merge pull request #620 from codestoryai/features/apply-edits-endpoint
[ide] apply edits endpoint
2 parents e6e0ebe + cc66cba commit 9eef4dd

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 { SidecarApplyEditsRequest, SidecarApplyEditsResponse } from './types';
7+
import * as vscode from 'vscode';
8+
9+
/**
10+
* We want to apply edits to the codebase over here and try to get this ti work
11+
*/
12+
13+
export async function applyEdits(request: SidecarApplyEditsRequest): Promise<SidecarApplyEditsResponse> {
14+
const filePath = request.fs_file_path;
15+
const startPosition = request.selected_range.startPosition;
16+
const endPosition = request.selected_range.endPosition;
17+
const replacedText = request.edited_content;
18+
const range = new vscode.Range(new vscode.Position(startPosition.line, startPosition.character), new vscode.Position(endPosition.line, endPosition.character));
19+
const workspaceEdit = new vscode.WorkspaceEdit();
20+
workspaceEdit.replace(
21+
vscode.Uri.file(filePath),
22+
range,
23+
replacedText,
24+
);
25+
const success = await vscode.workspace.applyEdit(workspaceEdit);
26+
// we calculate how many lines we get after replacing the text
27+
// once we make the edit on the range, the new range is presented to us
28+
// we have to calculate the new range and use that instead
29+
// simple algo here would be the following:
30+
const lines = replacedText.split(/\r\n|\r|\n/);
31+
let lastLineColumn = 0;
32+
if (lines.length > 0) {
33+
lastLineColumn = lines[lines.length - 1].length
34+
} else {
35+
lastLineColumn = replacedText.length + startPosition.character;
36+
}
37+
const newRange = {
38+
startPosition: {
39+
line: startPosition.line,
40+
character: startPosition.character,
41+
byte_offset: 0,
42+
},
43+
endPosition: {
44+
line: startPosition.line + replacedText.split(/\r\n|\r|\n/).length,
45+
character: lastLineColumn,
46+
byte_offset: 0,
47+
}
48+
};
49+
return {
50+
fs_file_path: filePath,
51+
success,
52+
new_range: newRange,
53+
};
54+
}

extensions/codestory/src/server/requestHandler.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
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, SidecarGoToImplementationRequest, SidecarOpenFileToolRequest, SidecarQuickFixInvocationRequest, SidecarQuickFixRequest } from './types';
6+
import { SidecarApplyEditsRequest, SidecarDiagnosticsRequest, SidecarGoToDefinitionRequest, SidecarGoToImplementationRequest, SidecarOpenFileToolRequest, SidecarQuickFixInvocationRequest, SidecarQuickFixRequest } from './types';
77
import { Position, Range } from 'vscode';
88
import { getDiagnosticsFromEditor } from './diagnostics';
99
import { openFileEditor } from './openFile';
1010
import { goToDefinition } from './goToDefinition';
1111
import { SIDECAR_CLIENT } from '../extension';
1212
import { goToImplementation } from './goToImplementation';
1313
import { quickFixInvocation, quickFixList } from './quickFix';
14+
import { applyEdits } from './applyEdits';
1415

1516
// Helper function to read the request body
1617
function readRequestBody(req: http.IncomingMessage): Promise<string> {
@@ -87,6 +88,13 @@ export async function handleRequest(req: http.IncomingMessage, res: http.ServerR
8788
const response = await quickFixInvocation(request);
8889
res.writeHead(200, { 'Content-Type': 'application/json' });
8990
res.end(JSON.stringify(response));
91+
} else if (req.method === 'POST' && req.url === '/apply_edits') {
92+
console.log('apply-edits');
93+
const body = await readRequestBody(req);
94+
const request: SidecarApplyEditsRequest = JSON.parse(body);
95+
const response = await applyEdits(request);
96+
res.writeHead(200, { 'Content-Type': 'application/json' });
97+
res.end(JSON.stringify(response));
9098
} else {
9199
console.log('HC request');
92100
res.writeHead(200, { 'Content-Type': 'application/json' });

extensions/codestory/src/server/types.ts

+32
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,35 @@ export type SidecarQuickFixInvocationResponse = {
101101
request_id: string;
102102
invocation_success: boolean;
103103
};
104+
105+
export type SidecarApplyEditsRequest = {
106+
fs_file_path: string;
107+
edited_content: string;
108+
selected_range: {
109+
startPosition: {
110+
line: number;
111+
character: number;
112+
};
113+
endPosition: {
114+
line: number;
115+
character: number;
116+
};
117+
};
118+
}
119+
120+
export type SidecarApplyEditsResponse = {
121+
fs_file_path: string;
122+
success: boolean;
123+
new_range: {
124+
startPosition: {
125+
line: number;
126+
character: number;
127+
byte_offset: number;
128+
};
129+
endPosition: {
130+
line: number;
131+
character: number;
132+
byte_offset: number;
133+
};
134+
};
135+
};

0 commit comments

Comments
 (0)