3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
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' ;
6
11
7
12
// Helper function to read the request body
8
13
function readRequestBody ( req : http . IncomingMessage ) : Promise < string > {
@@ -23,18 +28,37 @@ function readRequestBody(req: http.IncomingMessage): Promise<string> {
23
28
// Async handler function to handle incoming requests
24
29
export async function handleRequest ( req : http . IncomingMessage , res : http . ServerResponse ) {
25
30
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
+ } ;
30
43
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
+ }
38
62
} catch ( err ) {
39
63
console . error ( err ) ;
40
64
res . writeHead ( 500 , { 'Content-Type' : 'application/json' } ) ;
0 commit comments