From 5b725b55fd91f751898b2fd2caa669eebd4c0003 Mon Sep 17 00:00:00 2001 From: David Kincaid Date: Sun, 15 Sep 2024 16:04:31 -0400 Subject: [PATCH] Fix bare nodepaths referencing absolute/root paths (#712) --- src/formatter/snapshots/nodepaths/in.gd | 8 +++ src/formatter/snapshots/nodepaths/out.gd | 8 +++ src/formatter/textmate.ts | 10 +++- syntaxes/GDScript.tmLanguage.json | 14 ++--- syntaxes/examples/gdscript1.gd | 66 +++++++++++++----------- 5 files changed, 69 insertions(+), 37 deletions(-) diff --git a/src/formatter/snapshots/nodepaths/in.gd b/src/formatter/snapshots/nodepaths/in.gd index a581d26..fa7a6c4 100644 --- a/src/formatter/snapshots/nodepaths/in.gd +++ b/src/formatter/snapshots/nodepaths/in.gd @@ -11,6 +11,14 @@ func f(): @onready var score := $HBoxContainer/Score as Label +var a = $/root +var a = $ / root +var a = $/root/Child +var a = $ / root / Child +var a = $/root/Child/%Unique +var a = $ / root / Child/%Unique +var a = $/root/Child/GrandChild + var a = $Child var a = $Child/ GrandChild var a = $Child/ GrandChild / GreatGrandChild diff --git a/src/formatter/snapshots/nodepaths/out.gd b/src/formatter/snapshots/nodepaths/out.gd index f48c45d..076253b 100644 --- a/src/formatter/snapshots/nodepaths/out.gd +++ b/src/formatter/snapshots/nodepaths/out.gd @@ -11,6 +11,14 @@ func f(): @onready var score := $HBoxContainer/Score as Label +var a = $/root +var a = $/root +var a = $/root/Child +var a = $/root/Child +var a = $/root/Child/%Unique +var a = $/root/Child/%Unique +var a = $/root/Child/GrandChild + var a = $Child var a = $Child/GrandChild var a = $Child/GrandChild/GreatGrandChild diff --git a/src/formatter/textmate.ts b/src/formatter/textmate.ts index 763ad21..d8b6a2c 100644 --- a/src/formatter/textmate.ts +++ b/src/formatter/textmate.ts @@ -78,6 +78,11 @@ function parse_token(token: Token) { token.type = "nodepath"; return; } + if (token.scopes.includes("meta.literal.nodepath.bare.gdscript")) { + token.skip = true; + token.type = "bare_nodepath"; + return; + } if (token.scopes.includes("keyword.control.flow.gdscript")) { token.type = "keyword"; return; @@ -183,6 +188,7 @@ function between(tokens: Token[], current: number, options: FormatterOptions) { if (prev === "[" && nextToken.type === "symbol") return ""; if (prev === "[" && nextToken.type === "nodepath") return ""; + if (prev === "[" && nextToken.type === "bare_nodepath") return ""; if (prev === ":") return " "; if (prev === ";") return " "; if (prev === "##") return " "; @@ -225,7 +231,7 @@ export function format_document(document: TextDocument, _options?: FormatterOpti const edits: TextEdit[] = []; const options = _options ?? get_formatter_options(); - + let lineTokens: vsctm.ITokenizeLineResult = null; let onlyEmptyLinesSoFar = true; let emptyLineCount = 0; @@ -290,7 +296,7 @@ export function format_document(document: TextDocument, _options?: FormatterOpti tokens.push(token); } for (let i = 0; i < tokens.length; i++) { - // log.debug(i, tokens[i].value, tokens[i]); + log.debug(i, tokens[i].value, tokens[i]); if (i > 0 && tokens[i - 1].string === true && tokens[i].string === true) { nextLine += tokens[i].original; } else { diff --git a/syntaxes/GDScript.tmLanguage.json b/syntaxes/GDScript.tmLanguage.json index 319add4..70f1ee9 100644 --- a/syntaxes/GDScript.tmLanguage.json +++ b/syntaxes/GDScript.tmLanguage.json @@ -400,20 +400,22 @@ ] }, "builtin_get_node_shorthand_bare": { - "name": "meta.literal.nodepath.gdscript", - "match": "(?