Skip to content

Commit

Permalink
#445 Fix prettier with worker thread
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfranblanco committed Feb 15, 2024
1 parent 815c4ac commit 97a2d57
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"nethereum",
"solhint"
],
"version": "0.0.169",
"version": "0.0.170",
"publisher": "JuanBlanco",
"license": "MIT",
"engines": {
Expand Down
23 changes: 23 additions & 0 deletions src/client/formatter/formatterPrettierWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { parentPort } = require('worker_threads');

parentPort.on('message', async (task) => {
try {
// Dynamically import prettier and the plugin
const prettier = require(task.prettierPath);
const pluginPath = require(task.pluginPath);

// Resolve config
const config = await prettier.resolveConfig(task.documentPath);
if (config !== null) {
await prettier.clearConfigCache();
}

// Merge user config with default options
const options = { ...task.options, ...config, plugins: [pluginPath] };
const formatted = prettier.format(task.source, options);

parentPort.postMessage({ success: true, formatted });
} catch (error) {
parentPort.postMessage({ success: false, error: error.message });
}
});
67 changes: 40 additions & 27 deletions src/client/formatter/prettierFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
import * as prettier from 'prettier';

const { Worker } = require('worker_threads');

import * as vscode from 'vscode';
import * as path from 'path';
import * as workspaceUtil from '../workspaceUtil';
import * as solidityprettier from 'prettier-plugin-solidity';

export async function formatDocument(document: vscode.TextDocument, context: vscode.ExtensionContext): Promise<vscode.TextEdit[]> {
const rootPath = workspaceUtil.getCurrentProjectInWorkspaceRootFsPath();
const ignoreOptions = { ignorePath: path.join(rootPath, '.prettierignore') };
const fileInfo = await prettier.getFileInfo(document.uri.fsPath, ignoreOptions);
if (!fileInfo.ignored) {
const source = document.getText();
// const pluginPath = path.join(context.extensionPath, 'node_modules', 'prettier-plugin-solidity');
const options = {
'parser': 'solidity-parse',
'pluginSearchDirs': [context.extensionPath],
'plugins': [solidityprettier],
};
//
const config = await prettier.resolveConfig(document.uri.fsPath);
if (config !== null) {
await prettier.clearConfigCache();
}
Object.assign(options, config);
const firstLine = document.lineAt(0);
const lastLine = document.lineAt(document.lineCount - 1);
const fullTextRange = new vscode.Range(firstLine.range.start, lastLine.range.end);
const formatted = await prettier.format(source, options);
return [vscode.TextEdit.replace(fullTextRange, formatted)];
}
export async function formatDocument(document, context) : Promise<vscode.TextEdit[]> {
const source = document.getText();
const documentPath = document.uri.fsPath;
const pluginPathFile = path.join(context.extensionPath, 'node_modules', 'prettier-plugin-solidity', 'dist','standalone.cjs');
const prettierPathFile = path.join(context.extensionPath, 'node_modules', 'prettier');
const pluginPath = pluginPathFile ;
const prettierPath = prettierPathFile;
const options = {
parser: 'solidity-parse',
pluginSearchDirs: [context.extensionPath],
};

return new Promise((resolve, reject) => {
const workerPath = path.join(__dirname, 'formatterPrettierWorker.js');
let uri = vscode.Uri.file(workerPath).fsPath;
const worker = new Worker(uri.toString());
worker.on('message', (response) => {
worker.terminate();
if (response.success) {
const firstLine = document.lineAt(0);
const lastLine = document.lineAt(document.lineCount - 1);
const fullTextRange = new vscode.Range(firstLine.range.start, lastLine.range.end);
resolve([vscode.TextEdit.replace(fullTextRange, response.formatted)]);
} else {
console.error(response.error);
resolve([]);
}
});
worker.on('error', (err) => {
worker.terminate();
console.error(err);
resolve([]);
});

worker.postMessage({ source, options, documentPath, prettierPath, pluginPath });
});
}

0 comments on commit 97a2d57

Please sign in to comment.