Skip to content

Commit

Permalink
Provide completion and GoTo in @return phpdoc tag
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Jul 30, 2018
1 parent c483038 commit 6f2ed38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocReturnTag;
import com.jetbrains.php.lang.psi.elements.*;
import org.jetbrains.annotations.NotNull;
import org.klesun.deep_assoc_completion.helpers.SearchContext;
Expand Down Expand Up @@ -36,7 +37,7 @@ private static String makeMethOrderValue(Method meth)
private Opt<List<String>> extractTypedFqnPart(String docValue, PhpIndex idx)
{
return Opt.fst(list(opt(null)
, Tls.regex(" *= *([A-Z][A-Za-z0-9_]+)::([a-zA-Z0-9_]*?)(IntellijIdeaRulezzz.*)?", docValue)
, Tls.regex(" *([A-Z][A-Za-z0-9_]+)::([a-zA-Z0-9_]*?)(IntellijIdeaRulezzz.*)?", docValue)
// have to complete method
.map(mtch -> {
String clsName = mtch.gat(0).unw();
Expand All @@ -52,7 +53,7 @@ private Opt<List<String>> extractTypedFqnPart(String docValue, PhpIndex idx)
.flt(p -> metMatcher.prefixMatches(p))
.map(f -> f + "()"));
})
, Tls.regex(" *= *([A-Z][A-Za-z0-9_]+?)(IntellijIdeaRulezzz.*)?", docValue)
, Tls.regex(" *([A-Z][A-Za-z0-9_]+?)(IntellijIdeaRulezzz.*)?", docValue)
// have to complete class
.fop(m -> m.gat(0))
.map(CamelHumpMatcher::new)
Expand All @@ -68,22 +69,23 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
SearchContext search = new SearchContext(parameters).setDepth(depth);

opt(parameters.getPosition().getParent())
.thn(literal -> {
String docValue = literal.getText();
Project project = literal.getProject();
.thn(tagValue -> {
String docValue = tagValue.getText();
Project project = tagValue.getProject();
PhpIndex idx = PhpIndex.getInstance(project);
// method name completion
extractTypedFqnPart(docValue, idx)
.thn(options -> L(options)
.map((lookup) -> LookupElementBuilder.create(lookup)
.withIcon(DeepKeysPvdr.getIcon()))
.fch(result::addElement))
.els(() -> result.addLookupAdvertisement("No FQN-s found with such partial name - " + literal.getText()));

// assoc array completion
String prefix = "<?php\n$arg = ";
Tls.regex("^\\s*=\\s*(.+)$", docValue)
String regex = tagValue.getParent() instanceof PhpDocReturnTag
? "^\\s*(?:like|=|)\\s*(.+)$" : "^\\s*=\\s*(.+)$";
Tls.regex(regex, docValue)
.fop(match -> match.gat(0))
// method name completion
.thn(expr -> extractTypedFqnPart(expr, idx)
.thn(options -> L(options)
.map((lookup) -> LookupElementBuilder.create(lookup)
.withIcon(DeepKeysPvdr.getIcon()))
.fch(result::addElement)))
// assoc array completion
.map(expr -> prefix + expr + ";")
.map(expr -> PsiFileFactory.getInstance(project).createFileFromText(PhpLanguage.INSTANCE, expr))
.map(file -> file.findElementAt(file.getText().indexOf("IntellijIdeaRulezzz")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.intellij.psi.PsiFileFactory;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocParamTag;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocReturnTag;
import com.jetbrains.php.lang.psi.elements.ArrayIndex;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.PhpExpression;
Expand Down Expand Up @@ -91,7 +92,8 @@ private static L<PsiElement> resolveDocAt(PsiElement psiElement, int mouseOffset
String prefix = "<?php\n$arg ";
return opt(psiElement)
.map(psi -> psi.getParent())
.flt(doc -> Tls.regex("^\\s*=\\s*(.+)$", doc.getText()).has())
.flt(doc -> Tls.regex("^\\s*=\\s*(.+)$", doc.getText()).has() ||
doc.getParent() instanceof PhpDocReturnTag)
.fap(doc -> {
String fileText = prefix + doc.getText() + ";";
PsiFile file = PsiFileFactory.getInstance(doc.getProject())
Expand Down

0 comments on commit 6f2ed38

Please sign in to comment.