|
| 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 | + * bahlef - initial API and implementation and/or initial documentation |
| 9 | + */ |
| 10 | +package de.funfried.netbeans.plugins.external.formatter.java.palantir; |
| 11 | + |
| 12 | +import java.util.SortedSet; |
| 13 | +import java.util.prefs.Preferences; |
| 14 | + |
| 15 | +import javax.swing.text.Document; |
| 16 | +import javax.swing.text.StyledDocument; |
| 17 | + |
| 18 | +import org.apache.commons.lang3.tuple.Pair; |
| 19 | +import org.netbeans.api.annotations.common.CheckForNull; |
| 20 | +import org.netbeans.api.annotations.common.NonNull; |
| 21 | +import org.netbeans.api.editor.guards.GuardedSectionManager; |
| 22 | +import org.netbeans.api.project.Project; |
| 23 | +import org.openide.util.NbBundle; |
| 24 | +import org.openide.util.lookup.ServiceProvider; |
| 25 | + |
| 26 | +import com.palantir.javaformat.java.JavaFormatterOptions; |
| 27 | + |
| 28 | +import de.funfried.netbeans.plugins.external.formatter.FormatJob; |
| 29 | +import de.funfried.netbeans.plugins.external.formatter.FormatterService; |
| 30 | +import de.funfried.netbeans.plugins.external.formatter.java.base.AbstractJavaFormatterService; |
| 31 | +import de.funfried.netbeans.plugins.external.formatter.java.palantir.ui.PalantirJavaFormatterOptionsPanel; |
| 32 | +import de.funfried.netbeans.plugins.external.formatter.ui.options.FormatterOptionsPanel; |
| 33 | +import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings; |
| 34 | + |
| 35 | +/** |
| 36 | + * Palantir implementation of the {@link AbstractJavaFormatterService}. |
| 37 | + * |
| 38 | + * @author bahlef |
| 39 | + */ |
| 40 | +@NbBundle.Messages({ |
| 41 | + "FormatterName=Palantir Java Code Formatter" |
| 42 | +}) |
| 43 | +@ServiceProvider(service = FormatterService.class, position = 2000) |
| 44 | +public class PalantirJavaFormatterService extends AbstractJavaFormatterService { |
| 45 | + /** The ID of this formatter service. */ |
| 46 | + public static final String ID = "palantir-java-formatter"; |
| 47 | + |
| 48 | + /** * The {@link PalantirJavaFormatterWrapper} implementation. */ |
| 49 | + private final PalantirJavaFormatterWrapper formatter = new PalantirJavaFormatterWrapper(); |
| 50 | + |
| 51 | + /** |
| 52 | + * {@inheritDoc} |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public boolean canHandle(Document document) { |
| 56 | + // Cannot handle guarded blocks properly due to a bug in the Google Java Code Formatter: |
| 57 | + // https://github.com/google/google-java-format/issues/433 |
| 58 | + if (document instanceof StyledDocument) { |
| 59 | + StyledDocument styledDoc = (StyledDocument) document; |
| 60 | + |
| 61 | + if (GuardedSectionManager.getInstance(styledDoc) != null) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return super.canHandle(document); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritDoc} |
| 71 | + */ |
| 72 | + @NonNull |
| 73 | + @Override |
| 74 | + public String getDisplayName() { |
| 75 | + return NbBundle.getMessage(PalantirJavaFormatterService.class, "FormatterName"); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritDoc} |
| 80 | + */ |
| 81 | + @NonNull |
| 82 | + @Override |
| 83 | + public String getId() { |
| 84 | + return ID; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritDoc} |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public FormatterOptionsPanel createOptionsPanel(Project project) { |
| 92 | + return new PalantirJavaFormatterOptionsPanel(project); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritDoc} |
| 97 | + */ |
| 98 | + @CheckForNull |
| 99 | + @Override |
| 100 | + public Integer getContinuationIndentSize(Document document) { |
| 101 | + if (document == null) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + Integer ret = null; |
| 106 | + |
| 107 | + Preferences preferences = Settings.getActivePreferences(document); |
| 108 | + if (isUseFormatterIndentationSettings(preferences)) { |
| 109 | + ret = 8; |
| 110 | + } |
| 111 | + |
| 112 | + return ret; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * {@inheritDoc} |
| 117 | + */ |
| 118 | + @CheckForNull |
| 119 | + @Override |
| 120 | + public Integer getIndentSize(Document document) { |
| 121 | + if (document == null) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + Integer ret = null; |
| 126 | + |
| 127 | + Preferences preferences = Settings.getActivePreferences(document); |
| 128 | + if (isUseFormatterIndentationSettings(preferences)) { |
| 129 | + ret = 4; |
| 130 | + } |
| 131 | + |
| 132 | + return ret; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * {@inheritDoc} |
| 137 | + */ |
| 138 | + @CheckForNull |
| 139 | + @Override |
| 140 | + public Integer getRightMargin(Document document) { |
| 141 | + if (document == null) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + return JavaFormatterOptions.Style.PALANTIR.maxLineLength(); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * {@inheritDoc} |
| 150 | + */ |
| 151 | + @Override |
| 152 | + protected FormatJob getFormatJob(StyledDocument document, SortedSet<Pair<Integer, Integer>> changedElements) { |
| 153 | + return new PalantirFormatJob(document, formatter, changedElements); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * {@inheritDoc} |
| 158 | + */ |
| 159 | + @CheckForNull |
| 160 | + @Override |
| 161 | + public Integer getSpacesPerTab(Document document) { |
| 162 | + if (document == null) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + Integer ret = null; |
| 167 | + |
| 168 | + Preferences preferences = Settings.getActivePreferences(document); |
| 169 | + if (isUseFormatterIndentationSettings(preferences)) { |
| 170 | + if (preferences.getBoolean(Settings.OVERRIDE_TAB_SIZE, true)) { |
| 171 | + ret = preferences.getInt(Settings.OVERRIDE_TAB_SIZE_VALUE, 4); |
| 172 | + } else { |
| 173 | + ret = 4; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return ret; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * {@inheritDoc} |
| 182 | + */ |
| 183 | + @CheckForNull |
| 184 | + @Override |
| 185 | + public Boolean isExpandTabToSpaces(Document document) { |
| 186 | + if (document == null) { |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + Boolean ret = null; |
| 191 | + |
| 192 | + Preferences preferences = Settings.getActivePreferences(document); |
| 193 | + if (isUseFormatterIndentationSettings(preferences)) { |
| 194 | + ret = true; |
| 195 | + } |
| 196 | + |
| 197 | + return ret; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Returns {@code true} if using the formatter indentation settings from the external |
| 202 | + * formatter is activated, otherwise {@code false}. |
| 203 | + * |
| 204 | + * @param prefs the {@link Preferences} where to check |
| 205 | + * |
| 206 | + * @return {@code true} if using the formatter indentation settings from the external |
| 207 | + * formatter is activated, otherwise {@code false} |
| 208 | + */ |
| 209 | + private boolean isUseFormatterIndentationSettings(Preferences prefs) { |
| 210 | + return prefs.getBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true); |
| 211 | + } |
| 212 | +} |
0 commit comments