Skip to content

Commit 4d669e8

Browse files
committed
Add Quadroid JCF fork engine (better modern Delphi support, more indentation options).
Strip UTF BOM when reading formatted files back.
1 parent 3f67c82 commit 4d669e8

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"enum": [
7878
"embarcadero",
7979
"jcf",
80+
"jcf-quadroid",
8081
"ptop"
8182
]
8283
},

src/extension.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,13 @@ export async function activate(context: vscode.ExtensionContext) {
189189
const optionJCF = <vscode.MessageItem>{
190190
title: "Jedi Code Format"
191191
};
192+
const optionJCFQ = <vscode.MessageItem>{
193+
title: "JCF (Quadroid)"
194+
};
192195
const optionPTOP = <vscode.MessageItem>{
193196
title: "FreePascal PtoP"
194197
};
195-
vscode.window.showErrorMessage('The "pascal.formatter.engine" setting is not defined. Do you want to download some formatter tool first?', optionJCF, optionPTOP).then(option => {
198+
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 => {
196199
// nothing selected
197200
if (typeof option === 'undefined') {
198201
reject('undefined');
@@ -204,6 +207,10 @@ export async function activate(context: vscode.ExtensionContext) {
204207
vscode.env.openExternal(vscode.Uri.parse("http://jedicodeformat.sourceforge.net/"));
205208
break;
206209

210+
case optionJCFQ.title:
211+
vscode.env.openExternal(vscode.Uri.parse("https://github.com/quadroid/jcf-pascal-format"));
212+
break;
213+
207214
case optionPTOP.title:
208215
vscode.env.openExternal(vscode.Uri.parse("https://www.freepascal.org/tools/ptop.html"));
209216
break;

src/formatter.ts

+24-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import npath = require('path');
1111

1212
/*
1313
14-
linux:
14+
linux:
1515
"pascal.formatter.engine": "ptop",
1616
"pascal.formatter.enginePath": "/usr/bin/ptop",
1717
@@ -40,7 +40,7 @@ export class Formatter {
4040
public format(range: vscode.Range, engine: string, path: string, parameters: string, indent: number, wrapLineLength: number) {
4141

4242
return new Promise((resolve, reject) => {
43-
43+
4444
// entire document - if not range is provided
4545
range = range || new vscode.Range(
4646
0, 0,
@@ -60,42 +60,47 @@ export class Formatter {
6060
if (textToFormat) {
6161

6262
try {
63-
63+
6464
if (engine === 'embarcadero') {
6565
if (parameters !== '') {
66-
configFileParameters = ' -config ' + parameters;
66+
configFileParameters = ' -config ' + parameters;
6767
}
6868
command = "\"" + path + "\" -silent " + configFileParameters + ' "$file" ';
6969
command = command.replace('$file', tempFile);
7070
readFile = tempFile;
7171
} else if (engine === 'ptop') {
7272
if (parameters !== '') {
73-
configFileParameters = ' -c ' + parameters;
73+
configFileParameters = ' -c ' + parameters;
7474
}
75-
75+
7676
let indentConfig = '';
7777
if (indent > 0) {
7878
indentConfig = ' -i ' + indent;
7979
}
80-
80+
8181
let wrapLineLengthConfig = '';
8282
if (wrapLineLength > 0) {
8383
wrapLineLengthConfig = ' -l ' + wrapLineLength;
8484
}
85-
85+
8686
command = "\"" + path + "\" " + configFileParameters + indentConfig + wrapLineLengthConfig + ' "$file" "$outfile" ';
8787
command = command.replace('$file', tempFile);
88-
command = command.replace('$outfile', tempFileOut);
89-
readFile = tempFileOut
88+
command = command.replace('$outfile', tempFileOut);
89+
readFile = tempFileOut
9090
} else { // jcf
9191
if (parameters !== '') {
92-
configFileParameters = ' -config=' + parameters;
92+
configFileParameters = ' -config=' + parameters;
93+
}
94+
if (engine === 'jcf-quadroid') {
95+
command = "\"" + path + "\" " + configFileParameters + ' -out "$fileout" "$file" ';
96+
command = command.replace('$fileout', tempFileOut);
97+
} else {
98+
command = "\"" + path + "\" " + configFileParameters + ' -y -F "$file" ';
9399
}
94-
command = "\"" + path + "\" " + configFileParameters + ' -y -F "$file" ';
95100
command = command.replace('$file', tempFile);
96101
readFile = tempFileOut
97102
}
98-
103+
99104
console.log(command);
100105
cp.exec(command, function(error, stdout, stderr) {
101106
console.log('stdout' + stdout);
@@ -105,7 +110,11 @@ export class Formatter {
105110
reject(stdout.toString());
106111
}
107112
else {
108-
const formattedXml: string = fs.readFileSync(readFile, 'utf8');
113+
let formattedXml: string = fs.readFileSync(readFile, 'utf8');
114+
// remove UTF-8 BOM
115+
if (formattedXml.charCodeAt(0) === 0xfeff) {
116+
formattedXml = formattedXml.substr(1);
117+
}
109118
resolve(formattedXml);
110119
}
111120
});
@@ -123,4 +132,4 @@ export class Formatter {
123132

124133
}
125134

126-
}
135+
}

0 commit comments

Comments
 (0)