mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
do not indent if caret is currently at beginning of line (PY-3009)
This commit is contained in:
@@ -34,6 +34,7 @@
|
||||
<backspaceHandlerDelegate implementation="com.jetbrains.python.editor.PythonBackspaceHandler"/>
|
||||
<quoteHandler fileType="Python" className="com.jetbrains.python.editor.PythonQuoteHandler"/>
|
||||
<enterHandlerDelegate implementation="com.jetbrains.python.editor.PythonEnterHandler"/>
|
||||
<enterHandlerDelegate implementation="com.jetbrains.python.editor.PyEnterAtIndentHandler" order="first"/>
|
||||
<enterHandlerDelegate implementation="com.jetbrains.python.editor.PyEnterBetweenBracketsHandler"/>
|
||||
<sdkType implementation="com.jetbrains.python.sdk.PythonSdkType"/>
|
||||
<gotoClassContributor implementation="com.jetbrains.python.PyGotoClassContributor"/>
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.jetbrains.python.editor;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.BackspaceHandler;
|
||||
import com.intellij.codeInsight.editorActions.enter.EnterHandlerDelegateAdapter;
|
||||
import com.intellij.injected.editor.EditorWindow;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
|
||||
import com.jetbrains.python.psi.PyFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author yole
|
||||
*/
|
||||
public class PyEnterAtIndentHandler extends EnterHandlerDelegateAdapter {
|
||||
@Override
|
||||
public Result preprocessEnter(@NotNull PsiFile file,
|
||||
@NotNull Editor editor,
|
||||
@NotNull Ref<Integer> caretOffset,
|
||||
@NotNull Ref<Integer> caretAdvance,
|
||||
@NotNull DataContext dataContext,
|
||||
EditorActionHandler originalHandler) {
|
||||
int offset = caretOffset.get();
|
||||
if (editor instanceof EditorWindow) {
|
||||
file = InjectedLanguageUtil.getTopLevelFile(file);
|
||||
editor = InjectedLanguageUtil.getTopLevelEditor(editor);
|
||||
offset = editor.getCaretModel().getOffset();
|
||||
}
|
||||
if (!(file instanceof PyFile)) {
|
||||
return Result.Continue;
|
||||
}
|
||||
|
||||
// honor dedent (PY-3009)
|
||||
if (BackspaceHandler.isWhitespaceBeforeCaret(editor)) {
|
||||
return Result.DefaultSkipIndent;
|
||||
}
|
||||
return Result.Continue;
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ public class PythonEnterHandler extends EnterHandlerDelegateAdapter {
|
||||
if (isSplitLine != null) {
|
||||
return Result.Continue;
|
||||
}
|
||||
Document doc = editor.getDocument();
|
||||
final Document doc = editor.getDocument();
|
||||
PsiDocumentManager.getInstance(file.getProject()).commitDocument(doc);
|
||||
final PsiElement element = file.findElementAt(offset);
|
||||
CodeInsightSettings codeInsightSettings = CodeInsightSettings.getInstance();
|
||||
@@ -145,6 +145,14 @@ public class PythonEnterHandler extends EnterHandlerDelegateAdapter {
|
||||
if (!PyCodeInsightSettings.getInstance().INSERT_BACKSLASH_ON_WRAP) {
|
||||
return Result.Continue;
|
||||
}
|
||||
return checkInsertBackslash(file, caretOffset, dataContext, offset, doc);
|
||||
}
|
||||
|
||||
private static Result checkInsertBackslash(PsiFile file,
|
||||
Ref<Integer> caretOffset,
|
||||
DataContext dataContext,
|
||||
int offset,
|
||||
Document doc) {
|
||||
if (offset > 0) {
|
||||
final PsiElement beforeCaret = file.findElementAt(offset - 1);
|
||||
if (beforeCaret instanceof PsiWhiteSpace && beforeCaret.getText().indexOf('\\') >= 0) {
|
||||
|
||||
@@ -303,6 +303,16 @@ public class PyIndentTest extends PyTestCase {
|
||||
" <caret>)");
|
||||
}
|
||||
|
||||
public void testRespectDedent() { // PY-3009
|
||||
doTest("if True:\n" +
|
||||
" bar\n" +
|
||||
"<caret>",
|
||||
"if True:\n" +
|
||||
" bar\n" +
|
||||
"\n" +
|
||||
"<caret>");
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: formatter core problem?
|
||||
public void testAlignListBeforeEquals() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user