Skip to content

Commit

Permalink
Closes #161 - Auto-insert semicolon when completing brace for new fun…
Browse files Browse the repository at this point in the history
…ction declaration.
  • Loading branch information
ligasgr committed Dec 1, 2015
1 parent 463c3e7 commit b007c30
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2013-2015 Grzegorz Ligas <[email protected]> and other contributors
* (see the CONTRIBUTORS file).
*
* 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.intellij.xquery.braces;

import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.openapi.project.Project;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.intellij.xquery.XQueryLanguage;
import org.intellij.xquery.psi.XQueryFunctionBody;
import org.jetbrains.annotations.NotNull;

public class FunctionDeclarationBracesBodyHandler extends TypedHandlerDelegate {

@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile editedFile) {
if ((editedFile.getLanguage() instanceof XQueryLanguage) && (c == '{' || c == '}')) {
PsiDocumentManager.getInstance(project).commitAllDocuments();
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (file == null) return Result.CONTINUE;
FileViewProvider provider = file.getViewProvider();
final int offset = editor.getCaretModel().getOffset();
PsiElement element = provider.findElementAt(offset - 1, XQueryLanguage.class);
if (element == null) return Result.CONTINUE;
if (!(element.getLanguage() instanceof XQueryLanguage)) return Result.CONTINUE;
ASTNode prevLeaf = element.getNode();
if (prevLeaf == null) return Result.CONTINUE;
final String prevLeafText = prevLeaf.getText();

if (isInFunctionBodyAfterInsertionOfMatchingRightBrace(element, prevLeafText)) {
if (c == '{') {
editor.getDocument().insertString(offset + 1, ";");
} else {
EditorModificationUtil.insertStringAtCaret(editor, ";", false);
}
}

}

return Result.CONTINUE;
}

private boolean isInFunctionBodyAfterInsertionOfMatchingRightBrace(PsiElement element, String prevLeafText) {
return ("{".equals(prevLeafText) || "}".equals(prevLeafText)) && element.getParent() != null
&& element.getParent().getParent() != null
&& element.getParent().getParent() instanceof XQueryFunctionBody;
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<quoteHandler fileType="XQuery file" className="org.intellij.xquery.quotes.XQueryQuoteHandler"/>
<typedHandler implementation="org.intellij.xquery.completion.xml.XQueryXmlSlashTypedHandler" id="xqueryXmlSlash"/>
<typedHandler implementation="org.intellij.xquery.completion.xml.XQueryXmlGtTypedHandler" id="xqueryXmlGt"/>
<typedHandler implementation="org.intellij.xquery.braces.FunctionDeclarationBracesBodyHandler" id="xqueryFunDeclBraces"/>
<declarationRangeHandler key="org.intellij.xquery.psi.XQueryFunctionDecl" implementationClass="org.intellij.xquery.structure.XQueryFunctionDeclarationRangeHandler"/>
<annotator language="XQuery" implementationClass="org.intellij.xquery.annotator.XQueryAnnotator"/>
<additionalTextAttributes scheme="Default" file="colorSchemes/Default.xml"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2013-2015 Grzegorz Ligas <[email protected]> and other contributors
* (see the CONTRIBUTORS file).
*
* 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.intellij.xquery.braces;

import org.intellij.xquery.BaseFunctionalTestCase;
import org.intellij.xquery.XQueryFileType;

public class FunctionDeclarationBracesBodyHandlerTest extends BaseFunctionalTestCase {

public void testAddsSemicolonAfterCompletingOpeningBrace() {
myFixture.configureByText(XQueryFileType.INSTANCE, "declare function foo() <caret>");
myFixture.type("{");
myFixture.checkResult("declare function foo() {<caret>};");
}

public void testAddsSemicolonAfterCompletingClosingBrace() {
myFixture.configureByText(XQueryFileType.INSTANCE, "declare function foo() {<caret>");
myFixture.type("}");
myFixture.checkResult("declare function foo() {};<caret>");
}

public void testAddsSemicolonAfterCompletingClosingBraceWithSomeMethodBody() {
myFixture.configureByText(XQueryFileType.INSTANCE, "declare function foo() {(),'abc'<caret>");
myFixture.type("}");
myFixture.checkResult("declare function foo() {(),'abc'};<caret>");
}

public void testDoesNotAddSemicolonAfterCompletingClosingBraceWithinMethodBody() {
myFixture.configureByText(XQueryFileType.INSTANCE, "declare function fun($sdfsdf) {(), 'abc', element abc {'safsdf'<caret>");
myFixture.type("}");
myFixture.checkResult("declare function fun($sdfsdf) {(), 'abc', element abc {'safsdf'}<caret>");
}

public void testAddaSemicolonAfterCompletingClosingBraceWhenMethodBodyContainsBraces() {
myFixture.configureByText(XQueryFileType.INSTANCE, "declare function fun($sdfsdf) {(), 'abc', element abc {'safsdf'}<caret>");
myFixture.type("}");
myFixture.checkResult("declare function fun($sdfsdf) {(), 'abc', element abc {'safsdf'}};<caret>");
}
}

0 comments on commit b007c30

Please sign in to comment.