Skip to content

Commit e203368

Browse files
committed
Support file structure completions for env_file and label_file of a service
Signed-off-by: Remy Suen <[email protected]>
1 parent 669ed2c commit e203368

File tree

3 files changed

+298
-4
lines changed

3 files changed

+298
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ All notable changes to the Docker Language Server will be documented in this fil
88

99
- Compose
1010
- textDocument/completion
11-
- provide local file and folder name suggestions when modifying simple strings for service volumes ([#376](https://github.com/docker/docker-language-server/issues/376))
11+
- provide local file and folder name suggestions when modifying simple strings
12+
- service volumes ([#376](https://github.com/docker/docker-language-server/issues/376))
13+
- env_file and label_file attributes of a service ([#403](https://github.com/docker/docker-language-server/issues/403))
1214

1315
### Fixed
1416

internal/compose/completion.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,29 @@ func folderStructureCompletionItems(documentPath document.DocumentPath, path []*
415415
}
416416

417417
func directoryForPrefix(documentPath document.DocumentPath, path []*ast.MappingValueNode, prefix string) string {
418-
if len(path) == 3 && path[0].Key.GetToken().Value == "services" && path[2].Key.GetToken().Value == "volumes" {
419-
if strings.HasPrefix(prefix, "./") {
420-
_, folder := types.Concatenate(documentPath.Folder, prefix[0:strings.LastIndex(prefix, "/")], documentPath.WSLDollarSignHost)
418+
if len(path) == 3 && path[0].Key.GetToken().Value == "services" {
419+
if path[2].Key.GetToken().Value == "env_file" {
420+
idx := strings.LastIndex(prefix, "/")
421+
if idx == -1 {
422+
return documentPath.Folder
423+
}
424+
_, folder := types.Concatenate(documentPath.Folder, prefix[0:idx], documentPath.WSLDollarSignHost)
425+
return folder
426+
}
427+
if path[2].Key.GetToken().Value == "label_file" {
428+
idx := strings.LastIndex(prefix, "/")
429+
if idx == -1 {
430+
return documentPath.Folder
431+
}
432+
_, folder := types.Concatenate(documentPath.Folder, prefix[0:idx], documentPath.WSLDollarSignHost)
421433
return folder
422434
}
435+
if path[2].Key.GetToken().Value == "volumes" {
436+
if strings.HasPrefix(prefix, "./") {
437+
_, folder := types.Concatenate(documentPath.Folder, prefix[0:strings.LastIndex(prefix, "/")], documentPath.WSLDollarSignHost)
438+
return folder
439+
}
440+
}
423441
} else if len(path) == 4 && path[0].Key.GetToken().Value == "services" && path[2].Key.GetToken().Value == "volumes" && path[3].Key.GetToken().Value == "source" {
424442
if volumes, ok := path[2].Value.(*ast.SequenceNode); ok {
425443
for _, node := range volumes.Values {

internal/compose/completion_test.go

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4875,6 +4875,280 @@ services:
48754875
},
48764876
},
48774877
},
4878+
{
4879+
name: "env_file string attribute with no prefix",
4880+
content: `
4881+
services:
4882+
test:
4883+
env_file: `,
4884+
line: 3,
4885+
character: 14,
4886+
list: &protocol.CompletionList{
4887+
Items: []protocol.CompletionItem{
4888+
{
4889+
Label: "a.txt",
4890+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
4891+
},
4892+
{
4893+
Label: "b",
4894+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4895+
},
4896+
{
4897+
Label: "folder",
4898+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4899+
},
4900+
},
4901+
},
4902+
},
4903+
{
4904+
name: "env_file string attribute with a ./ prefix",
4905+
content: `
4906+
services:
4907+
test:
4908+
env_file: ./`,
4909+
line: 3,
4910+
character: 16,
4911+
list: &protocol.CompletionList{
4912+
Items: []protocol.CompletionItem{
4913+
{
4914+
Label: "a.txt",
4915+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
4916+
},
4917+
{
4918+
Label: "b",
4919+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4920+
},
4921+
{
4922+
Label: "folder",
4923+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4924+
},
4925+
},
4926+
},
4927+
},
4928+
{
4929+
name: "env_file string attribute with a ./folder/ prefix",
4930+
content: `
4931+
services:
4932+
test:
4933+
env_file: ./folder/`,
4934+
line: 3,
4935+
character: 23,
4936+
list: &protocol.CompletionList{
4937+
Items: []protocol.CompletionItem{
4938+
{
4939+
Label: "subfile.txt",
4940+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
4941+
},
4942+
},
4943+
},
4944+
},
4945+
{
4946+
name: "env_file string attribute with no prefix",
4947+
content: `
4948+
services:
4949+
test:
4950+
env_file:
4951+
- `,
4952+
line: 4,
4953+
character: 8,
4954+
list: &protocol.CompletionList{
4955+
Items: []protocol.CompletionItem{
4956+
{
4957+
Label: "a.txt",
4958+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
4959+
},
4960+
{
4961+
Label: "b",
4962+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4963+
},
4964+
{
4965+
Label: "folder",
4966+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4967+
},
4968+
},
4969+
},
4970+
},
4971+
{
4972+
name: "env_file string attribute with a ./ prefix",
4973+
content: `
4974+
services:
4975+
test:
4976+
env_file:
4977+
- ./`,
4978+
line: 4,
4979+
character: 10,
4980+
list: &protocol.CompletionList{
4981+
Items: []protocol.CompletionItem{
4982+
{
4983+
Label: "a.txt",
4984+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
4985+
},
4986+
{
4987+
Label: "b",
4988+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4989+
},
4990+
{
4991+
Label: "folder",
4992+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
4993+
},
4994+
},
4995+
},
4996+
},
4997+
{
4998+
name: "env_file string attribute with a ./folder/ prefix",
4999+
content: `
5000+
services:
5001+
test:
5002+
env_file:
5003+
- ./folder/`,
5004+
line: 4,
5005+
character: 17,
5006+
list: &protocol.CompletionList{
5007+
Items: []protocol.CompletionItem{
5008+
{
5009+
Label: "subfile.txt",
5010+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
5011+
},
5012+
},
5013+
},
5014+
},
5015+
{
5016+
name: "label_file string attribute with no prefix",
5017+
content: `
5018+
services:
5019+
test:
5020+
label_file: `,
5021+
line: 3,
5022+
character: 16,
5023+
list: &protocol.CompletionList{
5024+
Items: []protocol.CompletionItem{
5025+
{
5026+
Label: "a.txt",
5027+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
5028+
},
5029+
{
5030+
Label: "b",
5031+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5032+
},
5033+
{
5034+
Label: "folder",
5035+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5036+
},
5037+
},
5038+
},
5039+
},
5040+
{
5041+
name: "label_file string attribute with a ./ prefix",
5042+
content: `
5043+
services:
5044+
test:
5045+
label_file: ./`,
5046+
line: 3,
5047+
character: 18,
5048+
list: &protocol.CompletionList{
5049+
Items: []protocol.CompletionItem{
5050+
{
5051+
Label: "a.txt",
5052+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
5053+
},
5054+
{
5055+
Label: "b",
5056+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5057+
},
5058+
{
5059+
Label: "folder",
5060+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5061+
},
5062+
},
5063+
},
5064+
},
5065+
{
5066+
name: "label_file string attribute with a ./folder/ prefix",
5067+
content: `
5068+
services:
5069+
test:
5070+
label_file: ./folder/`,
5071+
line: 3,
5072+
character: 25,
5073+
list: &protocol.CompletionList{
5074+
Items: []protocol.CompletionItem{
5075+
{
5076+
Label: "subfile.txt",
5077+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
5078+
},
5079+
},
5080+
},
5081+
},
5082+
{
5083+
name: "label_file string attribute with no prefix",
5084+
content: `
5085+
services:
5086+
test:
5087+
label_file:
5088+
- `,
5089+
line: 4,
5090+
character: 8,
5091+
list: &protocol.CompletionList{
5092+
Items: []protocol.CompletionItem{
5093+
{
5094+
Label: "a.txt",
5095+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
5096+
},
5097+
{
5098+
Label: "b",
5099+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5100+
},
5101+
{
5102+
Label: "folder",
5103+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5104+
},
5105+
},
5106+
},
5107+
},
5108+
{
5109+
name: "label_file string attribute with a ./ prefix",
5110+
content: `
5111+
services:
5112+
test:
5113+
label_file:
5114+
- ./`,
5115+
line: 4,
5116+
character: 10,
5117+
list: &protocol.CompletionList{
5118+
Items: []protocol.CompletionItem{
5119+
{
5120+
Label: "a.txt",
5121+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
5122+
},
5123+
{
5124+
Label: "b",
5125+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5126+
},
5127+
{
5128+
Label: "folder",
5129+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFolder),
5130+
},
5131+
},
5132+
},
5133+
},
5134+
{
5135+
name: "label_file string attribute with a ./folder/ prefix",
5136+
content: `
5137+
services:
5138+
test:
5139+
label_file:
5140+
- ./folder/`,
5141+
line: 4,
5142+
character: 17,
5143+
list: &protocol.CompletionList{
5144+
Items: []protocol.CompletionItem{
5145+
{
5146+
Label: "subfile.txt",
5147+
Kind: types.CreateCompletionItemKindPointer(protocol.CompletionItemKindFile),
5148+
},
5149+
},
5150+
},
5151+
},
48785152
}
48795153

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

0 commit comments

Comments
 (0)