Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nvlad committed Jan 13, 2017
2 parents 455476b + 2a78877 commit 68968f3
Show file tree
Hide file tree
Showing 19 changed files with 729 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
out/
.idea/
Yii2.jar
32 changes: 32 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Yii2 Support is free software. It is released under the terms of
the following BSD License.

Copyright © 2017 by Vladislav Nikishin aka NVlad ([email protected])
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Yii2 Support nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
24 changes: 16 additions & 8 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
<idea-plugin version="2">
<id>com.nvlad.yii2-framework</id>
<name>Yii2 Framework</name>
<version>0.1</version>
<id>com.yii2support</id>
<name>Yii2 Support</name>
<version>0.2.2</version>
<vendor email="[email protected]" url="http://nvlad.com">NVlad</vendor>

<description><![CDATA[
Enter short description for your plugin here.<br>
<em>most HTML tags may be used</em>
Yii2 Support for PhpStorm<br/>
<br/>
<strong>Features</strong>
<ul>
<li>Views (autocomplete, jump to view)</li>
<li>i18n (autocomplete, generate params array)</li>
</ul>
]]></description>

<change-notes><![CDATA[
Add change notes here.<br>
<em>most HTML tags may be used</em>
<ul>
<li>Fix show autocompletion popup</li>
</ul>
]]>
</change-notes>

Expand All @@ -27,7 +33,9 @@
<depends>com.intellij.modules.platform</depends>

<extensions defaultExtensionNs="com.intellij">
<completion.contributor language="PHP" implementationClass="com.yii2framework.completion.views.CompletionContributor"/>
<completion.contributor language="PHP" implementationClass="com.yii2support.views.CompletionContributor"/>
<psi.referenceContributor language="PHP" implementation="com.yii2support.views.PsiReferenceContributor"/>
<completion.contributor language="PHP" implementationClass="com.yii2support.i18n.CompletionContributor"/>
</extensions>

<actions>
Expand Down
Binary file removed resources/icons/view.png
Binary file not shown.
Binary file removed resources/icons/view_dark.png
Binary file not shown.
23 changes: 0 additions & 23 deletions src/com/yii2framework/completion/views/CompletionContributor.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/com/yii2support/common/Patterns.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.yii2support.common;

import com.intellij.patterns.InitialPatternCondition;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.injection.PhpElementPattern.Capture;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import org.jetbrains.annotations.Nullable;

/**
* Created by NVlad on 12.01.2017.
*/
public class Patterns extends PlatformPatterns {
public static Capture<MethodReference> methodWithName(String... methodNames) {
return new Capture<>(new InitialPatternCondition<MethodReference>(MethodReference.class) {
@Override
public boolean accepts(@Nullable Object o, ProcessingContext context) {
if (o instanceof MethodReference) {
String methodReferenceName = ((MethodReference) o).getName();
return methodReferenceName != null && ArrayUtil.contains(methodReferenceName, methodNames);
}
return super.accepts(o, context);
}
});
}
}
41 changes: 41 additions & 0 deletions src/com/yii2support/i18n/CategoryLookupElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.yii2support.i18n;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

/**
* Created by NVlad on 06.01.2017.
*/
public class CategoryLookupElement extends LookupElement {
private PsiElement myCategory;

CategoryLookupElement(PsiElement category) {
myCategory = category;
}

@NotNull
@Override
public String getLookupString() {
if (myCategory instanceof PsiFile) {
String filename = ((PsiFile) myCategory).getName();
return filename.substring(0, filename.lastIndexOf("."));
}

return myCategory.getText();
}

@Override
public void renderElement(LookupElementPresentation presentation) {
super.renderElement(presentation);

if (myCategory instanceof PsiFile) {
PsiFile file = (PsiFile) myCategory;
String filename = file.getName();
presentation.setIcon(file.getIcon(0));
presentation.setItemText(filename.substring(0, filename.lastIndexOf(".")));
}
}
}
39 changes: 39 additions & 0 deletions src/com/yii2support/i18n/CompletionContributor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.yii2support.i18n;

import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.ParameterList;
import com.yii2support.common.Patterns;
import org.jetbrains.annotations.NotNull;

/**
* Created by NVlad on 06.01.2017.
*/
public class CompletionContributor extends com.intellij.codeInsight.completion.CompletionContributor {
public CompletionContributor() {
extend(CompletionType.BASIC, ElementPattern(), new CompletionProvider());
}

@Override
public boolean invokeAutoPopup(@NotNull PsiElement position, char typeChar) {
if (typeChar == '\'' || typeChar == '"') {
if (position instanceof LeafPsiElement && (position.getText().equals("$category") || position.getText().equals("$message"))) {
return true;
}
if (position.getNextSibling() instanceof ParameterList || position.getParent() instanceof MethodReference) {
return true;
}
}

return false;
}

private static ElementPattern<PsiElement> ElementPattern() {
return PlatformPatterns.psiElement(PsiElement.class)
.withSuperParent(3, Patterns.methodWithName("t"));
}
}
61 changes: 61 additions & 0 deletions src/com/yii2support/i18n/CompletionProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.yii2support.i18n;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.*;
import org.jetbrains.annotations.NotNull;

