下面列出了怎么用com.intellij.psi.codeStyle.CodeStyleSettings的API类实例代码及写法,或者点击链接到github查看源代码。
@NotNull
public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) {
ASTNode node = element.getNode();
assert node != null;
PsiFile containingFile = element.getContainingFile();
ASTNode astNode = containingFile.getNode();
assert astNode != null;
BashProjectSettings projectSettings = BashProjectSettings.storedSettings(containingFile.getProject());
if (!projectSettings.isFormatterEnabled()) {
return FormattingModelProvider.createFormattingModelForPsiFile(containingFile,
new NoOpBlock(astNode), settings);
}
return FormattingModelProvider.createFormattingModelForPsiFile(containingFile,
new BashBlock(astNode, null, Indent.getAbsoluteNoneIndent(), null, settings), settings);
}
/**
* We have to override {@link com.intellij.formatting.templateLanguages.TemplateLanguageFormattingModelBuilder#createModel}
* since after we delegate to some templated languages, those languages (xml/html for sure, potentially others)
* delegate right back to us to format the DustTypes.OUTER_TYPE token we tell them to ignore,
* causing an stack-overflowing loop.
*/
@NotNull
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
final PsiFile file = element.getContainingFile();
Block rootBlock;
ASTNode node = element.getNode();
if (node.getElementType() == DustFileViewProvider.OUTER_TYPE) {
// If we're looking at a DustTypes.HTML element, then we've been invoked by our templated
// language. Make a dummy block to allow that formatter to continue
return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings);
} else {
rootBlock = getRootBlock(file, file.getViewProvider(), settings);
}
return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, file.getFileType(), file);
}
/**
* Create spacing builder using given settings.
*/
public static SpacingBuilder createSpacingBuilder(CodeStyleSettings settings) {
CommonCodeStyleSettings protoSettings = settings.getCommonSettings(ProtoLanguage.INSTANCE);
SpacingBuilder builder = new SpacingBuilder(settings, ProtoLanguage.INSTANCE);
builder.around(ASSIGN).spacing(1, 1, 0, false, 0);
builder.before(SEMICOLON).spacing(0, 0, 0, false, 0);
builder.after(LINE_COMMENT).spacing(0, 0, 1, true, 2);
builder.after(LCURLY).spacing(0, 0, 1, true, 2);
builder.before(RCURLY).spacing(0, 0, 1, true, 2);
builder.after(LPAREN).spacing(0, 0, 0, false, 0);
builder.before(RPAREN).spacing(0, 0, 0, false, 0);
builder.after(LSQUARE).spacing(0, 0, 0, false, 0);
builder.before(RSQUARE).spacing(0, 0, 0, false, 0);
builder.before(LT).spacing(0, 0, 0, false, 0);
builder.after(LT).spacing(0, 0, 0, false, 0);
builder.before(GT).spacing(0, 0, 0, false, 0);
builder.before(COMMA).spacing(0, 0, 0, false, 0);
builder.before(SEMICOLON).spacing(0, 0, 0, false, 0);
builder.after(COMMA).spacing(1, 1, 0, false, 0);
return builder;
}
public FormatProcessor(final FormattingDocumentModel model, Block block, FormatOptions options, @Nonnull FormattingProgressCallback callback) {
myProgressCallback = callback;
CommonCodeStyleSettings.IndentOptions defaultIndentOption = options.myIndentOptions;
CodeStyleSettings settings = options.mySettings;
BlockIndentOptions blockIndentOptions = new BlockIndentOptions(settings, defaultIndentOption, block);
myDocument = model.getDocument();
myReformatContext = options.isReformatWithContext();
final InitialInfoBuilder builder = prepareToBuildBlocksSequentially(block, model, options, defaultIndentOption, myProgressCallback);
myWrapState = new WrapBlocksState(builder, blockIndentOptions);
FormatTextRanges ranges = options.myAffectedRanges;
if (ranges != null && myReformatContext) {
AdjustFormatRangesState adjustRangesState = new AdjustFormatRangesState(block, ranges, model);
myStateProcessor = new StateProcessor(adjustRangesState);
myStateProcessor.setNextState(myWrapState);
}
else {
myStateProcessor = new StateProcessor(myWrapState);
}
}
@Override
public void format(final FormattingModel model, final CodeStyleSettings settings, final CommonCodeStyleSettings.IndentOptions indentOptions, final FormatTextRanges affectedRanges)
throws IncorrectOperationException {
try {
validateModel(model);
SequentialTask task = new MyFormattingTask() {
@Nonnull
@Override
protected FormatProcessor buildProcessor() {
FormatOptions options = new FormatOptions(settings, indentOptions, affectedRanges);
FormatProcessor processor = new FormatProcessor(model.getDocumentModel(), model.getRootBlock(), options, getProgressCallback());
processor.format(model, true);
return processor;
}
};
execute(task);
}
catch (FormattingModelInconsistencyException e) {
LOG.error(e);
}
}
protected void defaultSettings() {
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject());
settings.ALIGN_MULTILINE_PARAMETERS = true;
settings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS = false;
settings.ALIGN_MULTILINE_FOR = true;
settings.ALIGN_MULTILINE_BINARY_OPERATION = false;
settings.ALIGN_MULTILINE_TERNARY_OPERATION = false;
settings.ALIGN_MULTILINE_THROWS_LIST = false;
settings.ALIGN_MULTILINE_EXTENDS_LIST = false;
settings.ALIGN_MULTILINE_PARENTHESIZED_EXPRESSION = false;
settings.DO_NOT_INDENT_TOP_LEVEL_CLASS_MEMBERS = false;
getSettings().SPACE_BEFORE_ANOTATION_PARAMETER_LIST = false;
getSettings().SPACE_AROUND_ASSIGNMENT_OPERATORS = true;
getSettings().SPACE_WITHIN_ANNOTATION_PARENTHESES = false;
getSettings().SPACE_AROUND_ASSIGNMENT_OPERATORS = true;
}
@NotNull
public FormattingModel createModel(final PsiElement psiElement, final CodeStyleSettings codeStyleSettings) {
return new FormattingModel() {
private FormattingModel myModel;
{
myModel = FormattingModelProvider.createFormattingModelForPsiFile(psiElement.getContainingFile(), new CppBlock(psiElement), codeStyleSettings);
}
@NotNull
public Block getRootBlock() {
return myModel.getRootBlock();
}
@NotNull
public FormattingDocumentModel getDocumentModel() {
return myModel.getDocumentModel();
}
public TextRange replaceWhiteSpace(TextRange textRange, String whiteSpace) {
return myModel.replaceWhiteSpace(textRange, whiteSpace);
}
public TextRange shiftIndentInsideRange(TextRange range, int indent) {
return myModel.shiftIndentInsideRange(range, indent);
}
public void commitChanges() {
myModel.commitChanges();
}
};
}
protected TemplateLanguageBlock(@Nonnull ASTNode node, @Nullable Wrap wrap, @Nullable Alignment alignment,
@Nonnull TemplateLanguageBlockFactory blockFactory,
@Nonnull CodeStyleSettings settings,
@Nullable List<DataLanguageBlockWrapper> foreignChildren) {
super(node, wrap, alignment);
myBlockFactory = blockFactory;
myForeignChildren = foreignChildren;
mySettings = settings;
}
@Nonnull
public static FormattingModel buildModel(@Nonnull FormattingModelBuilder builder,
@Nonnull PsiElement element,
@Nonnull TextRange range,
@Nonnull CodeStyleSettings settings,
@Nonnull FormattingMode mode) {
if (builder instanceof FormattingModelBuilderEx) {
return ((FormattingModelBuilderEx)builder).createModel(element, range, settings, mode);
}
else {
return builder.createModel(element, settings);
}
}
@Override
public TemplateLanguageBlock createTemplateLanguageBlock(
@NotNull ASTNode node,
@Nullable Wrap wrap,
@Nullable Alignment alignment,
@Nullable List<DataLanguageBlockWrapper> foreignChildren,
@NotNull CodeStyleSettings codeStyleSettings) {
final FormattingDocumentModelImpl documentModel =
FormattingDocumentModelImpl.createOn(node.getPsi().getContainingFile());
if (node.getPsi() instanceof TagElement) {
return new SoyTagBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
} else if(node.getPsi() instanceof TagBlockElement) {
return new SoyTagBlockBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
} else if (node.getPsi() instanceof SoyStatementList) {
return new SoyStatementListBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
} else {
return new SoyBlock(
this,
codeStyleSettings,
node,
foreignChildren,
new HtmlPolicy(codeStyleSettings, documentModel));
}
}
@NotNull
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
final PsiFile file = element.getContainingFile();
if (element.getNode().getElementType() == SoyTypes.OTHER) {
return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings);
} else {
return new DocumentBasedFormattingModel(
getRootBlock(file, file.getViewProvider(), settings),
element.getProject(),
settings,
file.getFileType(),
file);
}
}
private int getFieldValue(CodeStyleSettings settings) {
try {
if (myTargetClass != null) {
return myTarget.getInt(settings.getCustomSettings(myTargetClass));
}
CommonCodeStyleSettings commonSettings = settings.getCommonSettings(getDefaultLanguage());
return myTarget.getInt(commonSettings);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public SoyBlock(
@NotNull TemplateLanguageBlockFactory blockFactory,
@NotNull CodeStyleSettings settings,
@NotNull ASTNode node,
@Nullable List<DataLanguageBlockWrapper> foreignChildren,
HtmlPolicy htmlPolicy) {
super(blockFactory, settings, node, foreignChildren);
myHtmlPolicy = htmlPolicy;
}
protected HaxeBlock(ASTNode node,
Wrap wrap,
Alignment alignment,
CodeStyleSettings settings) {
super(node, wrap, alignment);
mySettings = settings;
final HaxeCodeStyleSettings haxeCodeStyleSettings = mySettings.getCustomSettings(HaxeCodeStyleSettings.class);
myIndentProcessor = new HaxeIndentProcessor(mySettings.getCommonSettings(HaxeLanguage.INSTANCE));
mySpacingProcessor = new HaxeSpacingProcessor(node, mySettings.getCommonSettings(HaxeLanguage.INSTANCE), haxeCodeStyleSettings);
myWrappingProcessor = new HaxeWrappingProcessor(node, mySettings.getCommonSettings(HaxeLanguage.INSTANCE));
myAlignmentProcessor = new HaxeAlignmentProcessor(node, mySettings.getCommonSettings(HaxeLanguage.INSTANCE));
myIndent = myIndentProcessor.getChildIndent(myNode);
}
private static void reformatCommentKeepingEmptyTags(@Nonnull PsiFile file, @Nonnull Project project, int start, int end) {
CodeStyleSettings tempSettings = CodeStyle.getSettings(file).clone();
LanguageCodeStyleSettingsProvider langProvider = LanguageCodeStyleSettingsProvider.forLanguage(file.getLanguage());
if (langProvider != null) {
DocCommentSettings docCommentSettings = langProvider.getDocCommentSettings(tempSettings);
docCommentSettings.setRemoveEmptyTags(false);
}
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
CodeStyle.doWithTemporarySettings(project, tempSettings, () -> codeStyleManager.reformatText(file, start, end));
}
@RequiredUIAccess
@Override
public void apply() throws ConfigurationException {
if (!myApplyCompleted) {
try {
super.apply();
for (CodeStyleScheme scheme : new ArrayList<>(myModel.getSchemes())) {
final boolean isDefaultModified = CodeStyleSchemesModel.cannotBeModified(scheme) && isSchemeModified(scheme);
if (isDefaultModified) {
CodeStyleScheme newscheme = myModel.createNewScheme(null, scheme);
CodeStyleSettings settingsWillBeModified = scheme.getCodeStyleSettings();
CodeStyleSettings notModifiedSettings = settingsWillBeModified.clone();
((CodeStyleSchemeImpl)scheme).setCodeStyleSettings(notModifiedSettings);
((CodeStyleSchemeImpl)newscheme).setCodeStyleSettings(settingsWillBeModified);
myModel.addScheme(newscheme, false);
if (myModel.getSelectedScheme() == scheme) {
myModel.selectScheme(newscheme, this);
}
}
}
for (CodeStyleConfigurableWrapper panel : myPanels) {
panel.applyPanel();
}
myModel.apply();
EditorFactory.getInstance().refreshAllEditors();
}
finally {
myApplyCompleted = true;
}
}
}
private Context(@Nonnull Rearranger<E> rearranger,
@Nonnull Collection<ArrangementEntryWrapper<E>> wrappers,
@Nonnull Document document,
@Nonnull List<ArrangementSectionRule> sectionRules,
@Nonnull List<? extends ArrangementMatchRule> rulesByPriority,
@Nonnull CodeStyleSettings settings, @Nonnull Changer changer)
{
this.rearranger = rearranger;
this.wrappers = wrappers;
this.document = document;
this.sectionRules = sectionRules;
this.rulesByPriority = rulesByPriority;
this.settings = settings;
this.changer = changer;
}
private List<LineIndentInfo> calcLineIndentInfo() {
if (myDocument == null) return null;
CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myProject);
FormattingModelBuilder modelBuilder = LanguageFormatting.INSTANCE.forContext(myFile);
if (modelBuilder == null) return null;
FormattingModel model = modelBuilder.createModel(myFile, settings);
Block rootBlock = model.getRootBlock();
return new FormatterBasedLineIndentInfoBuilder(myDocument, rootBlock).build();
}
public static Map<Integer, CsvColumnInfo<ASTNode>> createColumnInfoMap(ASTNode root, CodeStyleSettings settings) {
Map<Integer, CsvColumnInfo<ASTNode>> columnInfoMap = new HashMap<>();
ASTNode child = root.getFirstChildNode();
int row = 0;
while (child != null) {
if (child.getElementType() == CsvTypes.RECORD) {
Integer column = 0;
ASTNode subChild = child.getFirstChildNode();
while (subChild != null) {
if (subChild.getElementType() == CsvTypes.FIELD) {
int length = getTextLength(subChild, settings);
if (!columnInfoMap.containsKey(column)) {
columnInfoMap.put(column, new CsvColumnInfo(column, length, row));
} else if (columnInfoMap.get(column).getMaxLength() < length) {
columnInfoMap.get(column).setMaxLength(length, row);
}
columnInfoMap.get(column).addElement(subChild);
++column;
}
subChild = subChild.getTreeNext();
}
++row;
}
child = child.getTreeNext();
}
return columnInfoMap;
}
protected void customizeOptionsPanel() {
if (myInsertOverrideAnnotationCheckbox != null && myIsInsertOverrideVisible) {
CodeStyleSettings styleSettings = CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings();
myInsertOverrideAnnotationCheckbox.setSelected(styleSettings.INSERT_OVERRIDE_ANNOTATION);
}
if (myCopyJavadocCheckbox != null) {
myCopyJavadocCheckbox.setSelected(PropertiesComponent.getInstance().isTrueValue(PROP_COPYJAVADOC));
}
}
private static boolean isToggleNodeModified(MyToggleTreeNode childNode, final CodeStyleSettings settings) {
try {
BooleanOptionKey key = (BooleanOptionKey)childNode.getKey();
return childNode.isSelected() != key.getValue(settings);
}
catch (IllegalArgumentException | IllegalAccessException e) {
LOG.error(e);
}
return false;
}
@Nonnull
@Override
public CharSequence adjustWhiteSpaceIfNecessary(@Nonnull CharSequence whiteSpaceText,
@Nonnull CharSequence text,
int startOffset,
int endOffset, CodeStyleSettings codeStyleSettings, ASTNode nodeAfter)
{
// Does nothing
return whiteSpaceText;
}
@NotNull
@Override
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(ProtoLanguage.INSTANCE);
ASTNode fileNode = containingFile.getNode();
Wrap wrap = Wrap.createWrap(WrapType.NONE, false);
Alignment alignment = Alignment.createAlignment();
ProtoFileBlock block = new ProtoFileBlock(fileNode, wrap, alignment, settings);
return FormattingModelProvider.createFormattingModelForPsiFile(containingFile, block, settings);
}
@NotNull
@Override
public FormattingModel createModel(PsiElement psiElement, CodeStyleSettings codeStyleSettings) {
return FormattingModelProvider.createFormattingModelForPsiFile(
psiElement.getContainingFile(),
new GLSLFormattingBlock(psiElement.getNode(), null, null, createSpacingBuilder(codeStyleSettings)),
codeStyleSettings);
}
@Override
public Object getValue(CodeStyleSettings settings) {
try {
int value = field.getInt(getSettings(settings));
for (int i = 0; i < values.length; i++) {
if (values[i] == value) return options[i];
}
LOG.error("Invalid option value " + value + " for " + field.getName());
}
catch (IllegalAccessException ignore) {
}
return null;
}
private void resetMyTreeNode(MyToggleTreeNode childNode, final CodeStyleSettings settings) {
try {
BooleanOptionKey key = (BooleanOptionKey)childNode.getKey();
childNode.setSelected(key.getValue(settings));
childNode.setEnabled(key.isEnabled());
}
catch (IllegalArgumentException | IllegalAccessException e) {
LOG.error(e);
}
}
public void setTestStyleSettings(int indent) {
Project project = getProject();
CodeStyleSettings currSettings = CodeStyleSettingsManager.getSettings(project);
assertNotNull(currSettings);
CodeStyleSettings tempSettings = currSettings.clone();
CodeStyleSettings.IndentOptions indentOptions = tempSettings.getIndentOptions(HaxeFileType.HAXE_FILE_TYPE);
indentOptions.INDENT_SIZE = indent;
assertNotNull(indentOptions);
CodeStyleSettingsManager.getInstance(project).setTemporarySettings(tempSettings);
}
@Override
protected void resetImpl(final CodeStyleSettings settings) {
TreeModel treeModel = myTreeTable.getTree().getModel();
TreeNode root = (TreeNode)treeModel.getRoot();
resetNode(root, settings);
((DefaultTreeModel)treeModel).nodeChanged(root);
}
CypherBlock(@NotNull ASTNode node,
@Nullable Alignment alignment,
@Nullable Indent indent,
@Nullable Wrap wrap,
@NotNull CodeStyleSettings settings,
@NotNull SpacingBuilder spacingBuilder) {
this.node = node;
this.codeStyleSettings = settings;
this.wrap = wrap;
this.indent = indent;
this.alignment = alignment;
this.spacingBuilder = spacingBuilder;
}
public static XmlCodeStyleSettings getXmlSettings(Project project) {
CodeStyleSettings settings = getSettings(project);
if (settings != null) {
return settings.getCustomSettings(XmlCodeStyleSettings.class);
}
return null;
}