Language added to LineIndentProvider API

This commit is contained in:
Rustam Vishnyakov
2016-05-24 18:01:51 +03:00
parent ceeeb88015
commit 1c7df29ef9
7 changed files with 22 additions and 32 deletions
@@ -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);
@@ -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
@@ -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;
}
}
@@ -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) {
@@ -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);
}
@@ -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<LineIndentProvider> 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;
}
}
@@ -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);
}