vscode-json-languageservice 5.0.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +10 -2
  2. package/lib/esm/jsonContributions.d.ts +17 -17
  3. package/lib/esm/jsonContributions.js +1 -1
  4. package/lib/esm/jsonLanguageService.d.ts +29 -29
  5. package/lib/esm/jsonLanguageService.js +66 -66
  6. package/lib/esm/jsonLanguageTypes.d.ts +292 -279
  7. package/lib/esm/jsonLanguageTypes.js +55 -46
  8. package/lib/esm/jsonSchema.d.ts +89 -89
  9. package/lib/esm/jsonSchema.js +1 -1
  10. package/lib/esm/parser/jsonParser.js +1236 -1214
  11. package/lib/esm/services/configuration.js +528 -528
  12. package/lib/esm/services/jsonCompletion.js +924 -918
  13. package/lib/esm/services/jsonDocumentSymbols.js +267 -267
  14. package/lib/esm/services/jsonFolding.js +120 -120
  15. package/lib/esm/services/jsonHover.js +109 -109
  16. package/lib/esm/services/jsonLinks.js +72 -72
  17. package/lib/esm/services/jsonSchemaService.js +593 -586
  18. package/lib/esm/services/jsonSelectionRanges.js +61 -61
  19. package/lib/esm/services/jsonValidation.js +151 -151
  20. package/lib/esm/utils/colors.js +68 -68
  21. package/lib/esm/utils/glob.js +124 -124
  22. package/lib/esm/utils/json.js +42 -42
  23. package/lib/esm/utils/objects.js +68 -68
  24. package/lib/esm/utils/strings.js +79 -64
  25. package/lib/umd/jsonContributions.d.ts +17 -17
  26. package/lib/umd/jsonContributions.js +12 -12
  27. package/lib/umd/jsonLanguageService.d.ts +29 -29
  28. package/lib/umd/jsonLanguageService.js +94 -90
  29. package/lib/umd/jsonLanguageTypes.d.ts +292 -279
  30. package/lib/umd/jsonLanguageTypes.js +103 -93
  31. package/lib/umd/jsonSchema.d.ts +89 -89
  32. package/lib/umd/jsonSchema.js +12 -12
  33. package/lib/umd/parser/jsonParser.js +1265 -1243
  34. package/lib/umd/services/configuration.js +541 -541
  35. package/lib/umd/services/jsonCompletion.js +938 -932
  36. package/lib/umd/services/jsonDocumentSymbols.js +281 -281
  37. package/lib/umd/services/jsonFolding.js +134 -134
  38. package/lib/umd/services/jsonHover.js +123 -123
  39. package/lib/umd/services/jsonLinks.js +86 -86
  40. package/lib/umd/services/jsonSchemaService.js +609 -602
  41. package/lib/umd/services/jsonSelectionRanges.js +75 -75
  42. package/lib/umd/services/jsonValidation.js +165 -165
  43. package/lib/umd/utils/colors.js +84 -84
  44. package/lib/umd/utils/glob.js +138 -138
  45. package/lib/umd/utils/json.js +56 -56
  46. package/lib/umd/utils/objects.js +87 -87
  47. package/lib/umd/utils/strings.js +98 -82
  48. package/package.json +11 -10
