From 948e3503de29dc23ca79bb0a2ce92844fc46afb6 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 9 Oct 2024 22:09:17 +0200 Subject: [PATCH] feat(fstab): Handle leading comments --- server/handlers/fstab/ast/parser.go | 11 +++++++++-- server/handlers/fstab/fstab_test.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/server/handlers/fstab/ast/parser.go b/server/handlers/fstab/ast/parser.go index ac63d6f..f24a6fa 100644 --- a/server/handlers/fstab/ast/parser.go +++ b/server/handlers/fstab/ast/parser.go @@ -6,6 +6,7 @@ import ( "config-lsp/handlers/fstab/ast/parser" "config-lsp/utils" "regexp" + "strings" "github.com/antlr4-go/antlr/v4" "github.com/emirpasic/gods/maps/treemap" @@ -27,13 +28,15 @@ func (c *FstabConfig) Clear() { var commentPattern = regexp.MustCompile(`^\s*#`) var emptyPattern = regexp.MustCompile(`^\s*$`) +var leadingCommentPattern = regexp.MustCompile(`^(.+?)#`) func (c *FstabConfig) Parse(input string) []common.LSPError { errors := make([]common.LSPError, 0) lines := utils.SplitIntoLines(input) context := createListenerContext() - for rawLineNumber, line := range lines { + for rawLineNumber, rawLine := range lines { + line := rawLine lineNumber := uint32(rawLineNumber) context.line = lineNumber @@ -46,6 +49,11 @@ func (c *FstabConfig) Parse(input string) []common.LSPError { continue } + if strings.Contains(line, "#") { + matches := leadingCommentPattern.FindStringSubmatch(line) + line = matches[1] + } + errors = append( errors, c.parseStatement(context, line)..., @@ -55,7 +63,6 @@ func (c *FstabConfig) Parse(input string) []common.LSPError { return errors } -// TODO: Handle leading comments func (c *FstabConfig) parseStatement( context *fstabListenerContext, input string, diff --git a/server/handlers/fstab/fstab_test.go b/server/handlers/fstab/fstab_test.go index e36c5f3..9d47ecf 100644 --- a/server/handlers/fstab/fstab_test.go +++ b/server/handlers/fstab/fstab_test.go @@ -166,6 +166,24 @@ UUID=b411dc99-f0a0-4c87-9e05-184977be8539 /home ext4 defaults 0 2 } +func TestArchExample1WithComments(t *testing.T) { + input := utils.Dedent(` +# Hello there! +UUID=0a3407de-014b-458b-b5c1-848e92a327a3 / ext4 defaults 0 1 +# How are you? +UUID=f9fe0b69-a280-415d-a03a-a32752370dee none swap defaults 0 0 # And I am trailing! +UUID=b411dc99-f0a0-4c87-9e05-184977be8539 /home ext4 defaults 0 2 # I am tailing too! +`) + p := ast.NewFstabConfig() + + errors := p.Parse(input) + + if len(errors) > 0 { + t.Fatalf("ParseFromContent failed with error %v", errors) + } + +} + func TestArchExample2(t *testing.T) { input := utils.Dedent(` /dev/sda1 /boot vfat defaults 0 2