-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#445 Fix prettier with worker thread
- Loading branch information
1 parent
815c4ac
commit 97a2d57
Showing
3 changed files
with
64 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
} | ||
|