Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to the Docker Language Server will be documented in this fil

## [Unreleased]

### Added

- Compose
- textDocument/completion
- provide local file and folder name suggestions when modifying simple strings for service volumes ([#376](https://github.com/docker/docker-language-server/issues/376))

### Fixed

- Compose
Expand Down
6 changes: 4 additions & 2 deletions e2e-tests/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ func createGuaranteedInitializeResult() protocol.InitializeResult {
syncKind := protocol.TextDocumentSyncKindFull
return protocol.InitializeResult{
Capabilities: protocol.ServerCapabilities{
CodeActionProvider: protocol.CodeActionOptions{},
CompletionProvider: &protocol.CompletionOptions{},
CodeActionProvider: protocol.CodeActionOptions{},
CompletionProvider: &protocol.CompletionOptions{
TriggerCharacters: []string{"/"},
},
DefinitionProvider: protocol.DefinitionOptions{},
DocumentHighlightProvider: &protocol.DocumentHighlightOptions{},
DocumentLinkProvider: &protocol.DocumentLinkOptions{},
Expand Down
77 changes: 69 additions & 8 deletions internal/compose/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compose
import (
"context"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
Expand Down Expand Up @@ -213,7 +214,8 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
whitespaceLine := currentLineTrimmed == ""
line := int(lspLine) + 1
path := constructCompletionNodePath(file, line)
wordPrefix := protocol.UInteger(len(prefix(lines[lspLine], character-1)))
prefixContent := prefix(lines[lspLine], character-1)
prefixLength := protocol.UInteger(len(prefixContent))
if len(path) == 0 {
if topLevelNodeOffset != -1 && params.Position.Character != uint32(topLevelNodeOffset) {
return nil, nil
Expand All @@ -222,7 +224,7 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
} else if len(path) == 1 {
if path[0].Key.GetToken().Value == "include" {
schema := schemaProperties()["include"].Items.(*jsonschema.Schema)
items := createSchemaItems(params, schema.Ref.OneOf[1].Properties, lines, lspLine, whitespaceLine, wordPrefix, file, manager, documentPath, path)
items := createSchemaItems(params, schema.Ref.OneOf[1].Properties, lines, lspLine, whitespaceLine, prefixLength, file, manager, documentPath, path)
return processItems(items, whitespaceLine), nil
}
return nil, nil
Expand All @@ -231,30 +233,41 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, manager
}

path, nodeProps, arrayAttributes := nodeProperties(path, line, character)
dependencies := dependencyCompletionItems(file, documentPath, path, params, wordPrefix)
dependencies := dependencyCompletionItems(file, documentPath, path, params, prefixLength)
if len(dependencies) > 0 {
return &protocol.CompletionList{Items: dependencies}, nil
}
items, stop := buildTargetCompletionItems(params, manager, path, documentPath, wordPrefix)
items, stop := buildTargetCompletionItems(params, manager, path, documentPath, prefixLength)
if stop {
return &protocol.CompletionList{Items: items}, nil
}
folderStructureItems := folderStructureCompletionItems(documentPath, path, removeQuote(prefixContent))
if len(folderStructureItems) > 0 {
return processItems(folderStructureItems, whitespaceLine && arrayAttributes), nil
}

items = namedDependencyCompletionItems(file, path, "configs", "configs", params, wordPrefix)
items = namedDependencyCompletionItems(file, path, "configs", "configs", params, prefixLength)
if len(items) == 0 {
items = namedDependencyCompletionItems(file, path, "secrets", "secrets", params, wordPrefix)
items = namedDependencyCompletionItems(file, path, "secrets", "secrets", params, prefixLength)
}
if len(items) == 0 {
items = volumeDependencyCompletionItems(file, path, params, wordPrefix)
items = volumeDependencyCompletionItems(file, path, params, prefixLength)
}
schemaItems := createSchemaItems(params, nodeProps, lines, lspLine, whitespaceLine && arrayAttributes, wordPrefix, file, manager, documentPath, path)
schemaItems := createSchemaItems(params, nodeProps, lines, lspLine, whitespaceLine && arrayAttributes, prefixLength, file, manager, documentPath, path)
items = append(items, schemaItems...)
if len(items) == 0 {
return nil, nil
}
return processItems(items, whitespaceLine && arrayAttributes), nil
}

func removeQuote(prefix string) string {
if len(prefix) > 0 && (prefix[0] == 34 || prefix[0] == 39) {
return prefix[1:]
}
return prefix
}

func createEnumItems(schema *jsonschema.Schema, params *protocol.CompletionParams, wordPrefixLength protocol.UInteger) []protocol.CompletionItem {
items := []protocol.CompletionItem{}
for _, value := range schema.Enum.Values {
Expand Down Expand Up @@ -382,6 +395,54 @@ func modifyTextEdit(file *ast.File, manager *document.Manager, documentPath docu
return edit
}

func folderStructureCompletionItems(documentPath document.DocumentPath, path []*ast.MappingValueNode, prefix string) []protocol.CompletionItem {
folder := directoryForPrefix(documentPath, path, prefix)
if folder != "" {
items := []protocol.CompletionItem{}
entries, _ := os.ReadDir(folder)
for _, entry := range entries {
item := protocol.CompletionItem{Label: entry.Name()}
if entry.IsDir() {
item.Kind = types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder)
} else {
item.Kind = types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile)
}
items = append(items, item)
}
return items
}
return nil
}

func directoryForPrefix(documentPath document.DocumentPath, path []*ast.MappingValueNode, prefix string) string {
if len(path) == 3 && path[2].Key.GetToken().Value == "volumes" {
if strings.HasPrefix(prefix, "./") {
_, folder := types.Concatenate(documentPath.Folder, prefix[0:strings.LastIndex(prefix, "/")], documentPath.WSLDollarSignHost)
return folder
}
} else if len(path) == 4 && path[2].Key.GetToken().Value == "volumes" && path[3].Key.GetToken().Value == "source" {
if volumes, ok := path[2].Value.(*ast.SequenceNode); ok {
for _, node := range volumes.Values {
if volume, ok := node.(*ast.MappingNode); ok {
if slices.Contains(volume.Values, path[3]) {
for _, property := range volume.Values {
if property.Key.GetToken().Value == "type" && property.Value.GetToken().Value == "bind" {
if strings.HasPrefix(prefix, "./") {
_, folder := types.Concatenate(documentPath.Folder, prefix[0:strings.LastIndex(prefix, "/")], documentPath.WSLDollarSignHost)
return folder
}
return documentPath.Folder
}
}
return ""
}
}
}
}
}
return ""
}

func findDependencies(file *ast.File, dependencyType string) []string {
services := []string{}
for _, documentNode := range file.Docs {
Expand Down
Loading
Loading