From fd2c00c6903d4e46e3afb4b735abd3492b9200de Mon Sep 17 00:00:00 2001 From: Rustam Vishnyakov Date: Thu, 19 May 2016 20:03:15 +0300 Subject: [PATCH] Changes for IDEA-CR-10721 --- .../editorActions/EnterHandler.java | 2 +- .../source/codeStyle/CodeStyleFacadeImpl.java | 5 ++-- .../FormatterBasedLineIndentProvider.java | 3 ++- .../JavaLikeLangLineIndentProvider.java | 27 ++++++++++--------- .../source/codeStyle/LineIndentProvider.java | 4 +-- .../intellij/codeStyle/CodeStyleFacade.java | 9 +++---- 6 files changed, 25 insertions(+), 25 deletions(-) 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 c3be0f6351d9..c1c145b394b6 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EnterHandler.java @@ -480,7 +480,7 @@ public class EnterHandler extends BaseEnterHandler { private int adjustLineIndent(CharSequence docChars) { int indentStart = CharArrayUtil.shiftBackwardUntil(docChars, myOffset - 1, "\n") + 1; int indentEnd = CharArrayUtil.shiftForward(docChars, indentStart, " \t"); - String newIndent = CodeStyleFacade.getInstance(getProject()).getLineIndent(myEditor, myDocument, myOffset); + String newIndent = CodeStyleFacade.getInstance(getProject()).getLineIndent(myEditor, 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 8aa339af522a..796a3a6967f8 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,12 +59,13 @@ public class CodeStyleFacadeImpl extends CodeStyleFacade { } @Override - public String getLineIndent(@NotNull Editor editor, @NotNull Document document, int offset) { + public String getLineIndent(@NotNull Editor editor, 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, document, offset) : null; + return lineIndentProvider != null ? lineIndentProvider.getLineIndent(myProject, editor, 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 cb4ab7469a80..99180ad51aeb 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 @@ -30,7 +30,8 @@ import org.jetbrains.annotations.Nullable; public class FormatterBasedLineIndentProvider implements LineIndentProvider { @Nullable @Override - public String getLineIndent(@NotNull Project project, @NotNull Editor editor, @NotNull Document document, int offset) { + public String getLineIndent(@NotNull Project project, @NotNull Editor editor, int offset) { + Document document = editor.getDocument(); PsiDocumentManager.getInstance(project).commitDocument(document); return CodeStyleManager.getInstance(project).getLineIndent(document, offset); } 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 98d9a2e818e6..1e6679c677c6 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 @@ -17,7 +17,6 @@ package com.intellij.psi.impl.source.codeStyle; import com.intellij.formatting.Indent; import com.intellij.lang.Language; -import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ex.EditorEx; import com.intellij.openapi.editor.highlighter.EditorHighlighter; @@ -36,10 +35,10 @@ import org.jetbrains.annotations.Nullable; public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineIndentProvider { @Nullable @Override - public String getLineIndent(@NotNull Project project, @NotNull Editor editor, @NotNull Document document, int offset) { - Indent.Type indent = getIndent(editor, document, offset); + public String getLineIndent(@NotNull Project project, @NotNull Editor editor, int offset) { + Indent.Type indent = getIndent(editor, offset); if (indent == Indent.Type.NONE) return null; - return super.getLineIndent(project, editor, document, offset); + return super.getLineIndent(project, editor, offset); } @Override @@ -53,15 +52,17 @@ public abstract class JavaLikeLangLineIndentProvider extends FormatterBasedLineI protected abstract boolean isSuitableForLanguage(@NotNull Language language); @Nullable - protected Indent.Type getIndent(@NotNull Editor editor, @NotNull Document document, int offset) { - CharSequence docChars = document.getCharsSequence(); - EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter(); - HighlighterIterator iterator = highlighter.createIterator(offset - 1); - if (isWhitespace(iterator.getTokenType())) { - if (containsLineBreaks(iterator, docChars)) { - iterator.retreat(); - if (!iterator.atEnd()) { - if (isEndOfCodeBlock(iterator)) return Indent.Type.NONE; + protected Indent.Type getIndent(@NotNull Editor editor, int offset) { + if (offset > 0) { + CharSequence docChars = editor.getDocument().getCharsSequence(); + EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter(); + HighlighterIterator iterator = highlighter.createIterator(offset - 1); + if (isWhitespace(iterator.getTokenType())) { + if (containsLineBreaks(iterator, docChars)) { + iterator.retreat(); + if (!iterator.atEnd()) { + if (isEndOfCodeBlock(iterator)) return Indent.Type.NONE; + } } } } 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 0ca97d7d40f9..41d56c172ce3 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,7 +15,6 @@ */ package com.intellij.psi.impl.source.codeStyle; -import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; @@ -33,13 +32,12 @@ public interface LineIndentProvider { * * @param project The current project. * @param editor The current editor. - * @param document The document for which the indent should be calculated. * @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, @NotNull Document document, int offset); + String getLineIndent(@NotNull Project project, @NotNull Editor editor, int offset); boolean isSuitableFor(@Nullable PsiFile file); } diff --git a/platform/platform-api/src/com/intellij/codeStyle/CodeStyleFacade.java b/platform/platform-api/src/com/intellij/codeStyle/CodeStyleFacade.java index dd9680f5bfe9..eb8b9e3c40e3 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, Document, int)} instead. + * @deprecated Use {@link #getLineIndent(Editor, int)} instead. */ @Nullable @Deprecated @@ -55,17 +55,16 @@ public abstract class CodeStyleFacade { /** * Calculates the indent that should be used for the line at specified offset in the specified - * document. + * editor. * * @param editor the editor for which the indent must be returned. - * @param document the document for which the indent should be calculated. * @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, @NotNull Document document, int offset) { + public String getLineIndent(@NotNull Editor editor, int offset) { //noinspection deprecation - return getLineIndent(document, offset); + return getLineIndent(editor.getDocument(), offset); } public abstract int getIndentSize(FileType fileType);