@@ -1,61 +1,61 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for license information.
4
- *--------------------------------------------------------------------------------------------*/
5
- import { Range, SelectionRange } from '../jsonLanguageTypes';
6
- import { createScanner } from 'jsonc-parser';
7
- export function getSelectionRanges(document, positions, doc) {
8
- function getSelectionRange(position) {
9
- let offset = document.offsetAt(position);
10
- let node = doc.getNodeFromOffset(offset, true);
11
- const result = [];
12
- while (node) {
13
- switch (node.type) {
14
- case 'string':
15
- case 'object':
16
- case 'array':
17
- // range without ", [ or {
18
- const cStart = node.offset + 1, cEnd = node.offset + node.length - 1;
19
- if (cStart < cEnd && offset >= cStart && offset <= cEnd) {
20
- result.push(newRange(cStart, cEnd));
21
- }
22
- result.push(newRange(node.offset, node.offset + node.length));
23
- break;
24
- case 'number':
25
- case 'boolean':
26
- case 'null':
27
- case 'property':
28
- result.push(newRange(node.offset, node.offset + node.length));
29
- break;
30
- }
31
- if (node.type === 'property' || node.parent && node.parent.type === 'array') {
32
- const afterCommaOffset = getOffsetAfterNextToken(node.offset + node.length, 5 /* CommaToken */);
33
- if (afterCommaOffset !== -1) {
34
- result.push(newRange(node.offset, afterCommaOffset));
35
- }
36
- }
37
- node = node.parent;
38
- }
39
- let current = undefined;
40
- for (let index = result.length - 1; index >= 0; index--) {
41
- current = SelectionRange.create(result[index], current);
42
- }
43
- if (!current) {
44
- current = SelectionRange.create(Range.create(position, position));
45
- }
46
- return current;
47
- }
48
- function newRange(start, end) {
49
- return Range.create(document.positionAt(start), document.positionAt(end));
50
- }
51
- const scanner = createScanner(document.getText(), true);
52
- function getOffsetAfterNextToken(offset, expectedToken) {
53
- scanner.setPosition(offset);
54
- let token = scanner.scan();
55
- if (token === expectedToken) {
56
- return scanner.getTokenOffset() + scanner.getTokenLength();
57
- }
58
- return -1;
59
- }
60
- return positions.map(getSelectionRange);
61
- }
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { Range, SelectionRange } from '../jsonLanguageTypes';
6
+ import { createScanner } from 'jsonc-parser';
7
+ export function getSelectionRanges(document, positions, doc) {
8
+ function getSelectionRange(position) {
9
+ let offset = document.offsetAt(position);
10
+ let node = doc.getNodeFromOffset(offset, true);
11
+ const result = [];
12
+ while (node) {
13
+ switch (node.type) {
14
+ case 'string':
15
+ case 'object':
16
+ case 'array':
17
+ // range without ", [ or {
18
+ const cStart = node.offset + 1, cEnd = node.offset + node.length - 1;
19
+ if (cStart < cEnd && offset >= cStart && offset <= cEnd) {
20
+ result.push(newRange(cStart, cEnd));
21
+ }
22
+ result.push(newRange(node.offset, node.offset + node.length));
23
+ break;
24
+ case 'number':
25
+ case 'boolean':
26
+ case 'null':
27
+ case 'property':
28
+ result.push(newRange(node.offset, node.offset + node.length));
29
+ break;
30
+ }
31
+ if (node.type === 'property' || node.parent && node.parent.type === 'array') {
32
+ const afterCommaOffset = getOffsetAfterNextToken(node.offset + node.length, 5 /* SyntaxKind.CommaToken */);
33
+ if (afterCommaOffset !== -1) {
34
+ result.push(newRange(node.offset, afterCommaOffset));
35
+ }
36
+ }
37
+ node = node.parent;
38
+ }
39
+ let current = undefined;
40
+ for (let index = result.length - 1; index >= 0; index--) {
41
+ current = SelectionRange.create(result[index], current);
42
+ }
43
+ if (!current) {
44
+ current = SelectionRange.create(Range.create(position, position));
45
+ }
46
+ return current;
47
+ }
48
+ function newRange(start, end) {
49
+ return Range.create(document.positionAt(start), document.positionAt(end));
50
+ }
51
+ const scanner = createScanner(document.getText(), true);
52
+ function getOffsetAfterNextToken(offset, expectedToken) {
53
+ scanner.setPosition(offset);
54
+ let token = scanner.scan();
55
+ if (token === expectedToken) {
56
+ return scanner.getTokenOffset() + scanner.getTokenLength();
57
+ }
58
+ return -1;
59
+ }
60
+ return positions.map(getSelectionRange);
61
+ }
@@ -1,151 +1,151 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for license information.
4
- *--------------------------------------------------------------------------------------------*/
5
- import { ErrorCode, Diagnostic, DiagnosticSeverity, Range } from '../jsonLanguageTypes';
6
- import * as nls from 'vscode-nls';
7
- import { isBoolean } from '../utils/objects';
8
- const localize = nls.loadMessageBundle();
9
- export class JSONValidation {
10
- constructor(jsonSchemaService, promiseConstructor) {
11
- this.jsonSchemaService = jsonSchemaService;
12
- this.promise = promiseConstructor;
13
- this.validationEnabled = true;
14
- }
15
- configure(raw) {
16
- if (raw) {
17
- this.validationEnabled = raw.validate !== false;
18
- this.commentSeverity = raw.allowComments ? undefined : DiagnosticSeverity.Error;
19
- }
20
- }
21
- doValidation(textDocument, jsonDocument, documentSettings, schema) {
22
- if (!this.validationEnabled) {
23
- return this.promise.resolve([]);
24
- }
25
- const diagnostics = [];
26
- const added = {};
27
- const addProblem = (problem) => {
28
- // remove duplicated messages
29
- const signature = problem.range.start.line + ' ' + problem.range.start.character + ' ' + problem.message;
30
- if (!added[signature]) {
31
- added[signature] = true;
32
- diagnostics.push(problem);
33
- }
34
- };
35
- const getDiagnostics = (schema) => {
36
- let trailingCommaSeverity = documentSettings?.trailingCommas ? toDiagnosticSeverity(documentSettings.trailingCommas) : DiagnosticSeverity.Error;
37
- let commentSeverity = documentSettings?.comments ? toDiagnosticSeverity(documentSettings.comments) : this.commentSeverity;
38
- let schemaValidation = documentSettings?.schemaValidation ? toDiagnosticSeverity(documentSettings.schemaValidation) : DiagnosticSeverity.Warning;
39
- let schemaRequest = documentSettings?.schemaRequest ? toDiagnosticSeverity(documentSettings.schemaRequest) : DiagnosticSeverity.Warning;
40
- if (schema) {
41
- const addSchemaProblem = (errorMessage, errorCode) => {
42
- if (jsonDocument.root && schemaRequest) {
43
- const astRoot = jsonDocument.root;
44
- const property = astRoot.type === 'object' ? astRoot.properties[0] : undefined;
45
- if (property && property.keyNode.value === '$schema') {
46
- const node = property.valueNode || property;
47
- const range = Range.create(textDocument.positionAt(node.offset), textDocument.positionAt(node.offset + node.length));
48
- addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
49
- }
50
- else {
51
- const range = Range.create(textDocument.positionAt(astRoot.offset), textDocument.positionAt(astRoot.offset + 1));
52
- addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
53
- }
54
- }
55
- };
56
- if (schema.errors.length) {
57
- addSchemaProblem(schema.errors[0], ErrorCode.SchemaResolveError);
58
- }
59
- else if (schemaValidation) {
60
- for (const warning of schema.warnings) {
61
- addSchemaProblem(warning, ErrorCode.SchemaUnsupportedFeature);
62
- }
63
- const semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation);
64
- if (semanticErrors) {
65
- semanticErrors.forEach(addProblem);
66
- }
67
- }
68
- if (schemaAllowsComments(schema.schema)) {
69
- commentSeverity = undefined;
70
- }
71
- if (schemaAllowsTrailingCommas(schema.schema)) {
72
- trailingCommaSeverity = undefined;
73
- }
74
- }
75
- for (const p of jsonDocument.syntaxErrors) {
76
- if (p.code === ErrorCode.TrailingComma) {
77
- if (typeof trailingCommaSeverity !== 'number') {
78
- continue;
79
- }
80
- p.severity = trailingCommaSeverity;
81
- }
82
- addProblem(p);
83
- }
84
- if (typeof commentSeverity === 'number') {
85
- const message = localize('InvalidCommentToken', 'Comments are not permitted in JSON.');
86
- jsonDocument.comments.forEach(c => {
87
- addProblem(Diagnostic.create(c, message, commentSeverity, ErrorCode.CommentNotPermitted));
88
- });
89
- }
90
- return diagnostics;
91
- };
92
- if (schema) {
93
- const id = schema.id || ('schemaservice://untitled/' + idCounter++);
94
- const handle = this.jsonSchemaService.registerExternalSchema(id, [], schema);
95
- return handle.getResolvedSchema().then(resolvedSchema => {
96
- return getDiagnostics(resolvedSchema);
97
- });
98
- }
99
- return this.jsonSchemaService.getSchemaForResource(textDocument.uri, jsonDocument).then(schema => {
100
- return getDiagnostics(schema);
101
- });
102
- }
103
- getLanguageStatus(textDocument, jsonDocument) {
104
- return { schemas: this.jsonSchemaService.getSchemaURIsForResource(textDocument.uri, jsonDocument) };
105
- }
106
- }
107
- let idCounter = 0;
108
- function schemaAllowsComments(schemaRef) {
109
- if (schemaRef && typeof schemaRef === 'object') {
110
- if (isBoolean(schemaRef.allowComments)) {
111
- return schemaRef.allowComments;
112
- }
113
- if (schemaRef.allOf) {
114
- for (const schema of schemaRef.allOf) {
115
- const allow = schemaAllowsComments(schema);
116
- if (isBoolean(allow)) {
117
- return allow;
118
- }
119
- }
120
- }
121
- }
122
- return undefined;
123
- }
124
- function schemaAllowsTrailingCommas(schemaRef) {
125
- if (schemaRef && typeof schemaRef === 'object') {
126
- if (isBoolean(schemaRef.allowTrailingCommas)) {
127
- return schemaRef.allowTrailingCommas;
128
- }
129
- const deprSchemaRef = schemaRef;
130
- if (isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
131
- return deprSchemaRef['allowsTrailingCommas'];
132
- }
133
- if (schemaRef.allOf) {
134
- for (const schema of schemaRef.allOf) {
135
- const allow = schemaAllowsTrailingCommas(schema);
136
- if (isBoolean(allow)) {
137
- return allow;
138
- }
139
- }
140
- }
141
- }
142
- return undefined;
143
- }
144
- function toDiagnosticSeverity(severityLevel) {
145
- switch (severityLevel) {
146
- case 'error': return DiagnosticSeverity.Error;
147
- case 'warning': return DiagnosticSeverity.Warning;
148
- case 'ignore': return undefined;
149
- }
150
- return undefined;
151
- }
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { ErrorCode, Diagnostic, DiagnosticSeverity, Range } from '../jsonLanguageTypes';
6
+ import * as nls from 'vscode-nls';
7
+ import { isBoolean } from '../utils/objects';
8
+ const localize = nls.loadMessageBundle();
9
+ export class JSONValidation {
10
+ constructor(jsonSchemaService, promiseConstructor) {
11
+ this.jsonSchemaService = jsonSchemaService;
12
+ this.promise = promiseConstructor;
13
+ this.validationEnabled = true;
14
+ }
15
+ configure(raw) {
16
+ if (raw) {
17
+ this.validationEnabled = raw.validate !== false;
18
+ this.commentSeverity = raw.allowComments ? undefined : DiagnosticSeverity.Error;
19
+ }
20
+ }
21
+ doValidation(textDocument, jsonDocument, documentSettings, schema) {
22
+ if (!this.validationEnabled) {
23
+ return this.promise.resolve([]);
24
+ }
25
+ const diagnostics = [];
26
+ const added = {};
27
+ const addProblem = (problem) => {
28
+ // remove duplicated messages
29
+ const signature = problem.range.start.line + ' ' + problem.range.start.character + ' ' + problem.message;
30
+ if (!added[signature]) {
31
+ added[signature] = true;
32
+ diagnostics.push(problem);
33
+ }
34
+ };
35
+ const getDiagnostics = (schema) => {
36
+ let trailingCommaSeverity = documentSettings?.trailingCommas ? toDiagnosticSeverity(documentSettings.trailingCommas) : DiagnosticSeverity.Error;
37
+ let commentSeverity = documentSettings?.comments ? toDiagnosticSeverity(documentSettings.comments) : this.commentSeverity;
38
+ let schemaValidation = documentSettings?.schemaValidation ? toDiagnosticSeverity(documentSettings.schemaValidation) : DiagnosticSeverity.Warning;
39
+ let schemaRequest = documentSettings?.schemaRequest ? toDiagnosticSeverity(documentSettings.schemaRequest) : DiagnosticSeverity.Warning;
40
+ if (schema) {
41
+ const addSchemaProblem = (errorMessage, errorCode) => {
42
+ if (jsonDocument.root && schemaRequest) {
43
+ const astRoot = jsonDocument.root;
44
+ const property = astRoot.type === 'object' ? astRoot.properties[0] : undefined;
45
+ if (property && property.keyNode.value === '$schema') {
46
+ const node = property.valueNode || property;
47
+ const range = Range.create(textDocument.positionAt(node.offset), textDocument.positionAt(node.offset + node.length));
48
+ addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
49
+ }
50
+ else {
51
+ const range = Range.create(textDocument.positionAt(astRoot.offset), textDocument.positionAt(astRoot.offset + 1));
52
+ addProblem(Diagnostic.create(range, errorMessage, schemaRequest, errorCode));
53
+ }
54
+ }
55
+ };
56
+ if (schema.errors.length) {
57
+ addSchemaProblem(schema.errors[0], ErrorCode.SchemaResolveError);
58
+ }
59
+ else if (schemaValidation) {
60
+ for (const warning of schema.warnings) {
61
+ addSchemaProblem(warning, ErrorCode.SchemaUnsupportedFeature);
62
+ }
63
+ const semanticErrors = jsonDocument.validate(textDocument, schema.schema, schemaValidation, documentSettings?.schemaDraft);
64
+ if (semanticErrors) {
65
+ semanticErrors.forEach(addProblem);
66
+ }
67
+ }
68
+ if (schemaAllowsComments(schema.schema)) {
69
+ commentSeverity = undefined;
70
+ }
71
+ if (schemaAllowsTrailingCommas(schema.schema)) {
72
+ trailingCommaSeverity = undefined;
73
+ }
74
+ }
75
+ for (const p of jsonDocument.syntaxErrors) {
76
+ if (p.code === ErrorCode.TrailingComma) {
77
+ if (typeof trailingCommaSeverity !== 'number') {
78
+ continue;
79
+ }
80
+ p.severity = trailingCommaSeverity;
81
+ }
82
+ addProblem(p);
83
+ }
84
+ if (typeof commentSeverity === 'number') {
85
+ const message = localize('InvalidCommentToken', 'Comments are not permitted in JSON.');
86
+ jsonDocument.comments.forEach(c => {
87
+ addProblem(Diagnostic.create(c, message, commentSeverity, ErrorCode.CommentNotPermitted));
88
+ });
89
+ }
90
+ return diagnostics;
91
+ };
92
+ if (schema) {
93
+ const id = schema.id || ('schemaservice://untitled/' + idCounter++);
94
+ const handle = this.jsonSchemaService.registerExternalSchema(id, [], schema);
95
+ return handle.getResolvedSchema().then(resolvedSchema => {
96
+ return getDiagnostics(resolvedSchema);
97
+ });
98
+ }
99
+ return this.jsonSchemaService.getSchemaForResource(textDocument.uri, jsonDocument).then(schema => {
100
+ return getDiagnostics(schema);
101
+ });
102
+ }
103
+ getLanguageStatus(textDocument, jsonDocument) {
104
+ return { schemas: this.jsonSchemaService.getSchemaURIsForResource(textDocument.uri, jsonDocument) };
105
+ }
106
+ }
107
+ let idCounter = 0;
108
+ function schemaAllowsComments(schemaRef) {
109
+ if (schemaRef && typeof schemaRef === 'object') {
110
+ if (isBoolean(schemaRef.allowComments)) {
111
+ return schemaRef.allowComments;
112
+ }
113
+ if (schemaRef.allOf) {
114
+ for (const schema of schemaRef.allOf) {
115
+ const allow = schemaAllowsComments(schema);
116
+ if (isBoolean(allow)) {
117
+ return allow;
118
+ }
119
+ }
120
+ }
121
+ }
122
+ return undefined;
123
+ }
124
+ function schemaAllowsTrailingCommas(schemaRef) {
125
+ if (schemaRef && typeof schemaRef === 'object') {
126
+ if (isBoolean(schemaRef.allowTrailingCommas)) {
127
+ return schemaRef.allowTrailingCommas;
128
+ }
129
+ const deprSchemaRef = schemaRef;
130
+ if (isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated
131
+ return deprSchemaRef['allowsTrailingCommas'];
132
+ }
133
+ if (schemaRef.allOf) {
134
+ for (const schema of schemaRef.allOf) {
135
+ const allow = schemaAllowsTrailingCommas(schema);
136
+ if (isBoolean(allow)) {
137
+ return allow;
138
+ }
139
+ }
140
+ }
141
+ }
142
+ return undefined;
143
+ }
144
+ function toDiagnosticSeverity(severityLevel) {
145
+ switch (severityLevel) {
146
+ case 'error': return DiagnosticSeverity.Error;
147
+ case 'warning': return DiagnosticSeverity.Warning;
148
+ case 'ignore': return undefined;
149
+ }
150
+ return undefined;
151
+ }
@@ -1,68 +1,68 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Microsoft Corporation. All rights reserved.
3
- * Licensed under the MIT License. See License.txt in the project root for license information.
4
- *--------------------------------------------------------------------------------------------*/
5
- const Digit0 = 48;
6
- const Digit9 = 57;
7
- const A = 65;
8
- const a = 97;
9
- const f = 102;
10
- export function hexDigit(charCode) {
11
- if (charCode < Digit0) {
12
- return 0;
13
- }
14
- if (charCode <= Digit9) {
15
- return charCode - Digit0;
16
- }
17
- if (charCode < a) {
18
- charCode += (a - A);
19
- }
20
- if (charCode >= a && charCode <= f) {
21
- return charCode - a + 10;
22
- }
23
- return 0;
24
- }
25
- export function colorFromHex(text) {
26
- if (text[0] !== '#') {
27
- return undefined;
28
- }
29
- switch (text.length) {
30
- case 4:
31
- return {
32
- red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
33
- green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
34
- blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
35
- alpha: 1
36
- };
37
- case 5:
38
- return {
39
- red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
40
- green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
41
- blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
42
- alpha: (hexDigit(text.charCodeAt(4)) * 0x11) / 255.0,
43
- };
44
- case 7:
45
- return {
46
- red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
47
- green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
48
- blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
49
- alpha: 1
50
- };
51
- case 9:
52
- return {
53
- red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
54
- green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
55
- blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
56
- alpha: (hexDigit(text.charCodeAt(7)) * 0x10 + hexDigit(text.charCodeAt(8))) / 255.0
57
- };
58
- }
59
- return undefined;
60
- }
61
- export function colorFrom256RGB(red, green, blue, alpha = 1.0) {
62
- return {
63
- red: red / 255.0,
64
- green: green / 255.0,
65
- blue: blue / 255.0,
66
- alpha
67
- };
68
- }
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ const Digit0 = 48;
6
+ const Digit9 = 57;
7
+ const A = 65;
8
+ const a = 97;
9
+ const f = 102;
10
+ export function hexDigit(charCode) {
11
+ if (charCode < Digit0) {
12
+ return 0;
13
+ }
14
+ if (charCode <= Digit9) {
15
+ return charCode - Digit0;
16
+ }
17
+ if (charCode < a) {
18
+ charCode += (a - A);
19
+ }
20
+ if (charCode >= a && charCode <= f) {
21
+ return charCode - a + 10;
22
+ }
23
+ return 0;
24
+ }
25
+ export function colorFromHex(text) {
26
+ if (text[0] !== '#') {
27
+ return undefined;
28
+ }
29
+ switch (text.length) {
30
+ case 4:
31
+ return {
32
+ red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
33
+ green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
34
+ blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
35
+ alpha: 1
36
+ };
37
+ case 5:
38
+ return {
39
+ red: (hexDigit(text.charCodeAt(1)) * 0x11) / 255.0,
40
+ green: (hexDigit(text.charCodeAt(2)) * 0x11) / 255.0,
41
+ blue: (hexDigit(text.charCodeAt(3)) * 0x11) / 255.0,
42
+ alpha: (hexDigit(text.charCodeAt(4)) * 0x11) / 255.0,
43
+ };
44
+ case 7:
45
+ return {
46
+ red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
47
+ green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
48
+ blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
49
+ alpha: 1
50
+ };
51
+ case 9:
52
+ return {
53
+ red: (hexDigit(text.charCodeAt(1)) * 0x10 + hexDigit(text.charCodeAt(2))) / 255.0,
54
+ green: (hexDigit(text.charCodeAt(3)) * 0x10 + hexDigit(text.charCodeAt(4))) / 255.0,
55
+ blue: (hexDigit(text.charCodeAt(5)) * 0x10 + hexDigit(text.charCodeAt(6))) / 255.0,
56
+ alpha: (hexDigit(text.charCodeAt(7)) * 0x10 + hexDigit(text.charCodeAt(8))) / 255.0
57
+ };
58
+ }
59
+ return undefined;
60
+ }
61
+ export function colorFrom256RGB(red, green, blue, alpha = 1.0) {
62
+ return {
63
+ red: red / 255.0,
64
+ green: green / 255.0,
65
+ blue: blue / 255.0,
66
+ alpha
67
+ };
68
+ }