diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/TypedHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/TypedHandler.java index dff7f092a5ca..7d0a9cc9b8d8 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/TypedHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/TypedHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 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. @@ -221,6 +221,9 @@ public class TypedHandler extends TypedActionHandlerBase { else if ('}' == charTyped) { indentClosingBrace(project, editor); } + else if (')' == charTyped) { + indentClosingParenth(project, editor); + } for(TypedHandlerDelegate delegate: delegates) { final TypedHandlerDelegate.Result result = delegate.charTyped(charTyped, project, editor, file); @@ -234,6 +237,9 @@ public class TypedHandler extends TypedActionHandlerBase { if ('{' == charTyped) { indentOpenedBrace(project, editor); } + else if ('(' == charTyped) { + indentOpenedParenth(project, editor); + } } private static void autoPopupParameterInfo(Editor editor, char charTyped, Project project, PsiFile file) { @@ -370,7 +376,7 @@ public class TypedHandler extends TypedActionHandlerBase { } IElementType tokenType = iterator.getTokenType(); - + iterator.retreat(); IElementType lparenTokenType = braceMatcher.getOppositeBraceTokenType(tokenType); @@ -533,6 +539,14 @@ public class TypedHandler extends TypedActionHandlerBase { indentBrace(project, editor, '{'); } + private static void indentOpenedParenth(@NotNull Project project, @NotNull Editor editor){ + indentBrace(project, editor, '('); + } + + private static void indentClosingParenth(@NotNull Project project, @NotNull Editor editor){ + indentBrace(project, editor, ')'); + } + private static void indentBrace(@NotNull final Project project, @NotNull final Editor editor, final char braceChar) { final int offset = editor.getCaretModel().getOffset() - 1; final Document document = editor.getDocument(); @@ -552,9 +566,11 @@ public class TypedHandler extends TypedActionHandlerBase { EditorHighlighter highlighter = ((EditorEx)editor).getHighlighter(); HighlighterIterator iterator = highlighter.createIterator(offset); - BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(file.getFileType(), iterator); - if (element.getNode() != null && braceMatcher.isStructuralBrace(iterator, chars, file.getFileType())) { - final Runnable action = new Runnable() { + final FileType fileType = file.getFileType(); + BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator); + final boolean isBrace = braceMatcher.isLBraceToken(iterator, chars, fileType) || braceMatcher.isRBraceToken(iterator, chars, fileType); + if (element.getNode() != null && isBrace) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { @Override public void run(){ try{ @@ -567,8 +583,7 @@ public class TypedHandler extends TypedActionHandlerBase { LOG.error(e); } } - }; - ApplicationManager.getApplication().runWriteAction(action); + }); } } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.groovy index 4475267f66df..64ca99d876df 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyEditingTest.groovy @@ -1,37 +1,47 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 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 + * 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 + * 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. + * 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; import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.jetbrains.annotations.Nullable import org.jetbrains.plugins.groovy.util.TestUtils /** * @author peter */ public class GroovyEditingTest extends LightCodeInsightFixtureTestCase { - @Override - protected String getBasePath() { - return TestUtils.testDataPath + "editing/"; - } + final String basePath = TestUtils.testDataPath + "editing/" - private void doTest(final String c) { - myFixture.configureByFile(getTestName(false) + ".groovy"); - myFixture.type(c as char); - myFixture.checkResultByFile(getTestName(false) + "_after.groovy"); + private void doTest(@Nullable String before = null, final String chars, @Nullable String after = null) { + if (before != null) { + myFixture.configureByText(getTestName(false) + '.groovy', before) + } + else { + myFixture.configureByFile(getTestName(false) + ".groovy"); + } + + myFixture.type(chars); + + if (after != null) { + myFixture.checkResult(after) + } + else { + myFixture.checkResultByFile(getTestName(false) + "_after.groovy"); + } } public void testCodeBlockRightBrace() throws Throwable { doTest('{'); } @@ -46,38 +56,40 @@ public class GroovyEditingTest extends LightCodeInsightFixtureTestCase { public void testNoPairLess() throws Throwable {doTest('<');} public void testTripleString() { - myFixture.configureByText('_.groovy', '') - myFixture.type('\'') - myFixture.type('\'') - myFixture.type('\'') - myFixture.checkResult("''''''") + doTest('', "'''", "''''''") } public void testTripleGString() { - myFixture.configureByText('_.groovy', '') - myFixture.type('"') - myFixture.type('"') - myFixture.type('"') - myFixture.checkResult('""""""') + doTest('', '"""', '""""""') } public void "test pair brace after doc with mismatch"() { - myFixture.configureByText 'a.groovy', ''' + doTest(''' class Foo { /** * @param o closure to run in {@code ant.zip{ .. }} context */ void getProject( Object o ) } -''' - myFixture.type('{') - myFixture.checkResult ''' +''', '{', ''' class Foo { /** * @param o closure to run in {@code ant.zip{ .. }} context */ void getProject( Object o ) {} } -''' +''') + } + + void testRPareth() { + doTest(''' +@Anno( + foo + +''', ')', ''' +@Anno( + foo +) +''') } } \ No newline at end of file diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/parser/GroovyReparseTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/parser/GroovyReparseTest.groovy index 0efb7d39d3b6..1e8cf4574fff 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/parser/GroovyReparseTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/parser/GroovyReparseTest.groovy @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2012 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.parser import com.intellij.psi.PsiDocumentManager @@ -113,10 +128,10 @@ class GroovyReparseTest extends LightCodeInsightFixtureTestCase { public void testOpeningParenthesisAtBlockStart() { checkReparse """ def foo() { - String home - simplePlugins.each { - layoutPlugin it - } + String home + simplePlugins.each { + layoutPlugin it + } } }""", "(" diff --git a/plugins/groovy/testdata/reparse/OpeningParenthesisAtBlockStart.txt b/plugins/groovy/testdata/reparse/OpeningParenthesisAtBlockStart.txt index 68fc4f47f4c6..7a05e538ef98 100644 --- a/plugins/groovy/testdata/reparse/OpeningParenthesisAtBlockStart.txt +++ b/plugins/groovy/testdata/reparse/OpeningParenthesisAtBlockStart.txt @@ -14,7 +14,7 @@ Groovy script Open block PsiElement({)('{') - PsiWhiteSpace('\n ') + PsiWhiteSpace('\n ') Variable definitions Modifiers @@ -24,7 +24,7 @@ Groovy script PsiWhiteSpace(' ') Variable PsiElement(identifier)('home') - PsiElement(new line)('\n ') + PsiElement(new line)('\n ') Method call Reference expression Reference expression @@ -36,7 +36,7 @@ Groovy script Closable block PsiElement({)('{') - PsiWhiteSpace('\n ') + PsiWhiteSpace('\n ') Parameter list Call expression @@ -46,7 +46,7 @@ Groovy script Command arguments Reference expression PsiElement(identifier)('it') - PsiElement(new line)('\n ') + PsiElement(new line)('\n ') PsiElement(})('}') PsiElement(new line)('\n\n') PsiElement(})('}') @@ -70,7 +70,7 @@ Groovy script Open block PsiElement({)('{') - PsiWhiteSpace('\n ') + PsiWhiteSpace('\n ') Reference expression Call expression Parenthesized expression @@ -83,7 +83,7 @@ Groovy script Command arguments Reference expression PsiElement(identifier)('home') - PsiWhiteSpace('\n ') + PsiWhiteSpace('\n ') PsiElement(identifier)('simplePlugins') PsiErrorElement:';', '}' or new line expected @@ -96,7 +96,7 @@ Groovy script Closable block PsiElement({)('{') - PsiWhiteSpace('\n ') + PsiWhiteSpace('\n ') Parameter list Call expression @@ -106,7 +106,7 @@ Groovy script Command arguments Reference expression PsiElement(identifier)('it') - PsiElement(new line)('\n ') + PsiElement(new line)('\n ') PsiElement(})('}') PsiWhiteSpace('\n\n') PsiElement(})('}')