diff --git a/src/extension.ts b/src/extension.ts index e846e4f1..bd57cc48 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -83,233 +83,230 @@ export function activate(context: ExtensionContext) { Debug.info(message.message, message.stack); break; } + }); - commands.registerCommand("apollographql/showStats", () => { - const fileUri = window.activeTextEditor - ? window.activeTextEditor.document.uri.fsPath - : null; + commands.registerCommand("apollographql/showStats", () => { + const fileUri = window.activeTextEditor + ? window.activeTextEditor.document.uri.fsPath + : null; - // if no editor is open, but an output channel is, vscode returns something like - // output:extension-output-%234. If an editor IS open, this is something like file://Users/... - // This check is just for either a / or a \ anywhere in a fileUri - const fileOpen = fileUri && /[\/\\]/.test(fileUri); + // if no editor is open, but an output channel is, vscode returns something like + // output:extension-output-%234. If an editor IS open, this is something like file://Users/... + // This check is just for either a / or a \ anywhere in a fileUri + const fileOpen = fileUri && /[\/\\]/.test(fileUri); - if (fileOpen) { - client.sendNotification("apollographql/getStats", { uri: fileUri }); - return; - } - printNoFileOpenMessage(client, version); - client.outputChannel.show(); - }); + if (fileOpen) { + client.sendNotification("apollographql/getStats", { uri: fileUri }); + return; + } + printNoFileOpenMessage(client, version); + client.outputChannel.show(); + }); - client.onNotification("apollographql/statsLoaded", (params) => { - printStatsToClientOutputChannel(client, params, version); - client.outputChannel.show(); - }); - // For some reason, non-strings can only be sent in one direction. For now, messages - // coming from the language server just need to be stringified and parsed. - client.onNotification( - "apollographql/configFilesFound", - (params: string) => { - const response = JSON.parse(params) as Array | ErrorShape; - - const hasActiveTextEditor = Boolean(window.activeTextEditor); - if (isError(response)) { - statusBar.showWarningState({ - hasActiveTextEditor, - tooltip: "Configuration Error", - }); - outputChannel.append(response.stack); - - const infoButtonText = "More Info"; - window - .showInformationMessage(response.message, infoButtonText) - .then((clicked) => { - if (clicked === infoButtonText) { - outputChannel.show(); - } - }); - } else if (Array.isArray(response)) { - if (response.length === 0) { - statusBar.showWarningState({ - hasActiveTextEditor, - tooltip: "No apollo.config.js file found", - }); - } else { - statusBar.showLoadedState({ hasActiveTextEditor }); + client.onNotification("apollographql/statsLoaded", (params) => { + printStatsToClientOutputChannel(client, params, version); + client.outputChannel.show(); + }); + // For some reason, non-strings can only be sent in one direction. For now, messages + // coming from the language server just need to be stringified and parsed. + client.onNotification("apollographql/configFilesFound", (params: string) => { + const response = JSON.parse(params) as Array | ErrorShape; + + const hasActiveTextEditor = Boolean(window.activeTextEditor); + if (isError(response)) { + statusBar.showWarningState({ + hasActiveTextEditor, + tooltip: "Configuration Error", + }); + outputChannel.append(response.stack); + + const infoButtonText = "More Info"; + window + .showInformationMessage(response.message, infoButtonText) + .then((clicked) => { + if (clicked === infoButtonText) { + outputChannel.show(); } - } else { - Debug.error( - `Invalid response type in message apollographql/configFilesFound:\n${JSON.stringify( - response, - )}`, - ); - } - }, - ); - - commands.registerCommand("apollographql/reloadService", () => { - // wipe out tags when reloading - // XXX we should clean up this handling - schemaTagItems = []; - client.sendNotification("apollographql/reloadService"); - }); + }); + } else if (Array.isArray(response)) { + if (response.length === 0) { + statusBar.showWarningState({ + hasActiveTextEditor, + tooltip: "No apollo.config.js file found", + }); + } else { + statusBar.showLoadedState({ hasActiveTextEditor }); + } + } else { + Debug.error( + `Invalid response type in message apollographql/configFilesFound:\n${JSON.stringify( + response, + )}`, + ); + } + }); - // For some reason, non-strings can only be sent in one direction. For now, messages - // coming from the language server just need to be stringified and parsed. - client.onNotification("apollographql/tagsLoaded", (params) => { - const [serviceID, tags]: [string, string[]] = JSON.parse(params); - const items = tags.map((tag) => ({ - label: tag, - description: "", - detail: serviceID, - })); - - schemaTagItems = [...items, ...schemaTagItems]; - }); + commands.registerCommand("apollographql/reloadService", () => { + // wipe out tags when reloading + // XXX we should clean up this handling + schemaTagItems = []; + client.sendNotification("apollographql/reloadService"); + }); - commands.registerCommand("apollographql/selectSchemaTag", async () => { - const selection = await window.showQuickPick(schemaTagItems); - if (selection) { - client.sendNotification("apollographql/tagSelected", selection); - } - }); + // For some reason, non-strings can only be sent in one direction. For now, messages + // coming from the language server just need to be stringified and parsed. + client.onNotification("apollographql/tagsLoaded", (params) => { + const [serviceID, tags]: [string, string[]] = JSON.parse(params); + const items = tags.map((tag) => ({ + label: tag, + description: "", + detail: serviceID, + })); + + schemaTagItems = [...items, ...schemaTagItems]; + }); - let currentLoadingResolve: Map void> = new Map(); + commands.registerCommand("apollographql/selectSchemaTag", async () => { + const selection = await window.showQuickPick(schemaTagItems); + if (selection) { + client.sendNotification("apollographql/tagSelected", selection); + } + }); - client.onNotification("apollographql/loadingComplete", (token) => { - statusBar.showLoadedState({ - hasActiveTextEditor: Boolean(window.activeTextEditor), - }); - const inMap = currentLoadingResolve.get(token); - if (inMap) { - inMap(); - currentLoadingResolve.delete(token); - } - }); + let currentLoadingResolve: Map void> = new Map(); - client.onNotification("apollographql/loading", ({ message, token }) => { - window.withProgress( - { - location: ProgressLocation.Notification, - title: message, - cancellable: false, - }, - () => { - return new Promise((resolve) => { - currentLoadingResolve.set(token, resolve); - }); - }, - ); + client.onNotification("apollographql/loadingComplete", (token) => { + statusBar.showLoadedState({ + hasActiveTextEditor: Boolean(window.activeTextEditor), }); + const inMap = currentLoadingResolve.get(token); + if (inMap) { + inMap(); + currentLoadingResolve.delete(token); + } + }); - const runIconOnDiskPath = Uri.file( - join(context.extensionPath, "images", "IconRun.svg"), + client.onNotification("apollographql/loading", ({ message, token }) => { + window.withProgress( + { + location: ProgressLocation.Notification, + title: message, + cancellable: false, + }, + () => { + return new Promise((resolve) => { + currentLoadingResolve.set(token, resolve); + }); + }, ); + }); + + const runIconOnDiskPath = Uri.file( + join(context.extensionPath, "images", "IconRun.svg"), + ); - const textDecorationType = window.createTextEditorDecorationType({}); - const runGlyphDecorationType = window.createTextEditorDecorationType({}); - let latestDecorations: EngineDecoration[] | undefined = undefined; + const textDecorationType = window.createTextEditorDecorationType({}); + const runGlyphDecorationType = window.createTextEditorDecorationType({}); + let latestDecorations: EngineDecoration[] | undefined = undefined; - const updateDecorations = () => { - if (window.activeTextEditor && latestDecorations) { - const editor = window.activeTextEditor!; + const updateDecorations = () => { + if (window.activeTextEditor && latestDecorations) { + const editor = window.activeTextEditor!; - const decorationsForDocument = latestDecorations.filter( - (decoration) => - decoration.document === - window.activeTextEditor!.document.uri.toString(), - ); + const decorationsForDocument = latestDecorations.filter( + (decoration) => + decoration.document === + window.activeTextEditor!.document.uri.toString(), + ); - const textDecorations = decorationsForDocument.flatMap( - (decoration): DecorationOptions | DecorationOptions[] => { - if (decoration.type !== "text") { - return []; - } - - return { - range: editor.document.lineAt(decoration.range.start.line).range, - renderOptions: { - after: { - contentText: decoration.message, - textDecoration: "none; padding-left: 15px; opacity: .5", - }, + const textDecorations = decorationsForDocument.flatMap( + (decoration): DecorationOptions | DecorationOptions[] => { + if (decoration.type !== "text") { + return []; + } + + return { + range: editor.document.lineAt(decoration.range.start.line).range, + renderOptions: { + after: { + contentText: decoration.message, + textDecoration: "none; padding-left: 15px; opacity: .5", }, - }; - }, - ); + }, + }; + }, + ); + + const runGlyphDecorations = decorationsForDocument.flatMap( + (decoration): DecorationOptions | DecorationOptions[] => { + if (decoration.type !== "runGlyph") { + return []; + } + + const hoverMessage = + decoration.hoverMessage === undefined + ? undefined + : new MarkdownString(decoration.hoverMessage); + if (hoverMessage) { + hoverMessage.isTrusted = true; + } - const runGlyphDecorations = decorationsForDocument.flatMap( - (decoration): DecorationOptions | DecorationOptions[] => { - if (decoration.type !== "runGlyph") { - return []; - } - - const hoverMessage = - decoration.hoverMessage === undefined - ? undefined - : new MarkdownString(decoration.hoverMessage); - if (hoverMessage) { - hoverMessage.isTrusted = true; - } - - const endOfLinePosition = editor.document.lineAt( - decoration.range.start.line, - ).range.end; - return { - // Hover range of just the end of the line (and the icon) so the hover shows above the icon, - // not over at the start of the line - range: new Range(endOfLinePosition, endOfLinePosition), - renderOptions: { - after: { - contentIconPath: runIconOnDiskPath, - textDecoration: - "none; border-radius: .20rem; margin-left: 8px; text-align: center;", - backgroundColor: "#2075D6", - width: "18px", - height: "18px", - }, + const endOfLinePosition = editor.document.lineAt( + decoration.range.start.line, + ).range.end; + return { + // Hover range of just the end of the line (and the icon) so the hover shows above the icon, + // not over at the start of the line + range: new Range(endOfLinePosition, endOfLinePosition), + renderOptions: { + after: { + contentIconPath: runIconOnDiskPath, + textDecoration: + "none; border-radius: .20rem; margin-left: 8px; text-align: center;", + backgroundColor: "#2075D6", + width: "18px", + height: "18px", }, - hoverMessage, - }; - }, - ); + }, + hoverMessage, + }; + }, + ); + window.activeTextEditor!.setDecorations( + textDecorationType, + textDecorations, + ); + if ( + workspace + .getConfiguration("apollographql") + .get("display.showRunInStudioButton") + ) { window.activeTextEditor!.setDecorations( - textDecorationType, - textDecorations, + runGlyphDecorationType, + runGlyphDecorations, ); - if ( - workspace - .getConfiguration("apollographql") - .get("display.showRunInStudioButton") - ) { - window.activeTextEditor!.setDecorations( - runGlyphDecorationType, - runGlyphDecorations, - ); - } } - }; - - client.onNotification( - "apollographql/engineDecorations", - ({ decorations }) => { - latestDecorations = decorations; - updateDecorations(); - }, - ); + } + }; - window.onDidChangeActiveTextEditor(() => { + client.onNotification( + "apollographql/engineDecorations", + ({ decorations }) => { + latestDecorations = decorations; updateDecorations(); - }); + }, + ); - workspace.registerTextDocumentContentProvider("graphql-schema", { - provideTextDocumentContent(uri: Uri) { - // the schema source is provided inside the URI, just return that here - return uri.query; - }, - }); + window.onDidChangeActiveTextEditor(() => { + updateDecorations(); + }); + + workspace.registerTextDocumentContentProvider("graphql-schema", { + provideTextDocumentContent(uri: Uri) { + // the schema source is provided inside the URI, just return that here + return uri.query; + }, }); return client.start();