Skip to content

Show document with server debug info #694

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

Merged
merged 3 commits into from
Feb 18, 2025
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
85 changes: 85 additions & 0 deletions src/common/commands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as vscode from "vscode";
import { ExecuteCommandRequest, LanguageClient } from "vscode-languageclient/node";
import { getConfiguration } from "./vscodeapi";
import { ISettings } from "./settings";

const ISSUE_TRACKER = "https://github.com/astral-sh/ruff/issues";

Expand Down Expand Up @@ -36,3 +38,86 @@ async function executeCommand(lsClient: LanguageClient, command: string) {
);
});
}

/**
* Creates a debug information provider for the `ruff.printDebugInformation` command.
*
* This will open a new editor window with the debug information considering the active editor.
*/
export function createDebugInformationProvider(
getClient: () => LanguageClient | undefined,
serverId: string,
context: vscode.ExtensionContext,
) {
let configuration = getConfiguration(serverId) as unknown as ISettings;
if (configuration.nativeServer === false || configuration.nativeServer === "off") {
return async () => {
vscode.window.showInformationMessage(
"Debug information is only available when using the native server",
);
};
}

const contentProvider = new (class implements vscode.TextDocumentContentProvider {
readonly uri = vscode.Uri.parse("ruff-server-debug://debug");
readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();

async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> {
const lsClient = getClient();
if (!lsClient) {
return "";
}
const textEditor = vscode.window.activeTextEditor;
const notebookEditor = vscode.window.activeNotebookEditor;
const params = {
command: `${serverId}.printDebugInformation`,
arguments: [
{
textDocument: notebookEditor
? { uri: notebookEditor.notebook.uri.toString() }
: textEditor
? { uri: textEditor.document.uri.toString() }
: undefined,
},
],
};
Comment on lines +70 to +83
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this in this PR from main to check if we're in a notebook context and then use text document. I don't think there's any specific information for a notebook cell so using a notebook for them should be fine.

return await lsClient.sendRequest(ExecuteCommandRequest.type, params).then(
(result) => {
if (typeof result === "string") {
return result;
}
// For older Ruff version, we don't return a string but log the information.
return "";
},
async () => {
vscode.window.showErrorMessage(
`Failed to print debug information. Please consider opening an issue at ${ISSUE_TRACKER} with steps to reproduce.`,
);
return "";
},
);
}

get onDidChange(): vscode.Event<vscode.Uri> {
return this.eventEmitter.event;
}
})();

context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider("ruff-server-debug", contentProvider),
);

return async () => {
contentProvider.eventEmitter.fire(contentProvider.uri);
const document = await vscode.workspace.openTextDocument(contentProvider.uri);
const content = document.getText();

// Show the document only if it has content.
if (content.length > 0) {
void (await vscode.window.showTextDocument(document, {
viewColumn: vscode.ViewColumn.Two,
preserveFocus: true,
}));
}
};
}
37 changes: 15 additions & 22 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { ExecuteCommandRequest, LanguageClient } from "vscode-languageclient/node";
import { LanguageClient } from "vscode-languageclient/node";
import { LazyOutputChannel, logger } from "./common/logger";
import {
checkVersion,
Expand All @@ -24,12 +24,21 @@ import {
registerCommand,
} from "./common/vscodeapi";
import { getProjectRoot } from "./common/utilities";
import { executeAutofix, executeFormat, executeOrganizeImports } from "./common/commands";
import {
executeAutofix,
executeFormat,
executeOrganizeImports,
createDebugInformationProvider,
} from "./common/commands";

let lsClient: LanguageClient | undefined;
let restartInProgress = false;
let restartQueued = false;

function getClient(): LanguageClient | undefined {
return lsClient;
}

export async function activate(context: vscode.ExtensionContext): Promise<void> {
// This is required to get server name and module. This should be
// the first thing that we do in this extension.
Expand Down Expand Up @@ -188,26 +197,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
await executeOrganizeImports(lsClient, serverId);
}
}),
registerCommand(`${serverId}.debugInformation`, async () => {
let configuration = getConfiguration(serverId) as unknown as ISettings;
if (!lsClient || !configuration.nativeServer) {
return;
}

const editor = vscode.window.activeTextEditor;
const params = {
command: `${serverId}.printDebugInformation`,
arguments: [
{
textDocument: editor ? { uri: editor.document.uri.toString() } : null,
},
],
};

await lsClient.sendRequest(ExecuteCommandRequest.type, params).then(undefined, async () => {
vscode.window.showErrorMessage("Failed to print debug information.");
});
}),
registerCommand(
`${serverId}.debugInformation`,
createDebugInformationProvider(getClient, serverId, context),
),
Comment on lines +200 to +203
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to avoid creating the command if it is the native server?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. Looking at https://code.visualstudio.com/api/references/contribution-points#contributes.commands, we need to add it to the UI via package.json which makes this not possible (

ruff-vscode/package.json

Lines 434 to 438 in b4e0698

{
"title": "Print debug information (native server only)",
"category": "Ruff",
"command": "ruff.debugInformation"
},
).

registerLanguageStatusItem(serverId, serverName, `${serverId}.showLogs`),
);

Expand Down