forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage snippet registry to write snippet in JSON
Fixes eclipse-lemminx#640 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
acaa560
commit b930bdb
Showing
33 changed files
with
2,239 additions
and
626 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/snippets/ISnippetContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.commons.snippets; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Snippet context used to filter the snippet. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
* @param <T> the value type waited by the snippet context. | ||
*/ | ||
public interface ISnippetContext<T> { | ||
|
||
/** | ||
* Return true if the given value match the snippet context and false otherwise. | ||
* | ||
* @param value the value to check. | ||
* @return true if the given value match the snippet context and false | ||
* otherwise. | ||
*/ | ||
boolean isMatch(T value, Map<String, String> model); | ||
} |
38 changes: 38 additions & 0 deletions
38
...se.lemminx/src/main/java/org/eclipse/lemminx/commons/snippets/ISnippetRegistryLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.commons.snippets; | ||
|
||
/** | ||
* Loader used to load snippets in a given registry for a language id. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public interface ISnippetRegistryLoader { | ||
|
||
/** | ||
* Register snippets in the given snippet registry. | ||
* | ||
* @param registry | ||
* @throws Exception | ||
*/ | ||
void load(SnippetRegistry registry) throws Exception; | ||
|
||
/** | ||
* Returns the language id and null otherwise. | ||
* | ||
* @return the language id and null otherwise. | ||
*/ | ||
default String getLanguageId() { | ||
return null; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/snippets/Snippet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.commons.snippets; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.BiPredicate; | ||
|
||
/** | ||
* Snippet description (like vscode snippet). | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class Snippet { | ||
|
||
private List<String> prefixes; | ||
|
||
private List<String> body; | ||
|
||
private String description; | ||
|
||
private String scope; | ||
|
||
private ISnippetContext<?> context; | ||
|
||
public List<String> getPrefixes() { | ||
return prefixes; | ||
} | ||
|
||
public void setPrefixes(List<String> prefixes) { | ||
this.prefixes = prefixes; | ||
} | ||
|
||
public List<String> getBody() { | ||
return body; | ||
} | ||
|
||
public void setBody(List<String> body) { | ||
this.body = body; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getScope() { | ||
return scope; | ||
} | ||
|
||
public void setScope(String scope) { | ||
this.scope = scope; | ||
} | ||
|
||
public ISnippetContext<?> getContext() { | ||
return context; | ||
} | ||
|
||
public void setContext(ISnippetContext<?> context) { | ||
this.context = context; | ||
} | ||
|
||
public boolean hasContext() { | ||
return getContext() != null; | ||
} | ||
|
||
public boolean match(BiPredicate<ISnippetContext<?>, Map<String, String>> contextFilter, | ||
Map<String, String> model) { | ||
if (!hasContext()) { | ||
return true; | ||
} | ||
return contextFilter.test(getContext(), model); | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
...lipse.lemminx/src/main/java/org/eclipse/lemminx/commons/snippets/SnippetDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.commons.snippets; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.TypeAdapter; | ||
|
||
/** | ||
* GSON deserializer to build Snippet from vscode JSON snippet. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
class SnippetDeserializer implements JsonDeserializer<Snippet> { | ||
|
||
private static final String PREFIX_ELT = "prefix"; | ||
private static final String DESCRIPTION_ELT = "description"; | ||
private static final String SCOPE_ELT = "scope"; | ||
private static final String BODY_ELT = "body"; | ||
private static final String CONTEXT_ELT = "context"; | ||
|
||
private final TypeAdapter<? extends ISnippetContext<?>> contextDeserializer; | ||
|
||
public SnippetDeserializer(TypeAdapter<? extends ISnippetContext<?>> contextDeserializer) { | ||
this.contextDeserializer = contextDeserializer; | ||
} | ||
|
||
@Override | ||
public Snippet deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
Snippet snippet = new Snippet(); | ||
JsonObject snippetObj = json.getAsJsonObject(); | ||
|
||
// prefix | ||
List<String> prefixes = new ArrayList<>(); | ||
JsonElement prefixElt = snippetObj.get(PREFIX_ELT); | ||
if (prefixElt != null) { | ||
if (prefixElt.isJsonArray()) { | ||
JsonArray prefixArray = (JsonArray) prefixElt; | ||
prefixArray.forEach(elt -> { | ||
prefixes.add(elt.getAsString()); | ||
}); | ||
} else if (prefixElt.isJsonPrimitive()) { | ||
prefixes.add(prefixElt.getAsString()); | ||
} | ||
} | ||
snippet.setPrefixes(prefixes); | ||
|
||
// body | ||
List<String> body = new ArrayList<>(); | ||
JsonElement bodyElt = snippetObj.get(BODY_ELT); | ||
if (bodyElt != null) { | ||
if (bodyElt.isJsonArray()) { | ||
JsonArray bodyArray = (JsonArray) bodyElt; | ||
bodyArray.forEach(elt -> { | ||
body.add(elt.getAsString()); | ||
}); | ||
} else if (bodyElt.isJsonPrimitive()) { | ||
body.add(bodyElt.getAsString()); | ||
} | ||
} | ||
snippet.setBody(body); | ||
|
||
// description | ||
JsonElement descriptionElt = snippetObj.get(DESCRIPTION_ELT); | ||
if (descriptionElt != null) { | ||
String description = descriptionElt.getAsString(); | ||
snippet.setDescription(description); | ||
} | ||
|
||
// scope | ||
JsonElement scopeElt = snippetObj.get(SCOPE_ELT); | ||
if (scopeElt != null) { | ||
String scope = scopeElt.getAsString(); | ||
snippet.setScope(scope); | ||
} | ||
|
||
// context | ||
if (contextDeserializer != null) { | ||
JsonElement contextElt = snippetObj.get(CONTEXT_ELT); | ||
if (contextElt != null) { | ||
ISnippetContext<?> snippetContext = contextDeserializer.fromJsonTree(contextElt); | ||
snippet.setContext(snippetContext); | ||
} | ||
} | ||
|
||
return snippet; | ||
} | ||
|
||
} |
Oops, something went wrong.