提交 351b1e45 编写于 作者: M Martin Aeschlimann

[json] Don't propose keys that are already existing #662

上级 74acc249
......@@ -67,6 +67,7 @@ export class JSONIntellisense {
var node = doc.getNodeFromOffsetEndInclusive(offset);
var addValue = true;
var currentKey = currentWord;
var currentProperty : Parser.PropertyASTNode = null;
if (node) {
if (node.type === 'string') {
......@@ -76,6 +77,7 @@ export class JSONIntellisense {
result.overwriteBefore = position.column - nodeRange.startColumn;
result.overwriteAfter = nodeRange.endColumn - position.column;
addValue = !(node.parent && ((<Parser.PropertyASTNode> node.parent).value));
currentProperty = node.parent ? <Parser.PropertyASTNode> node.parent : null;
currentKey = modelMirror.getValueInRange({ startColumn: nodeRange.startColumn + 1, startLineNumber: nodeRange.startLineNumber, endColumn: position.column, endLineNumber: position.lineNumber });
if (node.parent) {
node = node.parent.parent;
......@@ -91,10 +93,16 @@ export class JSONIntellisense {
if (node.start === offset) {
return result;
}
// don't suggest properties that are already present
var properties = (<Parser.ObjectASTNode> node).properties;
properties.forEach(p => {
if (!currentProperty || currentProperty !== p) {
proposed[p.key.value] = true;
}
});
if (schema) {
// property proposals with schema
var properties = (<Parser.ObjectASTNode> node).properties;
var isLast = properties.length === 0 || offset >= properties[properties.length - 1].start;
collectionPromises.push(this.getPropertySuggestions(schema, doc, node, currentKey, addValue, isLast, collector));
......
......@@ -179,9 +179,8 @@ suite('JSON - Worker', () => {
assertSuggestion(result, 'name');
}),
testSuggestionsFor('[ { "name": "John", "address": { "street" : "MH Road", "number" : 5 } }, { "name": "Jack", "address": { "street" : "100 Feet Road", /**/ }', '/**/').then((result) => {
assert.strictEqual(result.suggestions.length, 2);
assert.strictEqual(result.suggestions.length, 1);
assertSuggestion(result, 'number');
assertSuggestion(result, 'street');
})
]).done(() => testDone(), (errors:any[]) => {
testDone(errors.reduce((e1, e2) => e1 || e2));
......@@ -216,8 +215,7 @@ suite('JSON - Worker', () => {
assertSuggestion(result, '"xoo"');
}),
testSuggestionsFor('[ { "data": "foo" }, { "data": "bar" }, { "data": "xoo" /**/ } ]', '/**/').then((result) => {
assert.strictEqual(result.suggestions.length, 1);
assertSuggestion(result, 'data');
assert.strictEqual(result.suggestions.length, 0);
})
]).done(() => testDone(), (errors:any[]) => {
testDone(errors.reduce((e1, e2) => e1 || e2));
......@@ -260,8 +258,7 @@ suite('JSON - Worker', () => {
assertSuggestion(result, 'a', 'A');
}),
testSuggestionsFor('{ "a" = 1;/**/}', '/**/', schema).then((result) => {
assert.strictEqual(result.suggestions.length, 3);
assertSuggestion(result, 'a', 'A');
assert.strictEqual(result.suggestions.length, 2);
assertSuggestion(result, 'b', 'B');
assertSuggestion(result, 'c', 'C');
})
......@@ -375,8 +372,7 @@ suite('JSON - Worker', () => {
assertSuggestion(result, 'd', 'D');
}),
testSuggestionsFor('{ "a": "", /**/}', '/**/', schema).then((result) => {
assert.strictEqual(result.suggestions.length, 2);
assertSuggestion(result, 'a', 'A');
assert.strictEqual(result.suggestions.length, 1);
assertSuggestion(result, 'b', 'B');
})
]).done(() => testDone(), (errors:any[]) => {
......@@ -417,8 +413,7 @@ suite('JSON - Worker', () => {
assertSuggestion(result, 'c');
}),
testSuggestionsFor('{ "type": "appartment", /**/}', '/**/', schema).then((result) => {
assert.strictEqual(result.suggestions.length, 2);
assertSuggestion(result, 'type');
assert.strictEqual(result.suggestions.length, 1);
assertSuggestion(result, 'c');
})
]).done(() => testDone(), (errors:any[]) => {
......@@ -480,9 +475,8 @@ suite('JSON - Worker', () => {
assertSuggestion(result, 'd', 'D');
}),
testSuggestionsFor('{ "b1": "", /**/}', '/**/', schema).then((result) => {
assert.strictEqual(result.suggestions.length, 3);
assert.strictEqual(result.suggestions.length, 2);
assertSuggestion(result, 'a', 'A');
assertSuggestion(result, 'b1', 'B1');
assertSuggestion(result, 'b2', 'B2');
})
]).done(() => testDone(), (errors:any[]) => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册