Skip to content

Commit

Permalink
Incorrect auto-insertion of } on Enter #216 : implementation attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Jul 5, 2024
1 parent b8c6caa commit 211575c
Showing 1 changed file with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +17,31 @@ public boolean isApplicable(@NotNull PsiFile file, int caretOffset) {
@Override
protected Pair<PsiElement, Integer> 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());
}
}

0 comments on commit 211575c

Please sign in to comment.