Skip to content

RuleML: support formatter #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion idea/spg-schema-idea-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ An IDEA plugin for OpenSPG Schema Mark Language
## Preview

<div align="center">
<img src="./docs/resources/screenshot.png" width="1200" alt="screenshot"/>
<img src="./docs/resources/screenshot.png" width="1200" alt="screenshot">
</div>

## Features:
Expand All @@ -22,6 +22,7 @@ An IDEA plugin for OpenSPG Schema Mark Language
- Syntax highlighter
- Structure view
- Preview
- Folding

## TODO

Expand Down
9 changes: 4 additions & 5 deletions idea/spg-schema-idea-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import org.jetbrains.kotlin.ir.backend.js.compile

plugins {
id("java")
Expand All @@ -7,14 +6,14 @@ plugins {
}

group = "org.openspg.idea"
version = "0.0.9"
version = "0.0.11"

repositories {
mavenCentral()
}

dependencies {
implementation("com.alibaba:fastjson:1.2.83")
implementation("com.alibaba:fastjson:2.0.57")
}

sourceSets {
Expand All @@ -29,7 +28,7 @@ sourceSets {
// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
intellij {
version.set("2024.1.7")
version.set("2023.1")
type.set("IC") // Target IDE Platform

plugins.set(listOf("com.intellij.java"))
Expand All @@ -46,7 +45,7 @@ tasks {
}

patchPluginXml {
sinceBuild.set("222")
sinceBuild.set("231")
untilBuild.set("")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openspg.idea.conceptRule;

import com.intellij.DynamicBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.PropertyKey;

import java.util.function.Supplier;

public class ConceptRuleBundle {

private static final @NonNls String BUNDLE = "messages.ConceptRuleBundle";
private static final DynamicBundle INSTANCE = new DynamicBundle(ConceptRuleBundle.class, BUNDLE);

private ConceptRuleBundle() {
}

public static @NotNull @Nls String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object @NotNull ... params) {
return INSTANCE.getMessage(key, params);
}

public static @NotNull Supplier<@Nls String> messagePointer(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object @NotNull ... params) {
return INSTANCE.getLazyMessage(key, params);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class ConceptRuleCommenter implements Commenter {

@Override
public String getLineCommentPrefix() {
return "#";
return "//";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ private ConceptRuleFileType() {
@NotNull
@Override
public String getName() {
return "OpenSPG Schema Rule File";
return "OpenSPG Concept Rule File";
}

@NotNull
@Override
public String getDescription() {
return "OpenSPG Schema rule file";
return "OpenSPG Concept rule file";
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.openspg.idea.conceptRule;

import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilderEx;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.DumbAware;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.openspg.idea.conceptRule.lang.psi.*;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Set;

public class ConceptRuleFoldingBuilder extends FoldingBuilderEx implements DumbAware {

private final Set<FoldingAdapter<? extends PsiElement>> adapters = Set.of(
new ConceptRuleCommentFoldingAdapter(),
new ConceptRuleRuleWrapperFoldingAdapter(),
new ConceptRuleTheDefineStructureFoldingAdapter(),
new ConceptRuleTheGraphStructureFoldingAdapter(),
new ConceptRuleTheRuleFoldingAdapter(),
new ConceptRuleCreateActionFoldingAdapter()
);

@Override
@SuppressWarnings("unchecked")
public FoldingDescriptor @NotNull [] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
Class<? extends PsiElement>[] classes = adapters
.stream()
.map(FoldingAdapter::getType)
.toArray(Class[]::new);

return PsiTreeUtil.findChildrenOfAnyType(root, classes)
.stream()
.map(element -> new FoldingDescriptor(element, element.getTextRange()))
.toArray(FoldingDescriptor[]::new);
}

@Override
public @Nullable String getPlaceholderText(@NotNull ASTNode node) {
PsiElement element = node.getPsi();
for (FoldingAdapter<? extends PsiElement> adapter : adapters) {
if (adapter.getType().isInstance(element)) {
return adapter.getPlaceholderText(node, element);
}
}
throw new IllegalArgumentException("Unknown PsiElement type: " + element.getClass());
}

@Override
public boolean isCollapsedByDefault(@NotNull ASTNode node) {
return node.getPsi() instanceof PsiComment;
}

public abstract static class FoldingAdapter<T extends PsiElement> {

@SuppressWarnings("unchecked")
public Class<T> getType() {
Type superClass = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) superClass).getActualTypeArguments();
return (Class<T>) params[0];
}

@SuppressWarnings("unchecked")
public String getPlaceholderText(@NotNull ASTNode node, @NotNull PsiElement element) {
assert getType().isInstance(element);
return getPlaceholderText((T) element);
}

protected abstract String getPlaceholderText(T element);
}

public static class ConceptRuleCommentFoldingAdapter extends FoldingAdapter<PsiComment> {
@Override
protected String getPlaceholderText(@NotNull PsiComment element) {
return "#...";
}
}

public static class ConceptRuleRuleWrapperFoldingAdapter extends FoldingAdapter<ConceptRuleRuleWrapper> {
@Override
protected String getPlaceholderText(@NotNull ConceptRuleRuleWrapper element) {
String placeHolder = element.getRuleWrapperHead().getText();
if (element.getRuleWrapperBody() != null) {
placeHolder += " ...";
}
return placeHolder;
}
}

public static class ConceptRuleTheDefineStructureFoldingAdapter extends FoldingAdapter<ConceptRuleTheDefineStructure> {
@Override
protected String getPlaceholderText(@NotNull ConceptRuleTheDefineStructure element) {
return "Define " + element.getPredicatedDefine().getText() + " {...}";
}
}

public static class ConceptRuleTheGraphStructureFoldingAdapter extends FoldingAdapter<ConceptRuleTheGraphStructure> {
@Override
protected String getPlaceholderText(@NotNull ConceptRuleTheGraphStructure element) {
return element.getGraphStructureHead().getText() + " {...}";
}
}

public static class ConceptRuleTheRuleFoldingAdapter extends FoldingAdapter<ConceptRuleTheRule> {
@Override
protected String getPlaceholderText(@NotNull ConceptRuleTheRule element) {
return element.getRuleHead().getText() + " {...}";
}
}

public static class ConceptRuleCreateActionFoldingAdapter extends FoldingAdapter<ConceptRuleCreateAction> {
@Override
protected String getPlaceholderText(@NotNull ConceptRuleCreateAction element) {
return element.getCreateActionSymbol().getText() + " {...}";
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public final class ConceptRuleLanguage extends Language {
public static final ConceptRuleLanguage INSTANCE = new ConceptRuleLanguage();

private ConceptRuleLanguage() {
super("OpenSPG Schema Rule");
super("OpenSPG Concept Rule");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.openspg.idea.conceptRule.codeStyle;


import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;


public class ConceptRuleCodeStyleSettings extends CustomCodeStyleSettings {

public static final int DEFAULT_INDENT_SIZE = 4;
public static final boolean DEFAULT_SPACE_BEFORE_COMMA = false;
public static final boolean DEFAULT_SPACE_AFTER_COMMA = false;
public static final boolean DEFAULT_SPACE_BEFORE_COLON = false;
public static final boolean DEFAULT_SPACE_AFTER_COLON = false;
public static final boolean DEFAULT_SPACE_WITHIN_BRACKETS = false;
public static final boolean DEFAULT_SPACE_WITHIN_PARENTHESES = false;


public ConceptRuleCodeStyleSettings(CodeStyleSettings container) {
super("ConceptRuleCodeStyleSettings", container);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.openspg.idea.conceptRule.codeStyle;

import com.intellij.application.options.CodeStyleAbstractConfigurable;
import com.intellij.application.options.CodeStyleAbstractPanel;
import com.intellij.application.options.TabbedLanguageCodeStylePanel;
import com.intellij.psi.codeStyle.CodeStyleConfigurable;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsProvider;
import com.intellij.psi.codeStyle.CustomCodeStyleSettings;
import org.jetbrains.annotations.NotNull;
import org.openspg.idea.conceptRule.ConceptRuleLanguage;


public final class ConceptRuleCodeStyleSettingsProvider extends CodeStyleSettingsProvider {

@Override
public CustomCodeStyleSettings createCustomSettings(@NotNull CodeStyleSettings settings) {
return new ConceptRuleCodeStyleSettings(settings);
}

@Override
public String getConfigurableDisplayName() {
return "OpenSPG Concept Rule";
}

@NotNull
public CodeStyleConfigurable createConfigurable(@NotNull CodeStyleSettings settings,
@NotNull CodeStyleSettings modelSettings) {
return new CodeStyleAbstractConfigurable(settings, modelSettings, this.getConfigurableDisplayName()) {
@Override
protected @NotNull CodeStyleAbstractPanel createPanel(@NotNull CodeStyleSettings settings) {
return new SchemaCodeStyleMainPanel(getCurrentSettings(), settings);
}
};
}

private static class SchemaCodeStyleMainPanel extends TabbedLanguageCodeStylePanel {

public SchemaCodeStyleMainPanel(CodeStyleSettings currentSettings, CodeStyleSettings settings) {
super(ConceptRuleLanguage.INSTANCE, currentSettings, settings);
}

@Override
protected void initTabs(CodeStyleSettings settings) {
addIndentOptionsTab(settings);
addSpacesTab(settings);
addBlankLinesTab(settings);
}

}

}
Loading