-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
dockerHighlight.ts
137 lines (124 loc) · 4.95 KB
/
dockerHighlight.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* --------------------------------------------------------------------------------------------
* Copyright (c) Remy Suen. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import {
TextDocument, Position, DocumentHighlight, DocumentHighlightKind
} from 'vscode-languageserver';
import { DockerfileParser, From } from 'dockerfile-ast';
import { DockerDefinition } from './dockerDefinition';
import { Util } from './docker';
export class DockerHighlight {
public computeHighlightRanges(document: TextDocument, position: Position): DocumentHighlight[] {
let dockerfile = DockerfileParser.parse(document.getText());
let provider = new DockerDefinition();
let location = provider.computeDefinition(document, position);
let image = location === null ? dockerfile.getContainingImage(position) : dockerfile.getContainingImage(location.range.start);
const highlights: DocumentHighlight[] = [];
if (location === null) {
for (let instruction of dockerfile.getCOPYs()) {
let flag = instruction.getFromFlag();
if (flag) {
let range = flag.getValueRange();
if (range && range.start.line === position.line && range.start.character <= position.character && position.character <= range.end.character) {
let stage = flag.getValue();
for (let other of dockerfile.getCOPYs()) {
let otherFlag = other.getFromFlag();
if (otherFlag && otherFlag.getValue() === stage) {
highlights.push(DocumentHighlight.create(otherFlag.getValueRange(), DocumentHighlightKind.Read));
}
}
return highlights;
}
}
}
for (const from of dockerfile.getFROMs()) {
for (const variable of from.getVariables()) {
if (Util.isInsideRange(position, variable.getNameRange())) {
const name = variable.getName();
for (const loopFrom of dockerfile.getFROMs()) {
for (const fromVariable of loopFrom.getVariables()) {
if (fromVariable.getName() === name) {
highlights.push(DocumentHighlight.create(fromVariable.getNameRange(), DocumentHighlightKind.Read));
}
}
}
return highlights;
}
}
}
for (let instruction of image.getInstructions()) {
for (let variable of instruction.getVariables()) {
if (Util.isInsideRange(position, variable.getNameRange())) {
let name = variable.getName();
for (let instruction of image.getInstructions()) {
if (!(instruction instanceof From)) {
for (let variable of instruction.getVariables()) {
if (variable.getName() === name) {
highlights.push(DocumentHighlight.create(variable.getNameRange(), DocumentHighlightKind.Read));
}
}
}
}
return highlights;
}
}
}
} else {
let definition = document.getText().substring(document.offsetAt(location.range.start), document.offsetAt(location.range.end));
for (let from of dockerfile.getFROMs()) {
let range = from.getBuildStageRange();
if (range && range.start.line === location.range.start.line) {
highlights.push(DocumentHighlight.create(location.range, DocumentHighlightKind.Write));
for (let instruction of dockerfile.getCOPYs()) {
let flag = instruction.getFromFlag();
if (flag) {
if (flag.getValue() === definition) {
highlights.push(DocumentHighlight.create(flag.getValueRange(), DocumentHighlightKind.Read));
}
}
}
return highlights;
}
}
for (let arg of image.getARGs()) {
let property = arg.getProperty();
// property may be null if it's an ARG with no arguments
if (property && property.getName() === definition) {
highlights.push(DocumentHighlight.create(property.getNameRange(), DocumentHighlightKind.Write));
}
}
for (let env of image.getENVs()) {
for (let property of env.getProperties()) {
if (property.getName() === definition) {
highlights.push(DocumentHighlight.create(property.getNameRange(), DocumentHighlightKind.Write));
}
}
}
for (let instruction of image.getInstructions()) {
// only highlight variables in non-FROM instructions
if (!(instruction instanceof From)) {
for (const variable of instruction.getVariables()) {
if (variable.getName() === definition) {
highlights.push(DocumentHighlight.create(variable.getNameRange(), DocumentHighlightKind.Read));
}
}
}
}
for (const arg of dockerfile.getInitialARGs()) {
const property = arg.getProperty();
if (property && Util.rangeEquals(property.getNameRange(), location.range)) {
for (const from of dockerfile.getFROMs()) {
for (const variable of from.getVariables()) {
if (variable.getName() === definition) {
highlights.push(DocumentHighlight.create(variable.getNameRange(), DocumentHighlightKind.Read));
}
}
}
}
}
}
return highlights;
}
}