Skip to content

Commit

Permalink
fix some scraping and add global and local scope stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SpazElectro committed Nov 25, 2023
1 parent 3a29bc2 commit 054132a
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 44 deletions.
6 changes: 4 additions & 2 deletions experiments/spazlint/hierachy2.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def getGlobalScopeVariables(lines):

output.append({
"type": getDataTypeOf(removeHandle(line)),
"name": getNameOf(dataTypes, removeHandle(line))
"name": getNameOf(dataTypes, removeHandle(line)),
"line": lineIndex
})

return output
Expand Down Expand Up @@ -205,7 +206,8 @@ def getLocalScopeVariables(lines, cursorLine):

output.append({
"type": getDataTypeOf(removeHandle(line)),
"name": getNameOf(dataTypes, removeHandle(line))
"name": getNameOf(dataTypes, removeHandle(line)),
"line": lineIndex
})

return output
Expand Down
77 changes: 70 additions & 7 deletions experiments/spazlint/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
# merge globalProperties into one
globalPropertiesTemp: dict = {}
for i in globalProperties:
if i == "eventsList":
for x in globalProperties[i]:
x["type"] = "event"

# example event
{
"name": "onChat",
"description": "onChat is called whenever a chat message pops up in game. onLocalChat is called only when chat is received from players on the same machine the script is executing on. A return value of true indicates that the chat message should be suppressed, whereas a return value of false will cause the message to be handled normally. clientID is a unique ID of the game client that sent the chat message. stringReceived is the text of the chat message that was received. chatType can take one of the following values: NORMAL, TEAMCHAT, WHISPER amd ME.\nAny message beginning with \"/\" is interpreted as a command, not as chat, and so will not be passed to either of these hooks. Messages beginning with \"!\" will, though, as will arguments of commands /whisper (and its aliases, /w and @) and /me.\n",
"full": "void onChat(int clientID, string &in stringReceived, CHAT::Type chatType)",
"type": "function",
"arguments":[
{"type": "int", "name": "clientID", "attributes": [],"items": []},
{"type": "string", "name": "&in", "attributes": [],"items": []},
{"type": "CHAT::Type", "name": "chatType", "attributes": [], "items": ["NORMAL", "TEAMCHAT", "WHISPER", "ME"]}
]
}

globalPropertiesTemp[i[:-4]] = globalProperties[i]

classPropertiesTemp: dict = {}
Expand Down Expand Up @@ -94,6 +111,8 @@ def lint(self, advanced=False):
line = line.strip()

if line and not line.endswith(";"):
if line_index != 0 and lines[line_index - 1].strip() == "// @ignore-semicolons":
continue
if line == "" or line.endswith("{") or line.endswith("}") or \
line.startswith("#") or line.startswith("//") or \
line.startswith("if") or "//" in line or \
Expand All @@ -102,8 +121,6 @@ def lint(self, advanced=False):
line.startswith("for") or (line.startswith("case") and line.endswith(":")) or \
line.startswith("default:") or line.endswith("&&"):
continue
if line_index != 0 and lines[line_index - 1].strip() == "// @ignore-semicolons":
continue
if lines[line_index + 1].strip() == "{" or lines[line_index + 1].strip() == ");" or lines[line_index + 1].strip() == "};":
continue
if line_index != 0 and lines[line_index - 1].strip().startswith("for"):
Expand Down Expand Up @@ -176,20 +193,20 @@ def autocomplete(self, lineN, rvrs, char):
className = None

def handleProp(prop):
name = prop["name"]

if prop["type"] == "function":
if prop["name"].startswith("on"):
print(prop)
suggestions.append({
"type": prop["type"],
"name": name,
"name": prop["name"],
"description": prop["description"],
"full": prop["full"],
"items": prop["arguments"]
})
else:
suggestions.append({
"type": prop["type"],
"name": name,
"name": prop["name"],
"description": prop["description"]
})

Expand Down Expand Up @@ -231,9 +248,49 @@ def handleProp(prop):
for prop in globalProperties[p]:
handleProp(prop)

