Skip to content

Commit

Permalink
Incorrect auto-insertion of } on Enter #216
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Jul 1, 2024
1 parent 44d980a commit deb61e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/intellij_awk/AwkEnterAfterUnmatchedBraceHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package intellij_awk;

import com.intellij.codeInsight.editorActions.enter.EnterAfterUnmatchedBraceHandler;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.util.text.CharArrayUtil;
import intellij_awk.psi.AwkFile;
import org.jetbrains.annotations.NotNull;

public class AwkEnterAfterUnmatchedBraceHandler extends EnterAfterUnmatchedBraceHandler {
@Override
public boolean isApplicable(@NotNull PsiFile file, int caretOffset) {
return file instanceof AwkFile;
}

@Override
protected Pair<PsiElement, Integer> calculateOffsetToInsertClosingBrace(
@NotNull PsiFile file, @NotNull CharSequence text, int offset) {
return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
}
}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
<codeInsight.parameterInfo language="AWK"
implementationClass="intellij_awk.AwkParameterInfoHandler"/>

<enterHandlerDelegate implementation="intellij_awk.AwkEnterAfterUnmatchedBraceHandler" order="before afterUnmatchedBrace"/>


</extensions>

<actions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public void testEnterCurlyBrace6() {
public void testEnterCurlyBrace7() {
doTest('\n', "NF {<caret>print 123", "NF {\n <caret>print 123\n}");
}
public void testEnterCurlyBrace8() {
doTest('\n', "function f()\n{<caret>print 123", "function f()\n{\n <caret>print 123\n}");
}

private void doTest(char brace, String code, String expectedCode) {
myFixture.configureByText("a.awk", code);
Expand Down

0 comments on commit deb61e3

Please sign in to comment.