Skip to content

Commit

Permalink
Enum generation
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
kunicmarko20 committed Oct 21, 2019
1 parent 3d7daa4 commit ff96df2
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.kunicmarko20.idea.helpable.actions.generation;

import com.github.kunicmarko20.idea.helpable.actions.generation.handlers.EnumHandler;
import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.codeInsight.actions.CodeInsightAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

public class EnumAction extends CodeInsightAction {
private final EnumHandler handler = new EnumHandler();

protected boolean isValidForFile(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
return this.handler.isValidFor(editor, file);
}

@NotNull
@Override
protected CodeInsightActionHandler getHandler() {
return this.handler;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.kunicmarko20.idea.helpable.actions.generation.handlers;

import com.github.kunicmarko20.idea.helpable.service.CaseConverter;
import com.github.kunicmarko20.idea.helpable.service.ClassConstantsFinder;
import com.intellij.codeInsight.hint.HintManager;
import com.jetbrains.php.lang.actions.PhpNamedElementNode;
import org.apache.commons.lang.WordUtils;
import org.jetbrains.annotations.NotNull;
import org.jtwig.JtwigModel;
import org.jtwig.JtwigTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EnumHandler extends ActionHandler {
@Override
@NotNull
protected String body() {
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/enum.twig.html");

JtwigModel model = JtwigModel.newModel()
.with("type", this.phpClass.getName())
.with("variants", this.camelCasedVariants(ClassConstantsFinder.find(this.phpClass)));

return template.render(model);
}

private HashMap<String, String> camelCasedVariants(PhpNamedElementNode[] constants) {
HashMap<String, String> camelCasedVariants = new HashMap<>();

for (PhpNamedElementNode constant : constants) {
camelCasedVariants.put(
CaseConverter.fromSnakeToCamelCase(constant.getText()),
constant.getText()
);
}

return camelCasedVariants;
}

@Override
@NotNull
protected boolean isValid() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.kunicmarko20.idea.helpable.service;

public class CaseConverter {
public static String fromSnakeToCamelCase(String phrase) {
StringBuilder camelCasedWords = new StringBuilder();

String[] words = phrase.split("_");

camelCasedWords.append(words[0].toLowerCase());

for (int i = 1; i < words.length; i++) {
camelCasedWords.append(Character.toUpperCase(words[i].charAt(0)));

if (words[i].length() > 1) {
camelCasedWords.append(words[i].substring(1).toLowerCase());
}
}

return camelCasedWords.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.kunicmarko20.idea.helpable.service;

import com.jetbrains.php.lang.actions.PhpNamedElementNode;
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;

public class ClassConstantsFinder {
@NotNull
public static PhpNamedElementNode[] find(@NotNull PhpClass phpClass) {
TreeMap<String, PhpNamedElementNode> nodes = new TreeMap<>();
Collection<Field> fields = phpClass.getFields();
Iterator fieldIterator = fields.iterator();

while (fieldIterator.hasNext()) {
Field field = (Field) fieldIterator.next();

if (field.isConstant()) {
nodes.put(field.getName(), new PhpNamedElementNode(field));
}
}

return nodes.values().toArray(new PhpNamedElementNode[0]);
}
}
5 changes: 5 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
text="Factory Method">
<add-to-group group-id="GenerateGroup" anchor="last"/>
</action>
<action id="com.github.kunicmarko20.idea.helpable.actions.generation.EnumAction"
class="com.github.kunicmarko20.idea.helpable.actions.generation.EnumAction"
text="Enum">
<add-to-group group-id="GenerateGroup" anchor="last"/>
</action>
</actions>

</idea-plugin>
50 changes: 50 additions & 0 deletions src/main/resources/templates/enum.twig.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
private const ALL_MAP = [{% for variant in variants %}
self::{{ variant }},
{%- endfor %}
];

/**
* @var string
*/
private $value;

/**
* @var {{ type }}[]
*/
private static $lazyLoad = [];

private function __construct(string $value)
{
\Assert\Assert::that($value)->inArray(self::ALL_MAP);

$this->value = $value;
}

public static function fromString(string $value): self
{
return new self($value);
}
{% for camel_cased_variant, variant in variants %}
public static function {{ camel_cased_variant }}(): self
{
return self::lazyLoad((self::{{ variant }}));
}
{% endfor %}
private static function lazyLoad(string $value): self
{
if (isset(self::$lazyLoad[$value])) {
return self::$lazyLoad[$value];
}

return self::$lazyLoad[$value] = new self($value);
}

public function toString(): string
{
return $this->value;
}

public function equals({{ type }} $other): bool
{
return $this->value === $other->value;
}

0 comments on commit ff96df2

Please sign in to comment.