-
Notifications
You must be signed in to change notification settings - Fork 12
/
extension.ts
199 lines (173 loc) · 6.4 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import vscode = require('vscode');
const { workspace, window } = vscode;
import http = require('http');
const API = require('cos-api4node');
const LOG = require('./log');
const panel = require('./status-bar-panel');
const CmdExport = require('./commands/export');
const { CurrentDoc } = require('./commands/currentdoc');
const IsApiError = require('./is-api-error');
import { ObjectScriptSymbolProvider } from './providers/ObjectScriptSymbolProvider';
import { AtelierAPI } from './api';
import { COSExplorerProvider } from './explorer/explorer';
export var cosExplorerProvider: COSExplorerProvider;
export async function activate(context: vscode.ExtensionContext): Promise<void> {
const languages = require(context.asAbsolutePath('./package.json'))['contributes']['languages'].map(lang => lang.id);
const log = LOG(window);
cosExplorerProvider = new COSExplorerProvider();
vscode.window.registerTreeDataProvider('cosExplorer', cosExplorerProvider);
const Config = workspace => {
let options = null;
const init = () => {
options = workspace.getConfiguration('cos');
};
init();
return {
init,
get: option => options.get(option),
conn: () => {
const _conn = options.get('conn');
_conn.toString = () => JSON.stringify(Object.assign({}, _conn, { password: '***' }), null, 4);
return _conn;
},
export: () => {
const root = workspace.rootPath;
return Object.assign({}, options.get('export'), { root });
}
};
};
let api;
let atelierApi: AtelierAPI;
const Connect = conn => {
api = API(conn);
atelierApi = new AtelierAPI(conn.host, conn.port, conn.username, conn.password, conn.ns, conn.https);
atelierApi
.serverInfo()
.then(info => {})
.catch(err => {});
api.headServer(err => {
const conn = config.conn();
if (err) return log('Connection FAILED: ' + conn, err);
log('Connected ' + conn);
panel.set(conn);
});
cosExplorerProvider.setAPI(api, conn.ns);
};
const config = Config(workspace);
Connect(config.conn());
let { exportAll, ExportDoc } = CmdExport({
api,
log,
options: config.export
});
workspace.onDidChangeConfiguration(
() => {
config.init();
Connect(config.conn());
({ exportAll, ExportDoc } = CmdExport({
api,
log,
options: config.export
}));
},
null,
context.subscriptions
); //reload config on event
workspace.onDidSaveTextDocument(file => {
if (!config.get('autoCompile')) {
return;
}
if (languages.includes(file.languageId)) {
importCompileExport();
}
});
const currentDoc = CurrentDoc({ window, languages, log });
const Save = ({ name, log, fileName }) => (err, data) => {
// IsApiError, ExportDoc - global
const isGetDocError = IsApiError(name, 'getDoc', log, window);
if (isGetDocError({ err, data })) return;
const completed = () => log('Completed.');
const exportDoc = ExportDoc({ name, cat: data.result.cat, fileName }, completed);
exportDoc({ err, data });
};
const Export = ({ api, name, log, fileName }) => (err, data) => {
// IsApiError, Save - from upper scope
const isCompileError = IsApiError(name, 'compile', log, window);
if (isCompileError({ err, data })) return;
// after compilation API returns updated storage definition
// but, currently, we don`t have any AST implementation
// so, just export again
data.console.forEach(log); //output compilation log
//log( ` Export ${ name }` )
const save = Save({ name, log, fileName });
api.getDoc(name, save);
};
const Compile = ({ api, name, log, fileName }) => (err, data) => {
// IsApiError, Export
const isImportError = IsApiError(name, 'import', log, window);
if (isImportError({ err, data })) return;
const exportCurrent = Export({ api, name, log, fileName });
//log( `Compile ${ name }` )
api.compile(name, exportCurrent);
window.showInformationMessage(`${name}: Compile successed`);
};
// import -> compile -> export
// save to server, compile, export to disk
const importCompileExport = () => {
// api, Compile, log
const { name, content, error, fileName } = currentDoc();
if (error) return log(error);
const compile = Compile({ api, name, log, fileName });
//log( ` Import ${ name }` )
api.putDoc(name, { enc: false, content }, { ignoreConflict: true }, compile);
};
const viewOther = () => {
const { name, error } = currentDoc();
if (error) return log(error);
const getOthers = info => {
return info.result.content[0].others;
};
atelierApi
.actionIndex([name])
.then(info => {
const listOthers = getOthers(info) || [];
if (!listOthers.length) {
return;
}
if (listOthers.length === 1) {
window.showTextDocument(vscode.Uri.parse(encodeURI(`cos:///${listOthers[0]}`)));
} else {
window.showQuickPick(listOthers).then(item => {
window.showTextDocument(vscode.Uri.parse(encodeURI(`cos:///${item}`)));
});
}
})
.catch(err => {
log('error' + err);
});
};
context.subscriptions.push(
vscode.commands.registerCommand('cos.compile', importCompileExport),
vscode.commands.registerCommand('cos.export', exportAll),
vscode.commands.registerCommand('vscode-cos.viewother', viewOther),
vscode.commands.registerCommand('vscode-cos.explorer.refresh', () => cosExplorerProvider.refresh()),
vscode.commands.registerCommand('vscode-cos.explorer.openClass', vscode.window.showTextDocument),
vscode.commands.registerCommand('vscode-cos.explorer.openRoutine', vscode.window.showTextDocument),
vscode.commands.registerCommand('vscode-cos.explorer.showSystem', () => {
vscode.commands.executeCommand('setContext', 'vscode-cos.explorer.showSystem', true);
cosExplorerProvider.showSystem = true;
}),
vscode.commands.registerCommand('vscode-cos.explorer.hideSystem', () => {
vscode.commands.executeCommand('setContext', 'vscode-cos.explorer.showSystem', false);
cosExplorerProvider.showSystem = false;
}),
vscode.workspace.registerTextDocumentContentProvider('cos', cosExplorerProvider),
vscode.languages.registerDocumentSymbolProvider(
{
language: 'cacheobjectscript'
},
new ObjectScriptSymbolProvider()
)
);
}
export async function deactivate() {}