From d8b367bec6bda839ea87b0458606a9f613b297bd Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Sat, 23 May 2015 01:34:10 +0300 Subject: [PATCH] Always take indent settings from underlying language for any python template, so Django, Jinja2, mako and others use HTML indents. (see PY-15974) --- .../templateLanguages/PyTemplatesUtil.java | 37 ++++++++++++-- .../PythonTemplateIndentOptionsProvider.java | 50 +++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 python/openapi/src/com/jetbrains/python/templateLanguages/PythonTemplateIndentOptionsProvider.java diff --git a/python/openapi/src/com/jetbrains/python/templateLanguages/PyTemplatesUtil.java b/python/openapi/src/com/jetbrains/python/templateLanguages/PyTemplatesUtil.java index 9ebdde38dfbb..d5420210ccc0 100644 --- a/python/openapi/src/com/jetbrains/python/templateLanguages/PyTemplatesUtil.java +++ b/python/openapi/src/com/jetbrains/python/templateLanguages/PyTemplatesUtil.java @@ -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 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(); + } } diff --git a/python/openapi/src/com/jetbrains/python/templateLanguages/PythonTemplateIndentOptionsProvider.java b/python/openapi/src/com/jetbrains/python/templateLanguages/PythonTemplateIndentOptionsProvider.java new file mode 100644 index 000000000000..cb8663544b77 --- /dev/null +++ b/python/openapi/src/com/jetbrains/python/templateLanguages/PythonTemplateIndentOptionsProvider.java @@ -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()); + } +}