vscode-json-languageservice
Advanced tools
Comparing version 2.0.0-next.5 to 2.0.0-next.6
@@ -43,3 +43,2 @@ (function (factory) { | ||
var overwriteRange = null; | ||
var filterText = void 0; | ||
var result = { | ||
@@ -51,7 +50,5 @@ items: [], | ||
overwriteRange = vscode_languageserver_types_1.Range.create(document.positionAt(node.start), document.positionAt(node.end)); | ||
filterText = document.getText().substring(node.start, offset); | ||
} | ||
else { | ||
overwriteRange = vscode_languageserver_types_1.Range.create(document.positionAt(offset - currentWord.length), position); | ||
filterText = document.getText().substring(offset - currentWord.length, offset); | ||
} | ||
@@ -113,3 +110,6 @@ var proposed = {}; | ||
}); | ||
var separatorAfter_1 = _this.needsCommaAfter(document, node, offset) ? ',' : ''; | ||
var separatorAfter_1 = ''; | ||
if (addValue) { | ||
separatorAfter_1 = _this.evaluateSeparatorAfter(document, document.offsetAt(overwriteRange.end)); | ||
} | ||
if (schema) { | ||
@@ -135,13 +135,19 @@ // property proposals with schema | ||
// proposals for values | ||
var offsetForSeparator = offset; | ||
if (node && (node.type === 'string' || node.type === 'number' || node.type === 'boolean' || node.type === 'null')) { | ||
offsetForSeparator = node.end; | ||
node = node.parent; | ||
} | ||
var separatorAfter = ''; | ||
if (node) { | ||
separatorAfter = _this.evaluateSeparatorAfter(document, offsetForSeparator); | ||
} | ||
var types = {}; | ||
if (schema) { | ||
// value proposals with schema | ||
_this.getValueCompletions(schema, doc, node, offset, document, collector, types); | ||
_this.getValueCompletions(schema, doc, node, offset, separatorAfter, collector, types); | ||
} | ||
else { | ||
// value proposals without schema | ||
_this.getSchemaLessValueCompletions(doc, node, offset, document, collector); | ||
_this.getSchemaLessValueCompletions(doc, node, offset, separatorAfter, document, collector); | ||
} | ||
@@ -173,3 +179,3 @@ if (!node) { | ||
if (collector.getNumberOfProposals() === 0) { | ||
_this.addFillerValueCompletions(types, '', collector); | ||
_this.addFillerValueCompletions(types, separatorAfter, collector); | ||
} | ||
@@ -229,5 +235,4 @@ return result; | ||
}; | ||
JSONCompletion.prototype.getSchemaLessValueCompletions = function (doc, node, offset, document, collector) { | ||
JSONCompletion.prototype.getSchemaLessValueCompletions = function (doc, node, offset, separatorAfter, document, collector) { | ||
var _this = this; | ||
var separatorAfter = this.needsCommaAfter(document, node, offset) ? ',' : ''; | ||
var collectSuggestionsForValues = function (value) { | ||
@@ -242,4 +247,4 @@ if (!value.parent.contains(offset, true)) { | ||
if (!node) { | ||
collector.add({ kind: this.getSuggestionKind('object'), label: 'Empty object', insertText: this.getInsertTextForValue({}, separatorAfter), documentation: '' }); | ||
collector.add({ kind: this.getSuggestionKind('array'), label: 'Empty array', insertText: this.getInsertTextForValue([], separatorAfter), documentation: '' }); | ||
collector.add({ kind: this.getSuggestionKind('object'), label: 'Empty object', insertText: this.getInsertTextForValue({}, ''), documentation: '' }); | ||
collector.add({ kind: this.getSuggestionKind('array'), label: 'Empty array', insertText: this.getInsertTextForValue([], ''), documentation: '' }); | ||
} | ||
@@ -291,3 +296,3 @@ else { | ||
}; | ||
JSONCompletion.prototype.getValueCompletions = function (schema, doc, node, offset, document, collector, types) { | ||
JSONCompletion.prototype.getValueCompletions = function (schema, doc, node, offset, separatorAfter, collector, types) { | ||
var _this = this; | ||
@@ -299,3 +304,3 @@ if (!node) { | ||
var parentKey_5 = null; | ||
if (node && (node.type === 'property') && offset > node.colonOffset) { | ||
if ((node.type === 'property') && offset > node.colonOffset) { | ||
var valueNode = node.value; | ||
@@ -311,7 +316,6 @@ if (valueNode && offset > valueNode.end) { | ||
doc.validate(schema.schema, matchingSchemas, node.start); | ||
var separatorAfter_2 = this.needsCommaAfter(document, node, offset) ? ',' : ''; | ||
matchingSchemas.forEach(function (s) { | ||
if (s.node === node && !s.inverted && s.schema) { | ||
if (s.schema.items) { | ||
_this.addSchemaValueCompletions(s.schema.items, separatorAfter_2, collector, types); | ||
_this.addSchemaValueCompletions(s.schema.items, separatorAfter, collector, types); | ||
} | ||
@@ -321,3 +325,3 @@ if (s.schema.properties) { | ||
if (propertySchema) { | ||
_this.addSchemaValueCompletions(propertySchema, separatorAfter_2, collector, types); | ||
_this.addSchemaValueCompletions(propertySchema, separatorAfter, collector, types); | ||
} | ||
@@ -328,10 +332,10 @@ } | ||
if (parentKey_5 === '$schema' && !node.parent) { | ||
this.addDollarSchemaCompletions(separatorAfter_2, collector); | ||
this.addDollarSchemaCompletions(separatorAfter, collector); | ||
} | ||
if (types['boolean']) { | ||
this.addBooleanValueCompletion(true, separatorAfter_2, collector); | ||
this.addBooleanValueCompletion(false, separatorAfter_2, collector); | ||
this.addBooleanValueCompletion(true, separatorAfter, collector); | ||
this.addBooleanValueCompletion(false, separatorAfter, collector); | ||
} | ||
if (types['null']) { | ||
this.addNullValueCompletion(separatorAfter_2, collector); | ||
this.addNullValueCompletion(separatorAfter, collector); | ||
} | ||
@@ -544,32 +548,41 @@ } | ||
} | ||
result.value += ': '; | ||
var resultText = result.value; | ||
resultText += ': '; | ||
if (propertySchema) { | ||
var defaultVal = propertySchema.default; | ||
if (typeof defaultVal !== 'undefined') { | ||
result.value += this.getInsertTextForGuessedValue(defaultVal, '').value; | ||
resultText += this.getInsertTextForGuessedValue(defaultVal, '').value; | ||
} | ||
else if (propertySchema.enum && propertySchema.enum.length > 0) { | ||
result.value += this.getInsertTextForGuessedValue(propertySchema.enum[0], '').value; | ||
resultText += this.getInsertTextForGuessedValue(propertySchema.enum[0], '').value; | ||
} | ||
else { | ||
var type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type; | ||
if (!type) { | ||
if (propertySchema.properties) { | ||
type = 'object'; | ||
} | ||
else if (propertySchema.items) { | ||
type = 'array'; | ||
} | ||
} | ||
switch (type) { | ||
case 'boolean': | ||
result.value += '${1:false}'; | ||
resultText += '${1:false}'; | ||
break; | ||
case 'string': | ||
result.value += '"$1"'; | ||
resultText += '"$1"'; | ||
break; | ||
case 'object': | ||
result.value += '{\n\t$1\n}'; | ||
resultText += '{\n\t$1\n}'; | ||
break; | ||
case 'array': | ||
result.value += '[\n\t$1\n]'; | ||
resultText += '[\n\t$1\n]'; | ||
break; | ||
case 'number': | ||
case 'integer': | ||
result.value += '${1:0}'; | ||
resultText += '${1:0}'; | ||
break; | ||
case 'null': | ||
result.value += '${1:null}'; | ||
resultText += '${1:null}'; | ||
break; | ||
@@ -582,6 +595,6 @@ default: | ||
else { | ||
result.value += '$1'; | ||
resultText += '$1'; | ||
} | ||
result.value += separatorAfter; | ||
return result; | ||
resultText += separatorAfter; | ||
return vscode_languageserver_types_1.SnippetString.create(resultText); | ||
}; | ||
@@ -596,13 +609,15 @@ JSONCompletion.prototype.getCurrentWord = function (document, offset) { | ||
}; | ||
JSONCompletion.prototype.needsCommaAfter = function (document, node, offset) { | ||
if (!node) { | ||
return false; | ||
} | ||
JSONCompletion.prototype.evaluateSeparatorAfter = function (document, offset) { | ||
var scanner = Json.createScanner(document.getText(), true); | ||
scanner.setPosition(node.start); | ||
scanner.setPosition(offset); | ||
var token = scanner.scan(); | ||
while (token !== Json.SyntaxKind.EOF && scanner.getTokenOffset() < offset) { | ||
token = scanner.scan(); | ||
switch (token) { | ||
case Json.SyntaxKind.CommaToken: | ||
case Json.SyntaxKind.CloseBraceToken: | ||
case Json.SyntaxKind.CloseBracketToken: | ||
case Json.SyntaxKind.EOF: | ||
return ''; | ||
default: | ||
return ','; | ||
} | ||
return token !== Json.SyntaxKind.CommaToken && token !== Json.SyntaxKind.CloseBraceToken && token !== Json.SyntaxKind.CloseBracketToken && token !== Json.SyntaxKind.EOF; | ||
}; | ||
@@ -609,0 +624,0 @@ return JSONCompletion; |
{ | ||
"name": "vscode-json-languageservice", | ||
"version": "2.0.0-next.5", | ||
"version": "2.0.0-next.6", | ||
"description": "Language service for JSON", | ||
@@ -5,0 +5,0 @@ "main": "./lib/jsonLanguageService.js", |
152618
2998