Skip to content

Commit

Permalink
Merge pull request #170 from nette-intellij/mn-variable-context
Browse files Browse the repository at this point in the history
Mn variable context
  • Loading branch information
mesour authored Jun 1, 2021
2 parents d234377 + 2591533 commit 232c082
Show file tree
Hide file tree
Showing 101 changed files with 2,575 additions and 1,012 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.jetbrains.grammarkit.tasks.*

plugins {
id 'org.jetbrains.intellij' version "0.6.5"
id "org.jetbrains.grammarkit" version "2020.3"
id 'org.jetbrains.intellij' version "0.7.3"
id "org.jetbrains.grammarkit" version "2021.1"
}

sourceSets {
Expand All @@ -19,7 +19,7 @@ properties.load(project.rootProject.file("local.properties").newDataInputStream(
sourceCompatibility = 1.8
targetCompatibility = 1.8

version '1.1.2'
version '1.1.3'
group 'com.jantvrdik.intellij.latte'

jar {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ideaVersion = 2020.3
phpPluginVersion = 203.5981.155
ideaVersion = 2021.1
phpPluginVersion = 211.6693.111
#toolboxPluginVersion = 0.4.6
#annotationPluginVersion = 5.3
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@
import com.jantvrdik.intellij.latte.intentions.*;
import com.jantvrdik.intellij.latte.psi.*;
import com.jantvrdik.intellij.latte.settings.LatteTagSettings;
import com.jantvrdik.intellij.latte.utils.LattePhpUtil;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

/**
* Annotator is mostly used to check semantic rules which can not be easily checked during parsing.
*/
Expand Down Expand Up @@ -59,7 +55,7 @@ private void checkNetteAttr(@NotNull LatteNetteAttr element, @NotNull Annotation
.range(attrName)
.withFix(new AddCustomPairMacro(tagName));
if (!prefixed) {
builder.withFix(new AddCustomAttrOnlyMacro(tagName));
builder = builder.withFix(new AddCustomAttrOnlyMacro(tagName));
}
builder.create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public LatteCompletionContributor() {

extend(CompletionType.BASIC, PlatformPatterns.psiElement(LatteTypes.T_MACRO_NAME).withLanguage(LatteLanguage.INSTANCE), new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement parent = parameters.getPosition().getParent();
Project project = parameters.getOriginalFile().getProject();
attachClassicMacrosCompletion(project, result, parent instanceof LatteMacroCloseTag);
Expand All @@ -42,7 +42,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi

extend(CompletionType.BASIC, PlatformPatterns.psiElement(LatteTypes.T_HTML_TAG_NATTR_NAME).withLanguage(LatteLanguage.INSTANCE), new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
Project project = parameters.getOriginalFile().getProject();
Map<String, LatteTagSettings> customMacros = LatteConfiguration.getInstance(project).getTags();
result.addAllElements(getAttrMacroCompletions(customMacros));
Expand All @@ -51,7 +51,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi

extend(CompletionType.BASIC, PlatformPatterns.psiElement().withLanguage(LatteLanguage.INSTANCE), new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement element = parameters.getPosition().getParent();
if (!LatteUtil.matchParentMacroName(element, "contentType")) {
return;
Expand All @@ -62,7 +62,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi

extend(CompletionType.BASIC, PlatformPatterns.psiElement(LatteTypes.T_MACRO_FILTERS).withLanguage(LatteLanguage.INSTANCE), new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement element = parameters.getPosition().getParent();
if (!(element instanceof LatteMacroModifier)) {
return;
Expand Down Expand Up @@ -143,7 +143,12 @@ private LookupElementBuilder createBuilderForMacro(LatteTagSettings tag, boolean
LookupElementBuilder builder = LookupElementBuilder.create(name);
builder = builder.withInsertHandler(MacroInsertHandler.getInstance());
if (!isEndTag) {
String appendText = tag.getType() == LatteTagSettings.Type.PAIR ? (" … {/" + tag.getMacroName() + "}") : "";
String appendText = "";
if (tag.getType() == LatteTagSettings.Type.PAIR) {
appendText = " … {/" + tag.getMacroName() + "}";
} else if (tag.getType() == LatteTagSettings.Type.AUTO_EMPTY) {
appendText = " … ?{/" + tag.getMacroName() + "}";
}
String arguments = tag.getArgumentsInfo();
if (arguments.length() > 0) {
builder = builder.withTailText(" " + arguments + "}" + appendText);
Expand Down Expand Up @@ -173,12 +178,8 @@ private LookupElementBuilder createBuilderForTag(String name) {
private List<LookupElement> getAttrMacroCompletions(Map<String, LatteTagSettings> macros) {
List<LookupElement> lookupElements = new ArrayList<>(macros.size());
for (LatteTagSettings macro : macros.values()) {
if (macro.getType() != LatteTagSettings.Type.UNPAIRED) {
lookupElements.add(createBuilderForTag("n:" + macro.getMacroName()));
if (macro.getType() == LatteTagSettings.Type.PAIR || macro.getType() == LatteTagSettings.Type.AUTO_EMPTY) {
lookupElements.add(createBuilderForTag("n:tag-" + macro.getMacroName()));
lookupElements.add(createBuilderForTag("n:inner-" + macro.getMacroName()));
}
for (String tagName : macro.getAllowedNetteAttributes()) {
lookupElements.add(createBuilderForTag(tagName));
}
}
return lookupElements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.jantvrdik.intellij.latte.psi.LattePhpClassUsage;
import com.jantvrdik.intellij.latte.psi.LatteTypes;
import com.jetbrains.php.completion.insert.PhpReferenceInsertHandler;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.jantvrdik.intellij.latte.completion.handlers;

import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.psi.PsiDocumentManager;
import com.jetbrains.php.completion.insert.PhpReferenceInsertHandler;
import com.jetbrains.php.lang.psi.elements.PhpNamespace;
import org.jetbrains.annotations.NotNull;

public class PhpNamespaceInsertHandler extends PhpReferenceInsertHandler {

private static final PhpNamespaceInsertHandler instance = new PhpNamespaceInsertHandler();

public PhpNamespaceInsertHandler() {
super();
}

public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
final Object object = lookupElement.getObject();
String fqn = object instanceof PhpNamespace ? ((PhpNamespace) object).getParentNamespaceName() : "";
if (fqn.isEmpty()) {
return;
}

int startOffset = context.getEditor().getCaretModel().getOffset();
String fileText = context.getEditor().getDocument().getText();
String current = fileText.substring(0, startOffset);
int lastSpace = current.lastIndexOf(" ");
current = current.substring(lastSpace + 1);

if (fqn.startsWith("\\")) {
fqn = fqn.substring(1);
}

context.getDocument().insertString(context.getStartOffset(), fqn);

if (!current.endsWith("\\")) {
EditorModificationUtil.insertStringAtCaret(context.getEditor(), "\\");
}

PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument());
}

public static PhpNamespaceInsertHandler getInstance() {
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jantvrdik.intellij.latte.psi.LattePhpClassUsage;
import com.jantvrdik.intellij.latte.php.LattePhpUtil;
import com.jetbrains.php.completion.PhpLookupElement;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,18 +15,42 @@
import java.util.Set;

abstract class BaseLatteCompletionProvider extends CompletionProvider<CompletionParameters> {
PhpLookupElement getPhpLookupElement(@NotNull PhpNamedElement phpNamedElement, @Nullable String searchedWord) {
PhpLookupElement lookupItem = new PhpLookupElement(phpNamedElement) {
protected PhpLookupElement getPhpLookupElement(@NotNull PhpNamedElement phpNamedElement, @Nullable String searchedWord) {
return getPhpLookupElement(phpNamedElement, searchedWord, null);
}

protected PhpLookupElement getPhpLookupElement(@NotNull PhpNamedElement phpNamedElement, @Nullable String searchedWord, @Nullable String tailText) {
PhpLookupElement element = new PhpLookupElement(phpNamedElement) {
@Override
public Set<String> getAllLookupStrings() {
Set<String> original = super.getAllLookupStrings();
Set<String> strings = new HashSet<String>(original.size() + 1);
Set<String> strings = new HashSet<>(original.size() + 1);
strings.addAll(original);
strings.add(searchedWord == null ? this.getNamedElement().getFQN() : searchedWord);
if (searchedWord != null || this.getNamedElement() != null) {
strings.add(searchedWord == null ? this.getNamedElement().getFQN() : searchedWord);
}
return strings;
}
};
return lookupItem;
if (tailText != null) {
element.tailText = tailText;
}
return element;
}

protected String getNamespaceName(PsiElement element) {
LattePhpClassUsage classUsage = PsiTreeUtil.getParentOfType(element, LattePhpClassUsage.class);
String namespaceName = "";
if (classUsage != null) {
String className = classUsage.getClassName().substring(1);
if (className.length() == 1) {
return "";
}
className = classUsage.getClassName();
int index = className.lastIndexOf("\\");
namespaceName = className.substring(0, index);
}
return LattePhpUtil.normalizeClassName(namespaceName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.intellij.util.ProcessingContext;
import com.jantvrdik.intellij.latte.completion.handlers.PhpClassInsertHandler;
import com.jantvrdik.intellij.latte.psi.LattePhpContent;
import com.jantvrdik.intellij.latte.utils.LattePhpUtil;
import com.jantvrdik.intellij.latte.php.LattePhpUtil;
import com.jetbrains.php.completion.PhpLookupElement;
import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
import org.jetbrains.annotations.NotNull;
Expand All @@ -34,15 +34,14 @@ protected void addCompletions(
return;
}

PrefixMatcher prefixMatcher = results.getPrefixMatcher();
String prefix = prefixMatcher.getPrefix();
if (prefix.contains("\\")) {
int index = prefix.lastIndexOf("\\");
prefixMatcher = prefixMatcher.cloneWithPrefix(prefix.substring(index + 1));
String prefix = results.getPrefixMatcher().getPrefix();
String namespaceName = getNamespaceName(curr);
if (namespaceName.length() > 0) {
namespaceName = namespaceName + "\\" + prefix;
}

Project project = params.getPosition().getProject();
Collection<String> classNames = LattePhpUtil.getAllExistingClassNames(project, prefixMatcher);
Collection<String> classNames = LattePhpUtil.getAllExistingClassNames(project, results.getPrefixMatcher().cloneWithPrefix(namespaceName));
Collection<PhpNamedElement> variants = LattePhpUtil.getAllClassNamesAndInterfaces(project, classNames);

// Add variants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.jantvrdik.intellij.latte.completion.handlers.PhpVariableInsertHandler;
import com.jantvrdik.intellij.latte.php.NettePhpType;
import com.jantvrdik.intellij.latte.psi.*;
import com.jantvrdik.intellij.latte.psi.elements.BaseLattePhpElement;
import com.jantvrdik.intellij.latte.utils.LattePhpType;
import com.jantvrdik.intellij.latte.utils.LatteTagsUtil;
import com.jantvrdik.intellij.latte.utils.LatteTypesUtil;
import com.jantvrdik.intellij.latte.utils.LatteUtil;
import com.jetbrains.php.completion.PhpLookupElement;
Expand All @@ -19,6 +20,7 @@
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpModifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

Expand All @@ -43,7 +45,8 @@ protected void addCompletions(
ProcessingContext context,
@NotNull CompletionResultSet result
) {
PsiElement element = parameters.getPosition().getParent();
PsiElement current = parameters.getPosition();
PsiElement element = current.getParent();
if (
element instanceof LattePhpStaticVariable
|| element instanceof LattePhpConstant
Expand All @@ -56,9 +59,17 @@ protected void addCompletions(
} else if (!(element instanceof LatteMacroModifier) && !(element instanceof LattePhpString)) {
classCompletionProvider.addCompletions(parameters, context, result);
namespaceCompletionProvider.addCompletions(parameters, context, result);
if (isInClassDefinition(element)) {
return;
}

if (LatteUtil.matchParentMacroName(element, "varType") || LatteUtil.matchParentMacroName(element, "var")) {
boolean parentType = LatteUtil.matchParentMacroName(element, LatteTagsUtil.Type.VAR_TYPE.getTagName());
boolean parentTemplateType = LatteUtil.matchParentMacroName(element, LatteTagsUtil.Type.TEMPLATE_TYPE.getTagName());
if (parentType || parentTemplateType || LatteUtil.matchParentMacroName(element, LatteTagsUtil.Type.VAR.getTagName())) {
attachVarTypes(result);
if (parentType || parentTemplateType || isInTypeDefinition(current)) {
return;
}
}

variableCompletionProvider.addCompletions(parameters, context, result);
Expand All @@ -79,10 +90,10 @@ private void attachPhpCompletions(
@NotNull BaseLattePhpElement psiElement,
boolean isStatic
) {
LattePhpType type = psiElement.getPhpType();
NettePhpType type = psiElement.getPhpType();

Collection<PhpClass> phpClasses = type.getPhpClasses(psiElement.getProject());
if (phpClasses == null || phpClasses.size() == 0) {
if (phpClasses.size() == 0) {
if (psiElement instanceof LattePhpMethod && (psiElement.getPhpStatementPart() == null || psiElement.getPhpStatementPart().getPrevPhpStatementPart() == null)) {
functionCompletionProvider.addCompletions(parameters, context, result);
}
Expand Down Expand Up @@ -137,6 +148,16 @@ private void attachPhpCompletions(
}
}

private boolean isInClassDefinition(@Nullable PsiElement element) {
return element != null && element.getNode().getElementType() == LatteTypes.PHP_CLASS_USAGE;
}

private boolean isInTypeDefinition(@Nullable PsiElement element) {
return element != null
&& (element.getPrevSibling() == null || (LatteTypesUtil.phpTypeTokens.contains(element.getPrevSibling().getNode().getElementType())))
&& element.getNode().getElementType() != LatteTypes.T_MACRO_ARGS_VAR;
}

private boolean canShowCompletionElement(boolean isStatic, @NotNull PhpModifier modifier) {
return (isStatic && modifier.isStatic()) || (!isStatic && !modifier.isStatic());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.jantvrdik.intellij.latte.config.LatteConfiguration;
import com.jantvrdik.intellij.latte.psi.LattePhpContent;
import com.jantvrdik.intellij.latte.settings.LatteFunctionSettings;
import com.jantvrdik.intellij.latte.utils.LattePhpUtil;
import com.jantvrdik.intellij.latte.php.LattePhpUtil;
import com.jetbrains.php.PhpIcons;
import com.jetbrains.php.completion.PhpLookupElement;
import com.jetbrains.php.completion.insert.PhpFunctionInsertHandler;
Expand Down
Loading

0 comments on commit 232c082

Please sign in to comment.