auto-reformat parentheses

This commit is contained in:
Max Medvedev
2012-11-30 15:11:28 +04:00
parent 2bd992df6b
commit 2a5e0996db
4 changed files with 94 additions and 52 deletions
@@ -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);
});
}
}
}
@@ -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("'''<caret>'''")
doTest('', "'''", "'''<caret>'''")
}
public void testTripleGString() {
myFixture.configureByText('_.groovy', '')
myFixture.type('"')
myFixture.type('"')
myFixture.type('"')
myFixture.checkResult('"""<caret>"""')
doTest('', '"""', '"""<caret>"""')
}
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 ) <caret>
}
'''
myFixture.type('{')
myFixture.checkResult '''
''', '{', '''
class Foo {
/**
* @param o closure to run in {@code ant.zip{ .. }} context
*/
void getProject( Object o ) {<caret>}
}
'''
''')
}
void testRPareth() {
doTest('''
@Anno(
foo
<caret>
''', ')', '''
@Anno(
foo
)<caret>
''')
}
}
@@ -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() {
<caret>String home
simplePlugins.each {
layoutPlugin it
}
<caret>String home
simplePlugins.each {
layoutPlugin it
}
}
}""", "("
@@ -14,7 +14,7 @@ Groovy script
<empty list>
Open block
PsiElement({)('{')
PsiWhiteSpace('\n ')
PsiWhiteSpace('\n ')
Variable definitions
Modifiers
<empty list>
@@ -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
<empty list>
Closable block
PsiElement({)('{')
PsiWhiteSpace('\n ')
PsiWhiteSpace('\n ')
Parameter list
<empty 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
<empty list>
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
<empty list>
@@ -96,7 +96,7 @@ Groovy script
<empty list>
Closable block
PsiElement({)('{')
PsiWhiteSpace('\n ')
PsiWhiteSpace('\n ')
Parameter list
<empty 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(})('}')