|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Taiming Wang - Initial implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable; |
| 15 | + |
| 16 | +import java.util.AbstractMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Set; |
| 19 | +import java.util.stream.Collectors; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +public class ConvertLoopOperation { |
| 23 | + protected static final String FOR_LOOP_ELEMENT_IDENTIFIER = "element"; //$NON-NLS-1$ |
| 24 | + |
| 25 | + private static final Map<String, String> IRREG_NOUNS = Stream |
| 26 | + .of(new AbstractMap.SimpleImmutableEntry<>("Children", "Child"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 27 | + new AbstractMap.SimpleImmutableEntry<>("Entries", "Entry"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 28 | + new AbstractMap.SimpleImmutableEntry<>("Proxies", "Proxy"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 29 | + new AbstractMap.SimpleImmutableEntry<>("Indices", "Index"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 30 | + new AbstractMap.SimpleImmutableEntry<>("People", "Person"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 31 | + new AbstractMap.SimpleImmutableEntry<>("Properties", "Property"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 32 | + new AbstractMap.SimpleImmutableEntry<>("Factories", "Factory"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 33 | + new AbstractMap.SimpleImmutableEntry<>("Archives", "archive"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 34 | + new AbstractMap.SimpleImmutableEntry<>("Aliases", "Alias"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 35 | + new AbstractMap.SimpleImmutableEntry<>("Alternatives", "Alternative"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 36 | + new AbstractMap.SimpleImmutableEntry<>("Capabilities", "Capability"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 37 | + new AbstractMap.SimpleImmutableEntry<>("Hashes", "Hash"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 38 | + new AbstractMap.SimpleImmutableEntry<>("Directories", "Directory"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 39 | + new AbstractMap.SimpleImmutableEntry<>("Statuses", "Status"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 40 | + new AbstractMap.SimpleImmutableEntry<>("Instances", "Instance"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 41 | + new AbstractMap.SimpleImmutableEntry<>("Classes", "Class"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 42 | + new AbstractMap.SimpleImmutableEntry<>("Deliveries", "Delivery"), //$NON-NLS-1$ //$NON-NLS-2$ |
| 43 | + new AbstractMap.SimpleImmutableEntry<>("Vertices", "Vertex")) //$NON-NLS-1$ //$NON-NLS-2$ |
| 44 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 45 | + |
| 46 | + private static final Set<String> NO_BASE_TYPES = Stream.of("integers", //$NON-NLS-1$ |
| 47 | + "floats", //$NON-NLS-1$ |
| 48 | + "doubles", //$NON-NLS-1$ |
| 49 | + "booleans", //$NON-NLS-1$ |
| 50 | + "bytes", //$NON-NLS-1$ |
| 51 | + "chars", //$NON-NLS-1$ |
| 52 | + "shorts", //$NON-NLS-1$ |
| 53 | + "longs") //$NON-NLS-1$ |
| 54 | + .collect(Collectors.toSet()); |
| 55 | + |
| 56 | + private static final Set<String> CUT_PREFIX = Stream.of("all") //$NON-NLS-1$ |
| 57 | + .collect(Collectors.toSet()); |
| 58 | + |
| 59 | + private static final Set<String> IRREG_ENDINGS = Stream.of("xes", //$NON-NLS-1$ |
| 60 | + "ies", //$NON-NLS-1$ |
| 61 | + "oes", //$NON-NLS-1$ |
| 62 | + "ses", //$NON-NLS-1$ |
| 63 | + "hes", //$NON-NLS-1$ |
| 64 | + "zes", //$NON-NLS-1$ |
| 65 | + "ves", //$NON-NLS-1$ |
| 66 | + "ces", //$NON-NLS-1$ |
| 67 | + "ss", //$NON-NLS-1$ |
| 68 | + "is", //$NON-NLS-1$ |
| 69 | + "us", //$NON-NLS-1$ |
| 70 | + "os", //$NON-NLS-1$ |
| 71 | + "as") //$NON-NLS-1$ |
| 72 | + .collect(Collectors.toSet()); |
| 73 | + |
| 74 | + public static String modifyBaseName(String suggestedName) { |
| 75 | + String name = suggestedName; |
| 76 | + for (String prefix : CUT_PREFIX) { |
| 77 | + if (prefix.length() >= suggestedName.length()) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + char afterPrefix = suggestedName.charAt(prefix.length()); |
| 81 | + if (Character.isUpperCase(afterPrefix) || afterPrefix == '_') { |
| 82 | + if (suggestedName.toLowerCase().startsWith(prefix)) { |
| 83 | + String nameWithoutPrefix = suggestedName.substring(prefix.length()); |
| 84 | + if (nameWithoutPrefix.startsWith("_") && nameWithoutPrefix.length() > 1) { //$NON-NLS-1$ |
| 85 | + name = nameWithoutPrefix.substring(1); |
| 86 | + } else { |
| 87 | + name = nameWithoutPrefix; |
| 88 | + } |
| 89 | + if (name.length() == 1) { |
| 90 | + return name; |
| 91 | + } |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + for (Map.Entry<String, String> entry : IRREG_NOUNS.entrySet()) { |
| 97 | + String suffix = entry.getKey(); |
| 98 | + if (name.toLowerCase().endsWith(suffix.toLowerCase())) { |
| 99 | + String firstPart = name.substring(0, name.length() - suffix.length()); |
| 100 | + return firstPart + entry.getValue(); |
| 101 | + } |
| 102 | + } |
| 103 | + for (String varname : NO_BASE_TYPES) { |
| 104 | + if (name.equalsIgnoreCase(varname)) { |
| 105 | + return FOR_LOOP_ELEMENT_IDENTIFIER; |
| 106 | + } |
| 107 | + } |
| 108 | + for (String suffix : IRREG_ENDINGS) { |
| 109 | + if (name.toLowerCase().endsWith(suffix)) { |
| 110 | + return FOR_LOOP_ELEMENT_IDENTIFIER; |
| 111 | + } |
| 112 | + } |
| 113 | + if (name.length() > 2 && name.endsWith("s")) { //$NON-NLS-1$ |
| 114 | + return name.substring(0, name.length() - 1); |
| 115 | + } |
| 116 | + return FOR_LOOP_ELEMENT_IDENTIFIER; |
| 117 | + } |
| 118 | +} |
0 commit comments