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, reformatTextWithContext
public CodeStyleManagerImpl(Project project)
public Project getProject()
CodeStyleManager
getProject
in class CodeStyleManager
public PsiElement reformat(PsiElement element) throws IncorrectOperationException
CodeStyleManager
reformat
in class CodeStyleManager
element
- 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
CodeStyleManager
reformat
in class CodeStyleManager
element
- 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
CodeStyleManager
reformatRange
in class CodeStyleManager
element
- 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
CodeStyleManager
reformatRange
in class CodeStyleManager
element
- 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
CodeStyleManager
CodeStyleManager.reformatText(PsiFile, Collection)
with the single range defined by the given offsets.reformatText
in class CodeStyleManager
file
- 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
CodeStyleManager
CodeStyleManager.reformatRange(PsiElement, int, int)
but invalidates the
PSI structure for the file.reformatText
in class CodeStyleManager
file
- 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 CodeStyleManager
IncorrectOperationException
public void reformatText(PsiFile file, java.util.Collection<? extends TextRange> ranges, Editor editor) throws IncorrectOperationException
IncorrectOperationException
public static void formatRanges(PsiFile file, FormatTextRanges ranges, java.lang.Runnable formatAction)
public void reformatNewlyAddedElement(ASTNode parent, ASTNode addedElement) throws IncorrectOperationException
CodeStyleManager
reformatNewlyAddedElement
in class CodeStyleManager
parent
- - 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
CodeStyleManager
adjustLineIndent
in class CodeStyleManager
file
- 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)
FormattingModeAwareIndentAdjuster
mode
.adjustLineIndent
in interface FormattingModeAwareIndentAdjuster
document
- The document to adjust line indent in.offset
- The offset in the document.mode
- The mode: FormattingMode.ADJUST_INDENT
or FormattingMode.ADJUST_INDENT_ON_ENTER
public int adjustLineIndent(Document document, int offset)
CodeStyleManager
adjustLineIndent
in class CodeStyleManager
document
- 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
CodeStyleManager
adjustLineIndent
in class CodeStyleManager
file
- 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)
CodeStyleManager
getLineIndent
in class CodeStyleManager
file
- 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)
CodeStyleManager
CodeStyleManager.getLineIndent(PsiFile, int)
getLineIndent
in class CodeStyleManager
file
- 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 FormattingMode
null
if it
was not possible to calculate the indent.public java.lang.String getLineIndent(Document document, int offset)
CodeStyleManager
getLineIndent
in class CodeStyleManager
document
- 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 CodeStyleManager
public 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 CodeStyleManager
public java.lang.String fillIndent(Indent indent, FileType fileType)
fillIndent
in class CodeStyleManager
public Indent zeroIndent()
zeroIndent
in class CodeStyleManager
public boolean isSequentialProcessingAllowed()
CodeStyleManager
isSequentialProcessingAllowed
in class CodeStyleManager
true
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)
CodeStyleManager
performActionWithFormatterDisabled
in class CodeStyleManager
r
- the operation to run.public <T extends java.lang.Throwable> void performActionWithFormatterDisabled(ThrowableRunnable<T> r) throws T extends java.lang.Throwable
performActionWithFormatterDisabled
in class CodeStyleManager
T extends java.lang.Throwable
public <T> T performActionWithFormatterDisabled(Computable<T> r)
performActionWithFormatterDisabled
in class CodeStyleManager
public FormattingMode getCurrentFormattingMode()
getCurrentFormattingMode
in interface FormattingModeAwareIndentAdjuster
public 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 CodeStyleManager
public 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 CodeStyleManager
public void runWithDocCommentFormattingDisabled(PsiFile file, java.lang.Runnable runnable)
CodeStyleManager
runWithDocCommentFormattingDisabled
in class CodeStyleManager
file
- The file for which doc comment formatting should be temporarily disabled.runnable
- The runnable to run.public DocCommentSettings getDocCommentSettings(PsiFile file)
getDocCommentSettings
in class CodeStyleManager
public void scheduleIndentAdjustment(Document document, int offset)
CodeStyleManager
FormatterBasedIndentAdjuster.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 CodeStyleManager
document
- The document to be modified.offset
- The offset in the line whose indent is to be adjusted.