View Javadoc
1   /*
2    * Copyright (c) 2020 bahlef.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v2.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v20.html
7    * Contributors:
8    * markiewb - initial API and implementation and/or initial documentation
9    * bahlef
10   */
11  package de.funfried.netbeans.plugins.external.formatter.ui.options;
12  
13  import java.util.prefs.Preferences;
14  
15  import javax.swing.text.Document;
16  
17  import org.apache.commons.lang3.StringUtils;
18  import org.netbeans.api.project.FileOwnerQuery;
19  import org.netbeans.api.project.Project;
20  import org.netbeans.api.project.ProjectUtils;
21  import org.netbeans.editor.BaseDocument;
22  import org.netbeans.modules.editor.NbEditorUtilities;
23  import org.openide.filesystems.FileObject;
24  import org.openide.loaders.DataObject;
25  import org.openide.util.NbPreferences;
26  
27  /**
28   * Settings utility class.
29   *
30   * @author markiewb
31   * @author bahlef
32   */
33  public class Settings {
34  	/**
35  	 * Property key prefix for the enabled formatter per mime type.
36  	 *
37  	 * @since 1.14
38  	 */
39  	public static final String ENABLED_FORMATTER_PREFIX = Settings.ENABLED_FORMATTER + ".";
40  
41  	/**
42  	 * Property key which defines the enabled formatter.
43  	 *
44  	 * @since 1.13
45  	 *
46  	 * @deprecated Use {@link #ENABLED_FORMATTER_PREFIX} instead
47  	 */
48  	@Deprecated
49  	public static final String ENABLED_FORMATTER = "enabledFormatter";
50  
51  	/**
52  	 * Property value of the default formatter to use (NetBeans internal formatter).
53  	 *
54  	 * @since 1.13
55  	 */
56  	public static final String DEFAULT_FORMATTER = "netbeans-formatter";
57  
58  	/** Property key which defines whether or not to use the settings of the external formatter in the NetBeans editor. */
59  	public static final String ENABLE_USE_OF_INDENTATION_SETTINGS = "enableIndentationSettings";
60  
61  	/** Property key which defines whether or not to use editorconfig in NetBeans if present. */
62  	public static final String ENABLE_USE_OF_EDITOR_CONFIG = "enableEditorConfig";
63  
64  	/** Property key which defines whether or not to use the {@link #OVERRIDE_TAB_SIZE_VALUE} instead of the one inside the external formatter configuration. */
65  	public static final String OVERRIDE_TAB_SIZE = "overrideTabSize";
66  
67  	/** Property key which defines the tab size which is used when {@link #OVERRIDE_TAB_SIZE} is actived. */
68  	public static final String OVERRIDE_TAB_SIZE_VALUE = "overrideTabSizeValue";
69  
70  	/** Property key which defines whether or not to show notifications after each formatting. */
71  	public static final String SHOW_NOTIFICATIONS = "showNotifications";
72  
73  	/** Property key which defines whether or not to use project specific settings instead of global formatter settings. */
74  	public static final String USE_PROJECT_SETTINGS = "useProjectSettings";
75  
76  	/**
77  	 * Private contructor because of static methods only.
78  	 */
79  	private Settings() {
80  	}
81  
82  	/**
83  	 * Returns the active {@link Preferences} object for the given {@link Document}, either the global
84  	 * preferences are returned or if the {@link Project} has a separate configuration it will return
85  	 * the project specific {@link Preferences}.
86  	 *
87  	 * @param document the document to get the {@link Preferences} for
88  	 *
89  	 * @return the active {@link Preferences} object for the given {@link Document}, either the global
90  	 *         preferences are returned or if the {@link Project} has a separate configuration it will return
91  	 *         the project specific {@link Preferences}
92  	 */
93  	public static Preferences getActivePreferences(Document document) {
94  		Preferences globalPreferences = NbPreferences.forModule(ExternalFormatterPanel.class);
95  		if (document != null) {
96  			DataObject dataObj = NbEditorUtilities.getDataObject(document);
97  			if (dataObj != null) {
98  				FileObject primaryFile = dataObj.getPrimaryFile();
99  				if (primaryFile != null) {
100 					Project project = FileOwnerQuery.getOwner(primaryFile);
101 					if (null != project) {
102 						Preferences projectPreferences = ProjectUtils.getPreferences(project, ExternalFormatterPanel.class, true);
103 						if (projectPreferences.getBoolean(USE_PROJECT_SETTINGS, false)) {
104 							return projectPreferences;
105 						}
106 					}
107 				}
108 			}
109 		}
110 
111 		return globalPreferences;
112 	}
113 
114 	/**
115 	 * Returns the real line feed characters for the given escaped line feed characters.
116 	 *
117 	 * @param lineFeedSetting escaped line feed characters, e.g. {@code \\n}
118 	 * @param fallback if the escaped line feed characters could not be matched to a real line feed setting
119 	 *
120 	 * @return the real line feed characters for the given escaped line feed characters, or the given
121 	 *         {@code fallback} if the escaped characters could not be matched to a real line feed setting
122 	 */
123 	public static String getLineFeed(String lineFeedSetting, String fallback) {
124 		String linefeed = fallback;
125 
126 		boolean usePlatformLinefeed = StringUtils.isBlank(lineFeedSetting);
127 		if (!usePlatformLinefeed) {
128 			switch (lineFeedSetting) {
129 				case "\\n":
130 					linefeed = BaseDocument.LS_LF;
131 					break;
132 				case "\\r":
133 					linefeed = BaseDocument.LS_CR;
134 					break;
135 				case "\\r\\n":
136 					linefeed = BaseDocument.LS_CRLF;
137 					break;
138 				default:
139 					linefeed = null;
140 					break;
141 			}
142 		}
143 
144 		return linefeed;
145 	}
146 }