Skip to content

Commit af6df23

Browse files
authored
Teach formatter to optionally add two spaces before end-of-line comments. (#855)
* Teach formatter to add two spaces before end-of-line comments.
1 parent 4d00f9f commit af6df23

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@
307307
"default": false,
308308
"description": "Whether extra space should be removed from function parameter lists"
309309
},
310+
"godotTools.formatter.spacesBeforeEndOfLineComment": {
311+
"type": "string",
312+
"enum": [
313+
"1",
314+
"2"
315+
],
316+
"enumDescriptions": [
317+
"1 space before EOL comments # Like this.",
318+
"2 spaces before EOL comments  # Like this."
319+
],
320+
"default": "1",
321+
"description": "Number of spaces before an end-of-line comment"
322+
},
310323
"godotTools.lsp.serverHost": {
311324
"type": "string",
312325
"default": "127.0.0.1",

src/formatter/formatter.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function normalizeLineEndings(str: string) {
1717
const defaultOptions: FormatterOptions = {
1818
maxEmptyLines: 2,
1919
denseFunctionParameters: false,
20+
spacesBeforeEndOfLineComment: 1,
2021
};
2122

2223
function get_options(folder: fs.Dirent) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# --- IN ---
2+
pass # Comment 1.
3+
pass ## Comment 2.
4+
5+
# --- IN ---
6+
pass # Comment 3.
7+
pass ## Comment 4.
8+
# --- OUT ---
9+
pass # Comment 3.
10+
pass ## Comment 4.
11+
12+
13+
# --- CONFIG ALL ---
14+
{"spacesBeforeEndOfLineComment": 1}
15+
16+
# --- IN ---
17+
pass # Comment 5.
18+
pass ## Comment 6.
19+
20+
# --- IN ---
21+
pass # Comment 7.
22+
pass ## Comment 8.
23+
# --- OUT ---
24+
pass # Comment 7.
25+
pass ## Comment 8.
26+
27+
28+
# --- CONFIG ALL ---
29+
{"spacesBeforeEndOfLineComment": 2}
30+
31+
# --- IN ---
32+
pass # Comment 9.
33+
pass ## Comment A.
34+
# --- OUT ---
35+
pass # Comment 9.
36+
pass ## Comment A.
37+
38+
# --- IN ---
39+
pass # Comment B.
40+
pass ## Comment C.
41+
42+
# --- IN ---
43+
pass # Comment D.
44+
pass ## Comment E.
45+
# --- OUT ---
46+
pass # Comment D.
47+
pass ## Comment E.

src/formatter/textmate.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ interface Token {
5656
export interface FormatterOptions {
5757
maxEmptyLines: 0 | 1 | 2;
5858
denseFunctionParameters: boolean;
59+
spacesBeforeEndOfLineComment: 1 | 2;
5960
}
6061

6162
function get_formatter_options() {
6263
const options: FormatterOptions = {
6364
maxEmptyLines: get_configuration("formatter.maxEmptyLines") === "1" ? 1 : 2,
6465
denseFunctionParameters: get_configuration("formatter.denseFunctionParameters"),
66+
spacesBeforeEndOfLineComment: get_configuration("formatter.spacesBeforeEndOfLineComment") === "1" ? 1 : 2,
6567
};
6668

6769
return options;
@@ -135,8 +137,8 @@ function between(tokens: Token[], current: number, options: FormatterOptions) {
135137

136138
if (!prev) return "";
137139

138-
if (next === "##") return " ";
139-
if (next === "#") return " ";
140+
if (next === "##") return options.spacesBeforeEndOfLineComment === 2 ? " " : " ";
141+
if (next === "#") return options.spacesBeforeEndOfLineComment === 2 ? " " : " ";
140142
if (prevToken.skip && nextToken.skip) return "";
141143

142144
if (prev === "(") return "";

0 commit comments

Comments
 (0)