# if prop["type"] == "function":
# suggestions.append({
# "type": prop["type"],
# "name": prop["name"],
# "description": prop["description"],
# "full": prop["full"],
# "items": prop["arguments"]
# })
# else:
# suggestions.append({
# "type": prop["type"],
# "name": prop["name"],
# "description": prop["description"]
# })
for p in globalScopeVars:
description = self.code.splitlines()[p["line"] - 1].strip()
if description != "" and description.startswith("//"):
description = '//'.join(description.split("//")[1:]).strip()
generatedP = {
"type": p["type"],
"name": p["name"],
# TODO get description by looking at the line before and checking for a comment
"description": description
}

handleProp(generatedP)
# CTRL+C, CTRL+V
for p in localScopeVars:
description = self.code.splitlines()[p["line"] - 1].strip()
if description != "" and description.startswith("//"):
description = '//'.join(description.split("//")[1:]).strip()
generatedP = {
"type": p["type"],
"name": p["name"],
# TODO get description by looking at the line before and checking for a comment
"description": description
}

handleProp(generatedP)

return suggestions

import socket, threading
import socket, threading, time

# file = sys.argv[1]
# line = int(sys.argv[2])
Expand Down Expand Up @@ -269,6 +326,8 @@ def handle_client(conn: socket.socket, addr: Tuple[str, int]):
char = data["char"]
file = data["file"]

startTime = time.time()

linter = JJ2PlusLinter(file)
autocomplete_data = linter.autocomplete(line, False, char)

Expand All @@ -278,6 +337,10 @@ def handle_client(conn: socket.socket, addr: Tuple[str, int]):
autocomplete_bytes = json.dumps(autocomplete_data).encode("utf-8")
linter_bytes = json.dumps(linter.lint(advanced == 1)).encode("utf-8")

endTime = time.time()

print(f"Took {endTime - startTime} to analyze!")

conn.sendall(autocomplete_bytes + b"\n" + linter_bytes)

while True:
Expand Down
32 changes: 18 additions & 14 deletions experiments/spazlint/vsc-extension/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function reconnectToServer(attempt=0) {
return;
}

console.log("Connection was closed!");

// if(!connectedToServer) {
// vscode.window.showInformationMessage("Attempting to reconnect to spazlint...");

Expand All @@ -55,14 +57,14 @@ function reconnectToServer(attempt=0) {
// resolve(stdout);
// }
// });

// setTimeout(() => {
// reconnectToServer(attempt + 1);
// }, 3_000);
// } else {
// reconnectToServer(attempt + 1);
// }, 3_000);
// } else {
// vscode.window.showInformationMessage("Disconnected from spazlint!");
// }

connectedToServer = false;
});
}
Expand All @@ -76,8 +78,6 @@ function stopServer() {
} else vscode.window.showErrorMessage("Attempted to stop server when python process has already been killed!")
}

reconnectToServer();

var extensionDiagnostics;

function getDefaultOf(type) {
Expand Down Expand Up @@ -106,30 +106,34 @@ function getFileLocation() {
async function runPythonScript(adv = false) {
return new Promise((resolve, reject) => {
const requestData = {
type: adv === "true" ? "ADVANCED_AUTOCOMPLETE" : "AUTOCOMPLETE",
type: adv === true ? "ADVANCED_AUTOCOMPLETE" : "AUTOCOMPLETE",
line: vscode.window.activeTextEditor.selection.active.line.toString(),
char: vscode.window.activeTextEditor.selection.active.character.toString(),
file: getFileLocation()
};

client.write(JSON.stringify(requestData));
console.log("Running script!");

let accumulatedData = "";

client.on("data", (data) => {
const onDataReceived = (data) => {
console.log(`chunkRecv, data: ${typeof(data)}; dataStringLength: ${data.toString().length}, accum: ${accumulatedData.length}`);
accumulatedData += data.toString();

try {
JSON.parse(accumulatedData.split("\n")[0]);
JSON.parse(accumulatedData.split("\n")[1]);

console.log("Full recv!");
resolve(accumulatedData);

accumulatedData = "";
client.off("data", onDataReceived);
} catch (error) {
// JSON parsing failed, the data is incomplete or not yet fully received
console.log(`ERR: ${error}`);
}
});
};

client.on("data", onDataReceived);
client.write(JSON.stringify(requestData));
});
}

Expand Down
Loading

0 comments on commit 054132a

Please sign in to comment.