diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java index 74246e431908..a70f5ed1766f 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java @@ -477,9 +477,10 @@ public class EnterHandler extends BaseEnterHandler { } private int adjustLineIndent(CharSequence docChars) { + Language language = getLanguage(myDataContext); int indentStart = CharArrayUtil.shiftBackwardUntil(docChars, myOffset - 1, "\n") + 1; int indentEnd = CharArrayUtil.shiftForward(docChars, indentStart, " \t"); - String newIndent = CodeStyleFacade.getInstance(getProject()).getLineIndent(myEditor, myOffset); + String newIndent = CodeStyleFacade.getInstance(getProject()).getLineIndent(myEditor, language, myOffset); if (newIndent == null) return myOffset; int delta = newIndent.length() - (indentEnd - indentStart); myDocument.replaceString(indentStart, indentEnd, newIndent); diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java index 796a3a6967f8..8473af9a7196 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/CodeStyleFacadeImpl.java @@ -59,13 +59,10 @@ public class CodeStyleFacadeImpl extends CodeStyleFacade { } @Override - public String getLineIndent(@NotNull Editor editor, int offset) { + public String getLineIndent(@NotNull Editor editor, @Nullable Language language, int offset) { if (myProject == null) return null; - Document document = editor.getDocument(); - PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject); - PsiFile file = documentManager.getPsiFile(document); - LineIndentProvider lineIndentProvider = LineIndentProviderEP.findLineIndentProvider(file); - return lineIndentProvider != null ? lineIndentProvider.getLineIndent(myProject, editor, offset) : null; + LineIndentProvider lineIndentProvider = LineIndentProviderEP.findLineIndentProvider(language); + return lineIndentProvider != null ? lineIndentProvider.getLineIndent(myProject, editor, language, offset) : null; } @Override diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/FormatterBasedLineIndentProvider.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/FormatterBasedLineIndentProvider.java index 99180ad51aeb..7de5a996cddd 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/FormatterBasedLineIndentProvider.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/FormatterBasedLineIndentProvider.java @@ -15,11 +15,11 @@ */ package com.intellij.psi.impl.source.codeStyle; +import com.intellij.lang.Language; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -30,14 +30,14 @@ import org.jetbrains.annotations.Nullable; public class FormatterBasedLineIndentProvider implements LineIndentProvider { @Nullable @Override - public String getLineIndent(@NotNull Project project, @NotNull Editor editor, int offset) { + public String getLineIndent(@NotNull Project project, @NotNull Editor editor, Language language, int offset) { Document document = editor.getDocument(); PsiDocumentManager.getInstance(project).commitDocument(document); return CodeStyleManager.getInstance(project).getLineIndent(document, offset); } @Override - public boolean isSuitableFor(@Nullable PsiFile file) { + public boolean isSuitableFor(@Nullable Language language) { return true; } } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java index 541468806e50..f457a3af317a 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/JavaLikeLangLineIndentProvider.java @@ -50,23 +50,13 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI @Nullable @Override - public String getLineIndent(@NotNull Project project, @NotNull Editor editor, int offset) { + public String getLineIndent(@NotNull Project project, @NotNull Editor editor, Language language, int offset) { Type indent = getIndent(editor, offset); if (indent != null){ return getIndentString(editor, offset, indent); } - return super.getLineIndent(project, editor, offset); + return super.getLineIndent(project, editor, language, offset); } - - @Override - public final boolean isSuitableFor(@Nullable PsiFile file) { - if (file != null) { - return isSuitableForLanguage(file.getLanguage()); - } - return false; - } - - protected abstract boolean isSuitableForLanguage(@NotNull Language language); @Nullable protected Type getIndent(@NotNull Editor editor, int offset) { diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProvider.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProvider.java index 41d56c172ce3..78b5dcce0554 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProvider.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProvider.java @@ -15,9 +15,9 @@ */ package com.intellij.psi.impl.source.codeStyle; +import com.intellij.lang.Language; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -32,12 +32,13 @@ public interface LineIndentProvider { * * @param project The current project. * @param editor The current editor. + * @param language Context language to be used at the current offset. * @param offset The caret offset in the editor. * @return The indent string (possibly consisting of tabs and/or white spaces), or null if * LineIndentProvider can't calculate the indent (in this case no indent adjustment will be made). */ @Nullable - String getLineIndent(@NotNull Project project, @NotNull Editor editor, int offset); + String getLineIndent(@NotNull Project project, @NotNull Editor editor, Language language, int offset); - boolean isSuitableFor(@Nullable PsiFile file); + boolean isSuitableFor(@Nullable Language language); } diff --git a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProviderEP.java b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProviderEP.java index 646b98526753..d1f2c9f34a0f 100644 --- a/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProviderEP.java +++ b/platform/lang-impl/src/com/intellij/psi/impl/source/codeStyle/LineIndentProviderEP.java @@ -15,8 +15,8 @@ */ package com.intellij.psi.impl.source.codeStyle; +import com.intellij.lang.Language; import com.intellij.openapi.extensions.ExtensionPointName; -import com.intellij.psi.PsiFile; import org.jetbrains.annotations.Nullable; /** @@ -26,10 +26,10 @@ public class LineIndentProviderEP { public final static ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.lineIndentProvider"); @Nullable - public static LineIndentProvider findLineIndentProvider(@Nullable PsiFile psiFile) { + public static LineIndentProvider findLineIndentProvider(@Nullable Language language) { LineIndentProvider foundProvider = null; for (LineIndentProvider provider : EP_NAME.getExtensions()) { - if (foundProvider == null || provider.isSuitableFor(psiFile) && foundProvider.getClass().isInstance(provider)) { + if (foundProvider == null || provider.isSuitableFor(language) && foundProvider.getClass().isInstance(provider)) { foundProvider = provider; } } diff --git a/platform/platform-api/src/com/intellij/codeStyle/CodeStyleFacade.java b/platform/platform-api/src/com/intellij/codeStyle/CodeStyleFacade.java index eb8b9e3c40e3..eeb4446b6a72 100644 --- a/platform/platform-api/src/com/intellij/codeStyle/CodeStyleFacade.java +++ b/platform/platform-api/src/com/intellij/codeStyle/CodeStyleFacade.java @@ -47,7 +47,7 @@ public abstract class CodeStyleFacade { * @param offset the caret offset in the editor. * @return the indent string (containing of tabs and/or white spaces), or null if it * was not possible to calculate the indent. - * @deprecated Use {@link #getLineIndent(Editor, int)} instead. + * @deprecated Use {@link #getLineIndent(Editor, Language, int)} instead. */ @Nullable @Deprecated @@ -57,12 +57,13 @@ public abstract class CodeStyleFacade { * Calculates the indent that should be used for the line at specified offset in the specified * editor. * - * @param editor the editor for which the indent must be returned. - * @param offset the caret offset in the editor. + * @param editor The editor for which the indent must be returned. + * @param language Context language + * @param offset The caret offset in the editor. * @return the indent string (containing of tabs and/or white spaces), or null if it * was not possible to calculate the indent. */ - public String getLineIndent(@NotNull Editor editor, int offset) { + public String getLineIndent(@NotNull Editor editor, @Nullable Language language, int offset) { //noinspection deprecation return getLineIndent(editor.getDocument(), offset); }