public class EnterAfterUnmatchedBraceHandler extends EnterHandlerDelegateAdapter
EnterHandlerDelegate.Result
EP_NAME
Constructor and Description |
---|
EnterAfterUnmatchedBraceHandler() |
Modifier and Type | Method and Description |
---|---|
protected Pair<PsiElement,java.lang.Integer> |
calculateOffsetToInsertClosingBrace(PsiFile file,
java.lang.CharSequence text,
int offset)
Current handler inserts closing curly brace (right brace) if necessary.
|
protected int |
calculateOffsetToInsertClosingBraceInsideElement(PsiElement element) |
protected void |
formatCodeFragmentBetweenBraces(PsiFile file,
Document document,
Editor editor,
int caretOffset,
int rBracesInsertOffset,
java.lang.String generatedRBraces)
Formats the code block between caret and inserted braces.
|
protected java.lang.String |
generateStringToInsert(Editor editor,
int caretOffset,
int maxRBraceCount)
Calculates the string of '}' that have to be inserted by handler.
|
protected int |
getMaxRBraceCount(PsiFile file,
Editor editor,
int caretOffset)
Calculates the maximum number of '}' that can be inserted by handler.
|
protected int |
getRBraceOffset(PsiFile file,
Editor editor,
int caretOffset)
Calculates the position for insertion of one or more '}'.
|
protected static int |
getUnmatchedLBracesNumberBefore(Editor editor,
int offset,
FileType fileType)
Calculates number of unmatched left braces before the given offset.
|
protected void |
insertRBraces(PsiFile file,
Editor editor,
int caretOffset,
int rBracesInsertOffset,
java.lang.String generatedRBraces)
Inserts the
generatedRBraces at the rBracesInsertOffset position and formats the code block. |
protected void |
insertRBracesAtPosition(Document document,
int caretOffset,
int rBracesInsertOffset,
java.lang.String generatedRBraces)
Inserts the
rBracesCount of '}' at the rBracesInsertOffset position. |
static boolean |
isAfterUnmatchedLBrace(Editor editor,
int offset,
FileType fileType) |
boolean |
isApplicable(PsiFile file,
int caretOffset)
Checks that the text context is in responsibility of the handler.
|
protected boolean |
isStopChar(char c)
Checks the character before the inserted '}' to reduce the count of inserted '}'.
|
EnterHandlerDelegate.Result |
preprocessEnter(PsiFile file,
Editor editor,
Ref<java.lang.Integer> caretOffsetRef,
Ref<java.lang.Integer> caretAdvance,
DataContext dataContext,
EditorActionHandler originalHandler)
Called before the actual Enter processing is done.
|
postProcessEnter
public EnterHandlerDelegate.Result preprocessEnter(PsiFile file, Editor editor, Ref<java.lang.Integer> caretOffsetRef, Ref<java.lang.Integer> caretAdvance, DataContext dataContext, EditorActionHandler originalHandler)
EnterHandlerDelegate
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument);
preprocessEnter
in interface EnterHandlerDelegate
preprocessEnter
in class EnterHandlerDelegateAdapter
file
- The PSI file associated with the document.editor
- The editor.caretOffsetRef
- Indicates a place where line break is to be inserted (it's a caret position initially). Method implementation
can change this value to adjust target line break position.caretAdvance
- A reference to the number of columns by which the caret must be moved forward.dataContext
- The data context passed to the enter handler.originalHandler
- The original handler.EnterHandlerDelegate.Result
values.
public boolean isApplicable(PsiFile file, int caretOffset)
file
- target PSI filecaretOffset
- target caret offsetprotected int getMaxRBraceCount(PsiFile file, Editor editor, int caretOffset)
0
or less in custom implementation to skip '}' insertion in the preprocessEnter
call
and switch to default implementation.file
- target PSI fileeditor
- target editorcaretOffset
- target caret offset0
or less to switch to default implementationprotected java.lang.String generateStringToInsert(Editor editor, int caretOffset, int maxRBraceCount)
editor
- target editorcaretOffset
- target caret offsetmaxRBraceCount
- the maximum number of '}' for insert at position, it always positiveprotected boolean isStopChar(char c)
c
- character to checkprotected int getRBraceOffset(PsiFile file, Editor editor, int caretOffset)
file
- target PSI fileeditor
- target editorcaretOffset
- target caret offsetcaretOffset
and the end of fileprotected void insertRBraces(PsiFile file, Editor editor, int caretOffset, int rBracesInsertOffset, java.lang.String generatedRBraces)
generatedRBraces
at the rBracesInsertOffset
position and formats the code block.file
- target PSI fileeditor
- target editorcaretOffset
- target caret offsetrBracesInsertOffset
- target position to insertgeneratedRBraces
- string of '}' to insertprotected void insertRBracesAtPosition(Document document, int caretOffset, int rBracesInsertOffset, java.lang.String generatedRBraces)
rBracesCount
of '}' at the rBracesInsertOffset
position.document
- target documentcaretOffset
- target caret offsetrBracesInsertOffset
- target position to insertgeneratedRBraces
- string of '}' to insertprotected void formatCodeFragmentBetweenBraces(PsiFile file, Document document, Editor editor, int caretOffset, int rBracesInsertOffset, java.lang.String generatedRBraces)
file
- target PSI filedocument
- target documenteditor
- target editorcaretOffset
- target caret offsetrBracesInsertOffset
- target position to insertgeneratedRBraces
- string of '}' to insertprotected Pair<PsiElement,java.lang.Integer> calculateOffsetToInsertClosingBrace(PsiFile file, java.lang.CharSequence text, int offset)
if (test1()) { } else {We want to get this after the processing:if (test2()) { foo(); }
if (test1()) { } else { if (test2()) { foo(); } }I.e. closing brace should be inserted two lines below current caret line. Hence, we need to calculate correct offset to use for brace inserting. This method is responsible for that. In essence it inspects PSI structure and finds PSE elements with the max length that starts at caret offset. End offset of that element is used as an insertion point.
file
- target PSI filetext
- text from the given fileoffset
- target offset where line feed will be insertedprotected int calculateOffsetToInsertClosingBraceInsideElement(PsiElement element)
public static boolean isAfterUnmatchedLBrace(Editor editor, int offset, FileType fileType)
protected static int getUnmatchedLBracesNumberBefore(Editor editor, int offset, FileType fileType)
editor
- target editoroffset
- target offsetfileType
- target file type