vscode: use ctx.subscriptions instead of local .disposables

This commit is contained in:
veetaha 2020-03-31 16:57:03 +03:00
parent 09a760e52e
commit 4fbca1c64d

View File

@ -12,8 +12,8 @@ const AST_FILE_SCHEME = "rust-analyzer";
export function syntaxTree(ctx: Ctx): Cmd { export function syntaxTree(ctx: Ctx): Cmd {
const tdcp = new TextDocumentContentProvider(ctx); const tdcp = new TextDocumentContentProvider(ctx);
ctx.pushCleanup(new AstInspector); void new AstInspector(ctx);
ctx.pushCleanup(tdcp);
ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider(AST_FILE_SCHEME, tdcp)); ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider(AST_FILE_SCHEME, tdcp));
return async () => { return async () => {
@ -35,17 +35,14 @@ export function syntaxTree(ctx: Ctx): Cmd {
}; };
} }
class TextDocumentContentProvider implements vscode.TextDocumentContentProvider, Disposable { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
readonly uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); readonly uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>(); readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
private readonly disposables: Disposable[] = [];
constructor(private readonly ctx: Ctx) { constructor(private readonly ctx: Ctx) {
vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, this.disposables); vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, this.disposables); vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
}
dispose() {
this.disposables.forEach(d => d.dispose());
} }
private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) { private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
@ -88,16 +85,16 @@ class AstInspector implements vscode.HoverProvider, Disposable {
border: "#ffffff 1px solid", border: "#ffffff 1px solid",
}); });
private rustEditor: undefined | RustEditor; private rustEditor: undefined | RustEditor;
private readonly disposables: Disposable[] = [];
constructor() { constructor(ctx: Ctx) {
this.disposables.push(vscode.languages.registerHoverProvider({ scheme: AST_FILE_SCHEME }, this)); ctx.pushCleanup(vscode.languages.registerHoverProvider({ scheme: AST_FILE_SCHEME }, this));
vscode.workspace.onDidCloseTextDocument(this.onDidCloseTextDocument, this, this.disposables); vscode.workspace.onDidCloseTextDocument(this.onDidCloseTextDocument, this, ctx.subscriptions);
vscode.window.onDidChangeVisibleTextEditors(this.onDidChangeVisibleTextEditors, this, this.disposables); vscode.window.onDidChangeVisibleTextEditors(this.onDidChangeVisibleTextEditors, this, ctx.subscriptions);
ctx.pushCleanup(this);
} }
dispose() { dispose() {
this.setRustEditor(undefined); this.setRustEditor(undefined);
this.disposables.forEach(d => d.dispose());
} }
private onDidCloseTextDocument(doc: vscode.TextDocument) { private onDidCloseTextDocument(doc: vscode.TextDocument) {