Skip to content

Commit efa3093

Browse files
committed
Print comments in a diffrent column
1 parent f2654c6 commit efa3093

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

src/compiler/commandLineParser.ts

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,12 @@ namespace ts {
980980
}
981981
}
982982

983+
function makePadding(paddingLength: number): string {
984+
return Array(paddingLength + 1).join(" ");
985+
}
986+
983987
function writeConfigurations() {
988+
// Filter applicable options to place in the file
984989
const categorizedOptions = reduceLeft(
985990
filter(optionDeclarations, o => o.category !== Diagnostics.CommandLine_Options && o.category !== Diagnostics.Advanced_Options),
986991
(memo, value) => {
@@ -990,44 +995,59 @@ namespace ts {
990995
}
991996
return memo;
992997
}, <MapLike<CommandLineOption[]>>{});
993-
const knownKesyCount = getOwnKeys(configurations.compilerOptions).length;
994998

995-
const newLine = "\n";
996-
const tab = " ";
997-
998-
let result = "";
999+
// Serialize all options and thier descriptions
1000+
let marginLength = 0;
9991001
let seenKnownKeys = 0;
1000-
result += `{${newLine}`;
1001-
result += `${tab}"compilerOptions": {${newLine}`;
1002+
const nameColumn: string[] = [];
1003+
const descriptionColumn: string[] = [];
1004+
const knownKesyCount = getOwnKeys(configurations.compilerOptions).length;
10021005
for (const category in categorizedOptions) {
1003-
result += `${tab}${tab}// ${category}${newLine}`;
1004-
result += `${newLine}`;
1006+
if (nameColumn.length !== 0) {
1007+
nameColumn.push("");
1008+
descriptionColumn.push("");
1009+
}
1010+
nameColumn.push(`/* ${category} */`);
1011+
descriptionColumn.push("");
10051012
for (const option of categorizedOptions[category]) {
1006-
result += `${tab}${tab}// ${option.description && getLocaleSpecificMessage(option.description) || option.name}${newLine}`;
1013+
let optionName;
10071014
if (configurations.compilerOptions[option.name]) {
1008-
result += `${tab}${tab}"${option.name}": ${JSON.stringify(configurations.compilerOptions[option.name])}${(seenKnownKeys += 1) === knownKesyCount ? "" : ","}${newLine}`;
1015+
optionName = `"${option.name}": ${JSON.stringify(configurations.compilerOptions[option.name])}${(seenKnownKeys += 1) === knownKesyCount ? "" : ","}`;
10091016
}
10101017
else {
1011-
result += `${tab}${tab}// "${option.name}": ${JSON.stringify(getDefaultValueForOption(option))},${newLine}`;
1018+
optionName = `// "${option.name}": ${JSON.stringify(getDefaultValueForOption(option))},`;
10121019
}
1013-
result += `${newLine}`;
1020+
nameColumn.push(optionName);
1021+
descriptionColumn.push(`/* ${option.description && getLocaleSpecificMessage(option.description) || option.name} */`);
1022+
marginLength = Math.max(optionName.length, marginLength);
10141023
}
1015-
result += `${newLine}`;
1024+
}
1025+
1026+
// Write the output
1027+
const tab = makePadding(2);
1028+
const result: string[] = [];
1029+
result.push(`{`);
1030+
result.push(`${tab}"compilerOptions": {`);
1031+
// Print out each row, aligning all the descriptions on the same column.
1032+
for (let i = 0; i < nameColumn.length; i++) {
1033+
const optionName = nameColumn[i];
1034+
const description = descriptionColumn[i];
1035+
result.push(tab + tab + optionName + makePadding(marginLength - optionName.length + 2) + description);
10161036
}
10171037
if (configurations.files && configurations.files.length) {
1018-
result += `${tab}},${newLine}`;
1019-
result += `${tab}"files": [${newLine}`;
1038+
result.push(`${tab}},`);
1039+
result.push(`${tab}"files": [`);
10201040
for (let i = 0; i < configurations.files.length; i++) {
1021-
result += `${tab}${tab}${JSON.stringify(configurations.files[i])}${i === configurations.files.length - 1 ? "" : ","}${newLine}`;
1041+
result.push(`${tab}${tab}${JSON.stringify(configurations.files[i])}${i === configurations.files.length - 1 ? "" : ","}`);
10221042
}
1023-
result += `${tab}]${newLine}`;
1043+
result.push(`${tab}]`);
10241044
}
10251045
else {
1026-
result += `${tab}}${newLine}`;
1046+
result.push(`${tab}}`);
10271047
}
1028-
result += `}${newLine}`;
1048+
result.push(`}`);
10291049

1030-
return result;
1050+
return result.join(sys.newLine);
10311051
}
10321052
}
10331053

0 commit comments

Comments
 (0)