Skip to content

Commit

Permalink
Merge pull request #154 from nette-intellij/mn-bug-fixes-02
Browse files Browse the repository at this point in the history
Fixed #135, #136, #145 and #149, fixed class completion, fixed /} for…
  • Loading branch information
mesour authored Feb 26, 2021
2 parents b5343f1 + 0c84a72 commit 58b5059
Show file tree
Hide file tree
Showing 23 changed files with 359 additions and 66 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.1'
version '1.1.2-RC1'
group 'com.jantvrdik.intellij.latte'

jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;

public class PhpClassInsertHandler extends PhpReferenceInsertHandler {
Expand All @@ -21,28 +22,34 @@ public PhpClassInsertHandler() {
}

public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
// for removing first `\` (because class completion is triggered if prev element is `\` and PHP completion adding `\` before)
super.handleInsert(context, lookupElement);

PsiElement prev = context.getFile().findElementAt(context.getStartOffset() - 1);
PsiElement element = context.getFile().findElementAt(context.getStartOffset());
String prevText = prev != null ? prev.getText() : null;
String text = element != null ? element.getText() : null;
if (prevText == null || text == null || (prevText.startsWith("\\") && !text.startsWith("\\"))) {
return;
}
LattePhpClassUsage classUsage = element.getParent() instanceof LattePhpClassUsage ? (LattePhpClassUsage) element.getParent() : null;
String[] className = (classUsage != null ? classUsage.getClassName() : "").split("\\\\");

if ((prevText.length() > 0 || className.length > 1) && element.getNode().getElementType() == LatteTypes.T_PHP_NAMESPACE_RESOLUTION) {
Editor editor = context.getEditor();
CaretModel caretModel = editor.getCaretModel();
int offset = caretModel.getOffset();
caretModel.moveToOffset(element.getTextOffset());
editor.getSelectionModel().setSelection(element.getTextOffset(), element.getTextOffset() + 1);
EditorModificationUtil.deleteSelectedText(editor);
caretModel.moveToOffset(offset - 1);
PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());
final Object object = lookupElement.getObject();
final String classNamespace = object instanceof PhpClass ? ((PhpClass) object).getNamespaceName() : "";

if (!classNamespace.isEmpty()) {
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);
int index = current.lastIndexOf("\\");
String existingNamespace = "";
if (index > 0 && current.length() >= index) {
existingNamespace = current.substring(0, index) + "\\";
}

String fqn = classNamespace;
if (!classNamespace.equals("\\") && !existingNamespace.startsWith("\\") && fqn.startsWith("\\")) {
fqn = fqn.substring(1);
} else if (classNamespace.equals("\\") && existingNamespace.length() == 0) {
fqn = "\\";
}

if (existingNamespace.length() > 0 && fqn.contains(existingNamespace)) {
fqn = fqn.replace(existingNamespace, "");
}

context.getDocument().insertString(context.getStartOffset(), fqn);
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,28 @@ public JBColor getColor() {
}
}

private boolean checkDumbMode;

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

@NotNull
private Project project;
private final Project project;

@Nullable
private final Collection<LatteXmlFileData> xmlFileData;

public LatteConfiguration(@NotNull Project project) {
public LatteConfiguration(@NotNull Project project, @Nullable Collection<LatteXmlFileData> xmlFileData) {
this.project = project;
this.xmlFileData = xmlFileData;
}

public static LatteConfiguration getInstance(@NotNull Project project) {
return getInstance(project, null);
}

public static LatteConfiguration getInstance(@NotNull Project project, @Nullable Collection<LatteXmlFileData> xmlFileData) {
if (!instances.containsKey(project)) {
instances.put(project, new LatteConfiguration(project));
instances.put(project, new LatteConfiguration(project, xmlFileData));
}
return instances.get(project);
}
Expand Down Expand Up @@ -106,6 +116,7 @@ public LatteVariableSettings getVariable(String name) {
return null;
}

@NotNull
private LatteSettings getSettings() {
return LatteSettings.getInstance(project);
}
Expand All @@ -125,15 +136,15 @@ public Map<String, LatteVariableSettings> getVariables(boolean enableCustom) {
}
}

