@@ -17,12 +17,14 @@ export class Document {
1717
1818 constructor (
1919 public readonly uri : DocumentUri ,
20- private readonly textDocument : ( uri : string ) => TextDocument ,
20+ private readonly textDocument : ( uri : string ) => TextDocument | undefined ,
2121 detectIndentation : boolean = true ,
2222 fallbackTabSize : number = DefaultSettings . editor . tabSize ,
2323 ) {
24- const doc = this . textDocument ( uri ) ;
25- const { extension, type } = detectDocumentType ( doc . uri , doc . getText ( ) ) ;
24+ const doc = this . getTextDocument ( ) ;
25+ const { extension, type } = doc
26+ ? detectDocumentType ( doc . uri , doc . getText ( ) )
27+ : { extension : '' , type : DocumentType . YAML } ;
2628
2729 this . extension = extension ;
2830 this . documentType = type ;
@@ -34,24 +36,28 @@ export class Document {
3436 this . processIndentation ( detectIndentation , fallbackTabSize ) ;
3537 }
3638
37- public get languageId ( ) : string {
38- return this . textDocument ( this . uri ) . languageId ;
39+ private getTextDocument ( ) : TextDocument | undefined {
40+ return this . textDocument ( this . uri ) ;
3941 }
4042
41- public get version ( ) : number {
42- return this . textDocument ( this . uri ) . version ;
43+ public get languageId ( ) : string | undefined {
44+ return this . getTextDocument ( ) ?. languageId ;
4345 }
4446
45- public get lineCount ( ) : number {
46- return this . textDocument ( this . uri ) . lineCount ;
47+ public get version ( ) : number | undefined {
48+ return this . getTextDocument ( ) ?. version ;
49+ }
50+
51+ public get lineCount ( ) : number | undefined {
52+ return this . getTextDocument ( ) ?. lineCount ;
4753 }
4854
4955 public get cfnFileType ( ) : CloudFormationFileType {
5056 return this . _cfnFileType ;
5157 }
5258
5359 public updateCfnFileType ( ) : void {
54- const content = this . textDocument ( this . uri ) . getText ( ) ;
60+ const content = this . getTextDocument ( ) ? .getText ( ) ?? '' ;
5561 if ( ! content . trim ( ) ) {
5662 this . _cfnFileType = CloudFormationFileType . Empty ;
5763 this . cachedParsedContent = undefined ;
@@ -69,7 +75,7 @@ export class Document {
6975 }
7076
7177 private parseContent ( ) : unknown {
72- const content = this . textDocument ( this . uri ) . getText ( ) ;
78+ const content = this . getTextDocument ( ) ? .getText ( ) ?? '' ;
7379 if ( this . documentType === DocumentType . JSON ) {
7480 return JSON . parse ( content ) ;
7581 }
@@ -132,27 +138,27 @@ export class Document {
132138 }
133139
134140 public getText ( range ?: Range ) {
135- return this . textDocument ( this . uri ) . getText ( range ) ;
141+ return this . getTextDocument ( ) ? .getText ( range ) ?? '' ;
136142 }
137143
138144 public getLines ( ) : string [ ] {
139145 return this . getText ( ) . split ( '\n' ) ;
140146 }
141147
142148 public positionAt ( offset : number ) {
143- return this . textDocument ( this . uri ) . positionAt ( offset ) ;
149+ return this . getTextDocument ( ) ? .positionAt ( offset ) ;
144150 }
145151
146152 public offsetAt ( position : Position ) {
147- return this . textDocument ( this . uri ) . offsetAt ( position ) ;
153+ return this . getTextDocument ( ) ? .offsetAt ( position ) ;
148154 }
149155
150156 public isTemplate ( ) {
151157 return this . cfnFileType === CloudFormationFileType . Template ;
152158 }
153159
154160 public contents ( ) {
155- return this . textDocument ( this . uri ) . getText ( ) ;
161+ return this . getTextDocument ( ) ? .getText ( ) ?? '' ;
156162 }
157163
158164 public metadata ( ) : DocumentMetadata {
@@ -162,9 +168,9 @@ export class Document {
162168 ext : this . extension ,
163169 type : this . documentType ,
164170 cfnType : this . cfnFileType ,
165- languageId : this . languageId ,
166- version : this . version ,
167- lineCount : this . lineCount ,
171+ languageId : this . languageId ?? '' ,
172+ version : this . version ?? 0 ,
173+ lineCount : this . lineCount ?? 0 ,
168174 } ;
169175 }
170176
0 commit comments