vscode: prerefactor util.ts and ctx.ts

This commit is contained in:
Veetaha 2020-03-07 14:07:44 +02:00
parent 3d93e2108e
commit a63446f254
2 changed files with 14 additions and 10 deletions

View file

@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient';
import { Config } from './config'; import { Config } from './config';
import { createClient } from './client'; import { createClient } from './client';
import { isRustDocument } from './util'; import { isRustEditor, RustEditor } from './util';
export class Ctx { export class Ctx {
private constructor( private constructor(
@ -22,17 +22,15 @@ export class Ctx {
return res; return res;
} }
get activeRustEditor(): vscode.TextEditor | undefined { get activeRustEditor(): RustEditor | undefined {
const editor = vscode.window.activeTextEditor; const editor = vscode.window.activeTextEditor;
return editor && isRustDocument(editor.document) return editor && isRustEditor(editor)
? editor ? editor
: undefined; : undefined;
} }
get visibleRustEditors(): vscode.TextEditor[] { get visibleRustEditors(): RustEditor[] {
return vscode.window.visibleTextEditors.filter( return vscode.window.visibleTextEditors.filter(isRustEditor);
editor => isRustDocument(editor.document),
);
} }
registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {

View file

@ -1,7 +1,6 @@
import * as lc from "vscode-languageclient"; import * as lc from "vscode-languageclient";
import * as vscode from "vscode"; import * as vscode from "vscode";
import { strict as nativeAssert } from "assert"; import { strict as nativeAssert } from "assert";
import { TextDocument } from "vscode";
export function assert(condition: boolean, explanation: string): asserts condition { export function assert(condition: boolean, explanation: string): asserts condition {
try { try {
@ -67,9 +66,16 @@ function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms)); 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' return document.languageId === 'rust'
// SCM diff views have the same URI as the on-disk document but not the same content // SCM diff views have the same URI as the on-disk document but not the same content
&& document.uri.scheme !== 'git' && document.uri.scheme !== 'git'
&& document.uri.scheme !== 'svn'; && document.uri.scheme !== 'svn';
} }
export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
return isRustDocument(editor.document);
}