private void applySettings(VirtualFile file) { if (file == null) return; if (!Utils.isEnabled(CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings())) return; // Get editorconfig settings final String filePath = Utils.getFilePath(myProject, file); final SettingsProviderComponent settingsProvider = SettingsProviderComponent.getInstance(); final List<EditorConfig.OutPair> outPairs = settingsProvider.getOutPairs(myProject, filePath); // Apply trailing spaces setting final String trimTrailingWhitespace = Utils.configValueForKey(outPairs, trimTrailingWhitespaceKey); applyConfigValueToUserData( file, TrailingSpacesStripper.OVERRIDE_STRIP_TRAILING_SPACES_KEY, trimTrailingWhitespaceKey, trimTrailingWhitespace, trimMap); // Apply final newline setting final String insertFinalNewline = Utils.configValueForKey(outPairs, insertFinalNewlineKey); applyConfigValueToUserData( file, TrailingSpacesStripper.OVERRIDE_ENSURE_NEWLINE_KEY, insertFinalNewlineKey, insertFinalNewline, newlineMap); }
@Nullable @Override public JComponent createComponent() { myEnabled = new JBCheckBox("Enable EditorConfig support"); final JPanel result = new JPanel(); result.setLayout(new BoxLayout(result, BoxLayout.LINE_AXIS)); final JPanel panel = new JPanel(new VerticalFlowLayout()); result.setBorder(IdeBorderFactory.createTitledBorder("EditorConfig", false)); panel.add(myEnabled); final JLabel warning = new JLabel("EditorConfig may override the IDE code style settings"); warning.setFont(UIUtil.getLabelFont(UIUtil.FontSize.SMALL)); warning.setBorder(IdeBorderFactory.createEmptyBorder(0, 20, 0, 0)); panel.add(warning); panel.setAlignmentY(Component.TOP_ALIGNMENT); result.add(panel); final JButton export = new JButton("Export"); export.addActionListener( (event) -> { final Component parent = UIUtil.findUltimateParent(result); if (parent instanceof IdeFrame) { Utils.export(((IdeFrame) parent).getProject()); } }); export.setAlignmentY(Component.TOP_ALIGNMENT); result.add(export); return result; }
private <T> void applyConfigValueToUserData( VirtualFile file, Key<T> userDataKey, String editorConfigKey, String configValue, Map<String, T> configMap) { if (configValue.isEmpty()) return; final T data = configMap.get(configValue); if (data == null) { Utils.invalidConfigMessage(myProject, configValue, editorConfigKey, file.getCanonicalPath()); } else { file.putUserData(userDataKey, data); } }