|
1 |
| -import * as prettier from 'prettier'; |
| 1 | + |
| 2 | +const { Worker } = require('worker_threads'); |
| 3 | + |
2 | 4 | import * as vscode from 'vscode';
|
3 | 5 | import * as path from 'path';
|
4 |
| -import * as workspaceUtil from '../workspaceUtil'; |
5 |
| -import * as solidityprettier from 'prettier-plugin-solidity'; |
6 | 6 |
|
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 | + }); |
31 | 43 | }
|
| 44 | + |
0 commit comments