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

Add macro expansion on hover #165

Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions package.json
Expand Up @@ -162,6 +162,11 @@
"default": false,
"description": "Show symbol information when mouse is hover."
},
"crystal-lang.macroExpansionHover": {
"type": "boolean",
"default": false,
"description": "Show macro expansion when mouse hovers over a macro."
},
"crystal-lang.completion": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -215,6 +220,20 @@
"description": "[Reload required]\nEnables support for Windows Subsystem Linux."
}
}
},
"commands": [
{
"command": "crystal-lang.showMacroExpansion",
"title": "Show macro expansion"
}
],
"menus": {
"editor/context": [
{
"command": "crystal-lang.showMacroExpansion",
"group": "z_commands"
}
]
}
},
"scripts": {
Expand Down
93 changes: 93 additions & 0 deletions src/crystalMacro.ts
@@ -0,0 +1,93 @@
import * as vscode from "vscode";
import { spawnSync } from 'node:child_process';

export async function registerCrystalMacroHoverProvider(context: vscode.ExtensionContext) {
let macroExpansionChannel = vscode.window.createOutputChannel("Crystal Macro Expansion", "crystal")

vscode.languages.registerHoverProvider({ scheme: 'file', language: 'crystal' }, {
provideHover(document, position, token) {
return expandMacroToHover(document, position);
}
})

vscode.commands.registerCommand('crystal-lang.showMacroExpansion', function() {
const activeEditor = vscode.window.activeTextEditor;
const document = activeEditor.document;
const position = activeEditor.selection.active;
const positionString = `${document.uri.path}:${position.line + 1}:${position.character + 1}`;

let stdout = execExpandMacro(document, position)

if (stdout === undefined || stdout === "") {
return;
}

if (stdout.startsWith("no expansion found")) {
stdout = `# No macro expansion for ${positionString}`;
} else {
stdout = `# Macro expansion for ${positionString}:\n\n${stdout}\n`
}

macroExpansionChannel.appendLine(stdout)
macroExpansionChannel.show()
})
}

function execExpandMacro(document: vscode.TextDocument, position: vscode.Position): string {
const config = vscode.workspace.getConfiguration("crystal-lang")

let currentWorkspacePath = vscode.workspace.getWorkspaceFolder(document.uri).uri.path

let documentPath = document.uri.path;
let line = position.line + 1;
let column = position.character + 1;

let expandArgs: string[] = ['tool', 'expand']

let mainFile = '';
if (config["mainFile"]) {
mainFile = config["mainFile"].replace("${workspaceRoot}", currentWorkspacePath)

if (mainFile !== documentPath) {
expandArgs.push(mainFile)
}
}

expandArgs.push(documentPath, '--cursor', `${documentPath}:${line}:${column}`)

let result = spawnSync(
config["compiler"], expandArgs,
{ cwd: currentWorkspacePath }
);

if (result.status !== 0) {
console.error(result.stderr.toString());
return;
}

let stdout = result.output.join('\n').replace(/^\n+|\n+$/g, '');

return stdout
}

function expandMacroToHover(document: vscode.TextDocument, position: vscode.Position): vscode.Hover {
const config = vscode.workspace.getConfiguration("crystal-lang")
if (!config["macroExpansionHover"]) {
return;
}

let stdout = execExpandMacro(document, position)

if (stdout.startsWith("no expansion found")) {
return;
}

let markdownResult = new vscode.MarkdownString("```\n" + stdout + "\n```");

let lineText = document.lineAt(position).text;
let lineStart = document.offsetAt(new vscode.Position(position.line, 0));
let lineEnd = lineStart + lineText.length;
let range = new vscode.Range(document.positionAt(lineStart), document.positionAt(lineEnd));

return new vscode.Hover(markdownResult, range)
}
3 changes: 3 additions & 0 deletions src/crystalMain.ts
Expand Up @@ -10,6 +10,7 @@ import { CrystalDocumentSymbolProvider } from "./crystalSymbols"
import { CrystalCompletionItemProvider } from "./crystalCompletion"
import { CrystalImplementationsProvider } from "./crystalImplementations"
import { registerCrystalTask } from "./crystalTasks"
import { registerCrystalMacroHoverProvider } from "./crystalMacro"

// Language configuration for identation and patterns. Based on vscode-ruby
const crystalConfiguration = {
Expand Down Expand Up @@ -47,6 +48,8 @@ export async function activate(context: vscode.ExtensionContext) {
// Register Tasks
registerCrystalTask(context)

registerCrystalMacroHoverProvider(context)

// Detect server and set configuration
let scry = config["server"]
if (fs.existsSync(scry)) {
Expand Down