summaryrefslogtreecommitdiff
path: root/caddyconfig/caddyfile/lexer.go
diff options
context:
space:
mode:
authorWeidiDeng <weidi_deng@icloud.com>2023-07-13 04:32:22 +0800
committerGitHub <noreply@github.com>2023-07-12 14:32:22 -0600
commitbbe1952a59a196b7598c6488beb770ec38a70dfc (patch)
tree24b670b7a4c6c4293dda378a0def0f9706a9df1a /caddyconfig/caddyfile/lexer.go
parent0e2c7e1d35b287fc0e56d6db2951f791e09b5a37 (diff)
caddyfile: Fix comparing if two tokens are on the same line (#5626)
* fix comparing if two tokens are on the same line * compare tokens from copies when importing
Diffstat (limited to 'caddyconfig/caddyfile/lexer.go')
-rw-r--r--caddyconfig/caddyfile/lexer.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/caddyconfig/caddyfile/lexer.go b/caddyconfig/caddyfile/lexer.go
index d1fbfbb..47aaa03 100644
--- a/caddyconfig/caddyfile/lexer.go
+++ b/caddyconfig/caddyfile/lexer.go
@@ -338,3 +338,28 @@ func (t Token) NumLineBreaks() int {
}
var heredocMarkerRegexp = regexp.MustCompile("^[A-Za-z0-9_-]+$")
+
+// isNextOnNewLine tests whether t2 is on a different line from t1
+func isNextOnNewLine(t1, t2 Token) bool {
+ // If the second token is from a different file,
+ // we can assume it's from a different line
+ if t1.File != t2.File {
+ return true
+ }
+
+ // If the second token is from a different import chain,
+ // we can assume it's from a different line
+ if len(t1.imports) != len(t2.imports) {
+ return true
+ }
+ for i, im := range t1.imports {
+ if im != t2.imports[i] {
+ return true
+ }
+ }
+
+ // If the first token (incl line breaks) ends
+ // on a line earlier than the next token,
+ // then the second token is on a new line
+ return t1.Line+t1.NumLineBreaks() < t2.Line
+}