Skip to content

Commit 407952a

Browse files
authored
Merge pull request #140 from docker/compose-config-name-suggestions
Suggest dependent configs in Compose completion items
2 parents d8843d7 + 9c8bd54 commit 407952a

File tree

2 files changed

+195
-35
lines changed

2 files changed

+195
-35
lines changed

internal/compose/completion.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ func array(line string, character int) bool {
3333
continue
3434
} else if line[i] == '-' {
3535
isArray = true
36-
} else {
37-
isArray = false
38-
break
36+
} else if isArray && line[i] == ':' {
37+
return false
3938
}
4039
}
4140
return isArray
@@ -83,7 +82,7 @@ func Completion(ctx context.Context, params *protocol.CompletionParams, doc docu
8382
return &protocol.CompletionList{Items: dependencies}, nil
8483
}
8584

86-
items := []protocol.CompletionItem{}
85+
items := namedDependencyCompletionItems(file, path, "configs", "configs", params, protocol.UInteger(len(wordPrefix)))
8786
isArray := array(lines[lspLine], character-1)
8887
nodeProps, arrayAttributes := nodeProperties(path, line, character)
8988
if isArray != arrayAttributes {
@@ -201,29 +200,37 @@ func dependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, par
201200
"networks": "networks",
202201
"volumes": "volumes",
203202
}
204-
for key, value := range dependency {
205-
if len(path) == 3 && path[2].Key.GetToken().Value == key {
206-
items := []protocol.CompletionItem{}
207-
for _, service := range findDependencies(file, value) {
208-
if service != path[1].Key.GetToken().Value {
209-
item := protocol.CompletionItem{
210-
Label: service,
211-
TextEdit: protocol.TextEdit{
212-
NewText: service,
213-
Range: protocol.Range{
214-
Start: protocol.Position{
215-
Line: params.Position.Line,
216-
Character: params.Position.Character - prefixLength,
217-
},
218-
End: params.Position,
203+
for serviceAttribute, dependencyType := range dependency {
204+
items := namedDependencyCompletionItems(file, path, serviceAttribute, dependencyType, params, prefixLength)
205+
if len(items) > 0 {
206+
return items
207+
}
208+
}
209+
return nil
210+
}
211+
212+
func namedDependencyCompletionItems(file *ast.File, path []*ast.MappingValueNode, serviceAttribute, dependencyType string, params *protocol.CompletionParams, prefixLength protocol.UInteger) []protocol.CompletionItem {
213+
if len(path) == 3 && path[2].Key.GetToken().Value == serviceAttribute {
214+
items := []protocol.CompletionItem{}
215+
for _, service := range findDependencies(file, dependencyType) {
216+
if service != path[1].Key.GetToken().Value {
217+
item := protocol.CompletionItem{
218+
Label: service,
219+
TextEdit: protocol.TextEdit{
220+
NewText: service,
221+
Range: protocol.Range{
222+
Start: protocol.Position{
223+
Line: params.Position.Line,
224+
Character: params.Position.Character - prefixLength,
219225
},
226+
End: params.Position,
220227
},
221-
}
222-
items = append(items, item)
228+
},
223229
}
230+
items = append(items, item)
224231
}
225-
return items
226232
}
233+
return items
227234
}
228235
return nil
229236
}

internal/compose/completion_test.go

Lines changed: 166 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ services:
19281928
},
19291929
},
19301930
{
1931-
name: "depends_on array items",
1931+
name: "depends_on array items with a prefix",
19321932
content: `
19331933
services:
19341934
test:
@@ -2015,16 +2015,15 @@ networks:
20152015
},
20162016
},
20172017
{
2018-
name: "networks array items",
2018+
name: "networks array items with a prefix",
20192019
content: `
20202020
services:
20212021
test:
20222022
image: alpine
20232023
networks:
20242024
- t
20252025
networks:
2026-
test2:
2027-
image: alpine`,
2026+
test2:`,
20282027
line: 5,
20292028
character: 9,
20302029
list: &protocol.CompletionList{
@@ -2045,8 +2044,7 @@ services:
20452044
networks:
20462045
20472046
networks:
2048-
test2:
2049-
image: alpine`,
2047+
test2:`,
20502048
line: 5,
20512049
character: 6,
20522050
list: &protocol.CompletionList{
@@ -2067,8 +2065,7 @@ services:
20672065
volumes:
20682066
-
20692067
volumes:
2070-
test2:
2071-
image: alpine`,
2068+
test2:`,
20722069
line: 5,
20732070
character: 8,
20742071
list: &protocol.CompletionList{
@@ -2104,16 +2101,15 @@ volumes:
21042101
},
21052102
},
21062103
{
2107-
name: "volumes array items",
2104+
name: "volumes array items with a prefix",
21082105
content: `
21092106
services:
21102107
test:
21112108
image: alpine
21122109
volumes:
21132110
- t
21142111
volumes:
2115-
test2:
2116-
image: alpine`,
2112+
test2:`,
21172113
line: 5,
21182114
character: 9,
21192115
list: &protocol.CompletionList{
@@ -2134,8 +2130,7 @@ services:
21342130
volumes:
21352131
21362132
volumes:
2137-
test2:
2138-
image: alpine`,
2133+
test2:`,
21392134
line: 5,
21402135
character: 6,
21412136
list: &protocol.CompletionList{
@@ -2147,6 +2142,164 @@ volumes:
21472142
},
21482143
},
21492144
},
2145+
{
2146+
name: "configs array items",
2147+
content: `
2148+
services:
2149+
test:
2150+
image: alpine
2151+
configs:
2152+
-
2153+
configs:
2154+
test2:
2155+
file: ./httpd.conf`,
2156+
line: 5,
2157+
character: 8,
2158+
list: &protocol.CompletionList{
2159+
Items: []protocol.CompletionItem{
2160+
{
2161+
Label: "gid",
2162+
Detail: types.CreateStringPointer("string"),
2163+
TextEdit: textEdit("gid: ", 5, 8, 0),
2164+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2165+
},
2166+
{
2167+
Label: "mode",
2168+
Detail: types.CreateStringPointer("number or string"),
2169+
TextEdit: textEdit("mode: ", 5, 8, 0),
2170+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2171+
},
2172+
{
2173+
Label: "source",
2174+
Detail: types.CreateStringPointer("string"),
2175+
TextEdit: textEdit("source: ", 5, 8, 0),
2176+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2177+
},
2178+
{
2179+
Label: "target",
2180+
Detail: types.CreateStringPointer("string"),
2181+
TextEdit: textEdit("target: ", 5, 8, 0),
2182+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2183+
},
2184+
{
2185+
Label: "test2",
2186+
TextEdit: textEdit("test2", 5, 8, 0),
2187+
},
2188+
{
2189+
Label: "uid",
2190+
Detail: types.CreateStringPointer("string"),
2191+
TextEdit: textEdit("uid: ", 5, 8, 0),
2192+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2193+
},
2194+
},
2195+
},
2196+
},
2197+
{
2198+
name: "configs array items across two files",
2199+
content: `
2200+
---
2201+
services:
2202+
test:
2203+
image: alpine
2204+
configs:
2205+
-
2206+
---
2207+
configs:
2208+
test2:
2209+
file: ./httpd.conf`,
2210+
line: 6,
2211+
character: 8,
2212+
list: &protocol.CompletionList{
2213+
Items: []protocol.CompletionItem{
2214+
{
2215+
Label: "gid",
2216+
Detail: types.CreateStringPointer("string"),
2217+
TextEdit: textEdit("gid: ", 6, 8, 0),
2218+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2219+
},
2220+
{
2221+
Label: "mode",
2222+
Detail: types.CreateStringPointer("number or string"),
2223+
TextEdit: textEdit("mode: ", 6, 8, 0),
2224+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2225+
},
2226+
{
2227+
Label: "source",
2228+
Detail: types.CreateStringPointer("string"),
2229+
TextEdit: textEdit("source: ", 6, 8, 0),
2230+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2231+
},
2232+
{
2233+
Label: "target",
2234+
Detail: types.CreateStringPointer("string"),
2235+
TextEdit: textEdit("target: ", 6, 8, 0),
2236+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2237+
},
2238+
{
2239+
Label: "test2",
2240+
TextEdit: textEdit("test2", 6, 8, 0),
2241+
},
2242+
{
2243+
Label: "uid",
2244+
Detail: types.CreateStringPointer("string"),
2245+
TextEdit: textEdit("uid: ", 6, 8, 0),
2246+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2247+
},
2248+
},
2249+
},
2250+
},
2251+
{
2252+
name: "configs array items with a prefix",
2253+
content: `
2254+
services:
2255+
test:
2256+
image: alpine
2257+
configs:
2258+
- t
2259+
configs:
2260+
test2:
2261+
file: ./httpd.conf`,
2262+
line: 5,
2263+
character: 9,
2264+
list: &protocol.CompletionList{
2265+
Items: []protocol.CompletionItem{
2266+
{
2267+
Label: "gid",
2268+
Detail: types.CreateStringPointer("string"),
2269+
TextEdit: textEdit("gid: ", 5, 9, 1),
2270+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2271+
},
2272+
{
2273+
Label: "mode",
2274+
Detail: types.CreateStringPointer("number or string"),
2275+
TextEdit: textEdit("mode: ", 5, 9, 1),
2276+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2277+
},
2278+
{
2279+
Label: "source",
2280+
Detail: types.CreateStringPointer("string"),
2281+
TextEdit: textEdit("source: ", 5, 9, 1),
2282+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2283+
},
2284+
{
2285+
Label: "target",
2286+
Detail: types.CreateStringPointer("string"),
2287+
TextEdit: textEdit("target: ", 5, 9, 1),
2288+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2289+
},
2290+
{
2291+
Label: "test2",
2292+
TextEdit: textEdit("test2", 5, 9, 1),
2293+
},
2294+
{
2295+
Label: "uid",
2296+
Detail: types.CreateStringPointer("string"),
2297+
TextEdit: textEdit("uid: ", 5, 9, 1),
2298+
InsertTextMode: types.CreateInsertTextModePointer(protocol.InsertTextModeAsIs),
2299+
},
2300+
},
2301+
},
2302+
},
21502303
}
21512304

21522305
composeFileURI := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "compose.yaml")), "/"))

0 commit comments

Comments
 (0)