Skip to content

Commit

Permalink
Fix #148 Add a completion item for COPY's --from flag
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Aug 19, 2017
1 parent db1525f commit 12c5628
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 114 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]
### Added
- textDocument/completion
- COPY's --from build stage flag ([#148](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/148))
- textDocument/publishDiagnostics
- warn if ENV or LABEL is missing closing quote ([#143](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/143))
- warn if FROM's build stage name is invalid ([#132](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/132))
Expand Down
40 changes: 30 additions & 10 deletions src/dockerAssist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class DockerAssist {
} else if (Util.isInsideRange(position, instruction.getRange())) {
switch (instruction.getKeyword()) {
case "COPY":
return this.createBuildStageProposals(dockerfile, instruction as Copy, position, offset);
return this.createCopyProposals(dockerfile, instruction as Copy, position, offset, prefix);
case "HEALTHCHECK":
let subcommand = (instruction as Healthcheck).getSubcommand();
if (subcommand && subcommand.isBefore(position)) {
Expand All @@ -145,12 +145,17 @@ export class DockerAssist {
break instructionsCheck;
} else {
let trigger = (instruction as Onbuild).getTriggerInstruction();
if (trigger !== null && trigger.getKeyword() === "HEALTHCHECK") {
let subcommand = (trigger as Healthcheck).getSubcommand();
if (subcommand && subcommand.isBefore(position)) {
return [];
if (trigger !== null) {
switch (trigger.getKeyword()) {
case "COPY":
return this.createCopyProposals(dockerfile, trigger as Copy, position, offset, prefix);
case "HEALTHCHECK":
let subcommand = (trigger as Healthcheck).getSubcommand();
if (subcommand && subcommand.isBefore(position)) {
return [];
}
return this.createHealthcheckProposals(dockerfile, position, offset, prefix);
}
return this.createHealthcheckProposals(dockerfile, position, offset, prefix);
}
}
return [];
Expand Down Expand Up @@ -219,24 +224,32 @@ export class DockerAssist {
return proposals;
}

private createBuildStageProposals(dockerfile: Dockerfile, copy: Copy, position: Position, offset: number) {
private createCopyProposals(dockerfile: Dockerfile, copy: Copy, position: Position, offset: number, prefix: string) {
let range = copy.getFromValueRange();
// is the user in the --from= area
if (range && Util.isInsideRange(position, copy.getFromValueRange())) {
// get the prefix
let prefix = this.document.getText().substring(this.document.offsetAt(range.start), offset).toLowerCase();
let stagePrefix = this.document.getText().substring(this.document.offsetAt(range.start), offset).toLowerCase();
let items: CompletionItem[] = [];
for (let from of dockerfile.getFROMs()) {
let stage = from.getBuildStage();
if (stage && stage.toLowerCase().indexOf(prefix) === 0) {
items.push(this.createSourceImageCompletionItem(stage, prefix.length, offset));
if (stage && stage.toLowerCase().indexOf(stagePrefix) === 0) {
items.push(this.createSourceImageCompletionItem(stage, stagePrefix.length, offset));
}
}
items.sort((item: CompletionItem, item2: CompletionItem) => {
return item.label.localeCompare(item2.label);
});
return items;
}

let copyArgs = copy.getArguments();
if (copyArgs.length === 0) {
return [ this.createCOPY_FlagFrom(0, offset) ];
} else if (Util.isInsideRange(position, copyArgs[0].getRange()) && prefix !== "--from=" && "--from=".indexOf(prefix) === 0) {
return [ this.createCOPY_FlagFrom(prefix.length, offset) ];
}

return [];
}

Expand Down Expand Up @@ -409,6 +422,13 @@ export class DockerAssist {
};
}

private createCOPY_FlagFrom(prefixLength: number, offset: number): CompletionItem {
if (this.snippetSupport) {
return this.createFlagCompletionItem("--from=stage", prefixLength, offset, "--from=${1:stage}", "COPY_FlagFrom");
}
return this.createFlagCompletionItem("--from=", prefixLength, offset, "--from=", "COPY_FlagFrom");
}

private createHEALTHCHECK_FlagInterval(prefixLength: number, offset: number): CompletionItem {
if (this.snippetSupport) {
return this.createFlagCompletionItem("--interval=30s", prefixLength, offset, "--interval=${1:30s}", "HEALTHCHECK_FlagInterval");
Expand Down
3 changes: 3 additions & 0 deletions src/dockerPlainText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class PlainTextDocumentation {
"hoverVolume": "Create a mount point with the specifid name and mark it as holding externally mounted volumes from the native host or from other containers.\n\n",
"hoverWorkdir": "Set the working directory for any subsequent ADD, COPY, CMD, ENTRYPOINT, or RUN` instructions that follow it in the `Dockerfile`.\n\n",

"hoverCopyFlagFrom": "The previous build stage to use as the source location instead of the build's context.",
"hoverHealthcheckFlagInterval": "The seconds to wait for the health check to run after the container has started, and then again the number of seconds to wait before running again after the previous check has completed.",
"hoverHealthcheckFlagRetries": "The number of consecutive failures of this health check before the container is considered to be `unhealthy`.",
"hoverHealthcheckFlagStartPeriod": "The number of seconds to wait for the container to startup. Failures during this grace period will not count towards the maximum number of retries. However, should a health check succeed during this period then any subsequent failures will count towards the maximum number of retries.",
Expand Down Expand Up @@ -70,6 +71,8 @@ export class PlainTextDocumentation {
"COPY hello.txt relative/to/workdir"
,

COPY_FlagFrom: this.dockerMessages["hoverCopyFlagFrom"],

ENTRYPOINT: this.dockerMessages["hoverEntrypoint"] +
"ENTRYPOINT [ \"/opt/app/run.sh\", \"--port\", \"8080\" ]"
,
Expand Down
Loading

0 comments on commit 12c5628

Please sign in to comment.