-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodifiableInstruction.ts
More file actions
110 lines (102 loc) · 4.74 KB
/
Copy pathmodifiableInstruction.ts
File metadata and controls
110 lines (102 loc) · 4.74 KB
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Remy Suen. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { TextDocument } from 'vscode-languageserver-textdocument';
import { Range } from 'vscode-languageserver-types';
import { Dockerfile } from './dockerfile';
import { Argument } from './argument';
import { Flag } from './flag';
import { Instruction } from './instruction';
export abstract class ModifiableInstruction extends Instruction {
private flags: Flag[];
constructor(document: TextDocument, range: Range, dockerfile: Dockerfile, escapeChar: string, instruction: string, instructionRange: Range) {
super(document, range, dockerfile, escapeChar, instruction, instructionRange);
}
protected abstract stopSearchingForFlags(value: string): boolean;
public getFlags(): Flag[] {
if (!this.flags) {
this.flags = [];
for (let arg of this.getArguments()) {
let value = arg.getValue();
if (this.stopSearchingForFlags(value)) {
return this.flags;
} else if (value.indexOf("--") === 0) {
let range = arg.getRange();
let rawValue = this.document.getText().substring(this.document.offsetAt(range.start), this.document.offsetAt(range.end));
let nameIndex = value.indexOf('=');
let index = rawValue.indexOf('=');
let firstMatch = false;
let secondMatch = false;
let startIndex = -1;
nameSearchLoop: for (let i = 0; i < rawValue.length; i++) {
switch (rawValue.charAt(i)) {
case '\\':
case ' ':
case '\t':
case '\r':
case '\n':
break;
case '-':
if (secondMatch) {
startIndex = i;
break nameSearchLoop;
} else if (firstMatch) {
secondMatch = true;
} else {
firstMatch = true;
}
break;
default:
startIndex = i;
break nameSearchLoop;
}
}
let nameStart = this.document.positionAt(this.document.offsetAt(range.start) + startIndex);
if (index === -1) {
this.flags.push(new Flag(
this.document,
range,
value.substring(2),
Range.create(nameStart, range.end),
null,
null)
);
} else if (index === value.length - 1) {
let nameEnd = this.document.positionAt(this.document.offsetAt(range.start) + index);
this.flags.push(new Flag(
this.document,
range,
value.substring(2, index),
Range.create(nameStart, nameEnd),
"",
Range.create(range.end, range.end))
);
} else {
let nameEnd = this.document.positionAt(this.document.offsetAt(range.start) + index);
this.flags.push(new Flag(
this.document,
range,
value.substring(2, nameIndex),
Range.create(nameStart, nameEnd),
value.substring(nameIndex + 1),
Range.create(this.document.positionAt(this.document.offsetAt(range.start) + index + 1), range.end))
);
}
}
}
}
return this.flags;
}
public getArguments(): Argument[] {
const args = super.getArguments();
const flags = this.getFlags();
if (flags.length === 0) {
return args;
}
for (let i = 0; i < flags.length; i++) {
args.shift();
}
return args;
}
}