Skip to content

Commit

Permalink
Fix #126 Support preparing renames for build stage references
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Aug 4, 2024
1 parent 8d903ed commit 6c3ec19
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]
### Added
- support preparing renames for here-documents ([#129](https://github.com/rcjsuen/dockerfile-language-service/issues/129))
- support preparing renames for a build stage referenced in a FROM ([#126](https://github.com/rcjsuen/dockerfile-language-service/issues/126))

### Fixed
- stop resolving definitions to build stages after the selected line ([#130](https://github.com/rcjsuen/dockerfile-language-service/issues/130))
Expand Down
13 changes: 12 additions & 1 deletion src/dockerRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ export class DockerRename {
}
}

for (let from of image.getFROMs()) {
const fromInstructions = dockerfile.getFROMs();
for (const from of fromInstructions) {
if (Util.isInsideRange(position, from.getBuildStageRange())) {
return from.getBuildStageRange();
}

const range = from.getImageNameRange();
if (Util.isInsideRange(position, range)) {
const imageName = from.getImageName();
for (const stageCheck of fromInstructions) {
if (stageCheck.getBuildStage() === imageName && stageCheck.getBuildStageRange().start.line < range.start.line) {
return range;
}
}
}
}

for (let env of image.getENVs()) {
Expand Down
4 changes: 4 additions & 0 deletions test/dockerRename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ describe("Dockerfile Document Rename tests", function () {
assertEdit(edits[0], "base2", 0, 15, 0, 19);
assertEdit(edits[1], "base2", 1, 5, 1, 9);

range = prepareRename(content, 1, 7);
assertRange(range, 1, 5, 1, 9);
edits = rename(content, 1, 7, "base2");
assert.strictEqual(edits.length, 2);
assertEdit(edits[0], "base2", 0, 15, 0, 19);
Expand All @@ -122,6 +124,8 @@ describe("Dockerfile Document Rename tests", function () {
assertEdit(edits[0], "base", 1, 16, 1, 22);
assertEdit(edits[1], "base", 2, 5, 2, 11);

range = prepareRename(content, 2, 8);
assertRange(range, 2, 5, 2, 11);
edits = rename(content, 2, 8, "base");
assert.strictEqual(edits.length, 2);
assertEdit(edits[0], "base", 1, 16, 1, 22);
Expand Down

0 comments on commit 6c3ec19

Please sign in to comment.