config-lsp/server/handlers/wireguard/ast/wireguard_fields.go
Myzel394 1227949f26
fix(server): Improve wireguard
Signed-off-by: Myzel394 <github.7a2op@simplelogin.co>
2025-03-16 00:23:55 +01:00

78 lines
1.3 KiB
Go

package ast
import (
"slices"
)
func (c *WGConfig) FindSectionByLine(line uint32) *WGSection {
index, found := slices.BinarySearchFunc(
c.Sections,
line,
func(current *WGSection, target uint32) int {
if target < current.Start.Line {
return -1
}
if target > current.End.Line {
return 1
}
return 0
},
)
if !found {
return nil
}
return c.Sections[index]
}
func (c *WGConfig) FindPropertyByLine(line uint32) *WGProperty {
section := c.FindSectionByLine(line)
if section == nil {
return nil
}
if property, found := section.Properties.Get(line); found {
return property.(*WGProperty)
}
return nil
}
func (s *WGSection) FindFirstPropertyByName(name string) *WGProperty {
it := s.Properties.Iterator()
for it.Next() {
property := it.Value().(*WGProperty)
if property.Key.Name == name {
return property
}
}
return nil
}
func (s *WGSection) FindPropertyByName(name string) *WGProperty {
it := s.Properties.Iterator()
for it.Next() {
property := it.Value().(*WGProperty)
if property.Key.Name == name {
return property
}
}
return nil
}
func (s *WGSection) GetLastProperty() *WGProperty {
if s.Properties.Size() == 0 {
return nil
}
lastLine, _ := s.Properties.Max()
lastProperty, _ := s.Properties.Get(lastLine)
return lastProperty.(*WGProperty)
}