Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: configure messaging when creating services #1007

Merged
merged 6 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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