All files / services/core settings-service.ts

100% Statements 28/28
100% Branches 9/9
100% Functions 8/8
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108              22x       22x       22x       22x           22x       22x       22x             14x   14x     4x 5x 4x                       8x                 11x   11x 10x   10x   10x                     2x       14x     14x   14x   1x 1x   1x 1x   12x 12x      
import * as vscode from 'vscode';
import { EventEmitter } from 'events'
import { getConfig } from '@src/utilities/vscode/config';
 
/**
 * Supported label styles of the definition tree.
 */
export enum TreeLabelStyle {
	/**
	 * Show the labels that are annotated with predicate such as `rdfs:label`.
	 */
	AnnotatedLabels,
	/**
	 * Show the local parts of the URI as labels.
	 */
	UriLabels,
	/**
	 * Show the local parts of the URI as labels with the prefix defined in the document.
	 */
	UriLabelsWithPrefix
};
 
/**
 * Supported layout types of the definition tree.
 */
export enum DefinitionTreeLayout {
	/**
	 * Shows all resources in a document grouped by type regardless of their definition source.
	 */
	ByType,
	/**
	 * Shows all resources in a document grouped by their definition source and then by type.
	 */
	BySource
};
 
/**
 * An API for the configuration settings of the Mentor extension.
 */
export class SettingsService extends EventEmitter {
	private _data: { [key: string]: any } = {};
 
	readonly _onDidChange = new vscode.EventEmitter<{ key: string, oldValue: any, newValue: any }>();
 
	onDidChange(key: string, callback: (e: { key: string, oldValue: any, newValue: any }) => void) {
		return this._onDidChange.event((e) => {
			if (e.key == key) {
				callback(e);
			}
		});
	}
 
	/**
	 * Return a value from this configuration.
	 * @param key Configuration variable name, supports _dotted_ names.
	 * @param defaultValue A value should be returned when no value could be found, is `undefined`.
	 * @returns The value `section` denotes or the default.
	 */
	get<T>(key: string, defaultValue?: T): T | undefined {
		return this._data[key] ?? defaultValue;
	}
 
	/**
	 * Set a value in this configuration.
	 * @param key Configuration variable name, supports _dotted_ names.
	 * @param value The value to be set for the configuration variable.
	 */
	set<T>(key: string, value: T) {
		let oldValue = this._data[key];
 
		if (oldValue != value) {
			this._data[key] = value;
 
			vscode.commands.executeCommand('setContext', key, value);
 
			this._onDidChange.fire({ key: key, oldValue: oldValue, newValue: value });
		}
	}
 
	/**
	 * Check if this configuration has a certain value.
	 *
	 * @param key Configuration name, supports _dotted_ names.
	 * @returns `true` if the section doesn't resolve to `undefined`.
	 */
	has(key: string): boolean {
		return this._data[key] != null;
	}
 
	constructor() {
		super();
 
		// Initialize the default label rendering style.
		const defaultStyle = getConfig().get('definitionTree.labelStyle');
 
		switch (defaultStyle) {
			case 'AnnotatedLabels':
				this._data['view.definitionTree.labelStyle'] = TreeLabelStyle.AnnotatedLabels;
				break;
			case 'UriLabelsWithPrefix':
				this._data['view.definitionTree.labelStyle'] = TreeLabelStyle.UriLabelsWithPrefix;
				break;
			default:
				this._data['view.definitionTree.labelStyle'] = TreeLabelStyle.UriLabels;
				break;
		}
	}
}