Skip to content

Commit

Permalink
Fix #44 Use prefix to suggest relevant build stages only
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Jul 16, 2017
1 parent 0e0fe58 commit c7ae5be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/dockerAssist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CompletionItem, CompletionItemKind, InsertTextFormat
} from 'vscode-languageserver';
import { Util, KEYWORDS, DIRECTIVE_ESCAPE } from './docker';
import { Dockerfile } from './parser/dockerfile';
import { DockerfileParser } from './parser/dockerfileParser';
import { Copy } from './parser/instructions/copy';

Expand Down Expand Up @@ -75,22 +76,7 @@ export class DockerAssist {
} else if (Util.isInsideRange(position, instruction.getRange())) {
switch (instruction.getKeyword()) {
case "COPY":
let copy = instruction as Copy;
let copyArgs = instruction.getArguments();
if (copyArgs.length !== 0 && copyArgs[0].getValue().indexOf("--from=") === 0 && copy.getFromValueRange().start.character === position.character) {
let items: CompletionItem[] = [];
for (let from of dockerfile.getFROMs()) {
let stage = from.getBuildStage();
if (stage) {
items.push(this.createSourceImageCompletionItem(stage, "", offset));
}
}
items.sort((item: CompletionItem, item2: CompletionItem) => {
return item.label.localeCompare(item2.label);
});
return items;
}
return [];
return this.createBuildStageProposals(dockerfile, instruction as Copy, position, offset);
case "ONBUILD":
let onbuildArgs = instruction.getArguments();
if (onbuildArgs.length === 0 || Util.isInsideRange(position, onbuildArgs[0].getRange())) {
Expand Down Expand Up @@ -160,6 +146,27 @@ export class DockerAssist {
return proposals;
}

private createBuildStageProposals(dockerfile: Dockerfile, copy: Copy, position: Position, offset: number) {
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);
let items: CompletionItem[] = [];
for (let from of dockerfile.getFROMs()) {
let stage = from.getBuildStage();
if (stage && stage.indexOf(prefix) === 0) {
items.push(this.createSourceImageCompletionItem(stage, prefix, offset));
}
}
items.sort((item: CompletionItem, item2: CompletionItem) => {
return item.label.localeCompare(item2.label);
});
return items;
}
return [];
}

/**
* Walks back in the text buffer to calculate the true prefix of the
* current text caret offset. This function will handle the
Expand Down
6 changes: 6 additions & 0 deletions test/dockerAssist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,12 @@ describe('Docker Content Assist Tests', function() {
assertSourceImage(proposals[0], "dev", 2, 12, 2, 12);
assertSourceImage(proposals[1], "setup", 2, 12, 2, 12);
});

it("source image prefix", function() {
var proposals = computePosition("FROM busybox AS setup\nFROM busybox AS dev\nCOPY --from=s", 2, 13);
assert.equal(proposals.length, 1);
assertSourceImage(proposals[0], "setup", 2, 12, 2, 13);
});
});
});

Expand Down

0 comments on commit c7ae5be

Please sign in to comment.