public class CodeStyleManagerImpl extends CodeStyleManager implements FormattingModeAwareIndentAdjuster
| Constructor and Description |
|---|
CodeStyleManagerImpl(Project project) |
| Modifier and Type | Method and Description |
|---|---|
int |
adjustLineIndent(Document document,
int offset)
Reformats the line at the specified offset in the specified file, modifying only the line indent
and leaving all other whitespace intact.
|
int |
adjustLineIndent(Document document,
int offset,
FormattingMode mode)
Adjust line indent at document offset using
mode. |
int |
adjustLineIndent(PsiFile file,
int offset)
Reformats the line at the specified offset in the specified file, modifying only the line indent
and leaving all other whitespace intact.
|
void |
adjustLineIndent(PsiFile file,
TextRange rangeToAdjust)
Re-formats the specified range of a file, modifying only line indents and leaving
all other whitespace intact.
|
java.lang.String |
fillIndent(Indent indent,
FileType fileType) |
static PsiElement |
findWhiteSpaceNode(PsiFile file,
int offset)
Allows to check if given offset points to white space element within the given PSI file and return that white space
element in the case of positive answer.
|
static void |
formatRanges(PsiFile file,
FormatTextRanges ranges,
java.lang.Runnable formatAction) |
FormattingMode |
getCurrentFormattingMode() |
DocCommentSettings |
getDocCommentSettings(PsiFile file) |
Indent |
getIndent(java.lang.String text,
FileType fileType) |
java.lang.String |
getLineIndent(Document document,
int offset)
Calculates the indent that should be used for the current line in the specified
editor.
|
java.lang.String |
getLineIndent(PsiFile file,
int offset)
Calculates the indent that should be used for the specified line in
the specified file.
|
java.lang.String |
getLineIndent(PsiFile file,
int offset,
FormattingMode mode)
Calculates the indent that should be used for the specified line in
the specified file with the given formatting mode.
|
int |
getMinLineFeeds(PsiFile file,
int offset)
Calculates minimum number of line feeds that should precede block starting at given offset, as dictated by formatting model.
|
Project |
getProject()
Gets the project with which the code style manager is associated.
|
int |
getSpacing(PsiFile file,
int offset)
Calculates minimum spacing, allowed by formatting model (in columns) for a block starting at given offset,
relative to its previous sibling block.
|
static TextRange |
insertNewLineIndentMarker(PsiFile file,
Document document,
int offset)
Formatter trims line that contains white spaces symbols only, however, there is a possible case that we want
to preserve them for particular line
(e.g.
|
boolean |
isLineToBeIndented(PsiFile file,
int offset)
Deprecated.
|
boolean |
isSequentialProcessingAllowed()
Formatting may be executed sequentially, i.e.
|
<T> T |
performActionWithFormatterDisabled(Computable<T> r) |
void |
performActionWithFormatterDisabled(java.lang.Runnable r)
Disables automatic formatting of modified PSI elements, runs the specified operation
and re-enables the formatting.
|
<T extends java.lang.Throwable> |
performActionWithFormatterDisabled(ThrowableRunnable<T> r) |
PsiElement |
reformat(PsiElement element)
Reformats the contents of the specified PSI element, enforces braces and splits import
statements according to the user's code style.
|
PsiElement |
reformat(PsiElement element,
boolean canChangeWhiteSpacesOnly)
Reformats the contents of the specified PSI element, and optionally enforces braces
and splits import statements according to the user's code style.
|
void |
reformatNewlyAddedElement(ASTNode parent,
ASTNode addedElement)
Reformats line indents inside new element and reformats white spaces around it
|
PsiElement |
reformatRange(PsiElement element,
int startOffset,
int endOffset)
Reformats part of the contents of the specified PSI element, enforces braces
and splits import statements according to the user's code style.
|
PsiElement |
reformatRange(PsiElement element,
int startOffset,
int endOffset,
boolean canChangeWhiteSpacesOnly)
Reformats part of the contents of the specified PSI element, and optionally enforces braces
and splits import statements according to the user's code style.
|
void |
reformatText(PsiFile file,
java.util.Collection<? extends TextRange> ranges,
Editor editor) |
void |
reformatText(PsiFile file,
java.util.Collection<TextRange> ranges)
Re-formats a ranges of text in the specified file.
|
void |
reformatText(PsiFile file,
int startOffset,
int endOffset)
Delegates to the
CodeStyleManager.reformatText(PsiFile, Collection) with the single range defined by the given offsets. |
void |
reformatTextWithContext(PsiFile file,
ChangedRangesInfo info) |
void |
runWithDocCommentFormattingDisabled(PsiFile file,
java.lang.Runnable runnable)
Run the given runnable disabling doc comment formatting.
|
void |
scheduleIndentAdjustment(Document document,
int offset)
Performs a delayed indent adjustment for large documents bigger than
FormatterBasedIndentAdjuster.MAX_SYNCHRONOUS_ADJUSTMENT_DOC_SIZE
by scheduling it to a time when the document is committed. |
static void |
setSequentialProcessingAllowed(boolean allowed)
Allows to define if
sequential processing should be allowed. |
Indent |
zeroIndent() |
getCurrentFormattingMode, getInstance, getInstance, reformatTextWithContextpublic CodeStyleManagerImpl(Project project)
public Project getProject()
CodeStyleManagergetProject in class CodeStyleManagerpublic PsiElement reformat(PsiElement element) throws IncorrectOperationException
CodeStyleManagerreformat in class CodeStyleManagerelement - the element to reformat.IncorrectOperationException - if the file to reformat is read-only.CodeStyleManager.reformatText(PsiFile, int, int)public PsiElement reformat(PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException
CodeStyleManagerreformat in class CodeStyleManagerelement - the element to reformat.canChangeWhiteSpacesOnly - if true, only reformatting is performed; if false,
braces and import statements also can be modified if necessary.IncorrectOperationException - if the file to reformat is read-only.CodeStyleManager.reformatText(PsiFile, int, int)public PsiElement reformatRange(PsiElement element, int startOffset, int endOffset, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException
CodeStyleManagerreformatRange in class CodeStyleManagerelement - the element to reformat.startOffset - the start offset in the document of the text range to reformat.endOffset - the end offset in the document of the text range to reformat.canChangeWhiteSpacesOnly - if true, only reformatting is performed; if false,
braces and import statements also can be modified if necessary.IncorrectOperationException - if the file to reformat is read-only.CodeStyleManager.reformatText(PsiFile, int, int)public PsiElement reformatRange(PsiElement element, int startOffset, int endOffset) throws IncorrectOperationException
CodeStyleManagerreformatRange in class CodeStyleManagerelement - the element to reformat.startOffset - the start offset in the document of the text range to reformat.endOffset - the end offset in the document of the text range to reformat.IncorrectOperationException - if the file to reformat is read-only.CodeStyleManager.reformatText(PsiFile, int, int)public void reformatText(PsiFile file, int startOffset, int endOffset) throws IncorrectOperationException
CodeStyleManagerCodeStyleManager.reformatText(PsiFile, Collection) with the single range defined by the given offsets.reformatText in class CodeStyleManagerfile - the file to reformat.startOffset - the start of the text range to reformat.endOffset - the end of the text range to reformat.IncorrectOperationException - if the file to reformat is read-only.public void reformatText(PsiFile file, java.util.Collection<TextRange> ranges) throws IncorrectOperationException
CodeStyleManagerCodeStyleManager.reformatRange(PsiElement, int, int) but invalidates the
PSI structure for the file.reformatText in class CodeStyleManagerfile - the file to reformatranges - ranges to processIncorrectOperationException - if the file to reformat is read-only.public void reformatTextWithContext(PsiFile file, ChangedRangesInfo info) throws IncorrectOperationException
reformatTextWithContext in class CodeStyleManagerIncorrectOperationExceptionpublic void reformatText(PsiFile file, java.util.Collection<? extends TextRange> ranges, Editor editor) throws IncorrectOperationException
IncorrectOperationExceptionpublic static void formatRanges(PsiFile file, FormatTextRanges ranges, java.lang.Runnable formatAction)
public void reformatNewlyAddedElement(ASTNode parent, ASTNode addedElement) throws IncorrectOperationException
CodeStyleManagerreformatNewlyAddedElement in class CodeStyleManagerparent - - added element parentaddedElement - - new elementIncorrectOperationException - if the operation fails for some reason (for example,
the file is read-only).public int adjustLineIndent(PsiFile file, int offset) throws IncorrectOperationException
CodeStyleManageradjustLineIndent in class CodeStyleManagerfile - the file to reformat.offset - the offset the line at which should be reformatted.IncorrectOperationException - if the file is read-only.CodeStyleManager.scheduleIndentAdjustment(Document, int)public int adjustLineIndent(Document document, int offset, FormattingMode mode)
FormattingModeAwareIndentAdjustermode.adjustLineIndent in interface FormattingModeAwareIndentAdjusterdocument - The document to adjust line indent in.offset - The offset in the document.mode - The mode: FormattingMode.ADJUST_INDENT or FormattingMode.ADJUST_INDENT_ON_ENTERpublic int adjustLineIndent(Document document, int offset)
CodeStyleManageradjustLineIndent in class CodeStyleManagerdocument - the document to reformat.offset - the offset the line at which should be reformatted.CodeStyleManager.scheduleIndentAdjustment(Document, int)public void adjustLineIndent(PsiFile file, TextRange rangeToAdjust) throws IncorrectOperationException
CodeStyleManageradjustLineIndent in class CodeStyleManagerfile - the file to reformat.rangeToAdjust - the range of text in which indents should be reformatted.IncorrectOperationException - if the file is read-only.public java.lang.String getLineIndent(PsiFile file, int offset)
CodeStyleManagergetLineIndent in class CodeStyleManagerfile - the file for which the indent should be calculated.offset - the offset for the line at which the indent should be calculated.null if it
was not possible to calculate the indent.public java.lang.String getLineIndent(PsiFile file, int offset, FormattingMode mode)
CodeStyleManagerCodeStyleManager.getLineIndent(PsiFile, int)getLineIndent in class CodeStyleManagerfile - the file for which the indent should be calculated.offset - the offset for the line at which the indent should be calculated.mode - the formatting mode FormattingModenull if it
was not possible to calculate the indent.public java.lang.String getLineIndent(Document document, int offset)
CodeStyleManagergetLineIndent in class CodeStyleManagerdocument - for which the indent should be calculated.null if it
was not possible to calculate the indent.@Deprecated public boolean isLineToBeIndented(PsiFile file, int offset)
isLineToBeIndented in class CodeStyleManagerpublic static TextRange insertNewLineIndentMarker(PsiFile file, Document document, int offset)
This method inserts that dummy comment (fallback to identifier
- Insert dummy text at the end of the blank line which white space symbols should be preserved;
- Perform formatting;
- Remove dummy text;
xxx, see createDummy(PsiFile))
if necessary.
Note: it's expected that the whole white space region that contains given offset is processed in a way that all
range markers registered for the given offset are expanded to the whole white space region.
E.g. there is a possible case that particular range marker serves for defining formatting range, hence, its start/end offsets
are updated correspondingly after current method call and whole white space region is reformatted.file - target PSI filedocument - target documentoffset - offset that defines end boundary of the target line text fragment (start boundary is the first line's symbol)null otherwiseIncorrectOperationException - if given file is read-onlypublic static PsiElement findWhiteSpaceNode(PsiFile file, int offset)
file - target fileoffset - offset that might point to white space element within the given PSI filenull otherwisepublic Indent getIndent(java.lang.String text, FileType fileType)
getIndent in class CodeStyleManagerpublic java.lang.String fillIndent(Indent indent, FileType fileType)
fillIndent in class CodeStyleManagerpublic Indent zeroIndent()
zeroIndent in class CodeStyleManagerpublic boolean isSequentialProcessingAllowed()
CodeStyleManagerisSequentialProcessingAllowed in class CodeStyleManagertrue if 'sequential' formatting is allowed now; false otherwisepublic static void setSequentialProcessingAllowed(boolean allowed)
sequential processing should be allowed.
Current approach is not allow to stop sequential processing for more than predefine amount of time (couple of seconds).
That means that call to this method with 'true' argument is not mandatory for successful processing even
if this method is called with 'false' argument before.allowed - flag that defines if sequential processing should be allowedpublic void performActionWithFormatterDisabled(java.lang.Runnable r)
CodeStyleManagerperformActionWithFormatterDisabled in class CodeStyleManagerr - the operation to run.public <T extends java.lang.Throwable> void performActionWithFormatterDisabled(ThrowableRunnable<T> r) throws T extends java.lang.Throwable
performActionWithFormatterDisabled in class CodeStyleManagerT extends java.lang.Throwablepublic <T> T performActionWithFormatterDisabled(Computable<T> r)
performActionWithFormatterDisabled in class CodeStyleManagerpublic FormattingMode getCurrentFormattingMode()
getCurrentFormattingMode in interface FormattingModeAwareIndentAdjusterpublic int getSpacing(PsiFile file, int offset)
CodeStyleManager-1, if required block cannot be found at provided offset,
or spacing cannot be calculated due to some other reason.getSpacing in class CodeStyleManagerpublic int getMinLineFeeds(PsiFile file, int offset)
CodeStyleManager-1, if required block cannot be found at provided offset,
or spacing cannot be calculated due to some other reason.getMinLineFeeds in class CodeStyleManagerpublic void runWithDocCommentFormattingDisabled(PsiFile file, java.lang.Runnable runnable)
CodeStyleManagerrunWithDocCommentFormattingDisabled in class CodeStyleManagerfile - The file for which doc comment formatting should be temporarily disabled.runnable - The runnable to run.public DocCommentSettings getDocCommentSettings(PsiFile file)
getDocCommentSettings in class CodeStyleManagerpublic void scheduleIndentAdjustment(Document document, int offset)
CodeStyleManagerFormatterBasedIndentAdjuster.MAX_SYNCHRONOUS_ADJUSTMENT_DOC_SIZE
by scheduling it to a time when the document is committed. Uses formatter to calculate the new indent on a
background thread. Only the actual change is done on EDT: the old indent is replaced with a new indent string
directly in the document. Doesn't commit the document, thus a subsequent PsiDocumentManager.commitDocument(Document)
may be required.
Note: visually it may lead to a text jump which becomes more obvious, more time it takes to calculate the
new indent using a formatting model. A better way to handle large documents is to implement LineIndentProvider returning a non-null value when possible.
scheduleIndentAdjustment in class CodeStyleManagerdocument - The document to be modified.offset - The offset in the line whose indent is to be adjusted.