Skip to content

Commit

Permalink
Merge pull request #52 from quadroid/master
Browse files Browse the repository at this point in the history
Add Quadroid JEDI Code Format engine
  • Loading branch information
alefragnani authored Feb 9, 2023
2 parents dd5db8d + 4d669e8 commit 32951e3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"enum": [
"embarcadero",
"jcf",
"jcf-quadroid",
"ptop"
]
},
Expand Down
9 changes: 8 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,13 @@ export async function activate(context: vscode.ExtensionContext) {
const optionJCF = <vscode.MessageItem>{
title: "Jedi Code Format"
};
const optionJCFQ = <vscode.MessageItem>{
title: "JCF (Quadroid)"
};
const optionPTOP = <vscode.MessageItem>{
title: "FreePascal PtoP"
};
vscode.window.showErrorMessage('The "pascal.formatter.engine" setting is not defined. Do you want to download some formatter tool first?', optionJCF, optionPTOP).then(option => {
vscode.window.showErrorMessage('The "pascal.formatter.engine" setting is not defined. Do you want to download some formatter tool first?', optionJCF, optionJCFQ, optionPTOP).then(option => {
// nothing selected
if (typeof option === 'undefined') {
reject('undefined');
Expand All @@ -204,6 +207,10 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.env.openExternal(vscode.Uri.parse("http://jedicodeformat.sourceforge.net/"));
break;

case optionJCFQ.title:
vscode.env.openExternal(vscode.Uri.parse("https://github.com/quadroid/jcf-pascal-format"));
break;

case optionPTOP.title:
vscode.env.openExternal(vscode.Uri.parse("https://www.freepascal.org/tools/ptop.html"));
break;
Expand Down
39 changes: 24 additions & 15 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import npath = require('path');

/*
linux:
linux:
"pascal.formatter.engine": "ptop",
"pascal.formatter.enginePath": "/usr/bin/ptop",
Expand Down Expand Up @@ -40,7 +40,7 @@ export class Formatter {
public format(range: vscode.Range, engine: string, path: string, parameters: string, indent: number, wrapLineLength: number) {

return new Promise((resolve, reject) => {

// entire document - if not range is provided
range = range || new vscode.Range(
0, 0,
Expand All @@ -60,42 +60,47 @@ export class Formatter {
if (textToFormat) {

try {

if (engine === 'embarcadero') {
if (parameters !== '') {
configFileParameters = ' -config ' + parameters;
configFileParameters = ' -config ' + parameters;
}
command = "\"" + path + "\" -silent " + configFileParameters + ' "$file" ';
command = command.replace('$file', tempFile);
readFile = tempFile;
} else if (engine === 'ptop') {
if (parameters !== '') {
configFileParameters = ' -c ' + parameters;
configFileParameters = ' -c ' + parameters;
}

let indentConfig = '';
if (indent > 0) {
indentConfig = ' -i ' + indent;
}

let wrapLineLengthConfig = '';
if (wrapLineLength > 0) {
wrapLineLengthConfig = ' -l ' + wrapLineLength;
}

command = "\"" + path + "\" " + configFileParameters + indentConfig + wrapLineLengthConfig + ' "$file" "$outfile" ';
command = command.replace('$file', tempFile);
command = command.replace('$outfile', tempFileOut);
readFile = tempFileOut
command = command.replace('$outfile', tempFileOut);
readFile = tempFileOut
} else { // jcf
if (parameters !== '') {
configFileParameters = ' -config=' + parameters;
configFileParameters = ' -config=' + parameters;
}
if (engine === 'jcf-quadroid') {
command = "\"" + path + "\" " + configFileParameters + ' -out "$fileout" "$file" ';
command = command.replace('$fileout', tempFileOut);
} else {
command = "\"" + path + "\" " + configFileParameters + ' -y -F "$file" ';
}
command = "\"" + path + "\" " + configFileParameters + ' -y -F "$file" ';
command = command.replace('$file', tempFile);
readFile = tempFileOut
}

console.log(command);
cp.exec(command, function(error, stdout, stderr) {
console.log('stdout' + stdout);
Expand All @@ -105,7 +110,11 @@ export class Formatter {
reject(stdout.toString());
}
else {
const formattedXml: string = fs.readFileSync(readFile, 'utf8');
let formattedXml: string = fs.readFileSync(readFile, 'utf8');
// remove UTF-8 BOM
if (formattedXml.charCodeAt(0) === 0xfeff) {
formattedXml = formattedXml.substr(1);
}
resolve(formattedXml);
}
});
Expand All @@ -123,4 +132,4 @@ export class Formatter {

}

}
}

0 comments on commit 32951e3

Please sign in to comment.