/**
* Created by NVlad on 06.01.2017.
*/
public class CompletionProvider extends com.intellij.codeInsight.completion.CompletionProvider<CompletionParameters> {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
PhpPsiElement psiElement = (PhpPsiElement) parameters.getPosition().getParent();

MethodReference methodReference = (MethodReference) psiElement.getParent().getParent();
PhpExpression classReference = methodReference.getClassReference();
if (classReference != null && classReference.getName() != null) {
if (methodReference.isStatic() && classReference.getName().equals("Yii")) {
String methodName = methodReference.getName();
if (methodName != null && methodReference.getParameterList() != null) {
PsiElement[] methodParameters = methodReference.getParameterList().getParameters();

int parameterIndex = -1;
for (int i = 0; i < methodParameters.length; i++) {
if (psiElement == methodParameters[i]) {
parameterIndex = i;
break;
}
}

switch (parameterIndex) {
case 0:
fillCategories(psiElement, result);
break;
case 1:
if (methodParameters[0] instanceof StringLiteralExpression) {
String category = ((StringLiteralExpression) methodParameters[0]).getContents();
fillMessages(psiElement, category, result);
}
break;
}
}
}
}
}

private void fillCategories(PhpPsiElement element, CompletionResultSet result) {
for (PsiElement category : Util.getCategories(element)) {
result.addElement(new CategoryLookupElement(category));
}
}

private void fillMessages(PhpPsiElement element, String category, CompletionResultSet result) {
for (ArrayHashElement message : Util.getMessages(element, category)) {
result.addElement(new MessageLookupElement(element, message));
}
}
}
120 changes: 120 additions & 0 deletions src/com/yii2support/i18n/MessageLookupElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.yii2support.i18n;

import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.AutoCompletionPolicy;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.lang.psi.elements.*;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by NVlad on 06.01.2017.
*/
public class MessageLookupElement extends LookupElement {
private PhpPsiElement myElement;
private ArrayHashElement myMessage;

MessageLookupElement(PhpPsiElement element, ArrayHashElement message) {
myElement = element;
myMessage = message;
}

@NotNull
@Override
public String getLookupString() {
return Util.PhpExpressionValue((PhpExpression) myMessage.getKey());
}

@Override
public void renderElement(LookupElementPresentation presentation) {
super.renderElement(presentation);

if (myMessage.getKey() instanceof StringLiteralExpression) {
presentation.setItemText(((StringLiteralExpression) myMessage.getKey()).getContents());
presentation.setIcon(myMessage.getKey().getIcon(0));
}

PhpExpression value = (PhpExpression) myMessage.getValue();
if (value != null) {
String text = Util.PhpExpressionValue(value);

if (!text.isEmpty()) {
presentation.setTailText(" = " + text, true);
}

presentation.setTypeText(value.getType().toString());
presentation.setTypeGrayed(true);
}
}

@Override
public void handleInsert(InsertionContext context) {
super.handleInsert(context);

int suffixLength = myElement.getText().length() - 21 - myElement.getText().lastIndexOf("IntellijIdeaRulezzz ");
int blockStart = context.getSelectionEndOffset();
context.getDocument().deleteString(blockStart, blockStart + suffixLength);

if (myMessage.getValue() instanceof StringLiteralExpression) {
ArrayList<String> matches = new ArrayList<>();

StringLiteralExpression value = (StringLiteralExpression) myMessage.getValue();

Pattern pointers = Pattern.compile("\\{([\\w\\d]+)[},]", Pattern.MULTILINE | Pattern.UNICODE_CASE);
Matcher matcher = pointers.matcher(value.getContents());
while (matcher.find()) {
String match = matcher.group(1);

if (!matches.contains(match)) {
matches.add(match);
}
}

if (matches.size() > 0) {
ParameterList parameterList = (ParameterList) myElement.getParent();
if (parameterList.getParameters().length == 2) {

String params = "";
if (matches.size() == 1 && matches.get(0).equals("0")) {
params = ", []";
} else {
for (String match : matches) {
if (params.length() > 0) {
params = params + ", ";
}
params = params.concat("'" + match + "' => ");
}
params = ", [" + params + "]";
}

context.getDocument().insertString(context.getSelectionEndOffset() + 1, params);
}
} else {
cleanParams(context);
}
} else {
cleanParams(context);
}
}

@Override
public AutoCompletionPolicy getAutoCompletionPolicy() {
return AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE;
}

private void cleanParams(InsertionContext context) {
ParameterList parameterList = (ParameterList) myElement.getParent();
if (parameterList.getParameters().length == 3) {
PsiElement[] parameters = parameterList.getParameters();
int blockStart = context.getSelectionEndOffset() + 1;
int paramSpace = parameters[2].getTextRange().getStartOffset() - parameters[1].getTextRange().getEndOffset();
int blockLength = parameters[2].getTextLength() + paramSpace;
context.getDocument().deleteString(blockStart, blockStart + blockLength);
}
}
}
Loading

0 comments on commit 68968f3

Please sign in to comment.