From a0a8053f3718321a890aae410a1f2933f1d6a933 Mon Sep 17 00:00:00 2001 From: NVlad Date: Wed, 28 Dec 2016 19:34:49 +0300 Subject: [PATCH] directory support --- .../completion/views/CompletionProvider.java | 40 ++++++++++++++-- .../views/DirectoryLookupElement.java | 46 +++++++++++++++++++ ...kupElement.java => ViewLookupElement.java} | 18 ++++++-- 3 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 src/com/yii2framework/completion/views/DirectoryLookupElement.java rename src/com/yii2framework/completion/views/{ExistLookupElement.java => ViewLookupElement.java} (74%) diff --git a/src/com/yii2framework/completion/views/CompletionProvider.java b/src/com/yii2framework/completion/views/CompletionProvider.java index 07ca8925..cb29aeaa 100644 --- a/src/com/yii2framework/completion/views/CompletionProvider.java +++ b/src/com/yii2framework/completion/views/CompletionProvider.java @@ -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; @@ -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(); @@ -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; } } diff --git a/src/com/yii2framework/completion/views/DirectoryLookupElement.java b/src/com/yii2framework/completion/views/DirectoryLookupElement.java new file mode 100644 index 00000000..a30d9594 --- /dev/null +++ b/src/com/yii2framework/completion/views/DirectoryLookupElement.java @@ -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); + } +} diff --git a/src/com/yii2framework/completion/views/ExistLookupElement.java b/src/com/yii2framework/completion/views/ViewLookupElement.java similarity index 74% rename from src/com/yii2framework/completion/views/ExistLookupElement.java rename to src/com/yii2framework/completion/views/ViewLookupElement.java index 6b48234b..16e945df 100644 --- a/src/com/yii2framework/completion/views/ExistLookupElement.java +++ b/src/com/yii2framework/completion/views/ViewLookupElement.java @@ -9,11 +9,12 @@ /** * 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(); @@ -21,16 +22,25 @@ public class ExistLookupElement extends LookupElement { 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); @@ -39,7 +49,5 @@ public void renderElement(LookupElementPresentation presentation) { } presentation.setTypeText("View"); presentation.setTypeGrayed(true); - - super.renderElement(presentation); } }