for (LatteVariableSettings variableSetting : LatteFileConfiguration.getInstance(project).getVariables().values()) {
for (LatteVariableSettings variableSetting : getFileConfiguration().getVariables().values()) {
if (!variableSettings.containsKey(variableSetting.getVarName())) {
variableSettings.put(variableSetting.getVarName(), variableSetting);
}
}

for (Vendor vendor : LatteDefaultConfiguration.getInstance(project).getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !LatteFileConfiguration.getInstance(project).hasVendor(vendor)) {
for (LatteVariableSettings variableSetting : LatteDefaultConfiguration.getInstance(project).getVariables(vendor).values()) {
for (Vendor vendor : getDefaultConfiguration().getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !getFileConfiguration().hasVendor(vendor)) {
for (LatteVariableSettings variableSetting : getDefaultConfiguration().getVariables(vendor).values()) {
if (!variableSettings.containsKey(variableSetting.getVarName())) {
variableSettings.put(variableSetting.getVarName(), variableSetting);
}
Expand All @@ -159,15 +170,15 @@ private Map<String, LatteFunctionSettings> getFunctions(boolean enableCustom) {
}
}

for (LatteFunctionSettings functionSetting : LatteFileConfiguration.getInstance(project).getFunctions().values()) {
for (LatteFunctionSettings functionSetting : getFileConfiguration().getFunctions().values()) {
if (!functionSettings.containsKey(functionSetting.getFunctionName())) {
functionSettings.put(functionSetting.getFunctionName(), functionSetting);
}
}

for (Vendor vendor : LatteDefaultConfiguration.getInstance(project).getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !LatteFileConfiguration.getInstance(project).hasVendor(vendor)) {
for (LatteFunctionSettings functionSetting : LatteDefaultConfiguration.getInstance(project).getFunctions(vendor).values()) {
for (Vendor vendor : getDefaultConfiguration().getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !getFileConfiguration().hasVendor(vendor)) {
for (LatteFunctionSettings functionSetting : getDefaultConfiguration().getFunctions(vendor).values()) {
if (!functionSettings.containsKey(functionSetting.getFunctionName())) {
functionSettings.put(functionSetting.getFunctionName(), functionSetting);
}
Expand All @@ -193,15 +204,15 @@ private Map<String, LatteTagSettings> getTags(boolean enableCustom) {
}
}

for (LatteTagSettings tagSetting : LatteFileConfiguration.getInstance(project).getTags().values()) {
for (LatteTagSettings tagSetting : getFileConfiguration().getTags().values()) {
if (!projectTags.containsKey(tagSetting.getMacroName())) {
projectTags.put(tagSetting.getMacroName(), tagSetting);
}
}

for (Vendor vendor : LatteDefaultConfiguration.getInstance(project).getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !LatteFileConfiguration.getInstance(project).hasVendor(vendor)) {
for (LatteTagSettings tagSetting : LatteDefaultConfiguration.getInstance(project).getTags(vendor).values()) {
for (Vendor vendor : getDefaultConfiguration().getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !getFileConfiguration().hasVendor(vendor)) {
for (LatteTagSettings tagSetting : getDefaultConfiguration().getTags(vendor).values()) {
if (!projectTags.containsKey(tagSetting.getMacroName())) {
projectTags.put(tagSetting.getMacroName(), tagSetting);
}
Expand All @@ -226,15 +237,15 @@ private Map<String, LatteFilterSettings> getFilters(boolean enableCustom) {
}
}

for (LatteFilterSettings filterSetting : LatteFileConfiguration.getInstance(project).getFilters().values()) {
for (LatteFilterSettings filterSetting : getFileConfiguration().getFilters().values()) {
if (!projectFilters.containsKey(filterSetting.getModifierName())) {
projectFilters.put(filterSetting.getModifierName(), filterSetting);
}
}

for (Vendor vendor : LatteDefaultConfiguration.getInstance(project).getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !LatteFileConfiguration.getInstance(project).hasVendor(vendor)) {
for (LatteFilterSettings filterSetting : LatteDefaultConfiguration.getInstance(project).getFilters(vendor).values()) {
for (Vendor vendor : getDefaultConfiguration().getVendors()) {
if (settings.isEnabledSourceVendor(vendor) && !getFileConfiguration().hasVendor(vendor)) {
for (LatteFilterSettings filterSetting : getDefaultConfiguration().getFilters(vendor).values()) {
if (!projectFilters.containsKey(filterSetting.getModifierName())) {
projectFilters.put(filterSetting.getModifierName(), filterSetting);
}
Expand Down Expand Up @@ -271,6 +282,14 @@ private LatteXmlFileData.VendorResult getVendorForSettings(BaseLatteSettings set
return LatteXmlFileData.VendorResult.CUSTOM;
}

private LatteFileConfiguration getFileConfiguration() {
return LatteFileConfiguration.getInstance(project, xmlFileData);
}

private LatteDefaultConfiguration getDefaultConfiguration() {
return LatteDefaultConfiguration.getInstance(project, xmlFileData != null);
}

public static boolean isValidVendor(String type) {

for (Vendor c : Vendor.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ public class LatteDefaultConfiguration {
@NotNull
private final Project project;

private final boolean disableLoading;

@Nullable
private Notification notification = null;

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

public void reinitialize() {
xmlData = new HashMap<>();
if (disableLoading) {
return;
}

LatteIdeHelper.saveFileToProjectTemp(project, "xmlSources/Latte.dtd");
for (String sourceFile : sourceFiles.keySet()) {
Path path = LatteIdeHelper.saveFileToProjectTemp(project, "xmlSources/" + sourceFile);
Expand All @@ -63,8 +70,12 @@ public void reinitialize() {
}

public static LatteDefaultConfiguration getInstance(@NotNull Project project) {
return getInstance(project, false);
}

public static LatteDefaultConfiguration getInstance(@NotNull Project project, boolean disableLoading) {
if (!instances.containsKey(project)) {
instances.put(project, new LatteDefaultConfiguration(project));
instances.put(project, new LatteDefaultConfiguration(project, disableLoading));
}
return instances.get(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.intellij.notification.Notification;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.PomTarget;
Expand Down Expand Up @@ -44,18 +46,28 @@ public class LatteFileConfiguration implements Serializable {

private List<LatteConfiguration.Vendor> vendors = new ArrayList<>();

private Project project;
final private Project project;

final private Collection<LatteXmlFileData> xmlFileData;

private Notification notification = null;

public LatteFileConfiguration(Project project) {
public LatteFileConfiguration(final Project project, final @Nullable Collection<LatteXmlFileData> xmlFileData) {
this.project = project;
this.xmlFileData = xmlFileData;
initialize();
}

public static LatteFileConfiguration getInstance(Project project) {
public static LatteFileConfiguration getInstance(final @NotNull Project project) {
return getInstance(project, null);
}

public static LatteFileConfiguration getInstance(
final @NotNull Project project,
final @Nullable Collection<LatteXmlFileData> xmlFileData
) {
if (!instances.containsKey(project)) {
instances.put(project, new LatteFileConfiguration(project));
instances.put(project, new LatteFileConfiguration(project, xmlFileData));
}
return instances.get(project);
}
Expand All @@ -65,26 +77,45 @@ private void initialize() {
return;
}

if (ActionUtil.isDumbMode(project)) {
if (xmlFileData != null) {
initializeFromXmlData(xmlFileData);
} else {
initializeFromFileIndex();
}
}

private void initializeFromXmlData(@NotNull Collection<LatteXmlFileData> xmlData) {
for (LatteXmlFileData data : xmlData) {
if (data == null) {
continue;
}
applyXmlData(data);
}
}

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

Collection<String> val = FileBasedIndex.getInstance().getAllKeys(LatteIndexExtension.KEY, project);
for (String key : val) {
Collection<LatteXmlFileData> xmlFileData = FileBasedIndex.getInstance().getValues(
LatteIndexExtension.KEY,
key,
GlobalSearchScope.allScope(project)
);
for (LatteXmlFileData data : xmlFileData) {
if (data == null) {
continue;
try {
Collection<String> val = FileBasedIndex.getInstance().getAllKeys(LatteIndexExtension.KEY, project);
for (String key : val) {
Collection<LatteXmlFileData> xmlData = FileBasedIndex.getInstance().getValues(
LatteIndexExtension.KEY,
key,
GlobalSearchScope.allScope(project)
);
if (xmlData != null) {
initializeFromXmlData(xmlData);
}
applyXmlData(data);
}

} catch (IndexNotReadyException e) {
notification = LatteReparseFilesUtil.notifyReparseFiles(project);
}
//PsiFile[] files = FilenameIndex.getFilesByName(project, FILE_NAME, GlobalSearchScope.allScope(project));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jantvrdik.intellij.latte.indexes;

import com.intellij.extapi.psi.StubBasedPsiElementBase;
import com.intellij.lang.ASTNode;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement;
import org.jetbrains.annotations.NotNull;

public class LatteStubBasedPsiElement<T extends StubElement> extends StubBasedPsiElementBase<T> {
public LatteStubBasedPsiElement(@NotNull T stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType);
}

public LatteStubBasedPsiElement(@NotNull ASTNode node) {
super(node);
}

@Override
public String toString() {
return this.getClass().getSimpleName() + "(" + getNode().getElementType().toString() + ")('" + getNode().getText() + "')";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ root ::= (structureToken)* autoClosedBlock?
private
//macro ::= macroComment | macroClassic
macro ::= macroComment | (
<<checkPairMacro true>> (pairMacro | emptyMacro)
<<checkPairMacro true>> (<<checkEmptyMacro>> emptyMacro | pairMacro)
| <<checkPairMacro false>> unpairedMacro
)

Expand Down
Loading

0 comments on commit 58b5059

Please sign in to comment.