Skip to content

Commit

Permalink
feat(frontend): log messages to console
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser committed Aug 31, 2024
1 parent 9714528 commit b32643c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
10 changes: 5 additions & 5 deletions crates/solang/src/languageserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@ impl LanguageServer for SolangServer {
self.client
.log_message(
MessageType::INFO,
format!("solang language server {} initialized", "solang"),
format!("Solang language server initialized"),
)
.await;
}
Expand All @@ -2078,24 +2078,24 @@ impl LanguageServer for SolangServer {

async fn did_change_workspace_folders(&self, _: DidChangeWorkspaceFoldersParams) {
self.client
.log_message(MessageType::INFO, "workspace folders changed!")
.log_message(MessageType::INFO, "Workspace folders changed!")
.await;
}

async fn did_change_configuration(&self, _: DidChangeConfigurationParams) {
self.client
.log_message(MessageType::INFO, "configuration changed!")
.log_message(MessageType::INFO, "Configuration changed!")
.await;
}

async fn did_change_watched_files(&self, _: DidChangeWatchedFilesParams) {
self.client
.log_message(MessageType::INFO, "watched files have changed!")
.log_message(MessageType::INFO, "Watched files have changed!")
.await;
}

async fn execute_command(&self, _: ExecuteCommandParams) -> Result<Option<Value>> {
self.client.log_message(MessageType::INFO, "command executed!").await;
self.client.log_message(MessageType::INFO, "Command executed!").await;
Ok(None)
}

Expand Down
14 changes: 14 additions & 0 deletions packages/app/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,27 @@ export default class App {
document.querySelector("#compile")!.addEventListener("click", () => {
let code = model.getValue();
console.log("Compiling code: ", code);
client.printToConsole(proto.MessageType.Info, "Compiling contract...");

(async () => {
const result = await compileRequest(
// FIXME: This should be configurable
{ compileUrl: "http://localhost:9000/compile" },
{ source: code }
);

if (result.type === 'OK' && result.payload.type === 'SUCCESS') {
client.notify(proto.LogMessageNotification.type.method, {
type: proto.MessageType.Info,
message: "Compilation successful",
});
} else {
let message = result.type === 'SERVER_ERROR'
? `Server error: ${result.payload.status}`
: 'Network error';
client.printToConsole(proto.MessageType.Error, message);
}

// Download the wasm file (result.payload.payload.wasm)
if (result.type === 'OK' && result.payload.type === 'SUCCESS') {
const wasm = result.payload.payload.wasm;
Expand Down
35 changes: 31 additions & 4 deletions packages/app/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ export default class Client extends jsrpc.JSONRPCServerAndClient {
const { type, message } = params as { type: proto.MessageType; message: string };
switch (type) {
case proto.MessageType.Error: {
consoleChannel.value += "[error] ";
consoleChannel.value += " ERROR: ";
break;
}
case proto.MessageType.Warning: {
consoleChannel.value += " [warn] ";
consoleChannel.value += " WARNING: ";
break;
}
case proto.MessageType.Info: {
consoleChannel.value += " [info] ";
consoleChannel.value += " INFO: ";
break;
}
case proto.MessageType.Log: {
consoleChannel.value += " [log] ";
consoleChannel.value += " LOG: ";
break;
}
}
Expand Down Expand Up @@ -97,6 +97,33 @@ export default class Client extends jsrpc.JSONRPCServerAndClient {
await this.receiveAndSend(request);
}
}

printToConsole(type: proto.MessageType, message: string): void {
if (consoleChannel) {
switch (type) {
case proto.MessageType.Error: {
consoleChannel.value += " ERROR: ";
break;
}
case proto.MessageType.Warning: {
consoleChannel.value += " WARNING: ";
break;
}
case proto.MessageType.Info: {
consoleChannel.value += " INFO: ";
break;
}
case proto.MessageType.Log: {
consoleChannel.value += " LOG: ";
break;
}
}
consoleChannel.value += message;
consoleChannel.value += "\n";
} else {
console.error("consoleChannel is not defined");
}
}

pushAfterInitializeHook(...hooks: (() => Promise<void>)[]): void {
this.afterInitializedHooks.push(...hooks);
Expand Down

0 comments on commit b32643c

Please sign in to comment.