Skip to content

Commit b80bfa8

Browse files
Deep Furiyasatyakigh
authored andcommitted
updated Document constructor to pass in TextDocument function with uri
1 parent 003fd46 commit b80bfa8

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

src/document/Document.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ export class Document {
1616
private cachedParsedContent: unknown;
1717

1818
constructor(
19-
private readonly textDocument: TextDocument,
19+
public readonly uri: DocumentUri,
20+
private readonly textDocument: (uri: string) => TextDocument,
2021
detectIndentation: boolean = true,
2122
fallbackTabSize: number = DefaultSettings.editor.tabSize,
22-
public readonly uri: DocumentUri = textDocument.uri,
23-
public readonly languageId: string = textDocument.languageId,
24-
public readonly version: number = textDocument.version,
25-
public readonly lineCount: number = textDocument.lineCount,
2623
) {
27-
const { extension, type } = detectDocumentType(textDocument.uri, textDocument.getText());
24+
const doc = this.textDocument(uri);
25+
const { extension, type } = detectDocumentType(doc.uri, doc.getText());
2826

2927
this.extension = extension;
3028
this.documentType = type;
@@ -36,12 +34,24 @@ export class Document {
3634
this.processIndentation(detectIndentation, fallbackTabSize);
3735
}
3836

37+
public get languageId(): string {
38+
return this.textDocument(this.uri).languageId;
39+
}
40+
41+
public get version(): number {
42+
return this.textDocument(this.uri).version;
43+
}
44+
45+
public get lineCount(): number {
46+
return this.textDocument(this.uri).lineCount;
47+
}
48+
3949
public get cfnFileType(): CloudFormationFileType {
4050
return this._cfnFileType;
4151
}
4252

4353
public updateCfnFileType(): void {
44-
const content = this.textDocument.getText();
54+
const content = this.textDocument(this.uri).getText();
4555
if (!content.trim()) {
4656
this._cfnFileType = CloudFormationFileType.Empty;
4757
this.cachedParsedContent = undefined;
@@ -54,14 +64,12 @@ export class Document {
5464
} catch {
5565
// If parsing fails, leave cfnFileType unchanged and clear cache
5666
this.cachedParsedContent = undefined;
57-
this.log.debug(
58-
`Failed to parse document ${this.textDocument.uri}, keeping cfnFileType as ${this._cfnFileType}`,
59-
);
67+
this.log.debug(`Failed to parse document ${this.uri}, keeping cfnFileType as ${this._cfnFileType}`);
6068
}
6169
}
6270

6371
private parseContent(): unknown {
64-
const content = this.textDocument.getText();
72+
const content = this.textDocument(this.uri).getText();
6573
if (this.documentType === DocumentType.JSON) {
6674
return JSON.parse(content);
6775
}
@@ -124,27 +132,27 @@ export class Document {
124132
}
125133

126134
public getText(range?: Range) {
127-
return this.textDocument.getText(range);
135+
return this.textDocument(this.uri).getText(range);
128136
}
129137

130138
public getLines(): string[] {
131139
return this.getText().split('\n');
132140
}
133141

134142
public positionAt(offset: number) {
135-
return this.textDocument.positionAt(offset);
143+
return this.textDocument(this.uri).positionAt(offset);
136144
}
137145

138146
public offsetAt(position: Position) {
139-
return this.textDocument.offsetAt(position);
147+
return this.textDocument(this.uri).offsetAt(position);
140148
}
141149

142150
public isTemplate() {
143151
return this.cfnFileType === CloudFormationFileType.Template;
144152
}
145153

146154
public contents() {
147-
return this.textDocument.getText();
155+
return this.textDocument(this.uri).getText();
148156
}
149157

150158
public metadata(): DocumentMetadata {

src/document/DocumentManager.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ export class DocumentManager implements SettingsConfigurable, Closeable {
5656
return;
5757
}
5858

59-
document = new Document(textDocument, this.editorSettings.detectIndentation, this.editorSettings.tabSize);
59+
document = new Document(
60+
uri,
61+
(u) => {
62+
const doc = this.documents.get(u);
63+
if (!doc) {
64+
throw new Error(`TextDocument not found for uri: ${u}`);
65+
}
66+
return doc;
67+
},
68+
this.editorSettings.detectIndentation,
69+
this.editorSettings.tabSize,
70+
);
6071
this.documentMap.set(uri, document);
6172
return document;
6273
}
@@ -73,7 +84,18 @@ export class DocumentManager implements SettingsConfigurable, Closeable {
7384
for (const textDoc of this.documents.all()) {
7485
let document = this.documentMap.get(textDoc.uri);
7586
if (!document) {
76-
document = new Document(textDoc, this.editorSettings.detectIndentation, this.editorSettings.tabSize);
87+
document = new Document(
88+
textDoc.uri,
89+
(u) => {
90+
const doc = this.documents.get(u);
91+
if (!doc) {
92+
throw new Error(`TextDocument not found for uri: ${u}`);
93+
}
94+
return doc;
95+
},
96+
this.editorSettings.detectIndentation,
97+
this.editorSettings.tabSize,
98+
);
7799
this.documentMap.set(textDoc.uri, document);
78100
}
79101
allDocs.push(document);

src/handlers/DocumentHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export function didSaveHandler(components: ServerComponents): (event: TextDocume
197197

198198
function updateSyntaxTree(syntaxTreeManager: SyntaxTreeManager, textDocument: TextDocument, edit: Edit) {
199199
const uri = textDocument.uri;
200-
const document = new Document(textDocument);
200+
const document = new Document(uri, () => textDocument);
201201
if (syntaxTreeManager.getSyntaxTree(uri)) {
202202
if (document.cfnFileType === CloudFormationFileType.Other) {
203203
syntaxTreeManager.deleteSyntaxTree(uri);

0 commit comments

Comments
 (0)