11import { basename , dirname , extname } from 'path'
2- import { Component , Environment } from '../environment'
2+ import { Environment } from '../environment'
3+ import { TextDocumentItem } from '../types/textDocument'
34
45/**
56 * 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 = {}
3435 * @param key the context property key to look up
3536 * @param scope the user interface component in whose scope this computation should occur
3637 */
37- export function getComputedContextProperty ( environment : Environment , key : string , scope ?: Component ) : any {
38+ export function getComputedContextProperty ( environment : Environment , key : string , scope ?: TextDocumentItem ) : any {
3839 if ( key . startsWith ( 'config.' ) ) {
3940 const prop = key . slice ( 'config.' . length )
4041 const value = environment . configuration . merged [ prop ]
@@ -43,12 +44,13 @@ export function getComputedContextProperty(environment: Environment, key: string
4344 // which a falsey null default is useful).
4445 return value === undefined ? null : value
4546 }
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
4951 }
5052 if ( key . startsWith ( 'resource.' ) ) {
51- if ( ! component ) {
53+ if ( ! textDocument ) {
5254 return undefined
5355 }
5456 // 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
5759 const prop = key . slice ( 'resource.' . length )
5860 switch ( prop ) {
5961 case 'uri' :
60- return component . document . uri
62+ return textDocument . uri
6163 case 'basename' :
62- return basename ( component . document . uri )
64+ return basename ( textDocument . uri )
6365 case 'dirname' :
64- return dirname ( component . document . uri )
66+ return dirname ( textDocument . uri )
6567 case 'extname' :
66- return extname ( component . document . uri )
68+ return extname ( textDocument . uri )
6769 case 'language' :
68- return component . document . languageId
70+ return textDocument . languageId
6971 case 'textContent' :
70- return component . document . text
72+ return textDocument . text
7173 case 'type' :
7274 return 'textDocument'
7375 }
7476 }
7577 if ( key . startsWith ( 'component.' ) ) {
76- if ( ! component ) {
78+ if ( ! textDocument ) {
7779 return undefined
7880 }
7981 const prop = key . slice ( 'component.' . length )
0 commit comments