mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
groovy smart enter refactoring: use SmartEnterProcessorWithFixers
This commit is contained in:
-30
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 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 org.jetbrains.plugins.groovy.lang.completion.smartEnter;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.smartEnter.EnterProcessor;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
|
||||
/**
|
||||
* @author Dmitry.Krasilschikov
|
||||
* Date: 29.07.2008
|
||||
*/
|
||||
public class GroovyCommentBreakerEnterProcessor implements EnterProcessor {
|
||||
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+46
-166
@@ -15,24 +15,16 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessor;
|
||||
import com.intellij.codeInsight.lookup.LookupManager;
|
||||
import com.intellij.openapi.actionSystem.IdeActions;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtilCore;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.OrderedSet;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -48,7 +40,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
|
||||
import org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -58,114 +49,47 @@ import java.util.List;
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 29.07.2008
|
||||
*/
|
||||
public class GroovySmartEnterProcessor extends SmartEnterProcessor {
|
||||
|
||||
private static final Logger LOG = Logger.getInstance("#org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor");
|
||||
|
||||
private static final List<GrFixer> ourFixers = Arrays.asList(
|
||||
new GrFixer() {
|
||||
@Override
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
GrCatchClause catchClause = PsiTreeUtil.getParentOfType(psiElement, GrCatchClause.class);
|
||||
if (catchClause == null || catchClause.getBody() != null) return;
|
||||
if (!PsiTreeUtil.isAncestor(catchClause.getParameter(), psiElement, false)) return;
|
||||
|
||||
final Document doc = editor.getDocument();
|
||||
|
||||
PsiElement lBrace = catchClause.getLBrace();
|
||||
if (lBrace != null) return;
|
||||
|
||||
PsiElement eltToInsertAfter = catchClause.getRParenth();
|
||||
String text = "{\n}";
|
||||
if (eltToInsertAfter == null) {
|
||||
eltToInsertAfter = catchClause.getParameter();
|
||||
text = "){\n}";
|
||||
}
|
||||
if (eltToInsertAfter != null) {
|
||||
doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
|
||||
}
|
||||
}
|
||||
},
|
||||
new GrMissingIfStatement(),
|
||||
new GrIfConditionFixer(),
|
||||
new GrLiteralFixer(),
|
||||
new GrMethodCallFixer(),
|
||||
new GrMethodBodyFixer(),
|
||||
new GrMethodParametersFixer(),
|
||||
new GrWhileConditionFixer(),
|
||||
new GrWhileBodyFixer(),
|
||||
new GrForBodyFixer(),
|
||||
new GrSwitchBodyFixer(),
|
||||
new GrListFixer(),
|
||||
new GrMethodCallWithSingleClosureArgFixer()
|
||||
);
|
||||
|
||||
private int myFirstErrorOffset = Integer.MAX_VALUE;
|
||||
private static final int MAX_ATTEMPTS = 20;
|
||||
private static final Key<Long> SMART_ENTER_TIMESTAMP = Key.create("smartEnterOriginalTimestamp");
|
||||
|
||||
public static class TooManyAttemptsException extends Exception {
|
||||
}
|
||||
|
||||
public boolean process(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile psiFile) {
|
||||
final Document document = editor.getDocument();
|
||||
final String textForRollback = document.getText();
|
||||
try {
|
||||
editor.putUserData(SMART_ENTER_TIMESTAMP, editor.getDocument().getModificationStamp());
|
||||
myFirstErrorOffset = Integer.MAX_VALUE;
|
||||
process(project, editor, psiFile, 0);
|
||||
}
|
||||
catch (TooManyAttemptsException e) {
|
||||
document.replaceString(0, document.getTextLength(), textForRollback);
|
||||
} finally {
|
||||
editor.putUserData(SMART_ENTER_TIMESTAMP, null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private void process(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file, final int attempt) throws TooManyAttemptsException {
|
||||
if (attempt > MAX_ATTEMPTS) throw new TooManyAttemptsException();
|
||||
|
||||
try {
|
||||
commit(editor);
|
||||
if (myFirstErrorOffset != Integer.MAX_VALUE) {
|
||||
editor.getCaretModel().moveToOffset(myFirstErrorOffset);
|
||||
}
|
||||
|
||||
myFirstErrorOffset = Integer.MAX_VALUE;
|
||||
|
||||
PsiElement atCaret = getStatementAtCaret(editor, file);
|
||||
if (atCaret == null) {
|
||||
if (!new GroovyCommentBreakerEnterProcessor().doEnter(editor, file, false)) {
|
||||
plainEnter(editor);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
List<PsiElement> queue = new ArrayList<PsiElement>();
|
||||
collectAllElements(atCaret, queue, true);
|
||||
queue.add(atCaret);
|
||||
|
||||
for (PsiElement psiElement : queue) {
|
||||
for (GrFixer fixer : ourFixers) {
|
||||
fixer.apply(editor, this, psiElement);
|
||||
if (LookupManager.getInstance(project).getActiveLookup() != null) {
|
||||
return;
|
||||
public class GroovySmartEnterProcessor extends SmartEnterProcessorWithFixers {
|
||||
public GroovySmartEnterProcessor() {
|
||||
final List<SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor>> ourFixers = Arrays.asList(
|
||||
new SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor>() {
|
||||
@Override
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
GrCatchClause catchClause = PsiTreeUtil.getParentOfType(psiElement, GrCatchClause.class);
|
||||
if (catchClause == null || catchClause.getBody() != null) return;
|
||||
if (!PsiTreeUtil.isAncestor(catchClause.getParameter(), psiElement, false)) return;
|
||||
|
||||
final Document doc = editor.getDocument();
|
||||
|
||||
PsiElement lBrace = catchClause.getLBrace();
|
||||
if (lBrace != null) return;
|
||||
|
||||
PsiElement eltToInsertAfter = catchClause.getRParenth();
|
||||
String text = "{\n}";
|
||||
if (eltToInsertAfter == null) {
|
||||
eltToInsertAfter = catchClause.getParameter();
|
||||
text = "){\n}";
|
||||
}
|
||||
if (eltToInsertAfter != null) {
|
||||
doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
|
||||
}
|
||||
}
|
||||
if (isUncommited(project) || !psiElement.isValid()) {
|
||||
moveCaretInsideBracesIfAny(editor, file);
|
||||
process(project, editor, file, attempt + 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
doEnter(atCaret, editor);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
},
|
||||
new GrMissingIfStatement(),
|
||||
new GrIfConditionFixer(),
|
||||
new GrLiteralFixer(),
|
||||
new GrMethodCallFixer(),
|
||||
new GrMethodBodyFixer(),
|
||||
new GrMethodParametersFixer(),
|
||||
new GrWhileConditionFixer(),
|
||||
new GrWhileBodyFixer(),
|
||||
new GrForBodyFixer(),
|
||||
new GrSwitchBodyFixer(),
|
||||
new GrListFixer(),
|
||||
new GrMethodCallWithSingleClosureArgFixer()
|
||||
);
|
||||
addFixers(ourFixers.toArray(new Fixer[ourFixers.size()]));
|
||||
addEnterProcessors(new GroovyPlainEnterProcessor());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -183,38 +107,7 @@ public class GroovySmartEnterProcessor extends SmartEnterProcessor {
|
||||
super.reformat(atCaret);
|
||||
}
|
||||
|
||||
|
||||
private void doEnter(PsiElement atCaret, Editor editor) throws IncorrectOperationException {
|
||||
final PsiFile psiFile = atCaret.getContainingFile();
|
||||
|
||||
final RangeMarker rangeMarker = createRangeMarker(atCaret);
|
||||
if (myFirstErrorOffset != Integer.MAX_VALUE) {
|
||||
editor.getCaretModel().moveToOffset(myFirstErrorOffset);
|
||||
reformat(atCaret);
|
||||
return;
|
||||
}
|
||||
|
||||
reformat(atCaret);
|
||||
commit(editor);
|
||||
|
||||
atCaret = GroovyRefactoringUtil.findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), atCaret.getClass());
|
||||
|
||||
if (atCaret != null && new GroovyPlainEnterProcessor().doEnter(editor, atCaret, isModified(editor))) return;
|
||||
|
||||
if (!isModified(editor)) {
|
||||
plainEnter(editor);
|
||||
}
|
||||
else {
|
||||
if (myFirstErrorOffset == Integer.MAX_VALUE) {
|
||||
editor.getCaretModel().moveToOffset(rangeMarker.getEndOffset());
|
||||
}
|
||||
else {
|
||||
editor.getCaretModel().moveToOffset(myFirstErrorOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void collectAllElements(PsiElement atCaret, List<PsiElement> res, boolean recurse) {
|
||||
protected void collectAllElements(@NotNull PsiElement atCaret, @NotNull OrderedSet<PsiElement> res, boolean recurse) {
|
||||
res.add(0, atCaret);
|
||||
if (doNotStepInto(atCaret)) {
|
||||
if (!recurse) return;
|
||||
@@ -241,7 +134,8 @@ public class GroovySmartEnterProcessor extends SmartEnterProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean doNotStepInto(PsiElement element) {
|
||||
@Override
|
||||
public boolean doNotStepInto(PsiElement element) {
|
||||
return element instanceof PsiClass || element instanceof GrCodeBlock || element instanceof GrStatement || element instanceof GrMethod;
|
||||
}
|
||||
|
||||
@@ -286,6 +180,7 @@ public class GroovySmartEnterProcessor extends SmartEnterProcessor {
|
||||
: null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void moveCaretInsideBracesIfAny(@NotNull final Editor editor, @NotNull final PsiFile file) throws IncorrectOperationException {
|
||||
int caretOffset = editor.getCaretModel().getOffset();
|
||||
final CharSequence chars = editor.getDocument().getCharsSequence();
|
||||
@@ -317,21 +212,6 @@ public class GroovySmartEnterProcessor extends SmartEnterProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
protected static void plainEnter(@NotNull final Editor editor) {
|
||||
getEnterHandler().execute(editor, ((EditorEx) editor).getDataContext());
|
||||
}
|
||||
|
||||
protected static EditorActionHandler getEnterHandler() {
|
||||
return EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE);
|
||||
}
|
||||
|
||||
protected static boolean isModified(@NotNull final Editor editor) {
|
||||
final Long timestamp = editor.getUserData(SMART_ENTER_TIMESTAMP);
|
||||
assert timestamp != null;
|
||||
return editor.getDocument().getModificationStamp() != timestamp.longValue();
|
||||
}
|
||||
|
||||
|
||||
private static PsiElement[] getChildren(PsiElement element) {
|
||||
PsiElement psiChild = element.getFirstChild();
|
||||
if (psiChild == null) return new PsiElement[0];
|
||||
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2009 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 org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
|
||||
/**
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 29.07.2008
|
||||
*/
|
||||
public interface GrFixer {
|
||||
void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException;
|
||||
}
|
||||
+4
-3
@@ -15,11 +15,12 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement;
|
||||
@@ -28,8 +29,8 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement;
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 14.08.2008
|
||||
*/
|
||||
public class GrForBodyFixer implements GrFixer{
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrForBodyFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
GrForStatement forStatement = PsiTreeUtil.getParentOfType(psiElement, GrForStatement.class);
|
||||
if (forStatement == null) return;
|
||||
|
||||
|
||||
+7
-6
@@ -15,21 +15,22 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
|
||||
/**
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 31.07.2008
|
||||
*/
|
||||
public class GrIfConditionFixer implements GrFixer {
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrIfConditionFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (psiElement instanceof GrIfStatement) {
|
||||
final Document doc = editor.getDocument();
|
||||
final GrIfStatement ifStatement = (GrIfStatement) psiElement;
|
||||
|
||||
+4
-3
@@ -15,9 +15,10 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
|
||||
@@ -26,9 +27,9 @@ import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
|
||||
/**
|
||||
* @author Maxim.Medvedev
|
||||
*/
|
||||
public class GrListFixer implements GrFixer {
|
||||
public class GrListFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
@Override
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (psiElement.getParent() instanceof GrListOrMap) {
|
||||
final PsiElement next = PsiUtil.skipWhitespacesAndComments(psiElement.getNextSibling(), true);
|
||||
if (next != null && !GroovyTokenTypes.mCOMMA.equals(next.getNode().getElementType())) {
|
||||
|
||||
+4
-3
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString;
|
||||
@@ -27,8 +28,8 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 04.08.2008
|
||||
*/
|
||||
public class GrLiteralFixer implements GrFixer {
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrLiteralFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (psiElement instanceof GrString) {
|
||||
String text = psiElement.getText();
|
||||
if (StringUtil.startsWith(text, "\"\"\"")) {
|
||||
|
||||
+4
-3
@@ -15,13 +15,14 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
|
||||
@@ -31,8 +32,8 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMe
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 08.08.2008
|
||||
*/
|
||||
public class GrMethodBodyFixer implements GrFixer {
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrMethodBodyFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (!(psiElement instanceof GrMethod)) return;
|
||||
GrMethod method = (GrMethod) psiElement;
|
||||
final PsiClass aClass = method.getContainingClass();
|
||||
|
||||
+4
-3
@@ -20,11 +20,12 @@ package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
* Date: 05.08.2008
|
||||
*/
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiErrorElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.text.CharArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall;
|
||||
@@ -32,8 +33,8 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrComman
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil;
|
||||
|
||||
public class GrMethodCallFixer implements GrFixer {
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrMethodCallFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
final GrArgumentList argList = psiElement instanceof GrCall ? ((GrCall)psiElement).getArgumentList() : null;
|
||||
if (argList == null || argList instanceof GrCommandArgumentList) return;
|
||||
|
||||
|
||||
+4
-3
@@ -15,11 +15,12 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiParameter;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall;
|
||||
@@ -30,9 +31,9 @@ import org.jetbrains.plugins.groovy.lang.psi.util.GroovyCommonClassNames;
|
||||
/**
|
||||
* @author Max Medvedev
|
||||
*/
|
||||
public class GrMethodCallWithSingleClosureArgFixer implements GrFixer {
|
||||
public class GrMethodCallWithSingleClosureArgFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
@Override
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
final PsiElement parent = psiElement.getParent();
|
||||
if (parent instanceof GrReferenceExpression && !(parent.getParent() instanceof GrMethodCall) &&
|
||||
hasOnlyClosureParam((GrReferenceExpression)parent)) {
|
||||
|
||||
+5
-5
@@ -15,21 +15,21 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
|
||||
|
||||
/**
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 08.08.2008
|
||||
*/
|
||||
public class GrMethodParametersFixer implements GrFixer{
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrMethodParametersFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (psiElement instanceof GrParameterList && psiElement.getParent() instanceof GrMethod) {
|
||||
PsiElement rParenth = psiElement.getNextSibling();
|
||||
if (rParenth == null) return;
|
||||
|
||||
+8
-7
@@ -15,21 +15,22 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
|
||||
/**
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 04.07.2008
|
||||
*/
|
||||
public class GrMissingIfStatement implements GrFixer {
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrMissingIfStatement extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (!(psiElement instanceof GrIfStatement)) return;
|
||||
|
||||
GrIfStatement ifStatement = (GrIfStatement) psiElement;
|
||||
|
||||
+4
-3
@@ -15,19 +15,20 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrSwitchStatement;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class GrSwitchBodyFixer implements GrFixer{
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrSwitchBodyFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
GrSwitchStatement switchStatement = PsiTreeUtil.getParentOfType(psiElement, GrSwitchStatement.class);
|
||||
if (switchStatement == null || switchStatement.getLBrace() != null) return;
|
||||
if (!PsiTreeUtil.isAncestor(switchStatement.getCondition(), psiElement, false)) return;
|
||||
|
||||
+7
-6
@@ -15,20 +15,21 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement;
|
||||
|
||||
/**
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 12.08.2008
|
||||
*/
|
||||
public class GrWhileBodyFixer implements GrFixer{
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrWhileBodyFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (!(psiElement instanceof GrWhileStatement)) return;
|
||||
GrWhileStatement whileStatement = (GrWhileStatement) psiElement;
|
||||
|
||||
|
||||
+7
-6
@@ -15,21 +15,22 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.fixers;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.completion.smartEnter.GroovySmartEnterProcessor;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement;
|
||||
|
||||
/**
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 12.08.2008
|
||||
*/
|
||||
public class GrWhileConditionFixer implements GrFixer {
|
||||
public void apply(Editor editor, GroovySmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
|
||||
public class GrWhileConditionFixer extends SmartEnterProcessorWithFixers.Fixer<GroovySmartEnterProcessor> {
|
||||
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
|
||||
if (psiElement instanceof GrWhileStatement) {
|
||||
final Document doc = editor.getDocument();
|
||||
final GrWhileStatement whileStatement = (GrWhileStatement) psiElement;
|
||||
|
||||
+19
-22
@@ -15,13 +15,11 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang.completion.smartEnter.processors;
|
||||
|
||||
import com.intellij.codeInsight.editorActions.smartEnter.EnterProcessor;
|
||||
import com.intellij.openapi.actionSystem.IdeActions;
|
||||
import com.intellij.lang.SmartEnterProcessorWithFixers;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
|
||||
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
|
||||
import com.intellij.openapi.editor.ex.EditorEx;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.*;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
|
||||
@@ -33,22 +31,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMe
|
||||
* User: Dmitry.Krasilschikov
|
||||
* Date: 05.08.2008
|
||||
*/
|
||||
public class GroovyPlainEnterProcessor implements EnterProcessor {
|
||||
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
|
||||
GrCodeBlock block = getControlStatementBlock(editor.getCaretModel().getOffset(), psiElement);
|
||||
|
||||
if (block != null) {
|
||||
PsiElement firstElement = block.getFirstChild().getNextSibling();
|
||||
|
||||
final int offset = firstElement != null ? firstElement.getTextRange().getStartOffset() - 1 : block.getTextRange().getEndOffset();
|
||||
editor.getCaretModel().moveToOffset(offset);
|
||||
}
|
||||
|
||||
final EditorActionHandler endterHandler = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE);
|
||||
endterHandler.execute(editor, ((EditorEx)editor).getDataContext());
|
||||
return true;
|
||||
}
|
||||
|
||||
public class GroovyPlainEnterProcessor extends SmartEnterProcessorWithFixers.FixEnterProcessor {
|
||||
@Nullable
|
||||
private static GrCodeBlock getControlStatementBlock(int caret, PsiElement element) {
|
||||
GrStatement body = null;
|
||||
@@ -77,7 +60,21 @@ public class GroovyPlainEnterProcessor implements EnterProcessor {
|
||||
return ((GrBlockStatement)body).getBlock();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doEnter(PsiElement psiElement, PsiFile file, @NotNull Editor editor, boolean modified) {
|
||||
GrCodeBlock block = getControlStatementBlock(editor.getCaretModel().getOffset(), psiElement);
|
||||
|
||||
if (block != null) {
|
||||
PsiElement firstElement = block.getFirstChild().getNextSibling();
|
||||
|
||||
final int offset = firstElement != null ? firstElement.getTextRange().getStartOffset() - 1 : block.getTextRange().getEndOffset();
|
||||
editor.getCaretModel().moveToOffset(offset);
|
||||
}
|
||||
|
||||
plainEnter(editor);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user