1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License. See License.txt in the project root for license information.
4
+ *--------------------------------------------------------------------------------------------*/
5
+
6
+ import * as vscode from 'vscode' ;
7
+ import * as Proto from '../protocol' ;
8
+ import { ITypeScriptServiceClient } from '../typescriptService' ;
9
+ import API from '../utils/api' ;
10
+ import { VersionDependentRegistration } from '../utils/dependentRegistration' ;
11
+ import * as typeConverters from '../utils/typeConverters' ;
12
+
13
+ class SmartSelection implements vscode . SelectionRangeProvider {
14
+ public static readonly minVersion = API . v350 ;
15
+
16
+ public constructor (
17
+ private readonly client : ITypeScriptServiceClient
18
+ ) { }
19
+
20
+ public async provideSelectionRanges (
21
+ document : vscode . TextDocument ,
22
+ positions : vscode . Position [ ] ,
23
+ token : vscode . CancellationToken ,
24
+ ) : Promise < vscode . SelectionRange [ ] | undefined > {
25
+ const file = this . client . toOpenedFilePath ( document ) ;
26
+ if ( ! file ) {
27
+ return undefined ;
28
+ }
29
+
30
+ const args : Proto . FileRequestArgs & { locations : Proto . Location [ ] } = {
31
+ file,
32
+ locations : positions . map ( typeConverters . Position . toLocation )
33
+ } ;
34
+ const response = await this . client . execute ( 'selectionRange' , args , token ) ;
35
+ if ( response . type !== 'response' || ! response . body ) {
36
+ return undefined ;
37
+ }
38
+ return response . body . map ( SmartSelection . convertSelectionRange ) ;
39
+ }
40
+
41
+ private static convertSelectionRange (
42
+ selectionRange : Proto . SelectionRange
43
+ ) : vscode . SelectionRange {
44
+ return new vscode . SelectionRange (
45
+ typeConverters . Range . fromTextSpan ( selectionRange . textSpan ) ,
46
+ selectionRange . parent ? SmartSelection . convertSelectionRange ( selectionRange . parent ) : undefined ,
47
+ ) ;
48
+ }
49
+ }
50
+
51
+ export function register (
52
+ selector : vscode . DocumentSelector ,
53
+ client : ITypeScriptServiceClient ,
54
+ ) {
55
+ return new VersionDependentRegistration ( client , SmartSelection . minVersion , ( ) =>
56
+ vscode . languages . registerSelectionRangeProvider ( selector , new SmartSelection ( client ) ) ) ;
57
+ }
0 commit comments