Skip to content

Commit

Permalink
refactor: configure messaging when creating services (#1007)
Browse files Browse the repository at this point in the history
Closes #1002

### Summary of Changes

* It's now possible to configure messaging (logging/user messaging) for
when creating services.
* The runner now uses the `MessagingProvider` instead of handling
messaging on its own.
  • Loading branch information
lars-reimann authored Apr 8, 2024
1 parent ef4bb6f commit 61fd8e6
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 104 deletions.
108 changes: 95 additions & 13 deletions packages/safe-ds-lang/src/language/lsp/safe-ds-messaging-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Disposable } from 'vscode-languageserver-protocol';
*/
export class SafeDsMessagingProvider {
private readonly connection: Connection | undefined;
private logger: Logger | undefined = undefined;
private userMessageProvider: UserMessageProvider | undefined = undefined;

constructor(services: SafeDsServices) {
this.connection = services.shared.lsp.Connection;
Expand All @@ -18,8 +20,10 @@ export class SafeDsMessagingProvider {
* Log the given data to the trace log.
*/
trace(tag: string, message: string, verbose?: string): void {
if (this.connection) {
const text = this.formatLogMessage(tag, message);
const text = this.formatLogMessage(tag, message);
if (this.logger?.trace) {
this.logger.trace(text, verbose);
} else if (this.connection) {
this.connection.tracer.log(text, verbose);
}
}
Expand All @@ -28,8 +32,10 @@ export class SafeDsMessagingProvider {
* Log a debug message.
*/
debug(tag: string, message: string): void {
if (this.connection) {
const text = this.formatLogMessage(tag, message);
const text = this.formatLogMessage(tag, message);
if (this.logger?.debug) {
this.logger.debug(text);
} else if (this.connection) {
this.connection.console.debug(text);
}
}
Expand All @@ -38,8 +44,10 @@ export class SafeDsMessagingProvider {
* Log an information message.
*/
info(tag: string, message: string): void {
if (this.connection) {
const text = this.formatLogMessage(tag, message);
const text = this.formatLogMessage(tag, message);
if (this.logger?.info) {
this.logger.info(text);
} else if (this.connection) {
this.connection.console.info(text);
}
}
Expand All @@ -48,8 +56,10 @@ export class SafeDsMessagingProvider {
* Log a warning message.
*/
warn(tag: string, message: string): void {
if (this.connection) {
const text = this.formatLogMessage(tag, message);
const text = this.formatLogMessage(tag, message);
if (this.logger?.warn) {
this.logger.warn(text);
} else if (this.connection) {
this.connection.console.warn(text);
}
}
Expand All @@ -58,8 +68,10 @@ export class SafeDsMessagingProvider {
* Log an error message.
*/
error(tag: string, message: string): void {
if (this.connection) {
const text = this.formatLogMessage(tag, message);
const text = this.formatLogMessage(tag, message);
if (this.logger?.error) {
this.logger.error(text);
} else if (this.connection) {
this.connection.console.error(text);
}
}
Expand All @@ -75,7 +87,9 @@ export class SafeDsMessagingProvider {
* notification center.
*/
showInformationMessage(message: string): void {
if (this.connection) {
if (this.userMessageProvider?.showInformationMessage) {
this.userMessageProvider.showInformationMessage(message);
} else if (this.connection) {
this.connection.window.showInformationMessage(message);
}
}
Expand All @@ -87,7 +101,9 @@ export class SafeDsMessagingProvider {
* notification center.
*/
showWarningMessage(message: string): void {
if (this.connection) {
if (this.userMessageProvider?.showWarningMessage) {
this.userMessageProvider.showWarningMessage(message);
} else if (this.connection) {
this.connection.window.showWarningMessage(message);
}
}
Expand All @@ -99,7 +115,9 @@ export class SafeDsMessagingProvider {
* notification center.
*/
showErrorMessage(message: string): void {
if (this.connection) {
if (this.userMessageProvider?.showErrorMessage) {
this.userMessageProvider.showErrorMessage(message);
} else if (this.connection) {
this.connection.window.showErrorMessage(message);
}
}
Expand Down Expand Up @@ -131,6 +149,70 @@ export class SafeDsMessagingProvider {
await this.connection.sendNotification(method, params);
}
}

/**
* Set the logger to use for logging messages.
*/
setLogger(logger: Logger) {
this.logger = logger;
}

/**
* Set the user message provider to use for showing messages to the user.
*/
setUserMessageProvider(userMessageProvider: UserMessageProvider) {
this.userMessageProvider = userMessageProvider;
}
}

/* c8 ignore stop */

/**
* A logging provider.
*/
export interface Logger {
/**
* Log the given data to the trace log.
*/
trace?: (message: string, verbose?: string) => void;

/**
* Log a debug message.
*/
debug?: (message: string) => void;

/**
* Log an information message.
*/
info?: (message: string) => void;

/**
* Log a warning message.
*/
warn?: (message: string) => void;

/**
* Log an error message.
*/
error?: (message: string) => void;
}

/**
* A service for showing messages to the user.
*/
export interface UserMessageProvider {
/**
* Prominently show an information message. The message should be short and human-readable.
*/
showInformationMessage?: (message: string) => void;

/**
* Prominently show a warning message. The message should be short and human-readable.
*/
showWarningMessage?: (message: string) => void;

/**
* Prominently show an error message. The message should be short and human-readable.
*/
showErrorMessage?: (message: string) => void;
}
Loading

0 comments on commit 61fd8e6

Please sign in to comment.