diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dfbbd3..537232e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog 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)) + ## [0.14.0] - 2024-06-18 ### Added - support computing highlight ranges for heredocs ([#121](https://github.com/rcjsuen/dockerfile-language-service/issues/121)) diff --git a/src/dockerRename.ts b/src/dockerRename.ts index fcd9026..0d1b789 100644 --- a/src/dockerRename.ts +++ b/src/dockerRename.ts @@ -5,7 +5,7 @@ 'use strict'; import { Range, Position, TextEdit, TextDocumentIdentifier } from 'vscode-languageserver-types'; -import { DockerfileParser } from 'dockerfile-ast'; +import { DockerfileParser, Copy, Run } from 'dockerfile-ast'; import { DockerHighlight } from './dockerHighlight'; import { Util } from './docker'; @@ -51,6 +51,20 @@ export class DockerRename { return variable.getNameRange(); } } + + if (instruction instanceof Copy || instruction instanceof Run) { + for (const heredoc of instruction.getHeredocs()) { + let range = heredoc.getNameRange(); + if (Util.isInsideRange(position, range)) { + return range; + } + + range = heredoc.getDelimiterRange(); + if (Util.isInsideRange(position, range)) { + return range; + } + } + } } return null; } diff --git a/test/dockerRename.test.ts b/test/dockerRename.test.ts index 7cb548f..6a37761 100644 --- a/test/dockerRename.test.ts +++ b/test/dockerRename.test.ts @@ -2803,4 +2803,100 @@ describe("Dockerfile Document Rename tests", function () { }); }); }); + + function createHeredocTests(instruction: string, offset: number) { + const tests = [ + { + testName: `< { + describe("definition", () => { + describe("prepareRename", () => { + tests.forEach((test) => { + it(test.testName, () => { + const range = prepareRename(test.content, 1, offset + 11); + assertRange(range, 1, offset + test.offset, 1, offset + test.offset + 4); + }); + }); + + it("outside name range", () => { + const content = `FROM alpine\n${instruction} echo < { + tests.forEach((test) => { + it(test.testName, () => { + const edits = rename(test.content, 1, offset + 11, "file2"); + assertEdit(edits[0], "file2", 1, offset + test.offset, 1, offset + test.offset + 4); + assertEdit(edits[1], "file2", 3, 0, 3, 4); + }); + }); + + it("outside name range", () => { + const content = `FROM alpine\n${instruction} echo < { + describe("prepareRename", () => { + tests.forEach((test) => { + it(test.testName, () => { + const range = prepareRename(test.content, 3, 2); + assertRange(range, 3, 0, 3, 4); + }); + }); + }); + + describe("computeRename", () => { + tests.forEach((test) => { + it(test.testName, () => { + const edits = rename(test.content, 3, 2, "file2"); + assertEdit(edits[0], "file2", 1, offset + test.offset, 1, offset + test.offset + 4); + assertEdit(edits[1], "file2", 3, 0, 3, 4); + }); + }); + }); + }); + }); + } + + describe("Heredoc", () => { + createHeredocTests("COPY", 4); + createHeredocTests("RUN", 3); + }); });