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.javascript.eclipse;
12  
13  import java.util.SortedSet;
14  import java.util.TreeSet;
15  import java.util.prefs.Preferences;
16  
17  import javax.swing.text.StyledDocument;
18  
19  import org.apache.commons.lang3.tuple.Pair;
20  import org.netbeans.api.annotations.common.NonNull;
21  
22  import de.funfried.netbeans.plugins.external.formatter.eclipse.AbstractEclipseFormatJob;
23  import de.funfried.netbeans.plugins.external.formatter.exceptions.CannotLoadConfigurationException;
24  import de.funfried.netbeans.plugins.external.formatter.exceptions.ConfigReadException;
25  import de.funfried.netbeans.plugins.external.formatter.exceptions.FormattingFailedException;
26  import de.funfried.netbeans.plugins.external.formatter.exceptions.ProfileNotFoundException;
27  import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
28  
29  /**
30   * Eclipse formatter implementation of the {@link AbstractEclipseFormatJob} to
31   * format a given document using the {@link EclipseJavascriptFormatterWrapper}.
32   *
33   * @author markiewb
34   * @author bahlef
35   */
36  class EclipseFormatJob extends AbstractEclipseFormatJob {
37  	/** The {@link EclipseJavascriptFormatterWrapper} implementation. */
38  	private final EclipseJavascriptFormatterWrapper formatter;
39  
40  	/**
41  	 * Package private constructor to create a new instance of {@link EclipseFormatJob}.
42  	 *
43  	 * @param document the {@link StyledDocument} which sould be formatted
44  	 * @param formatter the {@link EclipseJavascriptFormatterWrapper} to use
45  	 * @param changedElement an optional range as a {@link Pair} object defining the offsets which should be formatted
46  	 */
47  	EclipseFormatJob(StyledDocument document, EclipseJavascriptFormatterWrapper formatter, Pair<Integer, Integer> changedElement) {
48  		super(document, singletonSortedSet(changedElement));
49  
50  		this.formatter = formatter;
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	@Override
57  	protected String getFormattedContent(Preferences pref, String formatterFile, String formatterProfile, String code)
58  			throws ConfigReadException, ProfileNotFoundException, CannotLoadConfigurationException, FormattingFailedException {
59  		return formatter.format(formatterFile, formatterProfile, code, getLineFeed(pref), changedElements != null ? changedElements.first() : null);
60  	}
61  
62  	/**
63  	 * {@inheritDoc}
64  	 */
65  	@Override
66  	protected String getFormatterFile(Preferences pref) {
67  		return EclipseJavascriptFormatterSettings.getEclipseFormatterFile(pref, document);
68  	}
69  
70  	/**
71  	 * {@inheritDoc}
72  	 */
73  	@Override
74  	protected String getFormatterProfile(Preferences pref) {
75  		return pref.get(EclipseJavascriptFormatterSettings.ACTIVE_PROFILE, "");
76  	}
77  
78  	/**
79  	 * {@inheritDoc}
80  	 */
81  	@Override
82  	protected String getLineFeed(Preferences pref) {
83  		String lineFeedSetting = pref.get(EclipseJavascriptFormatterSettings.LINEFEED, "");
84  		return Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
85  	}
86  
87  	/**
88  	 * {@inheritDoc}
89  	 */
90  	@NonNull
91  	@Override
92  	protected String getNotificationMessageForEclipseFormatterConfigurationFileType(String formatterFile, String formatterProfile) {
93  		String msg = "";
94  		if (EclipseJavascriptFormatterSettings.isWorkspaceMechanicFile(formatterFile)) {
95  			//Workspace mechanic file
96  			msg = String.format("Using %s", formatterFile);
97  		} else if (EclipseJavascriptFormatterSettings.isXMLConfigurationFile(formatterFile)) {
98  			//XML file
99  			msg = String.format("Using profile '%s' from %s", formatterProfile, formatterFile);
100 		} else if (EclipseJavascriptFormatterSettings.isProjectSetting(formatterFile)) {
101 			//org.eclipse.jdt.core.prefs
102 			msg = String.format("Using %s", formatterFile);
103 		}
104 
105 		return msg;
106 	}
107 
108 	/**
109 	 * Creates and returns a {@link SortedSet} within the given {@link Pair} of {@link Integer}s or {@code null}
110 	 * if the given {@link Pair} was {@code null}.
111 	 *
112 	 * @param pair the {@link Pair} which should be added to the {@link SortedSet}
113 	 *
114 	 * @return a {@link SortedSet} within the given {@link Pair} of {@link Integer}s or {@code null}
115 	 *         if the given {@link Pair} was {@code null}
116 	 */
117 	private static SortedSet<Pair<Integer, Integer>> singletonSortedSet(Pair<Integer, Integer> pair) {
118 		if (pair == null) {
119 			return null;
120 		}
121 
122 		SortedSet<Pair<Integer, Integer>> sortedSet = new TreeSet<>();
123 		sortedSet.add(pair);
124 
125 		return sortedSet;
126 	}
127 }