Skip to content

Commit

Permalink
Use template engine
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 committed Oct 19, 2019
1 parent 1950ee0 commit 3d7daa4
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 81 deletions.
28 changes: 21 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
buildscript {
repositories {
mavenCentral()
plugins {
id 'org.jetbrains.intellij' version '0.4.10'
id 'com.github.johnrengelman.shadow' version '5.1.0'
}

maven { url 'https://dl.bintray.com/jetbrains/intellij-plugin-service' }
}
repositories {
mavenCentral()
jcenter()

maven { url 'https://dl.bintray.com/jetbrains/intellij-plugin-service' }
maven { url 'https://dl.bintray.com/jtwig/maven' }
}

plugins {
id 'org.jetbrains.intellij' version '0.4.10'
dependencies {
compile('org.jtwig:jtwig-core:5.87.0.RELEASE') {
exclude group: 'org.slf4j'
}
}

group 'com.github.kunicmarko20.idea.helpable'
version '0.2'

apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8

intellij {
version ideaVersion
Expand All @@ -39,3 +49,7 @@ wrapper {
jar {
baseName = "helpable"
}

shadowJar {
classifier = null
}
2 changes: 0 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
ideaVersion = IU-2019.2
javaVersion = 1.8
javaTargetVersion = 1.8
phpPluginVersion = 192.5728.108
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@

import com.intellij.codeInsight.hint.HintManager;
import org.jetbrains.annotations.NotNull;
import org.jtwig.JtwigModel;
import org.jtwig.JtwigTemplate;

public class EqualsHandler extends ActionHandler {
final private String TEMPLATE = "public function equals(%TYPE% $other):bool{return %BODY%;}";
final private String EXPRESSION = "$this->%PROPERTY% === $other->%PROPERTY%";

@Override
@NotNull
protected String body() {
StringBuilder body = new StringBuilder();

int propertiesLength = this.classProperties.length;
for (int i = 0; i < propertiesLength; i++) {
if (i > 0) {
body.append("&& ");
}

body.append(EXPRESSION.replace(
"%PROPERTY%",
this.classProperties[i].getText()
));
}
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/equals.twig.html");
JtwigModel model = JtwigModel.newModel()
.with("type", this.phpClass.getName())
.with("properties", this.classProperties);

return this.TEMPLATE
.replace("%TYPE%", this.phpClass.getName())
.replace("%BODY%", body);
return template.render(model);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,35 @@

import com.github.kunicmarko20.idea.helpable.service.PropertyTypeFinder;
import com.intellij.codeInsight.hint.HintManager;
import com.jetbrains.php.lang.actions.PhpNamedElementNode;
import com.jetbrains.php.lang.psi.elements.Field;
import org.jetbrains.annotations.NotNull;
import org.jtwig.JtwigModel;
import org.jtwig.JtwigTemplate;

public class FactoryMethodHandler extends ActionHandler {
final private String TEMPLATE = "public static function with(%PARAMETERS%):self{$instance = new self(); %BODY% return $instance;}";
final private String EXPRESSION_SET_VALUE = "$instance->%PROPERTY% = $%PROPERTY%;";
final private String EXPRESSION_PARAMETER = "%TYPE% $%NAME%";
import java.util.HashMap;

public class FactoryMethodHandler extends ActionHandler {
@Override
@NotNull
protected String body() {
StringBuilder body = new StringBuilder();
StringBuilder parameters = new StringBuilder();

int propertiesLength = this.classProperties.length;
for (int i = 0; i < propertiesLength; i++) {
if (i > 0) {
parameters.append(", ");
}

String propertyType = PropertyTypeFinder.find((Field) this.classProperties[i].getPsiElement(), this.project);

parameters.append(
EXPRESSION_PARAMETER.replace(
"%TYPE%",
propertyType
).replace(
"%NAME%",
this.classProperties[i].getText()
)
);

body.append(
EXPRESSION_SET_VALUE.replace(
"%PROPERTY%",
this.classProperties[i].getText()
HashMap<String, String> properties = new HashMap<>();

for (PhpNamedElementNode property: this.classProperties) {
properties.put(
property.getText(),
PropertyTypeFinder.find(
(Field) property.getPsiElement(),
this.project
)
);
}

return TEMPLATE.replace(
"%PARAMETERS%",
parameters
).replace(
"%BODY%",
body
);
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/factory_method.twig.html");
JtwigModel model = JtwigModel.newModel()
.with("properties", properties);

return template.render(model);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import com.jetbrains.php.lang.psi.elements.Field;
import org.apache.commons.lang.WordUtils;
import org.jetbrains.annotations.NotNull;
import org.jtwig.JtwigModel;
import org.jtwig.JtwigTemplate;

public class ToHandler extends ActionHandler {
final private String TEMPLATE = "public function to%TYPE_METHOD_NAME%():%TYPE%{return $this->%PROPERTY%;}";

@Override
protected String body() {
PhpNamedElementNode[] properties = this.classProperties;
Expand All @@ -31,10 +31,13 @@ protected String body() {
return "";
}

return this.TEMPLATE
.replace("%PROPERTY%", properties[0].getText())
.replace("%TYPE%", propertyType)
.replace("%TYPE_METHOD_NAME%", normalizedTypeName);
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/to.twig.html");
JtwigModel model = JtwigModel.newModel()
.with("property", properties[0].getText())
.with("type", propertyType)
.with("type_method_name", normalizedTypeName);

return template.render(model);
}

private String normalizeTypeName(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import com.jetbrains.php.lang.psi.elements.Field;
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.List;

public class WitherHandler extends ActionHandler {
final private String TEMPLATE = "public function with%CAPITALIZED_PROPERTY%(%PROPERTY_TYPE% $%PROPERTY%):self{$instance = clone $this; $instance->%PROPERTY% = $%PROPERTY%;return $instance;}";

@Override
protected String body() {
PhpNamedElementNode[] properties = this.classProperties;
Expand All @@ -27,17 +27,20 @@ protected String body() {
}

StringBuilder body = new StringBuilder();
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/with.twig.html");

for (PhpNamedElementNode property : properties) {
body.append(
TEMPLATE.replace(
"%CAPITALIZED_PROPERTY%",
WordUtils.capitalize(property.getText())
).replace(
"%PROPERTY%",
property.getText()
).replace(
"%PROPERTY_TYPE%",
PropertyTypeFinder.find((Field) property.getPsiElement(), this.project)
template.render(
JtwigModel.newModel()
.with("property", property.getText())
.with(
"property_type",
PropertyTypeFinder.find(
(Field) property.getPsiElement(),
this.project
)
)
)
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/templates/equals.twig.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public function equals({{ type }} $other): bool
{
return {%- for property in properties -%}
{%- if (loop.index > 1) -%}
&&
{%- endif -%}
$this->{{ property.getText() }} === $other->{{ property.getText() }}
{%- endfor -%};
}
13 changes: 13 additions & 0 deletions src/main/resources/templates/factory_method.twig.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public static function with(
{%- for property, type in properties -%}
{{ type }} ${{ property }} {%- if (loop.last == false) -%}, {%- endif -%}
{%- endfor -%}
): self
{
$instance = new self();
{%- for property, _ in properties -%}
$instance->{{ property }} = ${{ property }};
{%- endfor %}

return $instance;
}
4 changes: 4 additions & 0 deletions src/main/resources/templates/to.twig.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function to{{ type_method_name }}(): {{ type }}
{
return $this->{{ property }};
}
7 changes: 7 additions & 0 deletions src/main/resources/templates/with.twig.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public function with{{ capitalize(property) }}({{ property_type }} ${{ property }}):self
{
$instance = clone $this;
$instance->{{ property }} = ${{ property }};

return $instance;
}

0 comments on commit 3d7daa4

Please sign in to comment.