Skip to content

Commit f8988b5

Browse files
Merge pull request #286 from gjsjohnmurray/fix-285
fix #285 export serverForUri function as API
2 parents 7b1dc7b + 862243d commit f8988b5

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/api/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const DEFAULT_API_VERSION = 1;
2020
import * as Atelier from "./atelier";
2121

2222
export interface ConnectionSettings {
23+
serverName: string;
2324
active: boolean;
2425
apiVersion: number;
2526
https: boolean;
@@ -47,7 +48,7 @@ export class AtelierAPI {
4748
}
4849

4950
public get config(): ConnectionSettings {
50-
const { active = false, https = false, pathPrefix = "", username } = this._config;
51+
const { serverName, active = false, https = false, pathPrefix = "", username } = this._config;
5152
const ns = this.namespace || this._config.ns;
5253
const host = this.externalServer
5354
? this._config.host
@@ -58,6 +59,7 @@ export class AtelierAPI {
5859
const password = workspaceState.get(this.configName + ":password", this._config.password);
5960
const apiVersion = workspaceState.get(this.configName + ":apiVersion", DEFAULT_API_VERSION);
6061
return {
62+
serverName,
6163
active,
6264
apiVersion,
6365
https,
@@ -164,13 +166,14 @@ export class AtelierAPI {
164166
serverName = "";
165167
}
166168

167-
if (serverName && serverName.length) {
169+
if (serverName !== "") {
168170
const {
169171
webServer: { scheme, host, port, pathPrefix = "" },
170172
username,
171173
password,
172174
} = getResolvedConnectionSpec(serverName, config("intersystems.servers", workspaceFolderName).get(serverName));
173175
this._config = {
176+
serverName,
174177
active: this.externalServer || conn.active,
175178
apiVersion: workspaceState.get(this.configName + ":apiVersion", DEFAULT_API_VERSION),
176179
https: scheme === "https",
@@ -190,6 +193,7 @@ export class AtelierAPI {
190193
}
191194
} else {
192195
this._config = conn;
196+
this._config.serverName = serverName;
193197
}
194198
}
195199

src/extension.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ async function serverManager(): Promise<any> {
356356
}
357357
}
358358

359-
export async function activate(context: vscode.ExtensionContext): Promise<void> {
359+
export async function activate(context: vscode.ExtensionContext): Promise<any> {
360360
if (!packageJson.version.includes("SNAPSHOT")) {
361361
reporter = new TelemetryReporter(extensionId, extensionVersion, aiKey);
362362
}
@@ -687,10 +687,64 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
687687
new ObjectScriptClassCodeLensProvider()
688688
),
689689

690-
/* from proposed api */
690+
/* Anything we use from the VS Code proposed API */
691691
...proposed
692692
);
693693
reporter && reporter.sendTelemetryEvent("extensionActivated");
694+
695+
// The API we export
696+
const api = {
697+
serverForUri(uri: vscode.Uri): any {
698+
const { apiTarget } = connectionTarget(uri);
699+
const api = new AtelierAPI(apiTarget);
700+
const {
701+
serverName,
702+
active,
703+
host = "",
704+
https,
705+
port,
706+
pathPrefix,
707+
username,
708+
password,
709+
ns = "",
710+
apiVersion,
711+
} = api.config;
712+
return {
713+
serverName,
714+
active,
715+
scheme: https ? "https" : "http",
716+
host,
717+
port,
718+
pathPrefix,
719+
username,
720+
password,
721+
namespace: ns,
722+
apiVersion: active ? apiVersion : undefined,
723+
};
724+
},
725+
serverDocumentUriForUri(uri: vscode.Uri): vscode.Uri {
726+
const { apiTarget } = connectionTarget(uri);
727+
if (typeof apiTarget === "string") {
728+
// It was a file-type uri, so find its document (we hope it is open)
729+
const docs = vscode.workspace.textDocuments.filter((doc) => doc.uri === uri);
730+
let fileName = "";
731+
if (docs.length === 1) {
732+
// Found it, so work out the corresponding server-side name
733+
const file = currentFile(docs[0]);
734+
// For some local documents there is no server-side equivalent
735+
if (file) {
736+
fileName = file.name;
737+
}
738+
}
739+
// uri.path will be "/" if no mapping exists to a server-side equivalent
740+
uri = vscode.Uri.file(fileName).with({ scheme: OBJECTSCRIPT_FILE_SCHEMA, authority: apiTarget });
741+
}
742+
return uri;
743+
},
744+
};
745+
746+
// 'export' our public API
747+
return api;
694748
}
695749

696750
export function deactivate(): void {

0 commit comments

Comments
 (0)