Always take indent settings from underlying language for any python template, so Django, Jinja2, mako and others use HTML indents. (see PY-15974)

This commit is contained in:
Ilya.Kazakevich
2015-05-23 01:34:10 +03:00
parent 9124f312fd
commit d8b367bec6
2 changed files with 83 additions and 4 deletions
@@ -17,7 +17,11 @@ package com.jetbrains.python.templateLanguages;
import com.intellij.execution.ExecutionException;
import com.intellij.facet.ui.ValidationResult;
import com.intellij.lang.Language;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider;
import com.jetbrains.python.packaging.PyPackage;
import com.jetbrains.python.packaging.PyPackageManager;
import org.jetbrains.annotations.NonNls;
@@ -25,10 +29,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PyTemplatesUtil {
private PyTemplatesUtil(){}
private PyTemplatesUtil() {
}
public static ValidationResult checkInstalled(@Nullable final Sdk sdk, @NotNull final TemplateLanguagePanel templatesPanel,
@NotNull final String prefix) {
@NotNull final String prefix) {
if (sdk == null) return ValidationResult.OK;
String templateBinding = null;
@NonNls String language = templatesPanel.getTemplateLanguage();
@@ -41,8 +46,9 @@ public class PyTemplatesUtil {
if (TemplatesService.ALL_TEMPLATE_BINDINGS.contains(templateBinding)) {
try {
final PyPackage installedPackage = packageManager.findPackage(templateBinding, false);
if (installedPackage == null)
if (installedPackage == null) {
return new ValidationResult(templateBinding + " will be installed on selected interpreter");
}
}
catch (ExecutionException ignored) {
}
@@ -61,5 +67,28 @@ public class PyTemplatesUtil {
return null;
}
/**
* Fetches template data language if file has {@link TemplateLanguageFileViewProvider}
*
* @param psiElement element to get lang for
* @param expectedProvider only fetch language if provider has certain type. Pass null for any type.
* @return template data language
*/
@Nullable
public static Language getTemplateDataLanguage(@Nullable final PsiElement psiElement,
@Nullable final Class<? extends TemplateLanguageFileViewProvider> expectedProvider) {
if (psiElement == null) {
return null;
}
final FileViewProvider provider = psiElement.getContainingFile().getViewProvider();
if (provider instanceof TemplateLanguageFileViewProvider) {
if (expectedProvider == null || expectedProvider.isInstance(provider)) {
return (((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage());
}
}
return psiElement.getLanguage();
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.templateLanguages;
import com.intellij.lang.Language;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings.IndentOptions;
import com.intellij.psi.codeStyle.FileIndentOptionsProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Injects indents for Python templates.
* In Python we have template langs, but we should use indent from underlying language (like html)
* because templ. language never works standalone: it is always emebedded in some language
*
* @author Ilya.Kazakevich
*/
public class PythonTemplateIndentOptionsProvider extends FileIndentOptionsProvider {
@Nullable
@Override
public final IndentOptions getIndentOptions(@NotNull final CodeStyleSettings settings,
@NotNull final PsiFile file) {
final Language language = file.getLanguage();
if (!(language instanceof PythonTemplateLanguage)) {
return null; // We only care about python template files
}
// This template language has no settings, lets use parent language then
final Language templateDataLanguage = PyTemplatesUtil.getTemplateDataLanguage(file, null);
if (templateDataLanguage == null) {
return null; // No template data language
}
return settings.getIndentOptions(templateDataLanguage.getAssociatedFileType());
}
}