json schema, get rid of json schema name index -> anyway we have schema objects in memory

This commit is contained in:
irengrig
2017-01-16 17:18:12 +01:00
parent 2f2d3db039
commit e1c069d0ed
5 changed files with 16 additions and 182 deletions
@@ -1,72 +0,0 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.jsonSchema.extension.schema;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.jsonSchema.impl.JsonSchemaResourcesRootsProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Irina.Chernushina on 1/10/2017.
*/
public class JsonSchemaByPropertyIndexResolver {
@NotNull private final String myReferenceName;
@NotNull private final Project myProject;
@Nullable private final VirtualFile mySchemaFile;
private VirtualFile myFile;
private Integer myOffset;
public JsonSchemaByPropertyIndexResolver(@NotNull String referenceName,
@NotNull Project project,
@Nullable VirtualFile schemaFile) {
myReferenceName = referenceName;
myProject = project;
mySchemaFile = schemaFile;
}
public PsiElement resolveByName() {
final GlobalSearchScope scope;
if (mySchemaFile != null) {
scope = GlobalSearchScope.fileScope(myProject, mySchemaFile);
} else {
scope = JsonSchemaResourcesRootsProvider.enlarge(myProject, GlobalSearchScope.allScope(myProject));
}
FileBasedIndex.getInstance().processValues(JsonSchemaFileIndex.PROPERTIES_INDEX, myReferenceName, null, (file, value) -> {
if (!scope.contains(file)) return true;
myFile = file;
myOffset = value;
return false;
}, scope);
if (myFile != null) {
myOffset = myOffset == null ? 0 : myOffset;
final PsiFile file = PsiManager.getInstance(myProject).findFile(myFile);
if (file != null) {
return file.findElementAt(myOffset);
}
}
return null;
}
}
@@ -1,101 +0,0 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.jsonSchema.extension.schema;
import com.intellij.json.psi.JsonFile;
import com.intellij.json.psi.JsonProperty;
import com.intellij.json.psi.impl.JsonRecursiveElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.util.indexing.*;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.IntInlineKeyDescriptor;
import com.intellij.util.io.KeyDescriptor;
import com.jetbrains.jsonSchema.JsonSchemaFileType;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
/**
* @author Irina.Chernushina on 4/1/2016.
*/
public class JsonSchemaFileIndex extends FileBasedIndexExtension<String, Integer> {
public static final ID<String, Integer> PROPERTIES_INDEX = ID.create("json.schema.properties.index");
public static final int VERSION = 9;
private IntInlineKeyDescriptor myKeyDescriptor = new IntInlineKeyDescriptor();
@NotNull
@Override
public ID<String, Integer> getName() {
return PROPERTIES_INDEX;
}
@NotNull
@Override
public DataIndexer<String, Integer, FileContent> getIndexer() {
return new DataIndexer<String, Integer, FileContent>() {
@NotNull
@Override
public Map<String, Integer> map(@NotNull FileContent inputData) {
final Map<String, Integer> map = new HashMap<>();
final PsiFile file = inputData.getPsiFile();
if (file instanceof JsonFile) {
file.accept(new JsonRecursiveElementVisitor() {
private String myPrefix = "";
@Override
public void visitProperty(@NotNull JsonProperty property) {
String wasPrefix = myPrefix;
myPrefix = myPrefix + "/" + property.getName();
map.put(myPrefix, property.getTextRange().getStartOffset());
super.visitProperty(property);
myPrefix = wasPrefix;
}
});
}
return map;
}
};
}
@NotNull
@Override
public KeyDescriptor<String> getKeyDescriptor() {
return EnumeratorStringDescriptor.INSTANCE;
}
@NotNull
@Override
public DataExternalizer<Integer> getValueExternalizer() {
return myKeyDescriptor;
}
@Override
public int getVersion() {
return VERSION;
}
@NotNull
@Override
public FileBasedIndex.InputFilter getInputFilter() {
return new DefaultFileTypeSpecificInputFilter(JsonSchemaFileType.INSTANCE);
}
@Override
public boolean dependsOnFileContent() {
return true;
}
}
@@ -24,7 +24,6 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.jetbrains.jsonSchema.ide.JsonSchemaService;
import com.jetbrains.jsonSchema.impl.JsonSchemaExportedDefinitions;
import com.jetbrains.jsonSchema.impl.JsonSchemaWalker;
import org.jetbrains.annotations.NotNull;
@@ -50,10 +49,6 @@ public class JsonSchemaInsideSchemaResolver {
}
public PsiElement resolveInSchemaRecursively() {
final PsiElement element = new JsonSchemaByPropertyIndexResolver(JsonSchemaExportedDefinitions.normalizeId(myReference),
myProject, mySchemaFile).resolveByName();
if (element != null) return element;
final Ref<PsiElement> ref = new Ref<>();
JsonSchemaService.Impl.getEx(myProject).visitSchemaObject(mySchemaFile,
object -> {
@@ -16,7 +16,6 @@
package com.jetbrains.jsonSchema.extension.schema;
import com.intellij.json.psi.JsonValue;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.ElementManipulators;
@@ -31,6 +30,9 @@ import com.jetbrains.jsonSchema.impl.JsonSchemaWalker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
@@ -71,8 +73,19 @@ public class JsonSchemaRefReferenceProvider extends PsiReferenceProvider {
if (StringUtil.isEmptyOrSpaces(normalized) || normalized.replace("\\", "/").split("/").length == 0) {
return myElement.getManager().findFile(schemaFile);
}
final Pair<List<JsonSchemaWalker.Step>, String> steps = JsonSchemaWalker.buildSteps(normalized);
return new JsonSchemaInsideSchemaResolver(myElement.getProject(), schemaFile, normalized, steps.getFirst())
final ArrayList<String> chain = new ArrayList<String>(Arrays.asList(normalized.replace("\\", "/").split("/")));
final Iterator<String> iterator = chain.iterator();
boolean canSkip = true;
while (iterator.hasNext()) {
final String step = iterator.next();
if (canSkip && "properties".equals(step)) {
iterator.remove();
canSkip = false;
} else canSkip = true;
}
final List<JsonSchemaWalker.Step> steps = JsonSchemaWalker.buildSteps(StringUtil.join(chain, "/")).getFirst();
return new JsonSchemaInsideSchemaResolver(myElement.getProject(), schemaFile, normalized, steps)
.resolveInSchemaRecursively();
}
}
@@ -76,7 +76,6 @@
<editorNotificationProvider implementation="com.jetbrains.jsonSchema.impl.JsonSchemaConflictNotificationProvider"/>
<projectService serviceImplementation="com.jetbrains.jsonSchema.extension.JsonSchemaProjectSelfProviderFactory"/>
<fileBasedIndex implementation="com.jetbrains.jsonSchema.extension.schema.JsonSchemaFileIndex"/>
<psi.referenceContributor language="JSON" implementation="com.jetbrains.jsonSchema.extension.schema.JsonSchemaReferenceContributor"/>
<indexedRootsProvider implementation="com.jetbrains.jsonSchema.impl.JsonSchemaResourcesRootsProvider"/>
<psi.referenceContributor language="JSON" implementation="com.jetbrains.jsonSchema.impl.JsonBySchemaObjectReferenceContributor"/>