use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-plugins by JetBrains.
the class InjectionsFormattingTest method doReformat.
private void doReformat(@NotNull PsiElement file) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
WriteCommandAction.runWriteCommandAction(getProject(), () -> {
codeStyleManager.reformat(file);
});
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class GenerateAntTest method checkBuildsEqual.
private void checkBuildsEqual(String generated, String expected) throws IncorrectOperationException {
final CodeStyleManager manager = CodeStyleManager.getInstance(myProject);
XmlTag genTag = XmlElementFactory.getInstance(myProject).createTagFromText(StringUtil.convertLineSeparators(generated));
XmlTag expTag = XmlElementFactory.getInstance(myProject).createTagFromText(StringUtil.convertLineSeparators(expected));
if (!tagsEqual(genTag, expTag)) {
genTag = (XmlTag) manager.reformat(manager.reformat(genTag));
expTag = (XmlTag) manager.reformat(manager.reformat(expTag));
assertEquals("Text mismatch: ", expTag.getText(), genTag.getText());
}
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class CommentUtil method getMinLineIndent.
public static Indent getMinLineIndent(Project project, Document document, int line1, int line2, FileType fileType) {
CharSequence chars = document.getCharsSequence();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
Indent minIndent = null;
for (int line = line1; line <= line2; line++) {
int lineStart = document.getLineStartOffset(line);
int textStart = CharArrayUtil.shiftForward(chars, lineStart, " \t");
if (textStart >= document.getTextLength()) {
textStart = document.getTextLength();
} else {
char c = chars.charAt(textStart);
// empty line
if (c == '\n' || c == '\r')
continue;
}
String space = chars.subSequence(lineStart, textStart).toString();
Indent indent = codeStyleManager.getIndent(space, fileType);
minIndent = minIndent != null ? indent.min(minIndent) : indent;
}
if (minIndent == null && line1 == line2 && line1 < document.getLineCount() - 1) {
return getMinLineIndent(project, document, line1 + 1, line1 + 1, fileType);
}
//}
return minIndent;
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class MoverWrapper method indentLinesIn.
private static void indentLinesIn(final Editor editor, final PsiFile file, final Document document, final Project project, RangeMarker range) {
final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
int line1 = editor.offsetToLogicalPosition(range.getStartOffset()).line;
int line2 = editor.offsetToLogicalPosition(range.getEndOffset()).line;
while (!lineContainsNonSpaces(document, line1) && line1 < line2) line1++;
while (!lineContainsNonSpaces(document, line2) && line1 < line2) line2--;
final FileViewProvider provider = file.getViewProvider();
PsiFile rootToAdjustIndentIn = provider.getPsi(provider.getBaseLanguage());
codeStyleManager.adjustLineIndent(rootToAdjustIndentIn, new TextRange(document.getLineStartOffset(line1), document.getLineStartOffset(line2)));
}
use of com.intellij.psi.codeStyle.CodeStyleManager in project intellij-community by JetBrains.
the class FixDocCommentAction method generateComment.
/**
* Generates a comment if possible.
* <p/>
* It's assumed that this method {@link PsiDocumentManager#commitDocument(Document) syncs} all PSI-document
* changes during the processing.
*
* @param anchor target element for which a comment should be generated
* @param editor target editor
* @param commenter commenter to use
* @param project current project
*/
private static void generateComment(@NotNull PsiElement anchor, @NotNull Editor editor, @NotNull CodeDocumentationProvider documentationProvider, @NotNull CodeDocumentationAwareCommenter commenter, @NotNull Project project) {
Document document = editor.getDocument();
int commentStartOffset = anchor.getTextRange().getStartOffset();
int lineStartOffset = document.getLineStartOffset(document.getLineNumber(commentStartOffset));
if (lineStartOffset > 0 && lineStartOffset < commentStartOffset) {
// Example:
// void test1() {
// }
// void test2() {
// <offset>
// }
// We want to insert the comment at the start of the line where 'test2()' is declared.
int nonWhiteSpaceOffset = CharArrayUtil.shiftBackward(document.getCharsSequence(), commentStartOffset - 1, " \t");
commentStartOffset = Math.max(nonWhiteSpaceOffset, lineStartOffset);
}
int commentBodyRelativeOffset = 0;
int caretOffsetToSet = -1;
StringBuilder buffer = new StringBuilder();
String commentPrefix = commenter.getDocumentationCommentPrefix();
if (commentPrefix != null) {
buffer.append(commentPrefix).append("\n");
commentBodyRelativeOffset += commentPrefix.length() + 1;
}
String linePrefix = commenter.getDocumentationCommentLinePrefix();
if (linePrefix != null) {
buffer.append(linePrefix);
commentBodyRelativeOffset += linePrefix.length();
caretOffsetToSet = commentStartOffset + commentBodyRelativeOffset;
}
buffer.append("\n");
commentBodyRelativeOffset++;
String commentSuffix = commenter.getDocumentationCommentSuffix();
if (commentSuffix != null) {
buffer.append(commentSuffix).append("\n");
}
if (buffer.length() <= 0) {
return;
}
document.insertString(commentStartOffset, buffer);
PsiDocumentManager docManager = PsiDocumentManager.getInstance(project);
docManager.commitDocument(document);
Pair<PsiElement, PsiComment> pair = documentationProvider.parseContext(anchor);
if (pair == null || pair.second == null) {
return;
}
String stub = documentationProvider.generateDocumentationContentStub(pair.second);
CaretModel caretModel = editor.getCaretModel();
if (stub != null) {
int insertionOffset = commentStartOffset + commentBodyRelativeOffset;
//if (CodeStyleSettingsManager.getSettings(project).JD_ADD_BLANK_AFTER_DESCRIPTION) {
// buffer.setLength(0);
// if (linePrefix != null) {
// buffer.append(linePrefix);
// }
// buffer.append("\n");
// buffer.append(stub);
// stub = buffer.toString();
//}
document.insertString(insertionOffset, stub);
docManager.commitDocument(document);
pair = documentationProvider.parseContext(anchor);
}
if (caretOffsetToSet >= 0) {
caretModel.moveToOffset(caretOffsetToSet);
}
if (pair == null || pair.second == null) {
return;
}
int start = Math.min(calcStartReformatOffset(pair.first), calcStartReformatOffset(pair.second));
int end = pair.second.getTextRange().getEndOffset();
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformatText(anchor.getContainingFile(), start, end);
int caretOffset = caretModel.getOffset();
if (caretOffset > 0 && caretOffset <= document.getTextLength()) {
char c = document.getCharsSequence().charAt(caretOffset - 1);
if (!StringUtil.isWhiteSpace(c)) {
document.insertString(caretOffset, " ");
caretModel.moveToOffset(caretOffset + 1);
}
}
}
Aggregations