-
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.
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/github/kunicmarko20/idea/helpable/actions/generation/EnumAction.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,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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...n/java/com/github/kunicmarko20/idea/helpable/actions/generation/handlers/EnumHandler.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,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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/github/kunicmarko20/idea/helpable/service/CaseConverter.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,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(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/github/kunicmarko20/idea/helpable/service/ClassConstantsFinder.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,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]); | ||
} | ||
} |
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
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,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; | ||
} |