-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ErlangTerminatorSmartEnterProcessor
Introduce ErlangTerminatorSmartEnterProcessor to handle automatic insertion of terminators during smart enter actions in Erlang code. Added comprehensive tests to ensure the processor handles various scenarios effectively.
- Loading branch information
Showing
3 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/org/intellij/erlang/editor/ErlangTerminatorSmartEnterProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.intellij.erlang.editor; | ||
|
||
import com.intellij.codeInsight.editorActions.smartEnter.SmartEnterProcessor; | ||
import com.intellij.codeInsight.lookup.LookupElement; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.codeInsight.template.*; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.editor.LogicalPosition; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiWhiteSpace; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import org.intellij.erlang.psi.ErlangClauseBody; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class ErlangTerminatorSmartEnterProcessor extends SmartEnterProcessor { | ||
@Override | ||
public boolean process(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) { | ||
int offset = editor.getCaretModel().getOffset(); | ||
|
||
// Check file boundaries | ||
if (offset <= 0 || offset > psiFile.getTextLength()) { | ||
return false; | ||
} | ||
|
||
PsiElement element = psiFile.findElementAt(offset); | ||
|
||
// If element is null or whitespace, check the element at position -1 | ||
if (element == null || element instanceof PsiWhiteSpace) { | ||
element = psiFile.findElementAt(offset - 1); | ||
} | ||
|
||
// If still null after checking previous element, return false | ||
if (element == null) { | ||
return false; | ||
} | ||
|
||
ErlangClauseBody clauseBody = PsiTreeUtil.getParentOfType(element, ErlangClauseBody.class, false); | ||
|
||
if (clauseBody == null) return false; | ||
|
||
TemplateManager templateManager = TemplateManager.getInstance(project); | ||
Template template = createTerminatorTemplate(templateManager); | ||
|
||
moveCaretToEndOfLine(editor); | ||
templateManager.startTemplate(editor, template); | ||
|
||
return true; | ||
} | ||
|
||
private static void moveCaretToEndOfLine(Editor editor) { | ||
LogicalPosition logicalPosition = editor.getCaretModel().getLogicalPosition(); | ||
int lineNumber = logicalPosition.line; | ||
int lineEndOffset = editor.getDocument().getLineEndOffset(lineNumber); | ||
editor.getCaretModel().moveToOffset(lineEndOffset); | ||
} | ||
|
||
private static Template createTerminatorTemplate(TemplateManager templateManager) { | ||
Template template = templateManager.createTemplate("", ""); | ||
template.addVariable("terminator", new TerminatorExpressionNode(), true); | ||
return template; | ||
} | ||
|
||
private static class TerminatorExpressionNode extends Expression { | ||
@Override | ||
public Result calculateResult(ExpressionContext context) { | ||
return new TextResult(","); | ||
} | ||
|
||
@Override | ||
public Result calculateQuickResult(ExpressionContext context) { | ||
return calculateResult(context); | ||
} | ||
|
||
@Override | ||
public LookupElement[] calculateLookupItems(ExpressionContext context) { | ||
return new LookupElement[]{ | ||
LookupElementBuilder.create(","), | ||
LookupElementBuilder.create(";"), | ||
LookupElementBuilder.create("."), | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters