1
1
import { basename , dirname , extname } from 'path'
2
- import { Component , Environment } from '../environment'
2
+ import { Environment } from '../environment'
3
+ import { TextDocumentItem } from '../types/textDocument'
3
4
4
5
/**
5
6
* Returns a new context created by applying the update context to the base context. It is equivalent to `{...base,
@@ -34,7 +35,7 @@ export const EMPTY_CONTEXT: Context = {}
34
35
* @param key the context property key to look up
35
36
* @param scope the user interface component in whose scope this computation should occur
36
37
*/
37
- export function getComputedContextProperty ( environment : Environment , key : string , scope ?: Component ) : any {
38
+ export function getComputedContextProperty ( environment : Environment , key : string , scope ?: TextDocumentItem ) : any {
38
39
if ( key . startsWith ( 'config.' ) ) {
39
40
const prop = key . slice ( 'config.' . length )
40
41
const value = environment . configuration . merged [ prop ]
@@ -43,12 +44,13 @@ export function getComputedContextProperty(environment: Environment, key: string
43
44
// which a falsey null default is useful).
44
45
return value === undefined ? null : value
45
46
}
46
- const component = scope || environment . component
47
- if ( key === 'resource' || key === 'component' ) {
48
- return ! ! component
47
+ const textDocument : TextDocumentItem | null =
48
+ scope || ( environment . visibleTextDocuments && environment . visibleTextDocuments [ 0 ] )
49
+ if ( key === 'resource' || key === 'component' /* BACKCOMPAT: allow 'component' */ ) {
50
+ return ! ! textDocument
49
51
}
50
52
if ( key . startsWith ( 'resource.' ) ) {
51
- if ( ! component ) {
53
+ if ( ! textDocument ) {
52
54
return undefined
53
55
}
54
56
// TODO(sqs): Define these precisely. If the resource is in a repository, what is the "path"? Is it the
@@ -57,23 +59,23 @@ export function getComputedContextProperty(environment: Environment, key: string
57
59
const prop = key . slice ( 'resource.' . length )
58
60
switch ( prop ) {
59
61
case 'uri' :
60
- return component . document . uri
62
+ return textDocument . uri
61
63
case 'basename' :
62
- return basename ( component . document . uri )
64
+ return basename ( textDocument . uri )
63
65
case 'dirname' :
64
- return dirname ( component . document . uri )
66
+ return dirname ( textDocument . uri )
65
67
case 'extname' :
66
- return extname ( component . document . uri )
68
+ return extname ( textDocument . uri )
67
69
case 'language' :
68
- return component . document . languageId
70
+ return textDocument . languageId
69
71
case 'textContent' :
70
- return component . document . text
72
+ return textDocument . text
71
73
case 'type' :
72
74
return 'textDocument'
73
75
}
74
76
}
75
77
if ( key . startsWith ( 'component.' ) ) {
76
- if ( ! component ) {
78
+ if ( ! textDocument ) {
77
79
return undefined
78
80
}
79
81
const prop = key . slice ( 'component.' . length )
0 commit comments