From 211575cb93d613f9b9a08ea8b198f91e4fd73686 Mon Sep 17 00:00:00 2001 From: xonix Date: Sat, 6 Jul 2024 01:59:43 +0300 Subject: [PATCH] Incorrect auto-insertion of `}` on Enter #216 : implementation attempt --- .../AwkEnterAfterUnmatchedBraceHandler.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/intellij_awk/AwkEnterAfterUnmatchedBraceHandler.java b/src/main/java/intellij_awk/AwkEnterAfterUnmatchedBraceHandler.java index 1f82ce8..2409c85 100644 --- a/src/main/java/intellij_awk/AwkEnterAfterUnmatchedBraceHandler.java +++ b/src/main/java/intellij_awk/AwkEnterAfterUnmatchedBraceHandler.java @@ -5,6 +5,7 @@ import com.intellij.psi.*; import com.intellij.util.text.CharArrayUtil; import intellij_awk.psi.AwkFile; +import intellij_awk.psi.AwkStatement; import org.jetbrains.annotations.NotNull; public class AwkEnterAfterUnmatchedBraceHandler extends EnterAfterUnmatchedBraceHandler { @@ -16,6 +17,31 @@ public boolean isApplicable(@NotNull PsiFile file, int caretOffset) { @Override protected Pair calculateOffsetToInsertClosingBrace( @NotNull PsiFile file, @NotNull CharSequence text, int offset) { - return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n")); + + System.out.println("text="+text); + System.out.println("offset="+offset); + + String rest = text.subSequence(offset, text.length()).toString(); + System.out.println("rest="+rest); + + String code = "BEGIN{"+rest+"}"; + + System.out.println("code="+code); + + AwkFile awkFile = (AwkFile) + PsiFileFactory.getInstance(file.getProject()) + .createFileFromText("dummy.awk", AwkFileType.INSTANCE, code); + + AwkStatement awkStatement = (AwkStatement) AwkUtil.findFirstMatchedDeep(awkFile, AwkStatement.class::isInstance); + + System.out.println("zzz:"+CharArrayUtil.shiftForwardUntil(text, offset, "\n")+":"+(offset + awkStatement.getTextLength()+1)); + + if (awkStatement == null) { + return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n")); + } + + System.out.println("awkStatement="+awkStatement.getTextLength()); + System.out.println("awkStatement="+awkStatement.getText()); + return Pair.create(null, offset + awkStatement.getTextLength()); } }