Teach formatter to optionally add two spaces before end-of-line comments. (#855)

* Teach formatter to add two spaces before end-of-line comments.
This commit is contained in:
Tom Moertel
2025-05-11 16:19:19 -04:00
committed by GitHub
parent 4d00f9f41a
commit af6df23306
4 changed files with 65 additions and 2 deletions

View File

@@ -17,6 +17,7 @@ function normalizeLineEndings(str: string) {
const defaultOptions: FormatterOptions = {
maxEmptyLines: 2,
denseFunctionParameters: false,
spacesBeforeEndOfLineComment: 1,
};
function get_options(folder: fs.Dirent) {

View File

@@ -0,0 +1,47 @@
# --- IN ---
pass # Comment 1.
pass ## Comment 2.
# --- IN ---
pass # Comment 3.
pass ## Comment 4.
# --- OUT ---
pass # Comment 3.
pass ## Comment 4.
# --- CONFIG ALL ---
{"spacesBeforeEndOfLineComment": 1}
# --- IN ---
pass # Comment 5.
pass ## Comment 6.
# --- IN ---
pass # Comment 7.
pass ## Comment 8.
# --- OUT ---
pass # Comment 7.
pass ## Comment 8.
# --- CONFIG ALL ---
{"spacesBeforeEndOfLineComment": 2}
# --- IN ---
pass # Comment 9.
pass ## Comment A.
# --- OUT ---
pass # Comment 9.
pass ## Comment A.
# --- IN ---
pass # Comment B.
pass ## Comment C.
# --- IN ---
pass # Comment D.
pass ## Comment E.
# --- OUT ---
pass # Comment D.
pass ## Comment E.

View File

@@ -56,12 +56,14 @@ interface Token {
export interface FormatterOptions {
maxEmptyLines: 0 | 1 | 2;
denseFunctionParameters: boolean;
spacesBeforeEndOfLineComment: 1 | 2;
}
function get_formatter_options() {
const options: FormatterOptions = {
maxEmptyLines: get_configuration("formatter.maxEmptyLines") === "1" ? 1 : 2,
denseFunctionParameters: get_configuration("formatter.denseFunctionParameters"),
spacesBeforeEndOfLineComment: get_configuration("formatter.spacesBeforeEndOfLineComment") === "1" ? 1 : 2,
};
return options;
@@ -135,8 +137,8 @@ function between(tokens: Token[], current: number, options: FormatterOptions) {
if (!prev) return "";
if (next === "##") return " ";
if (next === "#") return " ";
if (next === "##") return options.spacesBeforeEndOfLineComment === 2 ? " " : " ";
if (next === "#") return options.spacesBeforeEndOfLineComment === 2 ? " " : " ";
if (prevToken.skip && nextToken.skip) return "";
if (prev === "(") return "";