Skip to content

Commit

Permalink
Merge pull request #8 from AsterAI/phalcon_class_completion
Browse files Browse the repository at this point in the history
[WIP] 0.3.2
  • Loading branch information
sergeyklay authored May 23, 2017
2 parents 3bba0ae + 9d77d9f commit bf347e8
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/com/zephir/completion/Priority.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
public class Priority {

public static double METHOD_SCOPE_PRIORITY = 2.0;

public static double CLASS_METHOD_PRIORITY = 1.7;
public static double CLASS_CONSTS_PRIORITY = 1.6;
public static double CLASS_PROPERTY_PRIORITY = 1.5;
}
113 changes: 95 additions & 18 deletions src/com/zephir/completion/providers/MethodScopeCompletionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.zephir.completion.Priority;
import com.zephir.psi.ZephirArgument;
import com.zephir.psi.ZephirArguments;
import com.zephir.psi.ZephirFile;
import com.zephir.psi.ZephirMethodDefinition;
import com.zephir.psi.*;
import org.jetbrains.annotations.NotNull;


Expand All @@ -22,7 +19,6 @@

public class MethodScopeCompletionProvider extends CompletionProvider<CompletionParameters> {


private static int MAX_SYNTAX_TREE_DEEP = 256;

private String[] keywords = new String[] {
Expand All @@ -48,19 +44,102 @@ protected void addCompletions(@NotNull CompletionParameters parameters,
return;
}

ZephirMethodDefinition methodDefinition = getMethodByCurrentPos(psiElement);
ZephirMethodDefinition methodDefinition = (ZephirMethodDefinition)getPsiByCurrentPos(psiElement, "method");
if (methodDefinition == null) {
return;
}

processMethodArguments(methodDefinition, result);

ZephirClassBody classBody = (ZephirClassBody)getPsiByCurrentPos(psiElement, "class");
if (classBody == null) {
return;
}
processClassMembers(classBody, result);
processClassConstants(classBody, result);
processClassMethods(classBody, result);

//process keywords
for (String keyword : this.keywords) {
result.addElement(LookupElementBuilder.create(keyword));
}

for (String typeHint : this.typeHints) {
result.addElement(LookupElementBuilder.create(typeHint));
}
}


private void processClassMembers(ZephirClassBody classBody, @NotNull CompletionResultSet result) {

for(ZephirPropertyDefinition propDef : classBody.getPropertyDefinitionList()) {
LookupElementBuilder completionElement = LookupElementBuilder
.create(propDef.getId().getText(), "this->" + propDef.getId().getText())
.withTypeText(propDef.getVisibility().getText(), true)
.withLookupString(propDef.getId().getText())
.withTailText(
propDef.getDefaultValue() != null ? " " + propDef.getDefaultValue().getText() : "",
true
);

result.addElement(
PrioritizedLookupElement.withPriority(completionElement, Priority.CLASS_PROPERTY_PRIORITY)
);
}
}

private void processClassConstants(ZephirClassBody classBody, @NotNull CompletionResultSet result) {

for(ZephirConstantDefinition constDef : classBody.getConstantDefinitionList()) {
LookupElementBuilder completionElement = LookupElementBuilder
.create(constDef.getId().getText(), "self::" + constDef.getId().getText())
.withTypeText(constDef.getDefaultValue().getText())
.withLookupString(constDef.getId().getText());

result.addElement(
PrioritizedLookupElement.withPriority(completionElement, Priority.CLASS_CONSTS_PRIORITY)
);
}
}

private void processClassMethods(ZephirClassBody classBody, @NotNull CompletionResultSet result) {

for(ZephirAbstractMethodDefinition abstractDef : classBody.getAbstractMethodDefinitionList()) {
ZephirInterfaceMethodDefinition interDef = abstractDef.getInterfaceMethodDefinition();

LookupElementBuilder completionElement = LookupElementBuilder
.create(interDef.getId().getText(), "this->" + interDef.getId().getText() + "()")
.withTypeText(interDef.getReturnType() != null ? interDef.getReturnType().getText() : "")
.withLookupString(interDef.getId().getText());

result.addElement(
PrioritizedLookupElement.withPriority(completionElement, Priority.CLASS_METHOD_PRIORITY)
);
}

for(ZephirMethodDefinition methodDef : classBody.getMethodDefinitionList()) {

LookupElementBuilder completionElement = LookupElementBuilder
.create(methodDef.getId().getText(), "this->" + methodDef.getId().getText() + "()")
.withTypeText(methodDef.getReturnType() != null ? methodDef.getReturnType().getText() : "")
.withLookupString(methodDef.getId().getText());

result.addElement(
PrioritizedLookupElement.withPriority(completionElement, Priority.CLASS_METHOD_PRIORITY)
);
}

}

private void processMethodArguments(ZephirMethodDefinition methodDefinition, @NotNull CompletionResultSet result) {

List<ZephirArgument> methodArgs = new LinkedList();
for(ZephirArguments args : methodDefinition.getArgumentsList()) {
methodArgs.addAll(args.getArgumentList());
}

for (ZephirArgument arg : methodArgs) {
LookupElementBuilder completionElement = LookupElementBuilder
//empty space to pull up arguments of method
.create(arg.getId().getText(), arg.getId().getText())
.withTypeText(arg.getType() != null ? arg.getType().getText() : "", true)
.withBoldness(true)
Expand All @@ -70,19 +149,14 @@ protected void addCompletions(@NotNull CompletionParameters parameters,
true
);

result.addElement(PrioritizedLookupElement.withPriority(completionElement, Priority.METHOD_SCOPE_PRIORITY));
result.addElement(
PrioritizedLookupElement.withPriority(completionElement, Priority.METHOD_SCOPE_PRIORITY)
);
}

for (String keyword : this.keywords) {
result.addElement(LookupElementBuilder.create(keyword));
}

for (String typeHint : this.typeHints) {
result.addElement(LookupElementBuilder.create(typeHint));
}
}

private ZephirMethodDefinition getMethodByCurrentPos(PsiElement psiElement) {
private PsiElement getPsiByCurrentPos(PsiElement psiElement, String objectType) {
PsiElement parent = psiElement.getParent();

if (parent == null || parent instanceof ZephirFile) {
Expand All @@ -94,12 +168,15 @@ private ZephirMethodDefinition getMethodByCurrentPos(PsiElement psiElement) {
parent = parent.getParent();
if (parent == null || parent instanceof ZephirFile) {
return null;
} else if (parent instanceof ZephirMethodDefinition) {
return (ZephirMethodDefinition)parent;
} else if (objectType.equals("method") && parent instanceof ZephirMethodDefinition) {
return parent;
} else if (objectType.equals("class") && parent instanceof ZephirClassBody) {
return parent;
}
++findLimitCounter;
} while (findLimitCounter < MAX_SYNTAX_TREE_DEEP); // to avoid possible infinite cycles

return null;
}

}

0 comments on commit bf347e8

Please sign in to comment.