author | ihse |
Sat, 03 Mar 2018 08:21:47 +0100 | |
branch | ihse-warnings-cflags-branch |
changeset 56230 | 489867818774 |
parent 47855 | 72b6d674bde2 |
permissions | -rw-r--r-- |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
1 |
/* |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
4 |
* |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
10 |
* |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
15 |
* accompanied this code). |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
16 |
* |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
20 |
* |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
23 |
* questions. |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
24 |
*/ |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
25 |
|
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
26 |
package jdk.nashorn.tools.jjs; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
27 |
|
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
28 |
import java.io.IOException; |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
29 |
import java.util.ArrayList; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
30 |
import java.util.Arrays; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
31 |
import java.util.Collections; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
32 |
import java.util.List; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
33 |
import java.util.WeakHashMap; |
34733
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
34 |
import java.util.regex.Pattern; |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
35 |
import java.util.stream.Collectors; |
38897
b92825d7708c
8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents:
34733
diff
changeset
|
36 |
import jdk.nashorn.internal.runtime.Context; |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
37 |
import jdk.nashorn.internal.runtime.JSType; |
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
38 |
import jdk.nashorn.internal.runtime.NativeJavaPackage; |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
39 |
import jdk.nashorn.internal.runtime.PropertyMap; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
40 |
import jdk.nashorn.internal.runtime.ScriptObject; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
41 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
42 |
import jdk.nashorn.internal.objects.NativeJava; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
43 |
|
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
44 |
/* |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
45 |
* A helper class to get properties of a given object for source code completion. |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
46 |
*/ |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
47 |
final class PropertiesHelper { |
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
48 |
// Java package properties helper, may be null |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
49 |
private PackagesHelper pkgsHelper; |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
50 |
// cached properties list |
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
51 |
private final WeakHashMap<Object, List<String>> propsCache = new WeakHashMap<>(); |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
52 |
|
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
53 |
/** |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
54 |
* Construct a new PropertiesHelper. |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
55 |
* |
38897
b92825d7708c
8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents:
34733
diff
changeset
|
56 |
* @param context the current nashorn Context |
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
57 |
*/ |
38897
b92825d7708c
8158922: jjs tab completion of Java classes shows package-private, "hidden" classes too
sundar
parents:
34733
diff
changeset
|
58 |
PropertiesHelper(final Context context) { |
34551
24317dc1c751
8145186: jjs package completion should have a fallback when javac is not available
sundar
parents:
32314
diff
changeset
|
59 |
try { |
47855
72b6d674bde2
8191468: jdk.scripting.nashorn.shell (jjs) module should use optional dependency for java.compiler module
sundar
parents:
47216
diff
changeset
|
60 |
this.pkgsHelper = PackagesHelper.create(context); |
34551
24317dc1c751
8145186: jjs package completion should have a fallback when javac is not available
sundar
parents:
32314
diff
changeset
|
61 |
} catch (final IOException exp) { |
24317dc1c751
8145186: jjs package completion should have a fallback when javac is not available
sundar
parents:
32314
diff
changeset
|
62 |
if (Main.DEBUG) { |
24317dc1c751
8145186: jjs package completion should have a fallback when javac is not available
sundar
parents:
32314
diff
changeset
|
63 |
exp.printStackTrace(); |
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
64 |
} |
34551
24317dc1c751
8145186: jjs package completion should have a fallback when javac is not available
sundar
parents:
32314
diff
changeset
|
65 |
this.pkgsHelper = null; |
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
66 |
} |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
67 |
} |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
68 |
|
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
69 |
void close() throws Exception { |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
70 |
propsCache.clear(); |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
71 |
pkgsHelper.close(); |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
72 |
} |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
73 |
|
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
74 |
/** |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
75 |
* returns the list of properties of the given object. |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
76 |
* |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
77 |
* @param obj object whose property list is returned |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
78 |
* @return the list of properties of the given object |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
79 |
*/ |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
80 |
List<String> getProperties(final Object obj) { |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
81 |
assert obj != null && obj != ScriptRuntime.UNDEFINED; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
82 |
|
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
83 |
// wrap JS primitives as objects before gettting properties |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
84 |
if (JSType.isPrimitive(obj)) { |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
85 |
return getProperties(JSType.toScriptObject(obj)); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
86 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
87 |
|
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
88 |
// Handle Java package prefix case first. Should do it before checking |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
89 |
// for its super class ScriptObject! |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
90 |
if (obj instanceof NativeJavaPackage) { |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
91 |
if (pkgsHelper != null) { |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
92 |
return pkgsHelper.getPackageProperties(((NativeJavaPackage)obj).getName()); |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
93 |
} else { |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
94 |
return Collections.<String>emptyList(); |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
95 |
} |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
96 |
} |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
97 |
|
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
98 |
// script object - all inherited and non-enumerable, non-index properties |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
99 |
if (obj instanceof ScriptObject) { |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
100 |
final ScriptObject sobj = (ScriptObject)obj; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
101 |
final PropertyMap pmap = sobj.getMap(); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
102 |
if (propsCache.containsKey(pmap)) { |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
103 |
return propsCache.get(pmap); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
104 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
105 |
final String[] keys = sobj.getAllKeys(); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
106 |
List<String> props = Arrays.asList(keys); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
107 |
props = props.stream() |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
108 |
.filter(s -> Character.isJavaIdentifierStart(s.charAt(0))) |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
109 |
.collect(Collectors.toList()); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
110 |
Collections.sort(props); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
111 |
// cache properties against the PropertyMap |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
112 |
propsCache.put(pmap, props); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
113 |
return props; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
114 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
115 |
|
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
116 |
// java class case - don't refer to StaticClass directly |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
117 |
if (NativeJava.isType(ScriptRuntime.UNDEFINED, obj)) { |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
118 |
if (propsCache.containsKey(obj)) { |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
119 |
return propsCache.get(obj); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
120 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
121 |
final List<String> props = NativeJava.getProperties(obj); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
122 |
Collections.sort(props); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
123 |
// cache properties against the StaticClass representing the class |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
124 |
propsCache.put(obj, props); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
125 |
return props; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
126 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
127 |
|
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
128 |
// any other Java object |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
129 |
final Class<?> clazz = obj.getClass(); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
130 |
if (propsCache.containsKey(clazz)) { |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
131 |
return propsCache.get(clazz); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
132 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
133 |
|
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
134 |
final List<String> props = NativeJava.getProperties(obj); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
135 |
Collections.sort(props); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
136 |
// cache properties against the Class object |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
137 |
propsCache.put(clazz, props); |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
138 |
return props; |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
139 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
140 |
|
34733
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
141 |
// This method creates a regex Pattern to use to do CamelCase |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
142 |
// matching. The pattern is derived from user supplied string |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
143 |
// containing one or more upper case characters in it. |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
144 |
private static Pattern makeCamelCasePattern(final String str) { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
145 |
assert !str.isEmpty(); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
146 |
|
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
147 |
final char[] chars = str.toCharArray(); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
148 |
final StringBuilder buf = new StringBuilder(); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
149 |
boolean seenUpperCase = false; |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
150 |
|
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
151 |
// Skip first char for case check. Even if it is upper case, |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
152 |
// we do not want to put lower case matching pattern before |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
153 |
// the first letter! |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
154 |
buf.append(chars[0]); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
155 |
|
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
156 |
for (int idx = 1; idx < chars.length; idx++) { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
157 |
final char ch = chars[idx]; |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
158 |
if (ch >= 'A' && ch <= 'Z') { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
159 |
seenUpperCase = true; |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
160 |
buf.append("[^A-Z]*"); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
161 |
} |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
162 |
buf.append(ch); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
163 |
} |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
164 |
|
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
165 |
if (seenUpperCase) { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
166 |
// match anything at the end! |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
167 |
buf.append(".*"); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
168 |
try { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
169 |
return Pattern.compile(buf.toString()); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
170 |
} catch (Exception exp) { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
171 |
} |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
172 |
} |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
173 |
|
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
174 |
return null; |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
175 |
} |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
176 |
|
32314
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
177 |
/** |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
178 |
* Returns the list of properties of the given object that start with the given prefix. |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
179 |
* |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
180 |
* @param obj object whose property list is returned |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
181 |
* @param prefix property prefix to be matched |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
182 |
* @return the list of properties of the given object |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
183 |
*/ |
8f7d23d3b1ad
8134255: Implement tab-completion for java package prefixes and package names
sundar
parents:
32240
diff
changeset
|
184 |
List<String> getProperties(final Object obj, final String prefix) { |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
185 |
assert prefix != null && !prefix.isEmpty(); |
34733
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
186 |
List<String> allProps = getProperties(obj); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
187 |
List<String> props = allProps.stream() |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
188 |
.filter(s -> s.startsWith(prefix)) |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
189 |
.collect(Collectors.toList()); |
34733
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
190 |
|
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
191 |
// If no match, try CamelCase completion.. |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
192 |
if (props.isEmpty()) { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
193 |
final Pattern pat = makeCamelCasePattern(prefix); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
194 |
if (pat != null) { |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
195 |
return allProps.stream() |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
196 |
.filter(s -> pat.matcher(s).matches()) |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
197 |
.collect(Collectors.toList()); |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
198 |
} |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
199 |
} |
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
200 |
|
dd9867d04e56
8145314: jjs tab-completion should support camel case completion
sundar
parents:
34551
diff
changeset
|
201 |
return props; |
32240
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
202 |
} |
d7c7a5dc92d8
8133652: Implement tab-completion for member select expressions
sundar
parents:
diff
changeset
|
203 |
} |