Skip to content

Commit

Permalink
Merge pull request #50 from nette-intellij/mn-code-style-and-improvem…
Browse files Browse the repository at this point in the history
…ent-usage

Code style settings and improvements for usage and performance
  • Loading branch information
mesour authored Mar 3, 2020
2 parents 86b2631 + 086f771 commit fb0b238
Show file tree
Hide file tree
Showing 28 changed files with 501 additions and 201 deletions.
Binary file modified latte-plugin.jar
Binary file not shown.
26 changes: 22 additions & 4 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>com.jantvrdik.intellij.latte</id>
<name>Latte</name>
<version>1.0.1</version>
<version>1.0.2</version>
<vendor>Jan Tvrdík</vendor>

<description><![CDATA[
Expand All @@ -18,10 +18,25 @@
]]></description>

<change-notes><![CDATA[
<p>1.0.2</p>
<ul>
<li>added code style settings (settings for use tabs or spaces) (issue #46, #30)</li>
<li>added support for define variable in tag {default $foo = 123}</li>
<li>added class namespaces completion</li>
<li>added support for define variables by {php [$foo, $bar] = $arr}</li>
<li>implemented some performance improvements</li>
<li>fixed resolving methods if a type is more classes FooClass|BarClass</li>
<li>fixed completion for unpaired macro after if missing end }</li>
<li>fixed filters on variables (issue #47)</li>
<li>fixed wrong indent for elseifset (issue #42)</li>
<li>fixed multiple variable definitions in for/foreach cycles</li>
<li>fixed auto-complete (disabled in strings, more improvements)</li>
<li>increased minimum compatibility to 2018.3</li>
</ul>
<p>1.0.1</p>
<ul>
<li>Fixed compatibility issues</li>
<li>Fixed structure view (added n:tags and images)</li>
<li>fixed compatibility issues</li>
<li>fixed structure view (added n:tags and images)</li>
</ul>
<p>1.0.0</p>
<ul>
Expand Down Expand Up @@ -141,7 +156,7 @@
</change-notes>

<!-- http://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges -->
<idea-version since-build="173.2099.1"/>
<idea-version since-build="183.3283.2"/>

<!-- http://confluence.jetbrains.com/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products -->
<depends>com.intellij.modules.lang</depends>
Expand Down Expand Up @@ -174,6 +189,9 @@
<renamePsiElementProcessor implementation="com.jantvrdik.intellij.latte.refactoring.LatteRenamePsiElementProcessor"/>
<lang.documentationProvider language="Latte" implementationClass="com.jantvrdik.intellij.latte.documentation.LatteDocumentationProvider"/>

<codeStyleSettingsProvider implementation="com.jantvrdik.intellij.latte.codeStyle.LatteCodeStyleSettingsProvider"/>
<langCodeStyleSettingsProvider implementation="com.jantvrdik.intellij.latte.codeStyle.LatteLanguageCodeStyleSettingsProvider"/>

<psi.referenceContributor implementation="com.jantvrdik.intellij.latte.reference.LatteReferenceContributor"/>

<projectConfigurable instance="com.jantvrdik.intellij.latte.ui.LatteSettingsForm"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jantvrdik.intellij.latte.codeStyle;

import com.intellij.psi.codeStyle.*;

public class LatteCodeStyleSettings extends CustomCodeStyleSettings {
public LatteCodeStyleSettings(CodeStyleSettings settings) {
super("LatteCodeStyleSettings", settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.jantvrdik.intellij.latte.codeStyle;

import com.intellij.application.options.*;
import com.intellij.psi.codeStyle.*;
import com.jantvrdik.intellij.latte.LatteLanguage;
import org.jetbrains.annotations.*;

public class LatteCodeStyleSettingsProvider extends CodeStyleSettingsProvider {
@Override
public CustomCodeStyleSettings createCustomSettings(CodeStyleSettings settings) {
return new LatteCodeStyleSettings(settings);
}

@Nullable
@Override
public String getConfigurableDisplayName() {
return "Latte";
}

@NotNull
public CodeStyleConfigurable createConfigurable(@NotNull CodeStyleSettings settings, @NotNull CodeStyleSettings modelSettings) {
return new CodeStyleAbstractConfigurable(settings, modelSettings, this.getConfigurableDisplayName()) {
@Override
protected CodeStyleAbstractPanel createPanel(CodeStyleSettings settings) {
return new LatteCodeStyleMainPanel(getCurrentSettings(), settings);
}
};
}

private static class LatteCodeStyleMainPanel extends TabbedLanguageCodeStylePanel {
public LatteCodeStyleMainPanel(CodeStyleSettings currentSettings, CodeStyleSettings settings) {
super(LatteLanguage.INSTANCE, currentSettings, settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.jantvrdik.intellij.latte.codeStyle;

import com.intellij.application.options.IndentOptionsEditor;
import com.jantvrdik.intellij.latte.LatteLanguage;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;

import com.intellij.lang.Language;
import com.intellij.psi.codeStyle.*;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.Set;

public class LatteLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettingsProvider {
@NotNull
@Override
public Language getLanguage() {
return LatteLanguage.INSTANCE;
}

@Override
public void customizeSettings(@NotNull CodeStyleSettingsCustomizable consumer, @NotNull SettingsType settingsType) {
if (settingsType == SettingsType.SPACING_SETTINGS) {
consumer.showStandardOptions("SPACE_AROUND_ASSIGNMENT_OPERATORS");
consumer.renameStandardOption("SPACE_AROUND_ASSIGNMENT_OPERATORS", "Separator");
} else if (settingsType == SettingsType.BLANK_LINES_SETTINGS) {
consumer.showStandardOptions("KEEP_BLANK_LINES_IN_CODE");
} else if (settingsType == SettingsType.INDENT_SETTINGS) {
consumer.showStandardOptions("USE_TAB_CHARACTER");
consumer.showStandardOptions("INDENT_SIZE");
consumer.showStandardOptions("TAB_SIZE");
}
}

@Override
public Set<String> getSupportedFields(SettingsType type) {
if (type == SettingsType.BLANK_LINES_SETTINGS) {
return Collections.emptySet();
}
return super.getSupportedFields(type);
}

@Override
public @Nullable IndentOptionsEditor getIndentOptionsEditor() {
return new IndentOptionsEditor();
}

@Override
public String getCodeSample(@NotNull SettingsType settingsType) {
return "{contentType text/html}\n" +
"{* comment *}\n" +
"{var $string = \"abc\", $number = 123}\n\n" +
"{foreach $data as $key => $value}\n" +
" {$key} {$value}\n" +
"{/foreach}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.jantvrdik.intellij.latte.completion.handlers.AttrMacroInsertHandler;
import com.jantvrdik.intellij.latte.completion.handlers.MacroInsertHandler;
import com.jantvrdik.intellij.latte.completion.providers.LattePhpCompletionProvider;
import com.jantvrdik.intellij.latte.completion.providers.LatteVariableCompletionProvider;
import com.jantvrdik.intellij.latte.config.LatteConfiguration;
import com.jantvrdik.intellij.latte.config.LatteMacro;
import com.jantvrdik.intellij.latte.config.LatteModifier;
Expand Down Expand Up @@ -87,9 +86,11 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
return;
}

LatteMacro macro = LatteConfiguration.INSTANCE.getMacro(element.getProject(), macroClassic.getOpenTag().getMacroName());
if (macro == null || !macro.allowedModifiers) {
return;
if (!((LatteMacroModifier) element).isVariableModifier()) {
LatteMacro macro = LatteConfiguration.INSTANCE.getMacro(element.getProject(), macroClassic.getOpenTag().getMacroName());
if (macro == null || !macro.allowedModifiers) {
return;
}
}

Map<String, LatteModifier> customModifiers = LatteConfiguration.INSTANCE.getCustomModifiers(element.getProject());
Expand All @@ -98,12 +99,6 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
}
});

extend(
CompletionType.BASIC,
PlatformPatterns.psiElement(LatteTypes.T_MACRO_ARGS_VAR).withLanguage(LatteLanguage.INSTANCE),
new LatteVariableCompletionProvider()
);

extend(
CompletionType.BASIC,
PlatformPatterns.psiElement().withLanguage(LatteLanguage.INSTANCE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,26 @@ public void handleInsert(@NotNull InsertionContext context, @NotNull LookupEleme
spaceInserted = 1;
}

if (isCloseTag || resolvePairMacro) {
int lastBraceOffset = text.indexOf("}", offset);
int endOfLineOffset = text.indexOf("\n", offset);
int lastBraceOffset = text.indexOf("}", offset);
int endOfLineOffset = text.indexOf("\n", offset);

if (endOfLineOffset == -1) {
endOfLineOffset = text.length();
}
if (lastBraceOffset == -1 || lastBraceOffset > endOfLineOffset) {
caretModel.moveToOffset(endOfLineOffset + spaceInserted);
EditorModificationUtil.insertStringAtCaret(editor, "}");
lastBraceOffset = endOfLineOffset;
endOfLineOffset++;
}
if (endOfLineOffset == -1) {
endOfLineOffset = text.length();
}
if (lastBraceOffset == -1 || lastBraceOffset > endOfLineOffset) {
caretModel.moveToOffset(endOfLineOffset + spaceInserted);
EditorModificationUtil.insertStringAtCaret(editor, "}");
lastBraceOffset = endOfLineOffset;
endOfLineOffset++;
}

if (resolvePairMacro) {
String endTag = "{/" + macroName + "}";
if (resolvePairMacro) {
String endTag = "{/" + macroName + "}";

int endTagOffset = text.indexOf(endTag, offset);
if (endTagOffset == -1 || endTagOffset > endOfLineOffset) {
caretModel.moveToOffset(lastBraceOffset + spaceInserted + 1);
EditorModificationUtil.insertStringAtCaret(editor, endTag);
}
int endTagOffset = text.indexOf(endTag, offset);
if (endTagOffset == -1 || endTagOffset > endOfLineOffset) {
caretModel.moveToOffset(lastBraceOffset + spaceInserted + 1);
EditorModificationUtil.insertStringAtCaret(editor, endTag);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public class LattePhpCompletionProvider extends BaseLatteCompletionProvider {

private final LattePhpFunctionCompletionProvider functionCompletionProvider;
private final LattePhpClassCompletionProvider classCompletionProvider;
private final LattePhpNamespaceCompletionProvider namespaceCompletionProvider;
private final LatteVariableCompletionProvider variableCompletionProvider;

public LattePhpCompletionProvider() {
super();
functionCompletionProvider = new LattePhpFunctionCompletionProvider();
classCompletionProvider = new LattePhpClassCompletionProvider();
namespaceCompletionProvider = new LattePhpNamespaceCompletionProvider();
variableCompletionProvider = new LatteVariableCompletionProvider();
}

@Override
Expand All @@ -49,13 +53,15 @@ protected void addCompletions(
} else if (element instanceof LattePhpProperty || (element instanceof LattePhpMethod && !((LattePhpMethod) element).isStatic())) {
attachPhpCompletions(result, (BaseLattePhpElement) element, false);

} else {
} else if (!(element instanceof LatteMacroModifier) && !(element instanceof LattePhpString)) {
classCompletionProvider.addCompletions(parameters, context, result);
namespaceCompletionProvider.addCompletions(parameters, context, result);

if (LatteUtil.matchParentMacroName(element, "varType") || LatteUtil.matchParentMacroName(element, "var")) {
attachVarTypes(result);
}

if (!(element instanceof LatteMacroModifier)) {
classCompletionProvider.addCompletions(parameters, context, result);
} else {
variableCompletionProvider.addCompletions(parameters, context, result);
functionCompletionProvider.addCompletions(parameters, context, result);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.jantvrdik.intellij.latte.completion.providers;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ProcessingContext;
import com.jantvrdik.intellij.latte.psi.LattePhpContent;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.completion.PhpCompletionUtil;
import com.jetbrains.php.completion.insert.PhpNamespaceInsertHandler;
import org.jetbrains.annotations.NotNull;


public class LattePhpNamespaceCompletionProvider extends CompletionProvider<CompletionParameters> {

public LattePhpNamespaceCompletionProvider() {
}

@Override
protected void addCompletions(@NotNull CompletionParameters params, ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement curr = params.getPosition().getOriginalElement();
if (PsiTreeUtil.getParentOfType(curr, LattePhpContent.class) == null) {
return;
}

PhpIndex phpIndex = PhpIndex.getInstance(curr.getProject());
String prefix = result.getPrefixMatcher().getPrefix();
String namespace = "";
if (prefix.contains("\\")) {
int index = prefix.lastIndexOf("\\");
namespace = prefix.substring(0, index) + "\\";
prefix = prefix.substring(index + 1);
}
PhpCompletionUtil.addSubNamespaces(namespace, result.withPrefixMatcher(prefix), phpIndex, PhpNamespaceInsertHandler.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void addCompletions(
@NotNull CompletionResultSet result
) {
PsiElement element = parameters.getPosition().getParent();
if (!(element instanceof LattePhpVariable) || ((LattePhpVariable) element).isDefinition()) {
if ((element instanceof LattePhpVariable) && ((LattePhpVariable) element).isDefinition()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/jantvrdik/intellij/latte/formatter/LatteBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected Indent getChildIndent(@NotNull ASTNode astNode) {
}
PsiElement el = astNode.getPsi();
LatteMacroTag openTag = ((LatteMacroClassic) el).getOpenTag();
if (openTag.getMacroName().equals("else") || openTag.getMacroName().equals("elseif")) {
if (openTag.matchMacroName("else") || openTag.matchMacroName("elseif") || openTag.matchMacroName("elseifset")) {
return Indent.getNoneIndent();
}
return Indent.getNormalIndent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final Inspe
file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LatteMacroTag && ((LatteMacroTag) element).getMacroName().equals("templateType")) {
if (element instanceof LatteMacroTag && ((LatteMacroTag) element).matchMacroName("templateType")) {
List<LatteMacroTag> allMacros = new ArrayList<LatteMacroTag>();
LatteUtil.findLatteMacroTemplateType(allMacros, (LatteFile) file);
if (allMacros.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final Inspe
file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LatteMacroTag && ((LatteMacroTag) element).getMacroName().equals("var")) {
if (element instanceof LatteMacroTag && ((LatteMacroTag) element).matchMacroName("var")) {
LatteMacroContent macroContent = PsiTreeUtil.findChildOfType(element, LatteMacroContent.class);
if (macroContent != null) {
List<LattePhpContent> phpContent = new ArrayList<>(macroContent.getPhpContentList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final Inspe
file.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LatteMacroTag && ((LatteMacroTag) element).getMacroName().equals("varType")) {
if (element instanceof LatteMacroTag && ((LatteMacroTag) element).matchMacroName("varType")) {
LatteMacroContent macroContent = PsiTreeUtil.findChildOfType(element, LatteMacroContent.class);
if (macroContent != null) {
List<LattePhpContent> phpContent = new ArrayList<>(macroContent.getPhpContentList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static void checkClassicMacro(
content.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof LatteMacroModifier) {
if (element instanceof LatteMacroModifier && !((LatteMacroModifier) element).isVariableModifier()) {
String description = "Modifiers are not allowed here";
ProblemDescriptor problem = manager.createProblemDescriptor(element, description, true, ProblemHighlightType.GENERIC_ERROR, isOnTheFly);
problems.add(problem);
Expand Down
Loading

0 comments on commit fb0b238

Please sign in to comment.