From 9da356aff3b060b2a0f01dd2478eb5d3530c447e Mon Sep 17 00:00:00 2001 From: Lucas Werkmeister Date: Thu, 2 Oct 2014 22:19:48 +0200 Subject: [PATCH] Completely fix formatter config inlineAnnotations The previous fix (677b021) wasn't enough - it still broke when the option was 'all'. To truly fix this, we need to be able to disable the text preference when it's created, before its listeners are attached. (Some other tabs can apparently enable and disable preferences during initialization without update problems - my best guess is that it's only problematic for string properties, but I didn't investigate it too much.) Also, the handling of this "union" option was pretty broken. Now we simply display 'all' in the text field when 'all' is checked (which makes sense because that's what we'll write to the config file). When that checkbox is unchecked, we insert the default inline annotations into the textfield. Also fixes the validity test. It's allowed to declare any annotations as inline, so we shouldn't have a list of "allowed" annotations; instead, we now only check if it's a valid annotation name. Includes a workaround for ceylon/ceylon-spec#1106. --- .../code/style/CeylonFormatterConstants.java | 5 ---- .../code/style/FormatterPreferences.java | 30 ++++++++----------- .../eclipse/code/style/FormatterTabMisc.java | 20 ++++++++----- .../eclipse/code/style/FormatterTabPage.java | 8 +++++ 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/CeylonFormatterConstants.java b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/CeylonFormatterConstants.java index e93151b5cb..f6d7d8b076 100644 --- a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/CeylonFormatterConstants.java +++ b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/CeylonFormatterConstants.java @@ -68,9 +68,4 @@ public class CeylonFormatterConstants { // required for preview setup public static final String FORMATTER_LINE_SPLIT = "lineSplit"; public static final String FORMATTER_TAB_SIZE = "tabSize"; - - public static final List acceptedInlineAnnotations = Collections.unmodifiableList( - Arrays.asList(new String[] { "abstract", - "actual", "annotation", "default", "final", "formal", "late", - "native", "optional", "shared", "variable" })); } diff --git a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterPreferences.java b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterPreferences.java index 206b44a550..8af93e5190 100644 --- a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterPreferences.java +++ b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterPreferences.java @@ -2,8 +2,6 @@ import static com.redhat.ceylon.eclipse.code.style.CeylonFormatterConstants.*; -import org.apache.commons.lang.StringUtils; - import ceylon.formatter.options.FormattingOptions; import ceylon.formatter.options.IndentMode; import ceylon.formatter.options.Mixed; @@ -35,7 +33,6 @@ public class FormatterPreferences { private VariableOptions options; private String space_AfterParamListClosingParen_Number; private String maxLineLength_Number; - private String inlineAnnotations_List; public FormatterPreferences(FormattingOptions options) { this.options = new VariableOptions(options); @@ -236,16 +233,12 @@ public String get(String key) { } break; case FORMATTER_inlineAnnotations_List: - if (! (options.getInlineAnnotations() instanceof ceylon.formatter.options.All)) { + if (options.getInlineAnnotations() instanceof ceylon.formatter.options.All) { + ret = "all"; + } else { @SuppressWarnings("unchecked") // checked by Ceylon type info ceylon.language.Iterable it = (ceylon.language.Iterable)options.getInlineAnnotations(); ret = ceylon.language.String.join(",", it); - this.inlineAnnotations_List = ret; // save - } - if (this.inlineAnnotations_List != null) { - ret = this.inlineAnnotations_List; - } else { - ret = StringUtils.join(acceptedInlineAnnotations, ','); } break; case FORMATTER_lineBreak: @@ -471,16 +464,19 @@ public void put(String key, String value) { if (TRUE.equals(value)) { options.setInlineAnnotations(all_.get_()); } else { - if (this.inlineAnnotations_List == null) { - options.setInlineAnnotations(StringUtils.join(acceptedInlineAnnotations, ',')); - } else { - options.setInlineAnnotations(acceptedInlineAnnotations.toArray()); - } + options.setInlineAnnotations(FormattingOptions.$default$inlineAnnotations(null, null, null, null, null, null, null, null)); } break; case FORMATTER_inlineAnnotations_List: - String[] anns = value == null ? new String[] {} : value.split(","); - options.setInlineAnnotations(anns); + if (value == null) { + options.setInlineAnnotations(all_.get_()); + } else { + if (value.equals("all")) { + options.setInlineAnnotations(all_.get_()); + } else { + options.setInlineAnnotations(ceylon.language.String.split(value.replace(',', ' ')).sequence()); // TODO remove .sequence() when ceylon/ceylon-spec#1106 is fixed + } + } break; case FORMATTER_lineBreak: if (ceylon.formatter.options.lf_.get_().toString().equals(value)) { diff --git a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabMisc.java b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabMisc.java index ff216b845f..4f07d3fce5 100644 --- a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabMisc.java +++ b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabMisc.java @@ -58,18 +58,24 @@ protected void doCreatePreferences(Composite composite, int numColumns) { public String isValid(String l) { String[] tokens = l.split(","); for (String token : tokens) { - if (!acceptedInlineAnnotations.contains(token.trim())) { - return "Invalid Annotation: " + token; + for (int i = 0; i < token.length(); i += Character.isBmpCodePoint(i) ? 1 : 2) { + int cp = token.codePointAt(i); + if (!Character.isLetterOrDigit(cp) && cp != '_' + || Character.isUpperCase(cp) + || i == 0 && Character.isDigit(cp)) { + StringBuilder ret = new StringBuilder(); + ret.append("Invalid character in annotation name: \'"); + ret.appendCodePoint(cp); + ret.append('\''); + return ret.toString(); + } } } return null; } - }); + }, + /* enabled = */ !allAnnotations.getChecked()); - if (allAnnotations.getChecked()) { - annotationsList.setEnabled(false); - } - allAnnotations.addObserver(new Observer() { public void update(Observable o, Object arg) { updatePreferences((String) arg, annotationsList, diff --git a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabPage.java b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabPage.java index bcb2de9b64..ec120d10cb 100644 --- a/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabPage.java +++ b/plugins/com.redhat.ceylon.eclipse.ui/src/com/redhat/ceylon/eclipse/code/style/FormatterTabPage.java @@ -973,8 +973,16 @@ protected NumberPreference createCompactNumberPref(Composite composite, protected StringPreference createStringPref(Composite composite, int numColumns, String name, String key, IInputValidator inputValidator) { + return createStringPref(composite, numColumns, name, key, inputValidator, true); + } + + protected StringPreference createStringPref(Composite composite, + int numColumns, String name, String key, + IInputValidator inputValidator, boolean enabled) { StringPreference pref = new StringPreference(composite, numColumns, workingValues, key, name, inputValidator); + if (!enabled) + pref.setEnabled(false); fDefaultFocusManager.add(pref); pref.addObserver(fUpdater); return pref;