Skip to content

Commit

Permalink
Fix #96 Consider semantic token ranges inside variables
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Oct 20, 2021
1 parent 1472d27 commit a6ff418
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/dockerSemanticTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,11 @@ export class DockerSemanticTokens {
let lastVariableRange = null;
for (const variable of instruction.getVariables()) {
const variableRange = variable.getRange();
if (Util.isInsideRange(variableRange.start, range)) {
if (Util.isInsideRange(range.start, variableRange) && Util.isInsideRange(range.end, variableRange)) {
// the token is completely inside the variable's range, render it as a variable
this.createToken(instruction, range, SemanticTokenTypes.variable, [], false);
return;
} else if (Util.isInsideRange(variableRange.start, range)) {
if (Util.positionBefore(startPosition, variableRange.start)) {
// create a parameter token for the characters
// before the variable
Expand Down
18 changes: 18 additions & 0 deletions test/dockerSemanticTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,15 @@ describe("Dockerfile Semantic Token tests", () => {
assertEdit(tokens.data, SemanticTokenTypes.variable, 10, 0, 5, 4);
});

it("RUN $var\\\\niable", () => {
const tokens = computeSemanticTokens("RUN $var\\\niable");
assert.strictEqual(20, tokens.data.length);
assertEdit(tokens.data, SemanticTokenTypes.keyword, 0, 0, 0, 3);
assertEdit(tokens.data, SemanticTokenTypes.variable, 5, 0, 4, 4);
assertEdit(tokens.data, SemanticTokenTypes.macro, 10, 0, 4, 1);
assertEdit(tokens.data, SemanticTokenTypes.variable, 15, 1, 0, 5);
});

it("FROM golang:$GO_VERSION AS", () => {
const tokens = computeSemanticTokens("FROM golang:$GO_VERSION AS");
assert.strictEqual(20, tokens.data.length);
Expand Down Expand Up @@ -660,6 +669,15 @@ describe("Dockerfile Semantic Token tests", () => {
assertEdit(tokens.data, SemanticTokenTypes.variable, 10, 0, 7, 6);
});

it("RUN ${var}\\\\niable", () => {
const tokens = computeSemanticTokens("RUN ${var}\\\niable");
assert.strictEqual(20, tokens.data.length);
assertEdit(tokens.data, SemanticTokenTypes.keyword, 0, 0, 0, 3);
assertEdit(tokens.data, SemanticTokenTypes.variable, 5, 0, 4, 6);
assertEdit(tokens.data, SemanticTokenTypes.macro, 10, 0, 6, 1);
assertEdit(tokens.data, SemanticTokenTypes.parameter, 15, 1, 0, 5);
});

it("HEALTHCHECK --timeout=${a} CMD ls", () => {
const tokens = computeSemanticTokens("HEALTHCHECK --timeout=${a} CMD ls");
assert.strictEqual(30, tokens.data.length);
Expand Down

0 comments on commit a6ff418

Please sign in to comment.