Theia APIs——Preferences
Preferences
{
// Enable/Disable the line numbers in the monaco editor
"monaco.lineNumbers": "off",
// Tab width in the editor
"monaco.tabWidth": 4,
"fs.watcherExcludes": "path/to/file"
}
我们以filesystem作为示例,它是使用preference service的一个模块。
使用inversity创建具有默认preferences的模块
为了提供preference的值,模块必须提供一个有效的json schema,用来验证所提供的值。模块必须像下面这样将PreferenceContributton绑定到值:
export interface PreferenceSchema {
[name: string]: Object,
properties: {
[name: string]: object
}
}
export interface PreferenceContribution {
readonly schema: PreferenceSchema;
}
例如,filesystem中的代码:
export const filesystemPreferenceSchema: PreferenceSchema = {
"type": "object",
"properties": {
"files.watcherExclude": {
"description": "List of paths to exclude from the filesystem watcher",
"additionalProperties": {
"type": "boolean"
}
}
}
};
bind(PreferenceContribution).toConstantValue(
{
schema: filesystemPreferenceSchema
});
通过配置监听preference的更改
要使用preference的值,只需从容器中获得注入的PreferenceService。
const preferences = ctx.container.get(PreferenceService);
对filesystem而言,服务在一开始绑定的时候获取。这里,你可以使用onPreferenceChanged方法来注册preference更改的回调。
constructor(@inject(PreferenceService) protected readonly prefService: PreferenceService
prefService.onPreferenceChanged(e => { callback }
这里,事件接收到的对象e是下面这种类型:
export interface PreferenceChangedEvent {
readonly preferenceName: string;
readonly newValue?: any;
readonly oldValue?: any;
}
虽然我们可以在类中直接这样使用,不过filesystem提供了一个特定于filesystem preferences的代理preference服务(该服务在后台运行),这样可以更快、更有效地搜索preference(因为它在filesystem preference service中进行搜索,而不是通过更通用的preference service搜索所有的内容)。从某种意义上来说,它也更高效,因为只有那些监视与某个模块相关的特定preferences的模块才会得到通知。要做到这一点,可以看看有关filesystem配置的代理接口是如何绑定使用preference代理接口的:
export type PreferenceProxy<T> = Readonly<T> & Disposable & PreferenceEventEmitter<T>;
export function createPreferenceProxy<T extends Configuration>(preferences: PreferenceService, configuration: T): PreferenceProxy<T> {
/* Register a client to the preference server
When a preference is received, it is validated against the schema and then fired if valid, otherwise the default value is provided. This proxy is also in charge of calling the configured preference service when the proxy object is called i.e editorPrefs['preferenceName'] It basically forwards methods to the real object, i.e dispose/ready etc.
}
要使用这个代理,只需要将它绑定到一个新类型X = PreferenceProxy<CONFIGURATION_INTERFACE>,然后使用上面的方法bind(X)到一个代理。
export interface FileSystemConfiguration {
'files.watcherExclude': { [globPattern: string]: boolean }
}
export const FileSystemPreferences = Symbol('FileSystemPreferences');
export type FileSystemPreferences = PreferenceProxy<FileSystemConfiguration>;
export function createFileSystemPreferences(preferences: PreferenceService): FileSystemPreferences {
return createPreferenceProxy(preferences, defaultFileSystemConfiguration, filesystemPreferenceSchema);
}
export function bindFileSystemPreferences(bind: interfaces.Bind): void {
bind(FileSystemPreferences).toDynamicValue(ctx => {
const preferences = ctx.container.get(PreferenceService);
return createFileSystemPreferences(preferences);
});
bind(PreferenceContribution).toConstantValue({ schema: filesystemPreferenceSchema });
}
最后,在模块中使用filesystem配置,只需要将它注入到你需要的地方。你可以像这样访问preference(以filesystem为例):
const patterns = this.preferences['files.watcherExclude'];
你也可以像这样监听preference的更改:
this.toDispose.push(preferences.onPreferenceChanged(e => {
if (e.preferenceName === 'files.watcherExclude') {
this.toRestartAll.dispose();
}
}));
constructor(...,
@inject(FileSystemPreferences) protected readonly preferences: FileSystemPreferences) {
...
this.toDispose.push(preferences.onPreferenceChanged(e => {
if (e.preferenceName === 'files.watcherExclude') {
this.toRestartAll.dispose();
}
}));
...
}
Preference的修改流程
.theia/settings.json -> JsonPreferenceServer -> CompoundPreferenceServer -> PreferenceService -> PreferenceProxy<FileSystemConfiguration> -> FileSystemWatcher
获取preference的值
对于filesystem来说,我们可以使用与上面相同的代理配置来访问preference的内容。
if (this.prefService['preferenceName']) {
...
}
if (this.prefService['preferenceName2']) {
...
}
})
它能正常工作,正如我们在上面所看到的那样,代理将简单地调用prefService.get('preferenceName')。
TODO/FIXME
- 在CompoundPreferenceServer中添加server优先级
- 当在theia的settings.json中修改内容时添加自动完成和描述功能
Theia APIs——Preferences的更多相关文章
- Theia APIs——事件
上一篇:Theia APIs——Preferences 事件 Theia中的事件或许会让你感到困惑,希望本节能阐述清楚. 来看下面的代码: (来自logger-watcher.ts) @injecta ...
- Theia APIs——通过JSON-RPC进行通信
上一篇:Theia APIs——事件 通过JSON-PRC进行通信 在本节中,我将讲解如何创建后端服务并通过JSON-PRC来连接它. 我将使用debug logging system作为例子来进行讲 ...
- Theia APIs——命令和快捷键
上一篇:使用Theia——创建语言支持 命令和快捷键 Theia可以通过多种不同的方式进行扩展.命令允许packages提供可以被其它包调用的唯一命令,还可以向这些命令添加快捷键和上下文,使得它们只能 ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值
前给例子介绍了如何使用PreferenceActivity 来显示修改应用偏好,用户对Preferences的修改自动存储在应用对应的Shared Preferences中. 本例介绍了如何从一个Ac ...
- Theia架构
上一篇:Theia——云端和桌面版的IDE 架构概述 本节描述了Theia的整体架构. Theia被设计为一个可以在本地运行的桌面应用程序,也可以在浏览器和远程服务器之间工作.为了支持这两种工作方式, ...
- 使用Theia——构建你自己的IDE
上一篇:Theia架构 构建你自己的IDE 本指南将教你如何构建你自己的Theia应用. 必要条件 你需要安装node 10版本(译者:事实上最新的node稳定版即可): curl -o- https ...
- 使用Theia——创建扩展包
上一篇:使用Theia——构建你自己的IDE 创建Theia扩展包 本例中,我们将添加一个菜单项“Say hello”用来显示一个通知“Hello world!”.本文将指导你完成所有必要的步骤. T ...
- (转载)构建public APIs与CORS
from: https://segmentfault.com/a/1190000000709909 理由:在操作层面详细的讲解了跨域的操作.尤其是对于option请求的详解.收藏. 在构建Public ...
随机推荐
- pytorch更新
Pytorch如何更新版本与卸载,使用pip,conda更新卸载Pytorch 2018年05月22日 07:33:52 醉雨轩Y 阅读数 19047 今天我们主要汇总如何使用使用ubuntu,C ...
- 整理了一下 ThinkPHP 历史 (2019-07-01)
整理了一下 ThinkPHP 历史 ThinkPHP 一款国内最流行的 PHP 开源框架. 版本 发布日期 最后更新日期 总天数 ThinkPHP(FCS) 0.6 2006-01-15 2006-0 ...
- 2019-8-31-PowerShell-通过-WMI-获取系统服务
title author date CreateTime categories PowerShell 通过 WMI 获取系统服务 lindexi 2019-08-31 16:55:58 +0800 2 ...
- win10 uwp xaml 绑定接口
本文告诉大家如何在 xaml 绑定属性使用显式继承接口 早上快乐 就在你的心问了我一个问题,他使用的属性是显式继承,但是无法在xaml绑定 我写了简单的代码,一个接口和属性 public class ...
- 【u232】围棋游戏
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 为了增强幼儿园小朋友的数数能力,小虎老师给了一个家庭游戏作业.让小虎那一块空的围棋盘,随机在一些方格中 ...
- 2018-12-25-WPF-如何在-WriteableBitmap-写文字
title author date CreateTime categories WPF 如何在 WriteableBitmap 写文字 lindexi 2018-12-25 09:13:57 +080 ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- ZR1158
ZR1158 http://www.zhengruioi.com/contest/446/problem/1158 给定限制的问题大多数都是容斥或者二分,或者二分之后容斥 首先,这个问题的第一步我们还 ...
- indexdb开cai发keng实zhi践lu
一直在维护一个用html2canvas截图转base64保存的项目,先不说html2canvas不同版本的不同坑的问题,就说转出来的这个base64字符长度实在太大了,尤其是遇到设计出图高度达到3千多 ...
- HDU 6444 Neko's loop(单调队列)
Neko has a loop of size nn. The loop has a happy value aiai on the i−th(0≤i≤n−1)i−th(0≤i≤n−1) grid. ...