Skip to content

Commit 73dd329

Browse files
authored
Merge branch 'master' into outline
2 parents 7faeed3 + 5e7f8a2 commit 73dd329

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

api/index.ts

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import httpModule = require('http');
2+
import httpsModule = require('https');
3+
4+
import { ClientResponse } from 'http';
5+
6+
export class AtelierAPI {
7+
private http;
8+
private url;
9+
private agent;
10+
private cookies: string[] = [];
11+
constructor(
12+
private host: string,
13+
private port: number,
14+
private username: string,
15+
private password: string,
16+
private ns: string,
17+
private secure: boolean
18+
) {
19+
this.http = secure ? httpsModule : httpModule;
20+
this.agent = new this.http.Agent({ keepAlive: true, maxSockets: 10 });
21+
}
22+
23+
updateCookies(cookies: string[]) {
24+
cookies.forEach(cookie => {
25+
let [cookieName] = cookie.split('=');
26+
let index = this.cookies.findIndex(el => el.startsWith(cookieName));
27+
if (index) {
28+
this.cookies[index] = cookie;
29+
} else {
30+
this.cookies.push(cookie);
31+
}
32+
});
33+
}
34+
35+
request(method: string, path?: string, body?: any): Promise<any> {
36+
const headers = {
37+
Accept: 'application/json',
38+
Cookie: this.cookies
39+
};
40+
method = method.toUpperCase();
41+
if (['PUT', 'POST'].includes(method)) {
42+
headers['Content-Type'] = 'application/json';
43+
}
44+
return new Promise((resolve, reject) => {
45+
const req: httpModule.ClientRequest = this.http
46+
.request(
47+
{
48+
method,
49+
host: this.host,
50+
port: this.port,
51+
path: encodeURI(`/api/atelier/${path || ''}`),
52+
agent: this.agent,
53+
headers,
54+
body
55+
},
56+
(response: ClientResponse) => {
57+
if (response.statusCode < 200 || response.statusCode > 299) {
58+
reject(new Error('Failed to load page "' + path + '", status code: ' + response.statusCode));
59+
}
60+
this.updateCookies(response.headers['set-cookie']);
61+
// temporary data holder
62+
let body: string = '';
63+
response.on('data', chunk => {
64+
body += chunk;
65+
});
66+
response.on('end', () => {
67+
if (response.headers['content-type'].includes('json')) {
68+
body = JSON.parse(body);
69+
}
70+
resolve(body);
71+
});
72+
}
73+
)
74+
.on('error', reject);
75+
if (['PUT', 'POST'].includes(method)) {
76+
req.write(JSON.stringify(body));
77+
}
78+
req.end();
79+
});
80+
}
81+
82+
serverInfo(): Promise<any> {
83+
return this.request('GET');
84+
}
85+
86+
getDocNames({
87+
generated = false,
88+
category = '*',
89+
filter = ''
90+
}: {
91+
generated: boolean;
92+
category: string;
93+
filter: string;
94+
}): Promise<any> {
95+
return this.request(
96+
'GET',
97+
`v2/${this.ns}/docnames/${category}?generated=${generated ? '1' : '0'}&filter=${filter}`
98+
);
99+
}
100+
101+
actionIndex(docs: string[]): Promise<any> {
102+
return this.request('POST', `v2/${this.ns}/action/index`, docs);
103+
}
104+
}

extension.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { CurrentDoc } = require('./commands/currentdoc');
1010
const IsApiError = require('./is-api-error');
1111

1212
import { ObjectScriptSymbolProvider } from './providers/ObjectScriptSymbolProvider';
13+
import { AtelierAPI } from './api';
1314

1415
import { COSExplorerProvider } from './explorer/explorer';
1516
export var cosExplorerProvider: COSExplorerProvider;
@@ -45,8 +46,14 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4546
};
4647

4748
let api;
49+
let atelierApi: AtelierAPI;
4850
const Connect = conn => {
4951
api = API(conn);
52+
atelierApi = new AtelierAPI(conn.host, conn.port, conn.username, conn.password, conn.ns, conn.https);
53+
atelierApi
54+
.serverInfo()
55+
.then(info => {})
56+
.catch(err => {});
5057
api.headServer(err => {
5158
const conn = config.conn();
5259
if (err) return log('Connection FAILED: ' + conn, err);
@@ -136,9 +143,38 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
136143
api.putDoc(name, { enc: false, content }, { ignoreConflict: true }, compile);
137144
};
138145

146+
const viewOther = () => {
147+
const { name, error } = currentDoc();
148+
if (error) return log(error);
149+
150+
const getOthers = info => {
151+
return info.result.content[0].others;
152+
};
153+
154+
atelierApi
155+
.actionIndex([name])
156+
.then(info => {
157+
const listOthers = getOthers(info) || [];
158+
if (!listOthers.length) {
159+
return;
160+
}
161+
if (listOthers.length === 1) {
162+
window.showTextDocument(vscode.Uri.parse(encodeURI(`cos:///${listOthers[0]}`)));
163+
} else {
164+
window.showQuickPick(listOthers).then(item => {
165+
window.showTextDocument(vscode.Uri.parse(encodeURI(`cos:///${item}`)));
166+
});
167+
}
168+
})
169+
.catch(err => {
170+
log('error' + err);
171+
});
172+
};
173+
139174
context.subscriptions.push(
140175
vscode.commands.registerCommand('cos.compile', importCompileExport),
141176
vscode.commands.registerCommand('cos.export', exportAll),
177+
vscode.commands.registerCommand('vscode-cos.viewother', viewOther),
142178
vscode.commands.registerCommand('vscode-cos.explorer.refresh', () => cosExplorerProvider.refresh()),
143179
vscode.commands.registerCommand('vscode-cos.explorer.openClass', vscode.window.showTextDocument),
144180
vscode.commands.registerCommand('vscode-cos.explorer.openRoutine', vscode.window.showTextDocument),
@@ -150,7 +186,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
150186
vscode.commands.executeCommand('setContext', 'vscode-cos.explorer.showSystem', false);
151187
cosExplorerProvider.showSystem = false;
152188
}),
153-
154189
vscode.workspace.registerTextDocumentContentProvider('cos', cosExplorerProvider),
155190
vscode.languages.registerDocumentSymbolProvider(
156191
{

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
],
5757
"extensions": [
5858
".cls",
59-
".mac"
59+
".mac",
60+
".int"
6061
],
6162
"configuration": "./language-configuration.json"
6263
},
@@ -118,13 +119,24 @@
118119
{
119120
"command": "vscode-cos.explorer.openRoutine",
120121
"title": "Open routine"
122+
},
123+
{
124+
"category": "COS",
125+
"command": "vscode-cos.viewother",
126+
"title": "View other"
121127
}
122128
],
123129
"keybindings": [
124130
{
125131
"command": "cos.compile",
126132
"key": "Ctrl+F7",
127133
"mac": "Cmd+F7"
134+
},
135+
{
136+
"command": "vscode-cos.viewother",
137+
"key": "Ctrl+Shift+V",
138+
"mac": "Cmd+Shift+V",
139+
"when": "editorLangId == 'cacheobjectscript'"
128140
}
129141
],
130142
"configuration": {

0 commit comments

Comments
 (0)