Skip to content

Commit

Permalink
Issue 10 (#14)
Browse files Browse the repository at this point in the history
* ISSUE-10 Fix call to execFile with a file larger than 200KB will fail

Update the execFile call to specify the maxBuffer size to be tuneable
and larger than 200KB.  By default the plugin will now look up the
current file size and allocate 2x the buffer to allow room for
whitespace and formatting expansion.

This also fixes the issue where the plugin could not format files
that were large than 200KB.

Add a new configuation option to allow a user to tune the buffer
multiplier to custom values.

Update documentation.

* ISSUE-10 Fix a comment

* Accidentally inserted tabs.  Switch to spaces.

* Capture the default maxBuffer size for execFile. Choose the maximum of
calculated new multiplier buffer size for a larger file or use the default
value for execFile to avoid any pointless buffer resizes.
  • Loading branch information
davidritter authored and welkineins committed Mar 30, 2018
1 parent b118713 commit bacc9d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Open the command palette (Ctrl-Shift-P / Cmd-Shift-P)

- `astyle.astylerc`: specify path of astylerc file. ${workspaceRoot} is supported to enable using different astyle file between projects. e.g. '${workspaceRoot}/.vscode/astylerc'. (default: null, lookup astylerc in original order of astyle)
- `astyle.cmd_options`: array of command line options for astyle. (You should avoid conflicted options) For example: `["--indent=tab", "--break-blocks"]`
- `astyle.max_buffer_multipler`: multiplier to indicate how large an output buffer to allocate for the transformed source code file after Astyle processes it. (default: 2, assumes most formatting/whitespace expansion will result in less than a 2x increase in file size).
- `astyle.additional_languages`: array of additional languages to use astyle as code formatter. For example: `["haxe"]`. (You don't have to enable the language in astyle config)

## FAQ
Expand Down
19 changes: 16 additions & 3 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ class AstyleFormatter {
this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
}

getFilesizeInBytes(filename) {
const stats = fs.statSync(filename)
const fileSizeInBytes = stats.size
return fileSizeInBytes
}

// interface required by vscode.DocumentFormattingEditProvider
provideDocumentFormattingEdits(document, options, token) {
return new Promise((resolve, reject) => {
let astyleBinPath = vscode.workspace.getConfiguration('astyle')['executable'] || 'astyle';
let astyleRcPath = vscode.workspace.getConfiguration('astyle')['astylerc'];
const astyleMaxBufferMultiplier = vscode.workspace.getConfiguration('astyle')['max_buffer_multipler'];
let args = vscode.workspace.getConfiguration('astyle')['cmd_options'] || [];

if (astyleRcPath) {
Expand All @@ -25,16 +32,22 @@ class AstyleFormatter {
});

astyleBinPath = astyleBinPath.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath);

let astyle = childProcess.execFile(astyleBinPath, args, {}, (err, stdout, stderr) => {

const defaultMaxBufferSize = 200 * 1024;
const maxBufferMultiplierSize = this.getFilesizeInBytes(document.fileName) * astyleMaxBufferMultiplier;
// Assume that the formatting/expansion of the document could double it's size
// Make sure the minimum size is still the default for execFile::maxBuffer
const maxBufferSize = Math.max(defaultMaxBufferSize, maxBufferMultiplierSize);

let astyle = childProcess.execFile(astyleBinPath, args, {maxBuffer: maxBufferSize}, (err, stdout, stderr) => {
if (err && err.code == 'ENOENT') {
vscode.window.showErrorMessage('Can\'t find astyle. (' + astyleBinPath + ')');
reject(null);
return;
}

if (err) {
vscode.window.showErrorMessage('Failed to launch astyle. (reason: "' + stderr.split(/\r\n|\r|\n/g).join(',') + '")');
vscode.window.showErrorMessage('Failed to launch astyle. (reason: "' + stderr.split(/\r\n|\r|\n/g).join(',') + ' ' + err + '")');
reject(null);
return;
}
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
"type": ["array"],
"default": [],
"description": "array of command line options"
},
"astyle.max_buffer_multipler": {
"type": "string",
"default": "2",
"description": "the multiplier for the new size of a file after formatting"
},
"astyle.additional_languages": {
"type": ["array"],
Expand Down

0 comments on commit bacc9d9

Please sign in to comment.