Skip to content

Commit 14ebce9

Browse files
authored
Merge pull request #142 from docker/volume-snippet-path-compose-completions
Include a path as a snippet for named volumes
2 parents 9a1888b + 167640b commit 14ebce9

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

internal/compose/completion.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
8282
return &protocol.CompletionList{Items: dependencies}, nil
8383
}
8484

85-
items := namedDependencyCompletionItems(file, path, "configs", "configs", params, protocol.UInteger(len(wordPrefix)))
85+
items := volumeDependencyCompletionItems(file, path, params, protocol.UInteger(len(wordPrefix)))
86+
if len(items) > 0 {
87+
return &protocol.CompletionList{Items: items}, nil
88+
}
89+
items = namedDependencyCompletionItems(file, path, "configs", "configs", params, protocol.UInteger(len(wordPrefix)))
8690
if len(items) == 0 {
8791
items = namedDependencyCompletionItems(file, path, "secrets", "secrets", params, protocol.UInteger(len(wordPrefix)))
8892
}
@@ -201,7 +205,6 @@ func dependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, par
201205
dependency := map[string]string{
202206
"depends_on": "services",
203207
"networks": "networks",
204-
"volumes": "volumes",
205208
}
206209
for serviceAttribute, dependencyType := range dependency {
207210
items := namedDependencyCompletionItems(file, path, serviceAttribute, dependencyType, params, prefixLength)
@@ -212,6 +215,19 @@ func dependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, par
212215
return nil
213216
}
214217

218+
func volumeDependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, params *protocol.CompletionParams, prefixLength protocol.UInteger) []protocol.CompletionItem {
219+
items := namedDependencyCompletionItems(file, path, "volumes", "volumes", params, prefixLength)
220+
for i := range items {
221+
edit := items[i].TextEdit.(protocol.TextEdit)
222+
items[i].TextEdit = protocol.TextEdit{
223+
NewText: fmt.Sprintf("%v:${1:/container/path}", edit.NewText),
224+
Range: edit.Range,
225+
}
226+
items[i].InsertTextFormat = types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet)
227+
}
228+
return items
229+
}
230+
215231
func namedDependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, serviceAttribute, dependencyType string, params *protocol.CompletionParams, prefixLength protocol.UInteger) []protocol.CompletionItem {
216232
if len(path) == 3 && path[2].Key.GetToken().Value == serviceAttribute {
217233
items := []protocol.CompletionItem{}

internal/compose/completion_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,8 +2071,9 @@ volumes:
20712071
list: &protocol.CompletionList{
20722072
Items: []protocol.CompletionItem{
20732073
{
2074-
Label: "test2",
2075-
TextEdit: textEdit("test2", 5, 8, 0),
2074+
Label: "test2",
2075+
TextEdit: textEdit("test2:${1:/container/path}", 5, 8, 0),
2076+
InsertTextFormat: types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet),
20762077
},
20772078
},
20782079
},
@@ -2094,8 +2095,9 @@ volumes:
20942095
list: &protocol.CompletionList{
20952096
Items: []protocol.CompletionItem{
20962097
{
2097-
Label: "test2",
2098-
TextEdit: textEdit("test2", 6, 8, 0),
2098+
Label: "test2",
2099+
TextEdit: textEdit("test2:${1:/container/path}", 6, 8, 0),
2100+
InsertTextFormat: types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet),
20992101
},
21002102
},
21012103
},
@@ -2115,8 +2117,9 @@ volumes:
21152117
list: &protocol.CompletionList{
21162118
Items: []protocol.CompletionItem{
21172119
{
2118-
Label: "test2",
2119-
TextEdit: textEdit("test2", 5, 9, 1),
2120+
Label: "test2",
2121+
TextEdit: textEdit("test2:${1:/container/path}", 5, 9, 1),
2122+
InsertTextFormat: types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet),
21202123
},
21212124
},
21222125
},
@@ -2136,8 +2139,9 @@ volumes:
21362139
list: &protocol.CompletionList{
21372140
Items: []protocol.CompletionItem{
21382141
{
2139-
Label: "test2",
2140-
TextEdit: textEdit("test2", 5, 6, 0),
2142+
Label: "test2",
2143+
TextEdit: textEdit("test2:${1:/container/path}", 5, 6, 0),
2144+
InsertTextFormat: types.CreateInsertTextFormatPointer(protocol.InsertTextFormatSnippet),
21412145
},
21422146
},
21432147
},

0 commit comments

Comments
 (0)