Skip to content

Commit 35e6d09

Browse files
committed
Version 1.2, adding full support for tab complete!
1 parent 36e31aa commit 35e6d09

4 files changed

Lines changed: 45 additions & 33 deletions

File tree

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Modern JavaScript for modern minecraft
1111
- ES Module support
1212
- classes, let/const, arrow-functions, import/export
1313
- Spigot event listening
14+
- Tab complete support
1415

1516
## Planned features
1617
- Sandboxed permissions
@@ -20,7 +21,6 @@ Modern JavaScript for modern minecraft
2021
- WebSocket / WebSocketServer
2122
- Non-blocking TCP/UDP sockets
2223
- WebWorker implementation
23-
- Full tab-complete support
2424

2525
## Planned plugins
2626
A set of pre-defined plugins will be<br/>

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
</dependency>
3131
<!--This adds the graalvm SDK API artifact to the build -->
3232
<dependency>
33-
<groupId>org.graalvm</groupId>
33+
<groupId>org.graalvm.sdk</groupId>
3434
<artifactId>graal-sdk</artifactId>
35-
<version>0.30</version>
35+
<version>20.0.0</version>
3636
</dependency>
3737
<!--This adds the Bukkit API artifact to the build -->
3838
<!-- Do not include this in the pom.xml file if the Spigot API is already added -->

src/main/java/com/roguecircuitry/repcraft/JSCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
public class JSCommand implements CommandExecutor, TabCompleter {
1616
RepCraft master;
1717

18+
Value jsOnTabCompleteFunc;
19+
1820
public JSCommand(RepCraft master) {
1921
super();
2022
this.master = master;
23+
this.jsOnTabCompleteFunc = (Value)this.master.eval("onTabComplete");
2124
}
2225

2326
@Override
@@ -48,10 +51,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
4851
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
4952
//Split the last space separated key up by object member access '.'
5053
String toAutoComplete = args[args.length-1];
54+
55+
//A list to store tab completes in
5156
List<String> results = new ArrayList<String>();
5257

53-
Value f = (Value)this.master.eval("onTabComplete");
54-
Value result = f.execute(toAutoComplete);
58+
//Send over to js onTabComplete global function
59+
Value result = this.jsOnTabCompleteFunc.execute(toAutoComplete);
60+
61+
//Put the resulting js Array<String> into our List<String>
5562
for (int i=0; i<result.getArraySize(); i++) {
5663
results.add( result.getArrayElement(i).asString() );
5764
}

src/main/java/com/roguecircuitry/repcraft/resources/repcraft.mjs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
//POLYFILL because no window object
3+
globalThis.window = globalThis;
34
let window = globalThis;
5+
globalThis.global = globalThis;
46
//END POLYFILL
57

68
const getProps = (obj) => {
@@ -12,15 +14,18 @@ const getProps = (obj) => {
1214
return [...properties.keys()];//.filter(item => typeof obj[item] === 'function')
1315
}
1416

17+
window.getProps = getProps;
18+
1519
const keyToDesc = (obj, key) => {
16-
return key + " : " + typeof(obj[key]);
20+
return key + " : " + typeof (obj[key]);
1721
}
1822

1923
export class EvalComplete {
2024
constructor() {
2125
this.lastObj = undefined;
2226
this.potentialObj = undefined;
2327
this.prop = undefined;
28+
/**@type {Array<String>} */
2429
this.lastKeys = undefined;
2530

2631
this.completeStrings = new Array();
@@ -33,9 +38,7 @@ export class EvalComplete {
3338
* @returns {boolean} successful iteration or not (not necessarily fail when false)
3439
*/
3540
complete(str, allPropsOfStr = false) {
36-
if (str.includes(" ")) {
37-
str = str.split(" ").pop();
38-
}
41+
if (str[str.length - 1] == ".") allPropsOfStr = true;
3942
if (allPropsOfStr) {
4043
str = str.substring(0, str.length - 1);
4144
try {
@@ -47,7 +50,7 @@ export class EvalComplete {
4750
this.lastObj = this.potentialObj;
4851
//Set complete strings to all keys of object
4952
this.completeStrings = getProps(this.lastObj);
50-
this.completeStrings.forEach((v)=>{
53+
this.completeStrings.forEach((v) => {
5154
return keyToDesc(this.lastObj, v);
5255
});
5356
}
@@ -61,34 +64,36 @@ export class EvalComplete {
6164
this.prop = str.split(".").pop();
6265

6366
//If we completed the word property return it by itself
64-
if (this.lastObj && this.lastObj[this.prop]) {
65-
this.completeStrings.length = 1;
66-
this.completeStrings[0] = keyToDesc(this.lastObj, this.prop);
67-
} else { //Else, try to complete the property be seeing if we have a part of it
68-
if (this.lastObj === undefined) {
69-
//If we don't have a last object and we're not referencing subobjects
70-
if (!str.includes(".")) {
71-
//Use global window object as reference for autocomplete on global
72-
this.lastObj = window;
73-
}
67+
// if (this.lastObj && this.lastObj[this.prop]) {
68+
// this.completeStrings.length = 1;
69+
// this.completeStrings[0] = keyToDesc(this.lastObj, this.prop);
70+
// } else { //Else, try to complete the property be seeing if we have a part of it
71+
if (this.lastObj === undefined) {
72+
//If we don't have a last object and we're not referencing subobjects
73+
if (!str.includes(".")) {
74+
//Use global window object as reference for autocomplete on global
75+
this.lastObj = window;
7476
}
75-
//If our last object reference is actually an object
76-
if (this.lastObj instanceof Object) {
77-
//Get its keys
78-
this.lastKeys = getProps(this.lastObj);//Object.keys(this.lastObj);
79-
//Clear our returned strings
80-
this.completeStrings.length = 0;
81-
//Loop through keys to see if we can match them with our search prop
82-
for (let key of this.lastKeys) {
83-
//if (key.startsWith(this.prop)) {
84-
if (key.includes(this.prop)) {
85-
key = keyToDesc(this.lastObj, key);
86-
this.completeStrings.push(key);
87-
}
77+
}
78+
//If our last object reference is actually an object
79+
if (this.lastObj instanceof Object) {
80+
//Get its keys
81+
this.lastKeys = getProps(this.lastObj);//Object.keys(this.lastObj);
82+
//Clear our returned strings
83+
this.completeStrings.length = 0;
84+
//Loop through keys to see if we can match them with our search prop
85+
let matchkey;
86+
for (let key of this.lastKeys) {
87+
matchkey = key.toLowerCase(); //More matching!
88+
//if (key.startsWith(this.prop)) {
89+
if (matchkey.includes(this.prop.toLowerCase())) {
90+
key = keyToDesc(this.lastObj, key);
91+
this.completeStrings.push(key);
8892
}
8993
}
9094
}
9195
//}
96+
//}
9297
}
9398
}
9499
}

0 commit comments

Comments
 (0)