UNPKG

4.89 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/* --------------------------------------------------------------------------------------------
4 * Copyright (c) Remy Suen. All rights reserved.
5 * Licensed under the MIT License. See License.txt in the project root for license information.
6 * ------------------------------------------------------------------------------------------ */
7const vscode_languageserver_types_1 = require("vscode-languageserver-types");
8const flagOption_1 = require("./flagOption");
9class Flag {
10 constructor(document, range, name, nameRange, value, valueRange) {
11 this.options = [];
12 this.range = range;
13 this.name = name;
14 this.nameRange = nameRange;
15 this.value = value;
16 this.valueRange = valueRange;
17 if (this.value !== null) {
18 let offset = document.offsetAt(valueRange.start);
19 let nameStart = -1;
20 let valueStart = -1;
21 let hasOptions = false;
22 for (let i = 0; i < value.length; i++) {
23 switch (value.charAt(i)) {
24 case '=':
25 hasOptions = true;
26 if (valueStart === -1) {
27 valueStart = i + 1;
28 break;
29 }
30 break;
31 case ',':
32 this.options.push(this.createFlagOption(document, value, offset, nameStart, valueStart, i));
33 nameStart = -1;
34 valueStart = -1;
35 break;
36 default:
37 if (nameStart === -1) {
38 nameStart = i;
39 }
40 break;
41 }
42 }
43 if (hasOptions && nameStart !== -1) {
44 this.options.push(this.createFlagOption(document, value, offset, nameStart, valueStart, value.length));
45 }
46 }
47 }
48 createFlagOption(document, content, documentOffset, nameStart, valueStart, valueEnd) {
49 const optionRange = vscode_languageserver_types_1.Range.create(document.positionAt(documentOffset + nameStart), document.positionAt(documentOffset + valueEnd));
50 if (valueStart === -1) {
51 return new flagOption_1.FlagOption(optionRange, content.substring(nameStart, valueEnd), optionRange, null, null);
52 }
53 return new flagOption_1.FlagOption(optionRange, content.substring(nameStart, valueStart - 1), vscode_languageserver_types_1.Range.create(document.positionAt(documentOffset + nameStart), document.positionAt(documentOffset + valueStart - 1)), content.substring(valueStart, valueEnd), vscode_languageserver_types_1.Range.create(document.positionAt(documentOffset + valueStart), document.positionAt(documentOffset + valueEnd)));
54 }
55 toString() {
56 if (this.valueRange) {
57 return "--" + this.name + "=" + this.value;
58 }
59 return "--" + this.name;
60 }
61 /**
62 * Returns the range that encompasses this entire flag. This includes the
63 * -- prefix in the beginning to the last character of the flag's value (if
64 * it has been defined).
65 *
66 * @return the entire range of this flag
67 */
68 getRange() {
69 return this.range;
70 }
71 /**
72 * Returns the name of this flag. The name does not include the -- prefix.
73 * Thus, for HEALTHCHECK's --interval flag, interval is the flag's name and
74 * not --interval.
75 *
76 * @return this flag's name
77 */
78 getName() {
79 return this.name;
80 }
81 /**
82 * Returns the range that encompasses the flag's name
83 *
84 * @return the range containing the flag's name
85 */
86 getNameRange() {
87 return this.nameRange;
88 }
89 /**
90 * Returns the value that has been set to this flag. May be null if the
91 * flag is invalid and has no value set like a --start-period. If the flag
92 * is instead a --start-period= with an equals sign then the flag's value
93 * is the empty string.
94 *
95 * @return this flag's value if it has been defined, null otherwise
96 */
97 getValue() {
98 return this.value;
99 }
100 /**
101 * Returns the range that encompasses this flag's value. If no value has
102 * been set then null will be returned.
103 *
104 * @return the range containing this flag's value, or null if the flag
105 * has no value defined
106 */
107 getValueRange() {
108 return this.valueRange;
109 }
110 getOption(name) {
111 for (const option of this.options) {
112 if (option.getName() === name) {
113 return option;
114 }
115 }
116 return null;
117 }
118 getOptions() {
119 return this.options;
120 }
121 hasOptions() {
122 return this.options.length > 0;
123 }
124}
125exports.Flag = Flag;