Skip to content

Commit a63446f

Browse files
author
Veetaha
committed
vscode: prerefactor util.ts and ctx.ts
1 parent 3d93e21 commit a63446f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

editors/code/src/ctx.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient';
33

44
import { Config } from './config';
55
import { createClient } from './client';
6-
import { isRustDocument } from './util';
6+
import { isRustEditor, RustEditor } from './util';
77

88
export class Ctx {
99
private constructor(
@@ -22,17 +22,15 @@ export class Ctx {
2222
return res;
2323
}
2424

25-
get activeRustEditor(): vscode.TextEditor | undefined {
25+
get activeRustEditor(): RustEditor | undefined {
2626
const editor = vscode.window.activeTextEditor;
27-
return editor && isRustDocument(editor.document)
27+
return editor && isRustEditor(editor)
2828
? editor
2929
: undefined;
3030
}
3131

32-
get visibleRustEditors(): vscode.TextEditor[] {
33-
return vscode.window.visibleTextEditors.filter(
34-
editor => isRustDocument(editor.document),
35-
);
32+
get visibleRustEditors(): RustEditor[] {
33+
return vscode.window.visibleTextEditors.filter(isRustEditor);
3634
}
3735

3836
registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {

editors/code/src/util.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as lc from "vscode-languageclient";
22
import * as vscode from "vscode";
33
import { strict as nativeAssert } from "assert";
4-
import { TextDocument } from "vscode";
54

65
export function assert(condition: boolean, explanation: string): asserts condition {
76
try {
@@ -67,9 +66,16 @@ function sleep(ms: number) {
6766
return new Promise(resolve => setTimeout(resolve, ms));
6867
}
6968

70-
export function isRustDocument(document: TextDocument) {
69+
export type RustDocument = vscode.TextDocument & { languageId: "rust" };
70+
export type RustEditor = vscode.TextEditor & { document: RustDocument; id: string };
71+
72+
export function isRustDocument(document: vscode.TextDocument): document is RustDocument {
7173
return document.languageId === 'rust'
7274
// SCM diff views have the same URI as the on-disk document but not the same content
7375
&& document.uri.scheme !== 'git'
7476
&& document.uri.scheme !== 'svn';
75-
}
77+
}
78+
79+
export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
80+
return isRustDocument(editor.document);
81+
}

0 commit comments

Comments
 (0)