feat(fstab): Handle leading comments

This commit is contained in:
Myzel394 2024-10-09 22:09:17 +02:00
parent 9f927b8685
commit 948e3503de
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
2 changed files with 27 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import (
"config-lsp/handlers/fstab/ast/parser" "config-lsp/handlers/fstab/ast/parser"
"config-lsp/utils" "config-lsp/utils"
"regexp" "regexp"
"strings"
"github.com/antlr4-go/antlr/v4" "github.com/antlr4-go/antlr/v4"
"github.com/emirpasic/gods/maps/treemap" "github.com/emirpasic/gods/maps/treemap"
@ -27,13 +28,15 @@ func (c *FstabConfig) Clear() {
var commentPattern = regexp.MustCompile(`^\s*#`) var commentPattern = regexp.MustCompile(`^\s*#`)
var emptyPattern = regexp.MustCompile(`^\s*$`) var emptyPattern = regexp.MustCompile(`^\s*$`)
var leadingCommentPattern = regexp.MustCompile(`^(.+?)#`)
func (c *FstabConfig) Parse(input string) []common.LSPError { func (c *FstabConfig) Parse(input string) []common.LSPError {
errors := make([]common.LSPError, 0) errors := make([]common.LSPError, 0)
lines := utils.SplitIntoLines(input) lines := utils.SplitIntoLines(input)
context := createListenerContext() context := createListenerContext()
for rawLineNumber, line := range lines { for rawLineNumber, rawLine := range lines {
line := rawLine
lineNumber := uint32(rawLineNumber) lineNumber := uint32(rawLineNumber)
context.line = lineNumber context.line = lineNumber
@ -46,6 +49,11 @@ func (c *FstabConfig) Parse(input string) []common.LSPError {
continue continue
} }
if strings.Contains(line, "#") {
matches := leadingCommentPattern.FindStringSubmatch(line)
line = matches[1]
}
errors = append( errors = append(
errors, errors,
c.parseStatement(context, line)..., c.parseStatement(context, line)...,
@ -55,7 +63,6 @@ func (c *FstabConfig) Parse(input string) []common.LSPError {
return errors return errors
} }
// TODO: Handle leading comments
func (c *FstabConfig) parseStatement( func (c *FstabConfig) parseStatement(
context *fstabListenerContext, context *fstabListenerContext,
input string, input string,

View File

@ -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) { func TestArchExample2(t *testing.T) {
input := utils.Dedent(` input := utils.Dedent(`
/dev/sda1 /boot vfat defaults 0 2 /dev/sda1 /boot vfat defaults 0 2