Skip to content

Commit 97a2d57

Browse files
#445 Fix prettier with worker thread
1 parent 815c4ac commit 97a2d57

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"nethereum",
1111
"solhint"
1212
],
13-
"version": "0.0.169",
13+
"version": "0.0.170",
1414
"publisher": "JuanBlanco",
1515
"license": "MIT",
1616
"engines": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { parentPort } = require('worker_threads');
2+
3+
parentPort.on('message', async (task) => {
4+
try {
5+
// Dynamically import prettier and the plugin
6+
const prettier = require(task.prettierPath);
7+
const pluginPath = require(task.pluginPath);
8+
9+
// Resolve config
10+
const config = await prettier.resolveConfig(task.documentPath);
11+
if (config !== null) {
12+
await prettier.clearConfigCache();
13+
}
14+
15+
// Merge user config with default options
16+
const options = { ...task.options, ...config, plugins: [pluginPath] };
17+
const formatted = prettier.format(task.source, options);
18+
19+
parentPort.postMessage({ success: true, formatted });
20+
} catch (error) {
21+
parentPort.postMessage({ success: false, error: error.message });
22+
}
23+
});
+40-27
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
1-
import * as prettier from 'prettier';
1+
2+
const { Worker } = require('worker_threads');
3+
24
import * as vscode from 'vscode';
35
import * as path from 'path';
4-
import * as workspaceUtil from '../workspaceUtil';
5-
import * as solidityprettier from 'prettier-plugin-solidity';
66

7-
export async function formatDocument(document: vscode.TextDocument, context: vscode.ExtensionContext): Promise<vscode.TextEdit[]> {
8-
const rootPath = workspaceUtil.getCurrentProjectInWorkspaceRootFsPath();
9-
const ignoreOptions = { ignorePath: path.join(rootPath, '.prettierignore') };
10-
const fileInfo = await prettier.getFileInfo(document.uri.fsPath, ignoreOptions);
11-
if (!fileInfo.ignored) {
12-
const source = document.getText();
13-
// const pluginPath = path.join(context.extensionPath, 'node_modules', 'prettier-plugin-solidity');
14-
const options = {
15-
'parser': 'solidity-parse',
16-
'pluginSearchDirs': [context.extensionPath],
17-
'plugins': [solidityprettier],
18-
};
19-
//
20-
const config = await prettier.resolveConfig(document.uri.fsPath);
21-
if (config !== null) {
22-
await prettier.clearConfigCache();
23-
}
24-
Object.assign(options, config);
25-
const firstLine = document.lineAt(0);
26-
const lastLine = document.lineAt(document.lineCount - 1);
27-
const fullTextRange = new vscode.Range(firstLine.range.start, lastLine.range.end);
28-
const formatted = await prettier.format(source, options);
29-
return [vscode.TextEdit.replace(fullTextRange, formatted)];
30-
}
7+
export async function formatDocument(document, context) : Promise<vscode.TextEdit[]> {
8+
const source = document.getText();
9+
const documentPath = document.uri.fsPath;
10+
const pluginPathFile = path.join(context.extensionPath, 'node_modules', 'prettier-plugin-solidity', 'dist','standalone.cjs');
11+
const prettierPathFile = path.join(context.extensionPath, 'node_modules', 'prettier');
12+
const pluginPath = pluginPathFile ;
13+
const prettierPath = prettierPathFile;
14+
const options = {
15+
parser: 'solidity-parse',
16+
pluginSearchDirs: [context.extensionPath],
17+
};
18+
19+
return new Promise((resolve, reject) => {
20+
const workerPath = path.join(__dirname, 'formatterPrettierWorker.js');
21+
let uri = vscode.Uri.file(workerPath).fsPath;
22+
const worker = new Worker(uri.toString());
23+
worker.on('message', (response) => {
24+
worker.terminate();
25+
if (response.success) {
26+
const firstLine = document.lineAt(0);
27+
const lastLine = document.lineAt(document.lineCount - 1);
28+
const fullTextRange = new vscode.Range(firstLine.range.start, lastLine.range.end);
29+
resolve([vscode.TextEdit.replace(fullTextRange, response.formatted)]);
30+
} else {
31+
console.error(response.error);
32+
resolve([]);
33+
}
34+
});
35+
worker.on('error', (err) => {
36+
worker.terminate();
37+
console.error(err);
38+
resolve([]);
39+
});
40+
41+
worker.postMessage({ source, options, documentPath, prettierPath, pluginPath });
42+
});
3143
}
44+

0 commit comments

Comments
 (0)