Skip to content

Commit

Permalink
Fix #129 Support preparing renames for heredocs
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Aug 2, 2024
1 parent 6898e04 commit 3830bee
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
16 changes: 15 additions & 1 deletion src/dockerRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down
96 changes: 96 additions & 0 deletions test/dockerRename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2803,4 +2803,100 @@ describe("Dockerfile Document Rename tests", function () {
});
});
});

function createHeredocTests(instruction: string, offset: number) {
const tests = [
{
testName: `<<file`,
content: `FROM alpine\n${instruction} echo <<file\nabc\nfile`,
offset: 8
},
{
testName: `<<-file`,
content: `FROM alpine\n${instruction} echo <<-file\nabc\nfile`,
offset: 9
},
{
testName: `<<'file'`,
content: `FROM alpine\n${instruction} echo <<'file'\nabc\nfile`,
offset: 9
},
{
testName: `<<-'file'`,
content: `FROM alpine\n${instruction} echo <<-'file'\nabc\nfile`,
offset: 10
},
{
testName: `<<"file"`,
content: `FROM alpine\n${instruction} echo <<"file"\nabc\nfile`,
offset: 9
},
{
testName: `<<-"file"`,
content: `FROM alpine\n${instruction} echo <<-"file"\nabc\nfile`,
offset: 10
}
];

describe(instruction, () => {
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 <<file\nfile`;
const range = prepareRename(content, 1, offset + 7);
assert.strictEqual(range, null);
});
});

describe("computeRename", () => {
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 <<file\nfile`;
const range = rename(content, 1, offset + 7, "file2");
assert.strictEqual(0, range.length);
});
});
});

describe("delimiter", () => {
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);
});
});

0 comments on commit 3830bee

Please sign in to comment.