mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-19 07:25:27 +02:00
refactor(gitconfig): Use slice instead of treemap for storing entries
This commit is contained in:
parent
ba1738c700
commit
45748a0634
@ -3,8 +3,6 @@ package ast
|
|||||||
import (
|
import (
|
||||||
"config-lsp/common"
|
"config-lsp/common"
|
||||||
"config-lsp/common/parser"
|
"config-lsp/common/parser"
|
||||||
|
|
||||||
"github.com/emirpasic/gods/maps/treemap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitKey struct {
|
type GitKey struct {
|
||||||
@ -41,7 +39,10 @@ type GitSectionHeader struct {
|
|||||||
|
|
||||||
type GitSection struct {
|
type GitSection struct {
|
||||||
common.LocationRange
|
common.LocationRange
|
||||||
Entries *treemap.Map
|
|
||||||
|
// This is a simple list because gitconfig supports multiline entries,
|
||||||
|
// and thus fetching by line number is not feasible
|
||||||
|
Entries []*GitEntry
|
||||||
Title *GitSectionHeader
|
Title *GitSectionHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,68 @@
|
|||||||
package ast
|
package ast
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
func (c *GitConfig) Clear() {
|
func (c *GitConfig) Clear() {
|
||||||
c.Sections = []*GitSection{}
|
c.Sections = []*GitSection{}
|
||||||
c.CommentLines = map[uint32]struct{}{}
|
c.CommentLines = map[uint32]struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *GitConfig) FindSection(line uint32) *GitSection {
|
||||||
|
index, found := slices.BinarySearchFunc(
|
||||||
|
c.Sections,
|
||||||
|
line,
|
||||||
|
func(current *GitSection, target uint32) int {
|
||||||
|
if target > current.End.Line {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if target < current.Start.Line {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Sections[index]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *GitConfig) FindOption(line uint32) (*GitSection, *GitEntry) {
|
||||||
|
section := c.FindSection(line)
|
||||||
|
|
||||||
|
if section == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := section.FindOption(line)
|
||||||
|
|
||||||
|
return section, entry
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *GitSection) FindOption(line uint32) *GitEntry {
|
||||||
|
index, found := slices.BinarySearchFunc(
|
||||||
|
s.Entries,
|
||||||
|
line,
|
||||||
|
func(current *GitEntry, target uint32) int {
|
||||||
|
if target > current.End.Line {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if target < current.Start.Line {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.Entries[index]
|
||||||
|
}
|
||||||
|
@ -46,10 +46,11 @@ func (s *gitconfigParserListener) EnterEntry(ctx *parser.EntryContext) {
|
|||||||
LocationRange: location,
|
LocationRange: location,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.gitconfigContext.currentSection.Entries.Put(
|
s.gitconfigContext.currentSection.Entries = append(
|
||||||
location.Start.Line,
|
s.gitconfigContext.currentSection.Entries,
|
||||||
s.gitconfigContext.currentEntry,
|
s.gitconfigContext.currentEntry,
|
||||||
)
|
)
|
||||||
|
s.gitconfigContext.currentSection.End = location.End
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *gitconfigParserListener) ExitEntry(ctx *parser.EntryContext) {
|
func (s *gitconfigParserListener) ExitEntry(ctx *parser.EntryContext) {
|
||||||
|
@ -8,9 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/antlr4-go/antlr/v4"
|
"github.com/antlr4-go/antlr/v4"
|
||||||
"github.com/emirpasic/gods/maps/treemap"
|
|
||||||
|
|
||||||
gods "github.com/emirpasic/gods/utils"
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -161,7 +159,7 @@ func (c *GitConfig) parseHeader(
|
|||||||
LocationRange: location,
|
LocationRange: location,
|
||||||
Title: input[leftBracketIndex+1 : rightBracketIndex],
|
Title: input[leftBracketIndex+1 : rightBracketIndex],
|
||||||
},
|
},
|
||||||
Entries: treemap.NewWith(gods.UInt32Comparator),
|
Entries: make([]*GitEntry, 0),
|
||||||
}
|
}
|
||||||
c.Sections = append(c.Sections, context.currentSection)
|
c.Sections = append(c.Sections, context.currentSection)
|
||||||
|
|
||||||
|
@ -26,23 +26,32 @@ func TestValidOneSectionExample(t *testing.T) {
|
|||||||
|
|
||||||
section := config.Sections[0]
|
section := config.Sections[0]
|
||||||
|
|
||||||
rawFirstOption, _ := section.Entries.Get(uint32(1))
|
firstOption := section.Entries[0]
|
||||||
firstOption := rawFirstOption.(*GitEntry)
|
|
||||||
if !(firstOption.Key.Value.Value == "repositoryformatversion" && firstOption.Value.Value == "0") {
|
if !(firstOption.Key.Value.Value == "repositoryformatversion" && firstOption.Value.Value == "0") {
|
||||||
t.Errorf("Expected repositoryformatversion, got %s", firstOption.Key.Value.Value)
|
t.Errorf("Expected repositoryformatversion, got %s", firstOption.Key.Value.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawSecondOption, _ := section.Entries.Get(uint32(2))
|
secondOption := section.Entries[1]
|
||||||
secondOption := rawSecondOption.(*GitEntry)
|
|
||||||
if !(secondOption.Key.Value.Value == "filemode" && secondOption.Value.Value == "true") {
|
if !(secondOption.Key.Value.Value == "filemode" && secondOption.Value.Value == "true") {
|
||||||
t.Errorf("Expected filemode, got %s", secondOption.Key.Value.Value)
|
t.Errorf("Expected filemode, got %s", secondOption.Key.Value.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawThirdOption, _ := section.Entries.Get(uint32(3))
|
thirdOption := section.Entries[2]
|
||||||
thirdOption := rawThirdOption.(*GitEntry)
|
|
||||||
if !(thirdOption.Key.Value.Value == "bare" && thirdOption.Value.Value == "false") {
|
if !(thirdOption.Key.Value.Value == "bare" && thirdOption.Value.Value == "false") {
|
||||||
t.Errorf("Expected bare, got %s", thirdOption.Key.Value.Value)
|
t.Errorf("Expected bare, got %s", thirdOption.Key.Value.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foundSection, foundEntry := config.FindOption(1)
|
||||||
|
|
||||||
|
if !(foundSection == section && foundEntry == firstOption) {
|
||||||
|
t.Errorf("Expected first option, got %s", foundEntry.Key.Value.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
foundSection, foundEntry = config.FindOption(0)
|
||||||
|
|
||||||
|
if !(foundSection == section && foundEntry == nil) {
|
||||||
|
t.Errorf("Expected nil, got %s", foundEntry)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestComplexExample(t *testing.T) {
|
func TestComplexExample(t *testing.T) {
|
||||||
@ -81,29 +90,23 @@ func TestComplexExample(t *testing.T) {
|
|||||||
t.Errorf("Expected core, got %s", section.Title.Title)
|
t.Errorf("Expected core, got %s", section.Title.Title)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !(section.Entries.Size() == 3) {
|
if !(len(section.Entries) == 3) {
|
||||||
t.Errorf("Expected 3 entries, got %d", section.Entries.Size())
|
t.Errorf("Expected 3 entries, got %d", len(section.Entries))
|
||||||
}
|
}
|
||||||
|
|
||||||
rawFirstOption, _ := section.Entries.Get(uint32(1))
|
firstOption := section.Entries[0]
|
||||||
firstOption := rawFirstOption.(*GitEntry)
|
|
||||||
if !(firstOption.Key.Value.Value == "repositoryformatversion" && firstOption.Value.Value == "0" && firstOption.Start.Line == 1 && firstOption.End.Line == 1 && firstOption.Start.Character == 4 && firstOption.End.Character == 31) {
|
if !(firstOption.Key.Value.Value == "repositoryformatversion" && firstOption.Value.Value == "0" && firstOption.Start.Line == 1 && firstOption.End.Line == 1 && firstOption.Start.Character == 4 && firstOption.End.Character == 31) {
|
||||||
t.Errorf("Expected 0, got %s", firstOption)
|
t.Errorf("Expected 0, got %s", firstOption)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, found := section.Entries.Get(uint32(3))
|
|
||||||
if found {
|
|
||||||
t.Errorf("Expected no entry at line 3")
|
|
||||||
}
|
|
||||||
|
|
||||||
section = config.Sections[1]
|
section = config.Sections[1]
|
||||||
|
|
||||||
if !(section.Title.Title == `remote "origin"`) {
|
if !(section.Title.Title == `remote "origin"`) {
|
||||||
t.Errorf("Expected remote \"origin\", got %s", section.Title.Title)
|
t.Errorf("Expected remote \"origin\", got %s", section.Title.Title)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !(section.Entries.Size() == 2) {
|
if !(len(section.Entries) == 2) {
|
||||||
t.Errorf("Expected 2 entries, got %d", section.Entries.Size())
|
t.Errorf("Expected 2 entries, got %d", len(section.Entries))
|
||||||
}
|
}
|
||||||
|
|
||||||
section = config.Sections[2]
|
section = config.Sections[2]
|
||||||
@ -112,8 +115,7 @@ func TestComplexExample(t *testing.T) {
|
|||||||
t.Errorf("Expected alias, got %s", section.Title.Title)
|
t.Errorf("Expected alias, got %s", section.Title.Title)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawSecondOption, _ := section.Entries.Get(uint32(13))
|
secondOption := section.Entries[0]
|
||||||
secondOption := rawSecondOption.(*GitEntry)
|
|
||||||
if !(secondOption.Key.Value.Value == "ours" && secondOption.Value.Value == "!f() { git checkout --ours $@ && git add $@; }; f" && secondOption.Start.Character == 1 && secondOption.End.Character == 59) {
|
if !(secondOption.Key.Value.Value == "ours" && secondOption.Value.Value == "!f() { git checkout --ours $@ && git add $@; }; f" && secondOption.Start.Character == 1 && secondOption.End.Character == 59) {
|
||||||
t.Errorf("Expected ours, got %s", secondOption.Key.Value.Value)
|
t.Errorf("Expected ours, got %s", secondOption.Key.Value.Value)
|
||||||
}
|
}
|
||||||
@ -162,8 +164,7 @@ func TestLeadingLine(t *testing.T) {
|
|||||||
t.Errorf("Expected core, got %s", section.Title.Title)
|
t.Errorf("Expected core, got %s", section.Title.Title)
|
||||||
}
|
}
|
||||||
|
|
||||||
rawFirstOption, _ := section.Entries.Get(uint32(1))
|
firstOption := section.Entries[0]
|
||||||
firstOption := rawFirstOption.(*GitEntry)
|
|
||||||
if !(firstOption.Key.Value.Value == "command" && firstOption.Value.Value == "git commit -m Hello World") {
|
if !(firstOption.Key.Value.Value == "command" && firstOption.Value.Value == "git commit -m Hello World") {
|
||||||
t.Errorf("Expected command, got %s", firstOption.Key.Value.Value)
|
t.Errorf("Expected command, got %s", firstOption.Key.Value.Value)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user