use of com.intellij.formatting.FormattingModelBuilder in project intellij-community by JetBrains.
the class AbstractNewLineBlocksIteratorTest method newLineBlockIterator.
@NotNull
protected static Iterator<Block> newLineBlockIterator() {
FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(myFile);
Assert.assertNotNull(builder);
CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings();
FormattingModel model = builder.createModel(myFile, settings);
Block root = model.getRootBlock();
Document document = PsiDocumentManager.getInstance(getProject()).getDocument(myFile);
Assert.assertNotNull(document);
return new NewLineBlocksIterator(root, document);
}
use of com.intellij.formatting.FormattingModelBuilder in project intellij-community by JetBrains.
the class CodeStyleManagerRunnable method perform.
public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) {
if (file instanceof PsiCompiledFile) {
file = ((PsiCompiledFile) file).getDecompiledPsiFile();
}
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject());
Document document = documentManager.getDocument(file);
if (document instanceof DocumentWindow) {
final DocumentWindow documentWindow = (DocumentWindow) document;
final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
if (!file.equals(topLevelFile)) {
if (range != null) {
range = documentWindow.injectedToHost(range);
}
if (offset != -1) {
offset = documentWindow.injectedToHost(offset);
}
return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow);
}
}
final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file);
if (templateFile != null) {
file = templateFile;
document = documentManager.getDocument(templateFile);
}
PsiElement element = null;
if (offset != -1) {
element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset);
if (element == null && offset != file.getTextLength()) {
return defaultValue;
}
if (isInsidePlainComment(offset, element)) {
return computeValueInsidePlainComment(file, offset, defaultValue);
}
}
final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder;
if (builder != null && elementBuilder != null) {
mySettings = CodeStyleSettingsManager.getSettings(myCodeStyleManager.getProject());
mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);
FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
myCodeStyleManager.setCurrentFormattingMode(myMode);
try {
myModel = buildModel(builder, file, document);
T result = doPerform(offset, range);
if (result != null) {
return result;
}
} finally {
myCodeStyleManager.setCurrentFormattingMode(currentMode);
}
}
return defaultValue;
}
use of com.intellij.formatting.FormattingModelBuilder in project intellij-community by JetBrains.
the class CodeStyleManagerRunnable method getSignificantRange.
private static TextRange getSignificantRange(final PsiFile file, final int offset) {
final ASTNode elementAtOffset = SourceTreeToPsiMap.psiElementToTree(CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset));
if (elementAtOffset == null) {
int significantRangeStart = CharArrayUtil.shiftBackward(file.getText(), offset - 1, "\n\r\t ");
return new TextRange(Math.max(significantRangeStart, 0), offset);
}
final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
if (builder != null) {
final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset);
if (textRange != null) {
return textRange;
}
}
final TextRange elementRange = elementAtOffset.getTextRange();
if (isWhiteSpace(elementAtOffset)) {
return extendRangeAtStartOffset(file, elementRange);
}
return elementRange;
}
Aggregations