mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
json schema: use StringUtil.split() for splitting paths (String.split returns empty array if there are no separators in the string)
This commit is contained in:
@@ -149,7 +149,7 @@ public class JsonSchemaMappingsConfigurationBase implements PersistentStateCompo
|
||||
}
|
||||
|
||||
final String path = FileUtilRt.toSystemIndependentName(pattern.getPath());
|
||||
final List<String> parts = ContainerUtil.filter(path.split("/"), s -> !".".equals(s));
|
||||
final List<String> parts = ContainerUtil.filter(StringUtil.split(path, "/"), s -> !".".equals(s));
|
||||
final VirtualFile relativeFile;
|
||||
if (parts.isEmpty()) {
|
||||
relativeFile = project.getBaseDir();
|
||||
@@ -171,7 +171,7 @@ public class JsonSchemaMappingsConfigurationBase implements PersistentStateCompo
|
||||
@Nullable
|
||||
public VirtualFile getSchemaFile(@NotNull final Project project) {
|
||||
final String pathToSchema = FileUtil.toSystemIndependentName(getRelativePathToSchema());
|
||||
final List<String> strings = ContainerUtil.filter(pathToSchema.split("/"), s -> !StringUtil.isEmptyOrSpaces(s));
|
||||
final List<String> strings = ContainerUtil.filter(StringUtil.split(pathToSchema, "/"), s -> !StringUtil.isEmptyOrSpaces(s));
|
||||
return VfsUtil.findRelativeFile(project.getBaseDir(), ArrayUtil.toStringArray(strings));
|
||||
}
|
||||
|
||||
|
||||
+3
-6
@@ -30,8 +30,6 @@ 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;
|
||||
|
||||
@@ -70,10 +68,10 @@ public class JsonSchemaRefReferenceProvider extends PsiReferenceProvider {
|
||||
}
|
||||
|
||||
final String normalized = JsonSchemaExportedDefinitions.normalizeId(splitter.getRelativePath());
|
||||
if (StringUtil.isEmptyOrSpaces(normalized) || normalized.replace("\\", "/").split("/").length == 0) {
|
||||
if (StringUtil.isEmptyOrSpaces(normalized) || StringUtil.split(normalized.replace("\\", "/"), "/").size() == 0) {
|
||||
return myElement.getManager().findFile(schemaFile);
|
||||
}
|
||||
final ArrayList<String> chain = new ArrayList<String>(Arrays.asList(normalized.replace("\\", "/").split("/")));
|
||||
final List<String> chain = StringUtil.split(normalized.replace("\\", "/"), "/");
|
||||
final Iterator<String> iterator = chain.iterator();
|
||||
boolean canSkip = true;
|
||||
while (iterator.hasNext()) {
|
||||
@@ -85,9 +83,8 @@ public class JsonSchemaRefReferenceProvider extends PsiReferenceProvider {
|
||||
}
|
||||
|
||||
final List<JsonSchemaWalker.Step> steps = JsonSchemaWalker.buildSteps(StringUtil.join(chain, "/")).getFirst();
|
||||
final PsiElement element = new JsonSchemaInsideSchemaResolver(myElement.getProject(), schemaFile, normalized, steps)
|
||||
return new JsonSchemaInsideSchemaResolver(myElement.getProject(), schemaFile, normalized, steps)
|
||||
.resolveInSchemaRecursively();
|
||||
return element;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.intellij.json.psi.JsonValue;
|
||||
import com.intellij.notification.NotificationGroup;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
@@ -190,21 +191,21 @@ public class JsonSchemaReader {
|
||||
}
|
||||
if (isAbsoluteReference(ref)) throw new RuntimeException("Non-relative or erroneous reference: " + ref);
|
||||
ref = ref.substring(2);
|
||||
final String[] parts = ref.split("/");
|
||||
final List<String> parts = StringUtil.split(ref, "/");
|
||||
JsonSchemaObject current = root;
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
for (int i = 0; i < parts.size(); i++) {
|
||||
if (current == null) return null;
|
||||
final String part = parts[i];
|
||||
final String part = parts.get(i);
|
||||
if ("definitions".equals(part)) {
|
||||
if (i == (parts.length - 1)) throw new RuntimeException("Incorrect definition reference: " + ref);
|
||||
if (i == (parts.size() - 1)) throw new RuntimeException("Incorrect definition reference: " + ref);
|
||||
//noinspection AssignmentToForLoopParameter
|
||||
current = current.getDefinitions().get(parts[++i]);
|
||||
current = current.getDefinitions().get(parts.get(++i));
|
||||
continue;
|
||||
}
|
||||
if ("properties".equals(part)) {
|
||||
if (i == (parts.length - 1)) throw new RuntimeException("Incorrect properties reference: " + ref);
|
||||
if (i == (parts.size() - 1)) throw new RuntimeException("Incorrect properties reference: " + ref);
|
||||
//noinspection AssignmentToForLoopParameter
|
||||
current = current.getProperties().get(parts[++i]);
|
||||
current = current.getProperties().get(parts.get(++i));
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VfsUtilCore;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
@@ -385,7 +386,8 @@ public class JsonSchemaServiceImpl implements JsonSchemaServiceEx {
|
||||
if (FileUtil.isAbsolute(normalizedId) || referent == null) return VfsUtil.findFileByIoFile(new File(normalizedId), false);
|
||||
VirtualFile dir = referent.isDirectory() ? referent : referent.getParent();
|
||||
if (dir != null && dir.isValid()) {
|
||||
return VfsUtil.findRelativeFile(dir, normalizedId.replace("\\", "/").split("/"));
|
||||
final List<String> parts = StringUtil.split(normalizedId.replace("\\", "/"), "/");
|
||||
return VfsUtil.findRelativeFile(dir, ArrayUtil.toStringArray(parts));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -70,9 +70,10 @@ public class JsonSchemaWalker {
|
||||
final List<Step> position = walker.findPosition(element, false, true);
|
||||
if (position == null || position.isEmpty()) return;
|
||||
// but this does not validate definitions section against general schema --> should be done separately
|
||||
final Step firstStep = position.get(0);
|
||||
if (JsonSchemaFileType.INSTANCE.equals(element.getContainingFile().getFileType()) &&
|
||||
position.get(0).getTransition() instanceof PropertyTransition &&
|
||||
"definitions".equals(((PropertyTransition)position.get(0).getTransition()).getName())) return;
|
||||
firstStep != null && firstStep.getTransition() instanceof PropertyTransition &&
|
||||
"definitions".equals(((PropertyTransition)firstStep.getTransition()).getName())) return;
|
||||
extractSchemaVariants(element.getProject(), consumer, schemaFile, rootSchema, false, position, true);
|
||||
}
|
||||
|
||||
@@ -110,10 +111,11 @@ public class JsonSchemaWalker {
|
||||
}
|
||||
|
||||
public static Pair<List<Step>, String> buildSteps(@NotNull String nameInSchema) {
|
||||
final String[] chain = JsonSchemaExportedDefinitions.normalizeId(nameInSchema).replace("\\", "/").split("/");
|
||||
final List<Step> steps = Arrays.stream(chain).filter(s -> !s.isEmpty()).map(item -> new Step(StateType._unknown, new PropertyTransition(item)))
|
||||
final List<String> chain = StringUtil.split(JsonSchemaExportedDefinitions.normalizeId(nameInSchema).replace("\\", "/"), "/");
|
||||
final List<Step> steps = chain.stream().filter(s -> !s.isEmpty()).map(item -> new Step(StateType._unknown, new PropertyTransition(item)))
|
||||
.collect(Collectors.toList());
|
||||
return Pair.create(steps, chain[chain.length - 1]);
|
||||
if (steps.isEmpty()) return Pair.create(Collections.emptyList(), nameInSchema);
|
||||
return Pair.create(steps, chain.get(chain.size() - 1));
|
||||
}
|
||||
|
||||
protected static class DefinitionsResolver {
|
||||
@@ -228,8 +230,10 @@ public class JsonSchemaWalker {
|
||||
if (variantSchemaFile == null) return;
|
||||
serviceEx.visitSchemaObject(variantSchemaFile,
|
||||
variantObject -> {
|
||||
final List<Step> variantSteps = buildSteps(splitter.getRelativePath()).getFirst();
|
||||
variantSteps.addAll(steps);
|
||||
List<Step> variantSteps = buildSteps(splitter.getRelativePath()).getFirst();
|
||||
// empty list might be not modifiable
|
||||
if (variantSteps.isEmpty()) variantSteps = steps;
|
||||
else variantSteps.addAll(steps);
|
||||
queue.add(Trinity.create(variantObject, variantSchemaFile, variantSteps));
|
||||
return true;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user