From a63446f2549afbeafe632c425112b7c38b5c9991 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Sat, 7 Mar 2020 14:07:44 +0200 Subject: [PATCH] vscode: prerefactor util.ts and ctx.ts --- editors/code/src/ctx.ts | 12 +++++------- editors/code/src/util.ts | 12 +++++++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index b4e983a0cd9..25ef38aed04 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient'; import { Config } from './config'; import { createClient } from './client'; -import { isRustDocument } from './util'; +import { isRustEditor, RustEditor } from './util'; export class Ctx { private constructor( @@ -22,17 +22,15 @@ export class Ctx { return res; } - get activeRustEditor(): vscode.TextEditor | undefined { + get activeRustEditor(): RustEditor | undefined { const editor = vscode.window.activeTextEditor; - return editor && isRustDocument(editor.document) + return editor && isRustEditor(editor) ? editor : undefined; } - get visibleRustEditors(): vscode.TextEditor[] { - return vscode.window.visibleTextEditors.filter( - editor => isRustDocument(editor.document), - ); + get visibleRustEditors(): RustEditor[] { + return vscode.window.visibleTextEditors.filter(isRustEditor); } registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 7c95769bb8c..95a5f1227ca 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -1,7 +1,6 @@ import * as lc from "vscode-languageclient"; import * as vscode from "vscode"; import { strict as nativeAssert } from "assert"; -import { TextDocument } from "vscode"; export function assert(condition: boolean, explanation: string): asserts condition { try { @@ -67,9 +66,16 @@ function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } -export function isRustDocument(document: TextDocument) { +export type RustDocument = vscode.TextDocument & { languageId: "rust" }; +export type RustEditor = vscode.TextEditor & { document: RustDocument; id: string }; + +export function isRustDocument(document: vscode.TextDocument): document is RustDocument { return document.languageId === 'rust' // SCM diff views have the same URI as the on-disk document but not the same content && document.uri.scheme !== 'git' && document.uri.scheme !== 'svn'; -} \ No newline at end of file +} + +export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor { + return isRustDocument(editor.document); +}