From 30c68e0d99cab61445003500821b487e438130a9 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Wed, 9 Oct 2024 11:05:18 +0200 Subject: [PATCH] fix(server): Overall improvements --- server/doc-values/value-array.go | 1 - .../doc-values/value-key-enum-assignment.go | 3 +- .../doc-values/value-key-value-assignment.go | 3 +- server/handlers/fstab/ast/parser.go | 50 ------------------- server/handlers/fstab/fields/mountoptions.go | 32 +++++------- server/handlers/fstab/handlers/completions.go | 45 +++++++++++++---- 6 files changed, 50 insertions(+), 84 deletions(-) diff --git a/server/doc-values/value-array.go b/server/doc-values/value-array.go index 0bb9f48..3d330f7 100644 --- a/server/doc-values/value-array.go +++ b/server/doc-values/value-array.go @@ -132,7 +132,6 @@ func (v ArrayValue) getCurrentValue(line string, cursor uint32) (string, uint32) // hello,worl[d] // hello,world[,] // hello[,]world,how,are,you - relativePosition, found := utils.FindPreviousCharacter( line, v.Separator, diff --git a/server/doc-values/value-key-enum-assignment.go b/server/doc-values/value-key-enum-assignment.go index b39127a..9e12c03 100644 --- a/server/doc-values/value-key-enum-assignment.go +++ b/server/doc-values/value-key-enum-assignment.go @@ -170,13 +170,14 @@ func (v KeyEnumAssignmentValue) DeprecatedFetchCompletions(line string, cursor u return v.FetchEnumCompletions() } - relativePosition, found := utils.FindPreviousCharacter( + foundPosition, found := utils.FindPreviousCharacter( line, v.Separator, int(cursor), ) if found { + relativePosition := max(1, foundPosition) - 1 selectedKey := line[:uint32(relativePosition)] line = line[uint32(relativePosition+len(v.Separator)):] cursor -= uint32(relativePosition) diff --git a/server/doc-values/value-key-value-assignment.go b/server/doc-values/value-key-value-assignment.go index 5a2efe7..ccefdaf 100644 --- a/server/doc-values/value-key-value-assignment.go +++ b/server/doc-values/value-key-value-assignment.go @@ -108,13 +108,14 @@ func (v KeyValueAssignmentValue) DeprecatedFetchCompletions(line string, cursor return v.Key.DeprecatedFetchCompletions(line, cursor) } - relativePosition, found := utils.FindPreviousCharacter( + foundPosition, found := utils.FindPreviousCharacter( line, v.Separator, int(cursor), ) if found { + relativePosition := max(1, foundPosition) - 1 selectedKey := line[:uint32(relativePosition)] line = line[uint32(relativePosition+len(v.Separator)):] cursor -= uint32(relativePosition) diff --git a/server/handlers/fstab/ast/parser.go b/server/handlers/fstab/ast/parser.go index e99c8f3..03dd0bf 100644 --- a/server/handlers/fstab/ast/parser.go +++ b/server/handlers/fstab/ast/parser.go @@ -88,42 +88,18 @@ func (c *FstabConfig) parseStatement( fallthrough case 5: freq = parseField(line, input, fields[4]) - - if pass == nil && fields[4][1] < len(input) { - pass = createPartialField(line, input, fields[4][1], len(input)) - } - fallthrough case 4: options = parseField(line, input, fields[3]) - - if freq == nil && fields[3][1] < len(input) { - freq = createPartialField(line, input, fields[3][1], len(input)) - } - fallthrough case 3: filesystemType = parseField(line, input, fields[2]) - - if options == nil && fields[2][1] < len(input) { - options = createPartialField(line, input, fields[2][1], len(input)) - } - fallthrough case 2: mountPoint = parseField(line, input, fields[1]) - - if filesystemType == nil && fields[1][1] < len(input) { - filesystemType = createPartialField(line, input, fields[1][1], len(input)) - } - fallthrough case 1: spec = parseField(line, input, fields[0]) - - if mountPoint == nil && fields[0][1] < len(input) { - mountPoint = createPartialField(line, input, fields[0][1], len(input)) - } } fstabLine := &FstabEntry{ @@ -181,29 +157,3 @@ func parseField( }), } } - -func createPartialField( - line uint32, - input string, - start int, - end int, -) *FstabField { - return nil - return &FstabField{ - LocationRange: common.LocationRange{ - Start: common.Location{ - Line: line, - Character: uint32(start), - }, - End: common.Location{ - Line: line, - Character: uint32(end), - }, - }, - Value: commonparser.ParseRawString(input[end:end], commonparser.ParseFeatures{ - ParseEscapedCharacters: true, - ParseDoubleQuotes: true, - Replacements: &map[string]string{}, - }), - } -} diff --git a/server/handlers/fstab/fields/mountoptions.go b/server/handlers/fstab/fields/mountoptions.go index f6227ef..ed2d4ed 100644 --- a/server/handlers/fstab/fields/mountoptions.go +++ b/server/handlers/fstab/fields/mountoptions.go @@ -39,22 +39,6 @@ var defaultOptions = []docvalues.EnumString{ "noauto", "Can only be mounted explicitly (i.e., the -a option will not cause the filesystem to be mounted).", ), - docvalues.CreateEnumStringWithDoc( - "context", - "The context= option is useful when mounting filesystems that do not support extended attributes, such as a floppy or hard disk formatted with VFAT, or systems that are not normally running under SELinux, such as an ext3 or ext4 formatted disk from a non-SELinux workstation. You can also use context= on filesystems you do not trust, such as a floppy. It also helps in compatibility with xattr-supporting filesystems on earlier 2.4. kernel versions. Even where xattrs are supported, you can save time not having to label every file by assigning the entire disk one security context.", - ), - docvalues.CreateEnumStringWithDoc( - "fscontext", - "The fscontext= option works for all filesystems, regardless of their xattr support. The fscontext option sets the overarching filesystem label to a specific security context. This filesystem label is separate from the individual labels on the files. It represents the entire filesystem for certain kinds of permission checks, such as during mount or file creation. Individual file labels are still obtained from the xattrs on the files themselves. The context option actually sets the aggregate context that fscontext provides, in addition to supplying the same label for individual files.", - ), - docvalues.CreateEnumStringWithDoc( - "defcontext", - "You can set the default security context for unlabeled files using defcontext= option. This overrides the value set for unlabeled files in the policy and requires a filesystem that supports xattr labeling.", - ), - docvalues.CreateEnumStringWithDoc( - "rootcontext", - "The rootcontext= option allows you to explicitly label the root inode of a FS being mounted before that FS or inode becomes visible to userspace. This was found to be useful for things like stateless Linux. The special value @target can be used to assign the current context of the target mountpoint location.", - ), docvalues.CreateEnumStringWithDoc( "defaults", "Use the default options: rw, suid, dev, exec, auto, nouser, and async. Note that the real set of all default mount options depends on the kernel and filesystem type. See the beginning of this section for more details.", @@ -345,13 +329,21 @@ See TimeoutSec= below for details. Added in version 233.`, ): docvalues.StringValue{}, + docvalues.CreateEnumStringWithDoc( + "fscontext", + "The fscontext= option works for all filesystems, regardless of their xattr support. The fscontext option sets the overarching filesystem label to a specific security context. This filesystem label is separate from the individual labels on the files. It represents the entire filesystem for certain kinds of permission checks, such as during mount or file creation. Individual file labels are still obtained from the xattrs on the files themselves. The context option actually sets the aggregate context that fscontext provides, in addition to supplying the same label for individual files.", + ): docvalues.StringValue{}, + docvalues.CreateEnumStringWithDoc( + "defcontext", + "You can set the default security context for unlabeled files using defcontext= option. This overrides the value set for unlabeled files in the policy and requires a filesystem that supports xattr labeling.", + ): docvalues.StringValue{}, } func createMountOptionField( options []docvalues.EnumString, assignOption map[docvalues.EnumString]docvalues.DeprecatedValue, ) docvalues.DeprecatedValue { - dynamicOptions := docvalues.MergeKeyEnumAssignmentMaps(defaultAssignOptions, assignOption) + // dynamicOptions := docvalues.MergeKeyEnumAssignmentMaps(defaultAssignOptions, assignOption) return docvalues.ArrayValue{ Separator: ",", @@ -359,20 +351,20 @@ func createMountOptionField( SubValue: docvalues.OrValue{ Values: []docvalues.DeprecatedValue{ docvalues.KeyEnumAssignmentValue{ - Values: dynamicOptions, + Values: assignOption, ValueIsOptional: false, Separator: "=", }, docvalues.EnumValue{ EnforceValues: true, - Values: append(defaultOptions, options...), + Values: options, }, }, }, } } -var DefaultMountOptionsField = createMountOptionField([]docvalues.EnumString{}, map[docvalues.EnumString]docvalues.DeprecatedValue{}) +var DefaultMountOptionsField = createMountOptionField(defaultOptions, defaultAssignOptions) var MountOptionsMapField = map[string]docvalues.DeprecatedValue{ "adfs": createMountOptionField( diff --git a/server/handlers/fstab/handlers/completions.go b/server/handlers/fstab/handlers/completions.go index e965789..8b4598c 100644 --- a/server/handlers/fstab/handlers/completions.go +++ b/server/handlers/fstab/handlers/completions.go @@ -2,9 +2,9 @@ package handlers import ( "config-lsp/common" - "config-lsp/doc-values" "config-lsp/handlers/fstab/ast" "config-lsp/handlers/fstab/fields" + "fmt" "github.com/tliron/glsp/protocol_3_16" ) @@ -38,22 +38,45 @@ func GetCompletion( cursor, ), nil case ast.FstabFieldOptions: + line, cursor := getFieldSafely(entry.Fields.Options, cursor) fileSystemType := entry.Fields.FilesystemType.Value.Value + completions := make([]protocol.CompletionItem, 0, 50) - var optionsField docvalues.DeprecatedValue + for _, completion := range fields.DefaultMountOptionsField.DeprecatedFetchCompletions(line, cursor) { + var documentation string - if foundField, found := fields.MountOptionsMapField[fileSystemType]; found { - optionsField = foundField - } else { - optionsField = fields.DefaultMountOptionsField + switch completion.Documentation.(type) { + case string: + documentation = completion.Documentation.(string) + case *string: + documentation = *completion.Documentation.(*string) + } + + completion.Documentation = protocol.MarkupContent{ + Kind: protocol.MarkupKindMarkdown, + Value: documentation + "\n\n" + "From: _Default Mount Options_", + } + completions = append(completions, completion) } - value, cursor := getFieldSafely(entry.Fields.Options, cursor) + if optionsField, found := fields.MountOptionsMapField[fileSystemType]; found { + for _, completion := range optionsField.DeprecatedFetchCompletions(line, cursor) { + var documentation string - completions := optionsField.DeprecatedFetchCompletions( - value, - cursor, - ) + switch completion.Documentation.(type) { + case string: + documentation = completion.Documentation.(string) + case *string: + documentation = *completion.Documentation.(*string) + } + + completion.Documentation = protocol.MarkupContent{ + Kind: protocol.MarkupKindMarkdown, + Value: documentation + "\n\n" + fmt.Sprintf("From: _%s_", fileSystemType), + } + completions = append(completions, completion) + } + } return completions, nil case ast.FstabFieldFreq: