From aee83dd2a40e0d58f50125efc228cdabc68ebbb9 Mon Sep 17 00:00:00 2001 From: David Kincaid Date: Wed, 18 Dec 2024 10:45:05 -0500 Subject: [PATCH] Update float syntax rules and formatting (#756) * Update float syntax rules and formatting * Add missing export_group annotation * Moved abstract from an annotation to a keyword * Add an example of the other type of string interpolation. --- src/formatter/snapshots/float_notation.gd | 25 +++++++++++++++++++ .../snapshots/leave_strings_alone.gd | 1 + src/formatter/textmate.ts | 11 +++++--- syntaxes/GDScript.tmLanguage.json | 15 ++++------- 4 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 src/formatter/snapshots/float_notation.gd diff --git a/src/formatter/snapshots/float_notation.gd b/src/formatter/snapshots/float_notation.gd new file mode 100644 index 0000000..be24a8d --- /dev/null +++ b/src/formatter/snapshots/float_notation.gd @@ -0,0 +1,25 @@ +# --- IN --- +func test(): + # The following floating-point notations are all valid: + print(is_equal_approx(123., 123)) + print(is_equal_approx(.123, 0.123)) + print(is_equal_approx(.123e4, 1230)) + print(is_equal_approx(123.e4, 1.23e6)) + print(is_equal_approx(.123e-1, 0.0123)) + print(is_equal_approx(123.e-1, 12.3)) + + # Same as above, but with negative numbers. + print(is_equal_approx(-123., -123)) + print(is_equal_approx(-.123, -0.123)) + print(is_equal_approx(-.123e4, -1230)) + print(is_equal_approx(-123.e4, -1.23e6)) + print(is_equal_approx(-.123e-1, -0.0123)) + print(is_equal_approx(-123.e-1, -12.3)) + + # Same as above, but with explicit positive numbers (which is redundant). + print(is_equal_approx(+123., +123)) + print(is_equal_approx(+.123, +0.123)) + print(is_equal_approx(+.123e4, +1230)) + print(is_equal_approx(+123.e4, +1.23e6)) + print(is_equal_approx(+.123e-1, +0.0123)) + print(is_equal_approx(+123.e-1, +12.3)) \ No newline at end of file diff --git a/src/formatter/snapshots/leave_strings_alone.gd b/src/formatter/snapshots/leave_strings_alone.gd index 44b1f6b..6013d7c 100644 --- a/src/formatter/snapshots/leave_strings_alone.gd +++ b/src/formatter/snapshots/leave_strings_alone.gd @@ -12,5 +12,6 @@ func dump() -> String: preview_texture: %s, explorer_layer: %s, connections: %s, + test {test}, } """ diff --git a/src/formatter/textmate.ts b/src/formatter/textmate.ts index 0534efe..bf1e592 100644 --- a/src/formatter/textmate.ts +++ b/src/formatter/textmate.ts @@ -74,6 +74,10 @@ function parse_token(token: Token) { if (token.scopes.includes("meta.function.parameters.gdscript")) { token.param = true; } + if (token.scopes[0].includes("constant.numeric")) { + token.type = "literal"; + return; + } if (token.value.match(/[A-Za-z_]\w+/)) { token.identifier = true; } @@ -144,7 +148,7 @@ function between(tokens: Token[], current: number, options: FormatterOptions) { if (nextToken.param) { if (options.denseFunctionParameters) { - if (prev === "-") { + if (prev === "-" || prev === "+") { if (tokens[current - 2]?.value === "=") return ""; if (["keyword", "symbol"].includes(tokens[current - 2]?.type)) { return ""; @@ -181,8 +185,9 @@ function between(tokens: Token[], current: number, options: FormatterOptions) { } if (prev === "@") return ""; - if (prev === "-") { + if (prev === "-" || prev === "+") { if (nextToken.identifier) return " "; + if (next === "(") return " "; if (current === 1) return ""; if (["keyword", "symbol"].includes(tokens[current - 2]?.type)) { return ""; @@ -304,7 +309,7 @@ export function format_document(document: TextDocument, _options?: FormatterOpti const tokens: Token[] = []; for (const t of lineTokens.tokens) { const token: Token = { - scopes: t.scopes, + scopes: [t.scopes.join(" "), ...t.scopes], original: line.text.slice(t.startIndex, t.endIndex), value: line.text.slice(t.startIndex, t.endIndex).trim(), }; diff --git a/syntaxes/GDScript.tmLanguage.json b/syntaxes/GDScript.tmLanguage.json index a830ce2..caf2b02 100644 --- a/syntaxes/GDScript.tmLanguage.json +++ b/syntaxes/GDScript.tmLanguage.json @@ -245,7 +245,7 @@ "captures": { "1": { "name": "keyword.control.gdscript" } } }, "keywords": { - "match": "\\b(?:class|class_name|is|onready|tool|static|export|as|void|enum|assert|breakpoint|sync|remote|master|puppet|slave|remotesync|mastersync|puppetsync|trait|namespace)\\b", + "match": "\\b(?:class|class_name|abstract|is|onready|tool|static|export|as|void|enum|assert|breakpoint|sync|remote|master|puppet|slave|remotesync|mastersync|puppetsync|trait|namespace)\\b", "name": "keyword.language.gdscript" }, "letter": { @@ -263,22 +263,17 @@ "name": "constant.numeric.integer.hexadecimal.gdscript" }, { - "match": "[-]?([0-9][0-9_]+\\.[0-9_]*(e[\\-\\+]?[0-9_]+)?)", + "match": "\\.[0-9][0-9_]*([eE][+-]?[0-9_]+)?", "name": "constant.numeric.float.gdscript" }, { - "match": "[-]?(\\.[0-9][0-9_]*(e[\\-\\+]?[0-9_]+)?)", + "match": "([0-9][0-9_]*)?\\.[0-9_]*([eE][+-]?[0-9_]+)?", "name": "constant.numeric.float.gdscript" }, { - "match": "[-]?([0-9][0-9_]*e[\\-\\+]?\\[0-9_])", + "match": "[0-9][0-9_]*[eE][+-]?[0-9_]+", "name": "constant.numeric.float.gdscript" }, - { - "name": "constant.numeric.float.gdscript", - "match": "(?x)\n (?