1
1
import type { Completions } from "../../shared/completions" ;
2
2
import type { LastSelectedTabRepository } from "../repositories/LastSelectedTabRepository" ;
3
3
4
- export class BufferCommandHelper {
4
+ type TabOption = {
5
+ includePinned : boolean ;
6
+ } ;
7
+
8
+ export class TabQueryHelper {
5
9
constructor (
6
10
private readonly lastSelectedTabRepository : LastSelectedTabRepository ,
7
11
) { }
8
12
9
- async getCompletions ( force : boolean , query : string ) : Promise < Completions > {
13
+ // async getCompletions(force: boolean, query: string): Promise<Completions> {
14
+ async getCompletions ( query : string , opts : TabOption ) : Promise < Completions > {
10
15
const lastTabId =
11
16
await this . lastSelectedTabRepository . getLastSelectedTabId ( ) ;
12
- const allTabs = await this . getAllTabs ( force ) ;
17
+ const allTabs = await this . getAllTabs ( opts ) ;
13
18
const num = parseInt ( query , 10 ) ;
14
19
let tabs : chrome . tabs . Tab [ ] = [ ] ;
15
20
if ( ! isNaN ( num ) ) {
@@ -28,7 +33,7 @@ export class BufferCommandHelper {
28
33
tabs = [ tab ] ;
29
34
}
30
35
} else {
31
- tabs = await this . queryTabs ( force , query ) ;
36
+ tabs = await this . queryTabs ( query , opts ) ;
32
37
}
33
38
34
39
const items = tabs . map ( ( tab ) => {
@@ -50,28 +55,25 @@ export class BufferCommandHelper {
50
55
return [ { name : "Buffers" , items } ] ;
51
56
}
52
57
53
- async queryTabs ( force : boolean , query : string ) : Promise < chrome . tabs . Tab [ ] > {
54
- const tabs = await chrome . tabs . query ( { currentWindow : true } ) ;
55
- const matched = tabs
58
+ async queryTabs ( query : string , opts : TabOption ) : Promise < chrome . tabs . Tab [ ] > {
59
+ const tabs = await chrome . tabs . query ( {
60
+ currentWindow : true ,
61
+ pinned : opts . includePinned ? undefined : false ,
62
+ } ) ;
63
+ return tabs
56
64
. filter ( ( t ) => {
57
65
return (
58
66
( t . url && t . url . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ) ||
59
67
( t . title && t . title . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) )
60
68
) ;
61
69
} )
62
70
. filter ( ( item ) => item . id && item . title && item . url ) ;
63
-
64
- if ( force ) {
65
- return matched ;
66
- }
67
- return matched . filter ( ( tab ) => ! tab . pinned ) ;
68
71
}
69
72
70
- private async getAllTabs ( force : boolean ) : Promise < chrome . tabs . Tab [ ] > {
71
- const tabs = await chrome . tabs . query ( { currentWindow : true } ) ;
72
- if ( force ) {
73
- return tabs ;
74
- }
75
- return tabs . filter ( ( tab ) => ! tab . pinned ) ;
73
+ private async getAllTabs ( opts : TabOption ) : Promise < chrome . tabs . Tab [ ] > {
74
+ return await chrome . tabs . query ( {
75
+ currentWindow : true ,
76
+ pinned : opts . includePinned ? undefined : false ,
77
+ } ) ;
76
78
}
77
79
}
0 commit comments