Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode: use preview editors for json logs #764

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tools/vscode/src/core/vscode/association.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ConfigurationTarget, workspace } from "vscode";


export interface EditorAssociation {
viewType: string;
filenamePattern: string;
}


export async function withEditorAssociation(
association: EditorAssociation,
fn: () => Promise<void>) {

// get existing associations
const kEditorAssociations = 'editorAssociations';
const config = workspace.getConfiguration('workbench');
const configuredAssociations: Record<string, string> | undefined = config.get(kEditorAssociations);
const existingAssociations = (configuredAssociations && Object.keys(configuredAssociations).length > 0)
? configuredAssociations
: undefined;


// temporarily update
const updatedAssociations: Record<string, string> = {
...existingAssociations,
[association.filenamePattern]: association.viewType
};
await config.update(kEditorAssociations, updatedAssociations, ConfigurationTarget.Workspace);

// execute and unwind update
try {
await fn();
} finally {
await config.update(kEditorAssociations, existingAssociations, ConfigurationTarget.Workspace);
}

}
34 changes: 21 additions & 13 deletions tools/vscode/src/providers/openlog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { kInspectLogViewType } from "./logview/logview-editor";
import { hasMinimumInspectVersion } from "../inspect/version";
import { kInspectEvalLogFormatVersion } from "./inspect/inspect-constants";
import { InspectViewManager } from "./logview/logview-view";
import { withEditorAssociation } from "../core/vscode/association";


export function activateOpenLog(
Expand All @@ -12,23 +13,30 @@ export function activateOpenLog(

context.subscriptions.push(commands.registerCommand('inspect.openLogViewer', async (uri: Uri) => {

// function to open using defualt editor in preview mode
const openLogViewer = async () => {
await commands.executeCommand(
'vscode.open',
uri,
<TextDocumentShowOptions>{ preview: true }
);
}

if (hasMinimumInspectVersion(kInspectEvalLogFormatVersion)) {
if (uri.path.endsWith(".eval")) {
// normal default path for .eval files (so they get preview treatment)
await commands.executeCommand(
'vscode.open',
uri,
<TextDocumentShowOptions>{ preview: true }
);

await openLogViewer();

} else {
// force our custom editor for .json as we aren't the default. this has the issue of not
// using preview so proliferates more editors
await commands.executeCommand(
'vscode.openWith',
uri,
kInspectLogViewType,
<TextDocumentShowOptions>{ preview: true }

await withEditorAssociation(
{
viewType: kInspectLogViewType,
filenamePattern: "{[0-9][0-9][0-9][0-9]}-{[0-9][0-9]}-{[0-9][0-9]}T{[0-9][0-9]}[:-]{[0-9][0-9]}[:-]{[0-9][0-9]}*{[A-Za-z0-9]{21}}*.json"
},
openLogViewer
);

}

// notify the logs pane that we are doing this so that it can take a reveal action
Expand Down
Loading