1+ import axios from "axios" ;
2+ import * as https from "https" ;
3+ import * as path from "path" ;
4+ import * as vscode from "vscode" ;
5+
6+ import { AtelierAPI } from "../api" ;
7+ import { handleError } from "../utils" ;
8+
9+ interface ResolveContextExpressionResponse {
10+ status ?: string ;
11+ textExpression ?: string ;
12+ message ?: string ;
13+ }
14+
15+ export async function resolveContextExpression ( ) : Promise < void > {
16+ const editor = vscode . window . activeTextEditor ;
17+ if ( ! editor ) {
18+ return ;
19+ }
20+
21+ const { document, selection } = editor ;
22+ const contextExpression = selection . isEmpty
23+ ? document . lineAt ( selection . active . line ) . text . trim ( )
24+ : document . getText ( selection ) . trim ( ) ;
25+
26+ if ( ! contextExpression ) {
27+ void vscode . window . showErrorMessage ( "Context expression is empty." ) ;
28+ return ;
29+ }
30+
31+ const routine = path . basename ( document . fileName ) ;
32+ const api = new AtelierAPI ( document . uri ) ;
33+ const { host, port, username, password, https : useHttps , pathPrefix } = api . config ;
34+
35+ if ( ! host || ! port ) {
36+ void vscode . window . showErrorMessage ( "No active InterSystems server connection for this file." ) ;
37+ return ;
38+ }
39+
40+ const normalizedPrefix = pathPrefix ? ( pathPrefix . startsWith ( "/" ) ? pathPrefix : `/${ pathPrefix } ` ) : "" ;
41+
42+ const baseUrl = `${ useHttps ? "https" : "http" } ://${ host } :${ port } ${ encodeURI ( normalizedPrefix ) } ` ;
43+ const url = `${ baseUrl } /api/sourcecontrol/vscode/resolveContextExpression` ;
44+
45+ const httpsAgent = new https . Agent ( {
46+ rejectUnauthorized : vscode . workspace . getConfiguration ( "http" ) . get ( "proxyStrictSSL" ) ,
47+ } ) ;
48+
49+ try {
50+ const response = await axios . post < ResolveContextExpressionResponse > (
51+ url ,
52+ {
53+ routine,
54+ contextExpression,
55+ } ,
56+ {
57+ headers : {
58+ "Content-Type" : "application/json" ,
59+ } ,
60+ auth :
61+ typeof username === "string" && typeof password === "string"
62+ ? {
63+ username,
64+ password,
65+ }
66+ : undefined ,
67+ httpsAgent,
68+ }
69+ ) ;
70+
71+ const data = response . data ?? { } ;
72+ if ( typeof data . status === "string" && data . status . toLowerCase ( ) === "success" && data . textExpression ) {
73+ const eol = document . eol === vscode . EndOfLine . CRLF ? "\r\n" : "\n" ;
74+ const textExpression = data . textExpression . replace ( / \r ? \n / g, eol ) ;
75+ const formattedTextExpression = textExpression . replace ( / ^ / , "\t" ) ;
76+ const lineRange = document . lineAt ( selection . active . line ) . range ;
77+ await editor . edit ( ( editBuilder ) => {
78+ editBuilder . replace ( lineRange , formattedTextExpression ) ;
79+ } ) ;
80+ } else {
81+ const errorMessage = data . message || "Failed to resolve context expression." ;
82+ void vscode . window . showErrorMessage ( errorMessage ) ;
83+ }
84+ } catch ( error ) {
85+ handleError ( error , "Failed to resolve context expression." ) ;
86+ }
87+ }
0 commit comments