Skip to content

Add client-side support for macro expansions #621

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

Closed
wants to merge 8 commits into from
Closed
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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@
"title": "Insert Function Comment",
"category": "Swift"
},
{
"command": "swift.expandMacro",
"title": "Expand Macro",
"category": "Swift"
},
{
"command": "swift.showTestCoverageReport",
"title": "Show Test Coverage Report",
Expand Down Expand Up @@ -991,4 +996,4 @@
"@types/lcov-parse": "1.0.0",
"lcov-parse": "1.0.0"
}
}
}
59 changes: 59 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { DarwinCompatibleTarget, SwiftToolchain } from "./toolchain/toolchain";
import { debugSnippet, runSnippet } from "./SwiftSnippets";
import { debugLaunchConfig, getLaunchConfiguration } from "./debugger/launch";
import { execFile } from "./utilities/utilities";
import { MacroExpansionParams, macroExpansionRequest } from "./sourcekit-lsp/lspExtensions";

/**
* References:
Expand Down Expand Up @@ -477,6 +478,61 @@ function insertFunctionComment(workspaceContext: WorkspaceContext) {
workspaceContext.commentCompletionProvider.insert(activeEditor, line);
}

function expandMacro(workspaceContext: WorkspaceContext) {
const activeEditor = vscode.window.activeTextEditor;
const document = activeEditor?.document;
const selection = activeEditor?.selection;
if (!document || !selection) {
return;
}
return workspaceContext.languageClientManager.useLanguageClient(async (client, token) => {
const params: MacroExpansionParams = {
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document),
range: client.code2ProtocolConverter.asRange(selection),
};
const expansions = await client.sendRequest(macroExpansionRequest, params, token);
if (expansions && expansions.length >= 1) {
// Present the macro expansion using a custom in-memory URI scheme
// TODO: This will currently reregister the provider for this scheme
// every time a new macro expansion is requested. It looks like the
// old expansions will be closed correctly, but perhaps there's
// a more elegant solution to this?
const scheme = "swift-macro-expansion";
const provider = vscode.workspace.registerTextDocumentContentProvider(scheme, {
Copy link
Contributor

Choose a reason for hiding this comment

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

We will need to manage the text document content providers slightly better. Currently you just keep creating them, without cleaning up the unused ones. Adding them to the workspaceContext subscriptions means they get removed when the workspace is closed, but not before then.

provideTextDocumentContent: async uri => expansions[+uri.fragment].sourceText,
});
workspaceContext.subscriptions.push(provider);
console.log(expansions);
// TODO: Use a proper URI scheme. See the discussion in
// https://github.com/apple/sourcekit-lsp/pull/892#discussion_r1358428808
// Perhaps we'd even want to have the server support these URIs directly?
const locations = expansions.map(
(_, i) =>
new vscode.Location(
vscode.Uri.from({
scheme,
path: "Macro Expansion.swift",
fragment: `${i}`,
}),
new vscode.Position(0, 0)
)
);
// TODO: Find a better way to preview multiple expansions, perhaps
// using a diff-style view showing the expandeed fragments within
// the peek editor?
await vscode.commands.executeCommand(
"editor.action.peekLocations",
document.uri,
client.protocol2CodeConverter.asPosition(expansions[0].position),
locations,
"peek"
);
} else {
vscode.window.showWarningMessage("Could not find a macro expansion.");
}
});
}

/** Restart the SourceKit-LSP server */
function restartLSPServer(workspaceContext: WorkspaceContext) {
workspaceContext.languageClientManager.restart();
Expand Down Expand Up @@ -690,6 +746,9 @@ export function register(ctx: WorkspaceContext) {
vscode.commands.registerCommand("swift.insertFunctionComment", () =>
insertFunctionComment(ctx)
),
vscode.commands.registerCommand("swift.expandMacro", () => {
expandMacro(ctx);
}),
vscode.commands.registerCommand("swift.showTestCoverageReport", () =>
showTestCoverageReport(ctx)
),
Expand Down
30 changes: 30 additions & 0 deletions src/sourcekit-lsp/lspExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,33 @@ export const legacyInlayHintsRequest = new langclient.RequestType<
LegacyInlayHint[],
unknown
>("sourcekit-lsp/inlayHints");

export interface MacroExpansionParams {
/**
* The text document.
*/
textDocument: langclient.TextDocumentIdentifier;

/**
* The range within the code at which the macro is used.
*/
range: langclient.Range;
}

export interface MacroExpansion {
/**
* The position in the source file where the expansion would be inserted.
*/
position: langclient.Position;

/**
* The source text of the expansion.
*/
sourceText: string;
}

export const macroExpansionRequest = new langclient.RequestType<
MacroExpansionParams,
MacroExpansion[],
unknown
>("sourcekit-lsp/macroExpansion");