mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-06-18 00:55:20 -04:00
fix(keybinds): parse niri configs with leading-underscore identifiers (#2646)
DMS reads the niri config with kdl-go, which rejects '_' as the first
character of a bare identifier ("unexpected character _") even though niri's
own parser and the KDL spec accept it. The common trigger is the
`_JAVA_AWT_WM_NONREPARENTING "1"` environment node (the standard Java /
tiling-WM fix). When the parse aborts, `dms keybinds show` returns nothing and
the Keyboard Shortcuts UI shows no binds at all.
Extend the existing preprocessor approach (the brace fix from #2230) with
quoteLeadingUnderscoreIdents, which double-quotes bare identifiers that begin
with '_' before the text reaches kdl-go. The scan is string/comment aware and
only touches a leading '_' at a token boundary, so mid-identifier underscores
(XDG_CURRENT_DESKTOP) and underscores inside strings/comments are left alone.
Token boundaries include the ends of block comments and KDL slashdash (/-), so
a node abutting a comment with no whitespace is handled too. This is safe
because the niri parser only dispatches on fixed node/section names that never
start with '_', so re-quoting such a name cannot change what DMS reads.
Refs #2230
This commit is contained in:
@@ -51,7 +51,7 @@ type NiriParser struct {
|
||||
}
|
||||
|
||||
func parseKDL(data []byte) (*document.Document, error) {
|
||||
return kdl.Parse(strings.NewReader(normalizeKDLBraces(string(data))))
|
||||
return kdl.Parse(strings.NewReader(normalizeKDLBraces(quoteLeadingUnderscoreIdents(string(data)))))
|
||||
}
|
||||
|
||||
func normalizeKDLBraces(input string) string {
|
||||
@@ -94,6 +94,93 @@ func normalizeKDLBraces(input string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// quoteLeadingUnderscoreIdents wraps bare KDL identifiers that begin with '_'
|
||||
// in double quotes. kdl-go rejects '_' as the first character of a bare
|
||||
// identifier (e.g. the common `_JAVA_AWT_WM_NONREPARENTING "1"` environment
|
||||
// node), even though niri's own parser and the KDL spec accept it — so without
|
||||
// this the whole config fails to parse and no keybinds load. Quoting lets
|
||||
// kdl-go parse it; this is safe because the niri parser only dispatches on
|
||||
// fixed node/section names (binds, recent-windows, include, ...) that never
|
||||
// start with '_', so re-quoting such a name cannot change what DMS reads.
|
||||
// Underscores elsewhere in an identifier (XDG_CURRENT_DESKTOP) are left
|
||||
// untouched, and underscores inside strings or comments are skipped. Only a
|
||||
// leading '_' is handled; other start characters kdl-go over-rejects (e.g. '.'
|
||||
// or '?') do not occur in niri configs.
|
||||
func quoteLeadingUnderscoreIdents(input string) string {
|
||||
var sb strings.Builder
|
||||
sb.Grow(len(input))
|
||||
|
||||
var prev byte
|
||||
n := len(input)
|
||||
for i := 0; i < n; {
|
||||
c := input[i]
|
||||
|
||||
switch {
|
||||
case c == '"':
|
||||
end := findStringEnd(input, i)
|
||||
sb.WriteString(input[i:end])
|
||||
prev = '"'
|
||||
i = end
|
||||
case c == '/' && i+1 < n && input[i+1] == '/':
|
||||
end := findLineCommentEnd(input, i)
|
||||
sb.WriteString(input[i:end])
|
||||
prev = '\n'
|
||||
i = end
|
||||
case c == '/' && i+1 < n && input[i+1] == '*':
|
||||
end := findBlockCommentEnd(input, i)
|
||||
sb.WriteString(input[i:end])
|
||||
prev = ' '
|
||||
i = end
|
||||
case c == '/' && i+1 < n && input[i+1] == '-':
|
||||
// KDL slashdash: /- comments out the next node/value. Keep the
|
||||
// marker but treat what follows as a fresh token start, so a
|
||||
// slashdashed leading-underscore node (e.g. `/-_FOO "1"`) still
|
||||
// gets quoted instead of crashing kdl-go.
|
||||
sb.WriteByte('/')
|
||||
sb.WriteByte('-')
|
||||
prev = ' '
|
||||
i += 2
|
||||
case c == '_' && isIdentBoundary(prev):
|
||||
end := scanBareIdent(input, i)
|
||||
sb.WriteByte('"')
|
||||
sb.WriteString(input[i:end])
|
||||
sb.WriteByte('"')
|
||||
prev = '"'
|
||||
i = end
|
||||
default:
|
||||
sb.WriteByte(c)
|
||||
prev = c
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// isIdentBoundary reports whether the previously emitted byte ends a token, so
|
||||
// that a following '_' starts a fresh bare identifier rather than sitting in
|
||||
// the middle of one.
|
||||
func isIdentBoundary(prev byte) bool {
|
||||
switch prev {
|
||||
case 0, ' ', '\t', '\n', '\r', '{', '}', ';', '=', '(', ')', ',':
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// scanBareIdent returns the index just past the bare identifier starting at
|
||||
// start, stopping at whitespace or any KDL delimiter.
|
||||
func scanBareIdent(s string, start int) int {
|
||||
n := len(s)
|
||||
for i := start; i < n; i++ {
|
||||
switch s[i] {
|
||||
case ' ', '\t', '\n', '\r', '"', '{', '}', '(', ')', ';', '=', ',', '/', '\\', '<', '>', '[', ']':
|
||||
return i
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func findStringEnd(s string, start int) int {
|
||||
n := len(s)
|
||||
for i := start + 1; i < n; {
|
||||
|
||||
Reference in New Issue
Block a user