All files / commands/sparql view-raw-sparql-response.ts

0% Statements 0/27
0% Branches 0/15
0% Functions 0/5
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                         
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { ISparqlQueryService } from '@src/languages/sparql/services';
import { SparqlQueryExecutionState } from '@src/languages/sparql/services/sparql-query-state';
import { IDocumentFactory } from '@src/services/document/document-factory.interface';
 
/**
 * Lets the user pick one of the executed queries that captured a raw response.
 * @param candidates The executions that have a raw response, most recent first.
 * @returns The selected execution, or `undefined` if the user dismissed the picker.
 */
async function pickQueryExecution(candidates: SparqlQueryExecutionState[]): Promise<SparqlQueryExecutionState | undefined> {
    if (candidates.length === 1) {
        return candidates[0];
    }
 
    const items = candidates.map(state => ({
        label: state.label ?? state.query?.split('\n')[0] ?? state.id,
        description: state.connectionName,
        detail: new Date(state.startTime).toLocaleTimeString(),
        state
    }));
 
    const selected = await vscode.window.showQuickPick(items, { placeHolder: 'Select a query execution' });
 
    return selected?.state;
}
 
export const viewRawSparqlResponse = {
    id: 'mentor.command.viewRawSparqlResponse',
    handler: async (queryId?: string): Promise<void> => {
        const queryService = container.resolve<ISparqlQueryService>(ServiceToken.SparqlQueryService);
        const history = queryService.getQueryHistory();
 
        const candidates = queryId
            ? history.filter(state => state.id === queryId)
            : [...history].filter(state => state.rawResponse).reverse();
 
        if (candidates.length === 0) {
            vscode.window.showInformationMessage('No raw response is available. Run a query first.');
            return;
        }
 
        const queryState = await pickQueryExecution(candidates);
        const rawResponse = queryState?.rawResponse;
 
        if (!rawResponse) {
            if (queryState) {
                vscode.window.showInformationMessage('No raw response is available for this query.');
            }
 
            return;
        }
 
        let language = 'plaintext';
 
        if (rawResponse.contentType) {
            const documentFactory = container.resolve<IDocumentFactory>(ServiceToken.DocumentFactory);
 
            language = await documentFactory.getLanguageIdFromMimeType(rawResponse.contentType);
        }
 
        const document = await vscode.workspace.openTextDocument({ content: rawResponse.body, language });
 
        await vscode.window.showTextDocument(document);
    }
};