Skip to content

Commit c07fe37

Browse files
authored
Fix bad formatting on several operators (#605)
Many, many formatter and syntax highlighting improvements.
1 parent 17af8e2 commit c07fe37

File tree

26 files changed

+357
-65
lines changed

26 files changed

+357
-65
lines changed

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,24 @@
271271
"default": true,
272272
"description": "Whether to reveal the terminal when launching the Godot Editor"
273273
},
274+
"godotTools.formatter.emptyLinesBeforeFunctions": {
275+
"type": "string",
276+
"enum": [
277+
"one",
278+
"two"
279+
],
280+
"enumDescriptions": [
281+
"One line before functions. A more compact style.",
282+
"Two lines before functions. Conforms to the official GDScript style guide."
283+
],
284+
"default": "two",
285+
"description": "Number of blank lines to leave before functions."
286+
},
287+
"godotTools.formatter.denseFunctionDeclarations": {
288+
"type": "boolean",
289+
"default": false,
290+
"description": "Whether extra space should be removed from function declarations"
291+
},
274292
"godotTools.lsp.serverProtocol": {
275293
"type": [
276294
"string"

src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from "path";
1+
import * as path from "node:path";
22
import * as vscode from "vscode";
33
import { attemptSettingsUpdate, get_extension_uri, clean_godot_path } from "./utils";
44
import {
@@ -81,6 +81,10 @@ export function activate(context: vscode.ExtensionContext) {
8181

8282
async function initial_setup() {
8383
const projectVersion = await get_project_version();
84+
if (projectVersion === undefined) {
85+
// TODO: actually handle this?
86+
return;
87+
}
8488
const settingName = `editorPath.godot${projectVersion[0]}`;
8589
const result = verify_godot_version(get_configuration(settingName), projectVersion[0]);
8690
const godotPath = result.godotPath;

src/formatter/formatter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from "vscode";
2-
import * as path from "path";
3-
import * as fs from "fs";
2+
import * as path from "node:path";
3+
import * as fs from "node:fs";
44

55
import { format_document } from "./textmate";
66

src/formatter/snapshot_template/in.gd

Whitespace-only changes.

src/formatter/snapshot_template/out.gd

Whitespace-only changes.

src/formatter/snapshots/arithmetic/in.gd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func f():
2626
a= 1.0* .2
2727
a =1.0 * 2.
2828

29+
a = 10**10
30+
a = min(10, 10**10)
31+
2932
a= a% b
3033
a =1%2
3134

@@ -36,3 +39,16 @@ func f():
3639

3740
var v = Vector2( 1 , - 1 )
3841
var w = Vector2(1,10-1)
42+
43+
print( - 1 )
44+
print( 1 - 1 )
45+
46+
print( - 1 + (1-1))
47+
print( - 1 + (-1-1))
48+
49+
if a > - 1:
50+
if a < - 1:
51+
if a == - 1:
52+
pass
53+
54+
return - 1

src/formatter/snapshots/arithmetic/out.gd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func f():
2626
a = 1.0 * .2
2727
a = 1.0 * 2.
2828

29+
a = 10 ** 10
30+
a = min(10, 10 ** 10)
31+
2932
a = a % b
3033
a = 1 % 2
3134

@@ -36,3 +39,16 @@ func f():
3639

3740
var v = Vector2(1, -1)
3841
var w = Vector2(1, 10 - 1)
42+
43+
print(-1)
44+
print(1 - 1)
45+
46+
print(-1 + (1 - 1))
47+
print(-1 + (-1 - 1))
48+
49+
if a > -1:
50+
if a < -1:
51+
if a == -1:
52+
pass
53+
54+
return -1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func f():
2+
# arithmetic
3+
x += 1
4+
x -= 1
5+
x *= 1
6+
x /= 1
7+
x %= 1
8+
9+
# bitwise
10+
x |= 1
11+
x &= 1
12+
x ~= 1
13+
x /= 1
14+
x >>= 1
15+
x <<= 1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func f():
2+
# arithmetic
3+
x += 1
4+
x -= 1
5+
x *= 1
6+
x /= 1
7+
x %= 1
8+
9+
# bitwise
10+
x |= 1
11+
x &= 1
12+
x ~= 1
13+
x /= 1
14+
x >>= 1
15+
x <<= 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func f():
2+
collision_mask = 1 << 1 | 1 << 3
3+
collision_mask = 1 << 1 & 1 << 3
4+
collision_mask = ~1
5+
collision_mask = 1 ^ ~ 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func f():
2+
collision_mask = 1 << 1 | 1 << 3
3+
collision_mask = 1 << 1 & 1 << 3
4+
collision_mask = ~1
5+
collision_mask = 1 ^ ~1

src/formatter/snapshots/boolean-operators/in.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ func f():
88
func g():
99
print(true and ( not false ) or ( true))
1010
print(true and not false or not (true) )
11+
12+
func h():
13+
print(true && ( not false ) || ( true))
14+
print(true && not false || not (true) )

src/formatter/snapshots/boolean-operators/out.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ func f():
88
func g():
99
print(true and (not false) or (true))
1010
print(true and not false or not (true))
11+
12+
func h():
13+
print(true && (not false) || (true))
14+
print(true && not false || not (true))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11

22

3+
4+
class Test:
5+
6+
7+
8+
9+
func _ready():
10+
11+
12+
pass
13+
14+
15+
316
func test():
417

18+
519
pass
620

21+
22+
23+
24+
# comments
25+
func with_comments():
26+
27+
pass
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
class Test:
2+
3+
4+
func _ready():
5+
6+
pass
7+
8+
19
func test():
210

311
pass
12+
13+
14+
# comments
15+
func with_comments():
16+
17+
pass

src/formatter/snapshots/initialization/out.gd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ var a = 10
22
var b := 10
33
var c: int = 10
44

5-
func f(b:=10):
6-
return func(c:=10):
5+
func f(b := 10):
6+
return func(c := 10):
77
pass
88

9-
func f(b: int=10):
10-
return func(c: int=10):
9+
func f(b: int = 10):
10+
return func(c: int = 10):
1111
pass
1212

13-
func f(b=10):
14-
return func(c=10):
13+
func f(b = 10):
14+
return func(c = 10):
1515
pass
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func f():
2+
const a = preload("res://a.gd")
3+
const b = load("res://b.gd")
4+
5+
var origin: Vector2 = Vector2.ZERO
6+
origin.x = 1
7+
var andigin: Vector2 = Vector2.ZERO
8+
andigin.x = 1
9+
10+
print(a)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func f():
2+
const a = preload("res://a.gd")
3+
const b = load("res://b.gd")
4+
5+
var origin: Vector2 = Vector2.ZERO
6+
origin.x = 1
7+
var andigin: Vector2 = Vector2.ZERO
8+
andigin.x = 1
9+
10+
print(a)

src/formatter/snapshots/match/in.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func f(x):
2+
match x:
3+
var y when y>20:
4+
pass

src/formatter/snapshots/match/out.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func f(x):
2+
match x:
3+
var y when y > 20:
4+
pass

src/formatter/snapshots/nodepaths/in.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ var a = $"%Child/GrandChild".some_method()
6969
var a = $Child.get_node('GrandChild').some_method()
7070
var a = $"Child".get_node('GrandChild').some_method()
7171
var a = $"%Child".get_node('GrandChild').some_method()
72+
73+
func f():
74+
$Child.add_child(%Unique)

src/formatter/snapshots/nodepaths/out.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ var a = $"%Child/GrandChild".some_method()
6969
var a = $Child.get_node('GrandChild').some_method()
7070
var a = $"Child".get_node('GrandChild').some_method()
7171
var a = $"%Child".get_node('GrandChild').some_method()
72+
73+
func f():
74+
$Child.add_child(%Unique)

src/formatter/symbols.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const keywords = [
2121
"master",
2222
"mastersync",
2323
"match",
24+
"when",
2425
"not",
2526
"onready",
2627
"or",
@@ -58,10 +59,14 @@ export const symbols = [
5859
"&=",
5960
"^=",
6061
"|=",
62+
"~=",
6163
"<<=",
6264
">>=",
6365
":=",
6466
"->",
67+
"&",
68+
"|",
69+
"^",
6570
"-",
6671
"+",
6772
"/",

0 commit comments

Comments
 (0)