diff --git a/package.json b/package.json index 131e2ce..31c1fe7 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "nethereum", "solhint" ], - "version": "0.0.169", + "version": "0.0.170", "publisher": "JuanBlanco", "license": "MIT", "engines": { diff --git a/src/client/formatter/formatterPrettierWorker.js b/src/client/formatter/formatterPrettierWorker.js new file mode 100644 index 0000000..7d5fcdc --- /dev/null +++ b/src/client/formatter/formatterPrettierWorker.js @@ -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 }); + } +}); \ No newline at end of file diff --git a/src/client/formatter/prettierFormatter.ts b/src/client/formatter/prettierFormatter.ts index 6c941d4..70e140a 100644 --- a/src/client/formatter/prettierFormatter.ts +++ b/src/client/formatter/prettierFormatter.ts @@ -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 { - 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 { + 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 }); + }); } +