Skip to content

Commit

Permalink
directory support
Browse files Browse the repository at this point in the history
  • Loading branch information
nvlad committed Dec 28, 2016
1 parent 38ad863 commit a0a8053
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
40 changes: 35 additions & 5 deletions src/com/yii2framework/completion/views/CompletionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.template.macro.SplitWordsMacro;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
Expand All @@ -15,21 +16,27 @@
public class CompletionProvider extends com.intellij.codeInsight.completion.CompletionProvider {
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
PhpPsiElement psiElement = (PhpPsiElement) completionParameters.getPosition().getParent().getParent().getParent();
String psiElementName = psiElement.getName();
PsiElement psiElement = completionParameters.getPosition();
PhpPsiElement phpPsiElement = (PhpPsiElement) psiElement.getParent().getParent().getParent();
String psiElementName = phpPsiElement.getName();

if (psiElementName != null && psiElementName.startsWith("render")) {
PsiDirectory viewsPath = getViewsPsiDirectory(completionParameters.getOriginalFile());
PsiDirectory viewsPath = getViewsPsiDirectory(completionParameters.getOriginalFile(), psiElement);

if (viewsPath != null) {
String enteredText = psiElement.getText().replaceAll("IntellijIdeaRulezzz ", "");
for (PsiDirectory psiDirectory : viewsPath.getSubdirectories()) {
completionResultSet.addElement(new DirectoryLookupElement(psiDirectory, enteredText));
}

for (PsiFile psiFile : viewsPath.getFiles()) {
completionResultSet.addElement(new ExistLookupElement(psiFile));
completionResultSet.addElement(new ViewLookupElement(psiFile, enteredText));
}
}
}
}

private PsiDirectory getViewsPsiDirectory(PsiFile psiFile) {
private PsiDirectory getViewsPsiDirectory(PsiFile psiFile, PsiElement psiElement) {
String fileName = psiFile.getName().substring(0, psiFile.getName().lastIndexOf("."));
PsiDirectory psiDirectory = psiFile.getContainingDirectory();

Expand All @@ -47,6 +54,29 @@ private PsiDirectory getViewsPsiDirectory(PsiFile psiFile) {
}
}

String enteredText = psiElement.getText().replaceAll("IntellijIdeaRulezzz ", "");
String enteredPath = enteredText;
if (enteredText.startsWith("/")) {
while (psiDirectory != null && !psiDirectory.getName().equals("views")) {
psiDirectory = psiDirectory.getParentDirectory();
}
enteredPath = enteredPath.substring(1);
}

String directory;
while (!enteredPath.equals("")) {
if (enteredPath.contains("/")) {
directory = enteredPath.substring(0, enteredPath.indexOf("/"));
enteredPath = enteredPath.substring(directory.length() + 1);
psiDirectory = psiDirectory.findSubdirectory(directory);
} else {
psiDirectory = psiDirectory.findSubdirectory(enteredPath);
enteredPath = "";
}
}

System.out.println(enteredPath);

return psiDirectory;
}
}
46 changes: 46 additions & 0 deletions src/com/yii2framework/completion/views/DirectoryLookupElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.yii2framework.completion.views;

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

import javax.swing.*;

/**
* Created by NVlad on 28.12.2016.
*/
public class DirectoryLookupElement extends LookupElement {
private Icon icon;
private String name;
private String prefix;
private String lookup;

DirectoryLookupElement(PsiDirectory directory, String searchPrefix) {
icon = directory.getIcon(0);
name = directory.getName();
prefix = searchPrefix;
if (searchPrefix.startsWith("/")) {
prefix = prefix.substring(1);
}
if (prefix.length() > 0 && !prefix.endsWith("/")) {
prefix = prefix.concat("/");
}
lookup = directory.getName().concat("/");
}

@NotNull
@Override
public String getLookupString() {
return prefix.concat(lookup);
}

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

presentation.setItemText(name);
presentation.setTailText("/", true);
presentation.setIcon(icon);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,38 @@
/**
* Created by NVlad on 28.12.2016.
*/
public class ExistLookupElement extends LookupElement {
public class ViewLookupElement extends LookupElement {
private String name;
private String tail;
private String prefix;

ExistLookupElement(PsiFile psiFile) {
ViewLookupElement(PsiFile psiFile, String searchPrefix) {
String filename = psiFile.getName().substring(0, psiFile.getName().lastIndexOf("."));
if (filename.contains(".")) {
name = psiFile.getName();
} else {
name = filename;
tail = psiFile.getName().substring(filename.length());
}
prefix = searchPrefix;
if (searchPrefix.startsWith("/")) {
prefix = prefix.substring(1);
}
if (prefix.length() > 0 && !prefix.endsWith("/")) {
prefix = prefix.concat("/");
}
}

@NotNull
@Override
public String getLookupString() {
return name;
return prefix.concat(name);
}

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

presentation.setIcon(IconLoader.getIcon("/icons/view.png"));
presentation.setItemText(name);
presentation.setItemTextBold(true);
Expand All @@ -39,7 +49,5 @@ public void renderElement(LookupElementPresentation presentation) {
}
presentation.setTypeText("View");
presentation.setTypeGrayed(true);

super.renderElement(presentation);
}
}

0 comments on commit a0a8053

Please sign in to comment.