Commit 31594926 authored by Yuriy Solodkyy's avatar Yuriy Solodkyy Committed by Anton Lobov
Browse files

Stop parsing scanning on both properties found.

Also: json5 allows keys to be single-quoted strings.

PR: https://github.com/JetBrains/intellij-community/pull/900
parent c88b76b8
Showing with 33 additions and 10 deletions
+33 -10
......@@ -54,7 +54,9 @@ public class JsonSchemaFileValuesIndex extends FileBasedIndexExtension<String, S
// We only care about properties at the root level having the form of "property" : "value".
int nesting = 0;
while (lexer.getCurrentPosition().getOffset() < lexer.getBufferEnd()) {
boolean idFound = false;
boolean schemaFound = false;
while (!(idFound && schemaFound) && lexer.getCurrentPosition().getOffset() < lexer.getBufferEnd()) {
IElementType token = lexer.getTokenType();
// Nesting level can only change at curly braces.
if (token == JsonElementTypes.L_CURLY) {
......@@ -63,16 +65,21 @@ public class JsonSchemaFileValuesIndex extends FileBasedIndexExtension<String, S
else if (token == JsonElementTypes.R_CURLY) {
nesting--;
}
else if (nesting == 1 && (token == JsonElementTypes.DOUBLE_QUOTED_STRING || token == JsonElementTypes.IDENTIFIER)) {
else if (nesting == 1 &&
(token == JsonElementTypes.DOUBLE_QUOTED_STRING
|| token == JsonElementTypes.SINGLE_QUOTED_STRING
|| token == JsonElementTypes.IDENTIFIER)) {
// We are looking for two special properties at the root level.
switch (lexer.getTokenText()) {
case "\"$id\"":
case "$id":
captureValueIfString(lexer, map, JsonCachedValues.ID_CACHE_KEY);
case "\"$id\"":
case "'$id'":
idFound |= captureValueIfString(lexer, map, JsonCachedValues.ID_CACHE_KEY);
break;
case "\"$schema\"":
case "$schema":
captureValueIfString(lexer, map, JsonCachedValues.URL_CACHE_KEY);
case "\"$schema\"":
case "'$schema'":
schemaFound |= captureValueIfString(lexer, map, JsonCachedValues.URL_CACHE_KEY);
break;
}
}
......@@ -81,7 +88,7 @@ public class JsonSchemaFileValuesIndex extends FileBasedIndexExtension<String, S
return map;
}
private void captureValueIfString(Lexer lexer, HashMap<String, String> destMap, String key) {
private boolean captureValueIfString(Lexer lexer, HashMap<String, String> destMap, String key) {
IElementType token;
lexer.advance();
token = skipWhitespacesAndGetTokenType(lexer);
......@@ -90,8 +97,10 @@ public class JsonSchemaFileValuesIndex extends FileBasedIndexExtension<String, S
token = skipWhitespacesAndGetTokenType(lexer);
if (token == JsonElementTypes.DOUBLE_QUOTED_STRING || token == JsonElementTypes.SINGLE_QUOTED_STRING) {
destMap.put(key, lexer.getTokenText().substring(1, lexer.getTokenText().length() - 1));
return true;
}
}
return false;
}
private IElementType skipWhitespacesAndGetTokenType(Lexer lexer) {
......
......@@ -15,14 +15,15 @@
*/
package com.jetbrains.jsonSchema.impl;
import static com.jetbrains.jsonSchema.impl.JsonCachedValues.ID_CACHE_KEY;
import static com.jetbrains.jsonSchema.impl.JsonCachedValues.URL_CACHE_KEY;
import com.intellij.json.JsonTestCase;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.indexing.FileContentImpl;
import java.util.Map;
import static com.jetbrains.jsonSchema.impl.JsonCachedValues.ID_CACHE_KEY;
import static com.jetbrains.jsonSchema.impl.JsonCachedValues.URL_CACHE_KEY;
public class JsonSchemaFileValuesIndexTest extends JsonTestCase {
public void testEmpty() {
......@@ -56,4 +57,11 @@ public class JsonSchemaFileValuesIndexTest extends JsonTestCase {
Map<String, String> map = new JsonSchemaFileValuesIndex().getIndexer().map(FileContentImpl.createByFile(file));
assertTrue(map.isEmpty());
}
public void testStopsOnAllFound() {
final VirtualFile file = myFixture.configureByFile("indexing/duplicates.json5").getVirtualFile();
Map<String, String> map = new JsonSchemaFileValuesIndex().getIndexer().map(FileContentImpl.createByFile(file));
assertEquals("the-schema", map.get(URL_CACHE_KEY));
assertEquals("the-id", map.get(ID_CACHE_KEY));
}
}
{
'$schema': 'the-schema',
$id: "the-id",
"$id": "not-id",
"$schema": "something"
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment