Skip to content

Commit

Permalink
Merge pull request #182 from nette-intellij/mn-patch-02
Browse files Browse the repository at this point in the history
Small improvements
  • Loading branch information
mesour authored Dec 4, 2021
2 parents 344d481 + dc5da52 commit d569b5d
Show file tree
Hide file tree
Showing 154 changed files with 3,627 additions and 1,807 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ properties.load(project.rootProject.file("local.properties").newDataInputStream(
sourceCompatibility = 1.8
targetCompatibility = 1.8

version '1.1.3'
version '1.1.4'
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 = 2021.1
phpPluginVersion = 211.6693.111
ideaVersion = 2021.2.3
phpPluginVersion = 212.5457.49
#toolboxPluginVersion = 0.4.6
#annotationPluginVersion = 5.3
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jantvrdik.intellij.latte.config.LatteConfiguration;
import com.jantvrdik.intellij.latte.intentions.*;
import com.jantvrdik.intellij.latte.psi.*;
Expand Down Expand Up @@ -106,11 +105,10 @@ private void checkMacroClassic(@NotNull LatteMacroClassic element, @NotNull Anno
&& closeTag == null
&& ((element instanceof LattePairMacro && macro.getType() == LatteTagSettings.Type.AUTO_EMPTY) || macro.getType() == LatteTagSettings.Type.PAIR)
) {
//if (!macro.isTagBlock() || element.getContainingFile().getLastChild() == openTag.getParent()) {
final int[] unclosed = {0};
openTag.getParent().acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
public void visitElement(@NotNull PsiElement element) {
if (element instanceof LattePairMacro) {
LatteMacroTag tag = ((LattePairMacro) element).getOpenTag();
if (tag.getMacroName().equals("block") && ((LattePairMacro) element).getCloseTag() == null) {
Expand All @@ -124,7 +122,7 @@ public void visitElement(PsiElement element) {
}
}
});
PsiElement el = PsiTreeUtil.getChildOfAnyType(openTag.getParent(), LattePairMacro.class);
//PsiElement el = PsiTreeUtil.getChildOfAnyType(openTag.getParent(), LattePairMacro.class);
if (!macro.isTagBlock() || unclosed[0] > 0) {
createErrorAnnotation(holder, openTag, "Unclosed tag " + openTagName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void attachPhpCompletions(
@NotNull BaseLattePhpElement psiElement,
boolean isStatic
) {
NettePhpType type = psiElement.getPhpType();
NettePhpType type = psiElement.getPrevReturnType();

Collection<PhpClass> phpClasses = type.getPhpClasses(psiElement.getProject());
if (phpClasses.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.ProcessingContext;
import com.jantvrdik.intellij.latte.completion.handlers.PhpVariableInsertHandler;
import com.jantvrdik.intellij.latte.config.LatteConfiguration;
import com.jantvrdik.intellij.latte.php.LattePhpVariableUtil;
import com.jantvrdik.intellij.latte.php.NettePhpType;
import com.jantvrdik.intellij.latte.psi.elements.LattePsiElement;
import com.jantvrdik.intellij.latte.settings.LatteVariableSettings;
import com.jantvrdik.intellij.latte.psi.LatteFile;
import com.jantvrdik.intellij.latte.psi.LattePhpVariable;
import com.jantvrdik.intellij.latte.utils.LatteUtil;
import com.jantvrdik.intellij.latte.utils.PsiCachedElement;
import com.jantvrdik.intellij.latte.utils.LattePhpCachedVariable;
import com.jetbrains.php.PhpIcons;
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.PhpClass;
Expand Down Expand Up @@ -72,18 +73,23 @@ private void attachTemplateTypeCompletions(@NotNull CompletionResultSet result,
}

private List<LookupElement> attachPhpVariableCompletions(@NotNull PsiElement psiElement) {
PsiFile file = psiElement instanceof LattePsiElement ? ((LattePsiElement) psiElement).getLatteFile() : psiElement.getContainingFile();
if (!(file instanceof LatteFile)) {
return Collections.emptyList();
}

List<LookupElement> lookupElements = new ArrayList<>();
List<String> foundVariables = new ArrayList<>();

for (PsiCachedElement element : LattePhpVariableUtil.getVariablesDefinitionsBeforeElement(psiElement)) {
for (LattePhpCachedVariable element : ((LatteFile) file).getCachedVariableDefinitions(psiElement.getTextOffset())) {
String variableName = element.getElement().getVariableName();
if (foundVariables.stream().anyMatch(variableName::equals)) {
continue;
}

LookupElementBuilder builder = LookupElementBuilder.create(element.getElement(), "$" + variableName);
builder = builder.withInsertHandler(PhpVariableInsertHandler.getInstance());
builder = builder.withTypeText(element.getElement().getPhpType().toString());
builder = builder.withTypeText(element.getElement().getPrevReturnType().toString());
builder = builder.withIcon(PhpIcons.VARIABLE);
builder = builder.withBoldness(true);
lookupElements.add(builder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.jantvrdik.intellij.latte.config;

import com.intellij.notification.Notification;
import com.intellij.openapi.project.Project;
import com.jantvrdik.intellij.latte.config.LatteConfiguration.Vendor;
import com.jantvrdik.intellij.latte.utils.LatteReparseFilesUtil;
import com.jantvrdik.intellij.latte.settings.*;
import com.jantvrdik.intellij.latte.settings.xml.LatteXmlFileData;
import com.jantvrdik.intellij.latte.settings.xml.LatteXmlFileDataFactory;
import com.jantvrdik.intellij.latte.utils.LatteIdeHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class LatteDefaultConfiguration {

Expand All @@ -24,28 +22,25 @@ public class LatteDefaultConfiguration {
put("NetteForms.xml", Vendor.NETTE_FORMS);
}};

private static Map<Project, LatteDefaultConfiguration> instances = new HashMap<>();
private static final Map<Project, LatteDefaultConfiguration> instances = new HashMap<>();

private Map<Vendor, LatteXmlFileData> xmlData = new HashMap<>();

@NotNull
private final Project project;
private final @NotNull Project project;

private final boolean disableLoading;

@Nullable
private Notification notification = null;

public LatteDefaultConfiguration(@NotNull Project project, boolean disableLoading) {
this.project = project;
this.disableLoading = disableLoading;
reinitialize();
}

public void reinitialize() {
public boolean reinitialize() {
int oldHash = hashCode();
xmlData = new HashMap<>();
if (disableLoading) {
return;
return false;
}

LatteIdeHelper.saveFileToProjectTemp(project, "xmlSources/Latte.dtd");
Expand All @@ -62,11 +57,23 @@ public void reinitialize() {
xmlData.put(vendorResult.vendor, data);

} else if (LatteIdeHelper.holdsReadLock()) {
if (notification == null || notification.isExpired() || LatteReparseFilesUtil.isNotificationOutdated(notification)) {
notification = LatteReparseFilesUtil.notifyDefaultReparse(project);
}
LatteReparseUtil.getInstance(project).reinitializeDefault();
}
}
return oldHash == hashCode();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LatteDefaultConfiguration that = (LatteDefaultConfiguration) o;
return Objects.equals(xmlData, that.xmlData) && project.equals(that.project);
}

@Override
public int hashCode() {
return Objects.hash(xmlData, project);
}

public static LatteDefaultConfiguration getInstance(@NotNull Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.intellij.util.xml.DomTarget;
import com.jantvrdik.intellij.latte.indexes.LatteIndexExtension;
import com.jantvrdik.intellij.latte.utils.LatteIdeHelper;
import com.jantvrdik.intellij.latte.utils.LatteReparseFilesUtil;
import com.jantvrdik.intellij.latte.settings.xml.LatteXmlFileData;
import com.jantvrdik.intellij.latte.settings.*;
import com.jantvrdik.intellij.latte.settings.xml.LatteXmlFileDataFactory;
Expand All @@ -34,14 +33,11 @@ public class LatteFileConfiguration implements Serializable {

final public static List<String> REFERENCED_TAGS = Arrays.asList("filter", "variable", "function");

private static Map<Project, LatteFileConfiguration> instances = new HashMap<>();
private static final Map<Project, LatteFileConfiguration> instances = new HashMap<>();

private Map<String, LatteTagSettings> tags = new HashMap<>();

private Map<String, LatteFilterSettings> filters = new HashMap<>();

private Map<String, LatteVariableSettings> variables = new HashMap<>();

private Map<String, LatteFunctionSettings> functions = new HashMap<>();

private List<LatteConfiguration.Vendor> vendors = new ArrayList<>();
Expand All @@ -50,8 +46,6 @@ public class LatteFileConfiguration implements Serializable {

final private Collection<LatteXmlFileData> xmlFileData;

private Notification notification = null;

public LatteFileConfiguration(final Project project, final @Nullable Collection<LatteXmlFileData> xmlFileData) {
this.project = project;
this.xmlFileData = xmlFileData;
Expand Down Expand Up @@ -94,10 +88,8 @@ private void initializeFromXmlData(@NotNull Collection<LatteXmlFileData> xmlData
}

private void initializeFromFileIndex() {
if (false && ActionUtil.isDumbMode(project)) {
if (notification == null || notification.isExpired() || LatteReparseFilesUtil.isNotificationOutdated(notification)) {
notification = LatteReparseFilesUtil.notifyReparseFiles(project);
}
if (ActionUtil.isDumbMode(project)) {
LatteReparseUtil.getInstance(project).reinitialize();
return;
}

Expand All @@ -109,15 +101,12 @@ private void initializeFromFileIndex() {
key,
GlobalSearchScope.allScope(project)
);
if (xmlData != null) {
initializeFromXmlData(xmlData);
}
initializeFromXmlData(xmlData);
}

} catch (IndexNotReadyException e) {
notification = LatteReparseFilesUtil.notifyReparseFiles(project);
LatteReparseUtil.getInstance(project).reinitialize();
}
//PsiFile[] files = FilenameIndex.getFilesByName(project, FILE_NAME, GlobalSearchScope.allScope(project));
}

public void applyXmlData(LatteXmlFileData data) {
Expand All @@ -144,13 +133,30 @@ private void removeAllWithVendor(LatteXmlFileData.VendorResult vendorResult) {
functions.entrySet().removeIf(entry -> entry.getValue().getVendorName().equals(vendorResult.vendorName));
}

public void reinitialize() {
public boolean reinitialize() {
int oldHash = hashCode();

tags = new HashMap<>();
filters = new HashMap<>();
variables = new HashMap<>();
functions = new HashMap<>();
vendors = new ArrayList<>();
initialize();

return oldHash == hashCode();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LatteFileConfiguration that = (LatteFileConfiguration) o;
return Objects.equals(tags, that.tags) && Objects.equals(filters, that.filters) && Objects.equals(variables, that.variables) && Objects.equals(functions, that.functions) && Objects.equals(vendors, that.vendors);
}

@Override
public int hashCode() {
return Objects.hash(tags, filters, variables, functions, vendors);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.jantvrdik.intellij.latte.config;

import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.util.FileContentUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class LatteReparseUtil {
final private static int WAIT_MILIS_BETWEEN_REPARSE = 5000;

private static final Map<Project, LatteReparseUtil> instances = new HashMap<>();

private long lastCall = 0;

private final @NotNull Project project;

public LatteReparseUtil(@NotNull Project project) {
this.project = project;
}

public static LatteReparseUtil getInstance(@NotNull Project project) {
if (!instances.containsKey(project)) {
instances.put(project, new LatteReparseUtil(project));
}
return instances.get(project);
}

public static void reinitialize(List<Project> projects) {
for (Project project : projects) {
LatteReparseUtil.getInstance(project).reinitialize();
}
}

public void reinitialize() {
//LatteFileConfiguration.getInstance(project).reinitialize();
reparseOpenedFiles(project, null, false);
}

public void reinitialize(Runnable runnable) {
//if (LatteFileConfiguration.getInstance(project).reinitialize()) {
reparseOpenedFiles(project, runnable, false);
//}
}

public void reinitializeDefault() {
//if (LatteDefaultConfiguration.getInstance(project).reinitialize()) {
reparseOpenedFiles(project, null, true);
//}
}

private void reparseOpenedFiles(@NotNull Project project, @Nullable Runnable runnable, boolean def) {
/*if ((lastCall + WAIT_MILIS_BETWEEN_REPARSE) > System.currentTimeMillis()) {
run(runnable);
return;
}*/

DumbService.getInstance(project).runWhenSmart(() -> {
if (def && !LatteDefaultConfiguration.getInstance(project).reinitialize()) {
return;
}

if (!def && !LatteFileConfiguration.getInstance(project).reinitialize()) {
return;
}

if ((lastCall + WAIT_MILIS_BETWEEN_REPARSE) > System.currentTimeMillis()) {
run(runnable);
return;
}

FileContentUtil.reparseOpenedFiles();
lastCall = System.currentTimeMillis();
run(runnable);
});
}

private void run(@Nullable Runnable runnable) {
if (runnable != null) {
runnable.run();
}
}
}
Loading

0 comments on commit d569b5d

Please sign in to comment.